{-# LANGUAGE DeriveDataTypeable #-}
import Data.Data
import Data.ByteString (ByteString)
import Data.JSON
data Person = Person {
firstName :: String
, lastName :: String
, age :: Int
} deriving (Eq, Show, Data, Typeable)
person :: ByteString
person = toJSON (Person "John" "Doe" 23)
This will resutl in JSON that is equivalent to:
{
"first_name":"John",
"last_name": "Doe",
"age": 23
}
parseJSON person >>= fromJSON :: Maybe Person
can be used to recover the
original data type.
data Message = Message {
messageBody :: String
, messageBodySize :: Int
} deriving (Eq, Show, Data, Typeable)
message :: ByteString
message = toJSON (Message "foobar" 6)
This will resutl in JSON that is equivalent to:
{
"body": "foobar",
"body_size": 6
}
parseJSON message >>= fromJSON :: Maybe Message
can be used to recover the
original data type.