Copyright | (c) 2011-2015 Bryan O'Sullivan (c) 2011 MailRank, Inc. |
---|---|
License | Apache |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell98 |
Functions to mechanically derive ToJSON
and FromJSON
instances. Note that
you need to enable the TemplateHaskell
language extension in order to use this
module.
An example shows how instances are generated for arbitrary data types. First we define a data type:
data D a = Nullary | Unary Int | Product String Char a | Record { testOne :: Double , testTwo :: Bool , testThree :: D a } deriving Eq
Next we derive the necessary instances. Note that we make use of the feature to change record field names. In this case we drop the first 4 characters of every field name. We also modify constructor names by lower-casing them:
$(deriveJSON
defaultOptions
{fieldLabelModifier
=drop
4,constructorTagModifier
= map toLower} ''D)
Now we can use the newly created instances.
d :: DInt
d = Record { testOne = 3.14159 , testTwo =True
, testThree = Product "test" 'A' 123 }
>>>
fromJSON (toJSON d) == Success d
> True
This also works for data family instances, but instead of passing in the data family name (with double quotes), we pass in a data family instance constructor (with a single quote):
data family DF a data instance DF Int = DF1 Int | DF2 Int Int deriving Eq $(deriveJSON
defaultOptions
'DF1) -- Alternatively, one could pass 'DF2 instead
Please note that you can derive instances for tuples using the following syntax:
-- FromJSON and ToJSON instances for 4-tuples. $(deriveJSON
defaultOptions
''(,,,))
- data Options = Options {}
- data SumEncoding
- defaultOptions :: Options
- defaultTaggedObject :: SumEncoding
- deriveJSON :: Options -> Name -> Q [Dec]
- deriveToJSON :: Options -> Name -> Q [Dec]
- deriveFromJSON :: Options -> Name -> Q [Dec]
- mkToJSON :: Options -> Name -> Q Exp
- mkToEncoding :: Options -> Name -> Q Exp
- mkParseJSON :: Options -> Name -> Q Exp
Encoding configuration
Options that specify how to encode/decode your datatype to/from JSON.
Options | |
|
data SumEncoding Source
Specifies how to encode constructors of a sum datatype.
TaggedObject | A constructor will be encoded to an object with a field
|
ObjectWithSingleField | A constructor will be encoded to an object with a single
field named after the constructor tag (modified by the
|
TwoElemArray | A constructor will be encoded to a 2-element array where the
first element is the tag of the constructor (modified by the
|
defaultOptions :: Options Source
Default encoding Options
:
Options
{fieldLabelModifier
= id ,constructorTagModifier
= id ,allNullaryToStringTag
= True ,omitNothingFields
= False ,sumEncoding
=defaultTaggedObject
}
defaultTaggedObject :: SumEncoding Source
Default TaggedObject
SumEncoding
options:
defaultTaggedObject =TaggedObject
{tagFieldName
= "tag" ,contentsFieldName
= "contents" }
FromJSON and ToJSON derivation
:: Options | Encoding options. |
-> Name | Name of the type for which to generate |
-> Q [Dec] |
Generates both ToJSON
and FromJSON
instance declarations for the given
data type or data family instance constructor.
This is a convienience function which is equivalent to calling both
deriveToJSON
and deriveFromJSON
.
:: Options | Encoding options. |
-> Name | Name of the type for which to generate a |
-> Q [Dec] |
Generates a ToJSON
instance declaration for the given data type or
data family instance constructor.
:: Options | Encoding options. |
-> Name | Name of the type for which to generate a |
-> Q [Dec] |
Generates a FromJSON
instance declaration for the given data type or
data family instance constructor.
Generates a lambda expression which encodes the given data type or
data family instance constructor as a Value
.
Generates a lambda expression which encodes the given data type or data family instance constructor as a JSON string.