module Network.JSONApi.Identifier
( HasIdentifier (..)
, Identifier (..)
, datatype
, ident
, metadata
) where
import Control.Lens.TH
import Data.Aeson (ToJSON, FromJSON, (.=), (.:), (.:?))
import qualified Data.Aeson as AE
import Data.Text (Text)
import Network.JSONApi.Meta (Meta)
import Prelude hiding (id)
data Identifier = Identifier
{ _ident :: Text
, _datatype :: Text
, _metadata :: Maybe Meta
} deriving (Show, Eq)
instance ToJSON Identifier where
toJSON (Identifier resId resType resMetaData) =
AE.object [ "id" .= resId
, "type" .= resType
, "meta" .= resMetaData
]
instance FromJSON Identifier where
parseJSON = AE.withObject "resourceIdentifier" $ \v -> do
id <- v .: "id"
typ <- v .: "type"
meta <- v .:? "meta"
return $ Identifier id typ meta
class HasIdentifier a where
identifier :: a -> Identifier
makeLenses ''Identifier