Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module should be used to derive the Elm instance alongside the JSON ones. The prefered usage is to convert statements such as :
$(deriveJSON defaultOptions{fieldLabelModifier = drop 4, constructorTagModifier = map toLower} ''D)
into:
$(deriveBoth defaultOptions{fieldLabelModifier = drop 4, constructorTagModifier = map toLower} ''D)
Which will derive both the aeson
and elm-bridge
instances at the same
time.
Synopsis
- data Options
- data SumEncoding
- defaultOptions :: Options
- defaultOptionsDropLower :: Int -> Options
- deriveElmDef :: Options -> Name -> Q [Dec]
- deriveBoth :: Options -> Name -> Q [Dec]
Options
Options that specify how to encode/decode your datatype to/from JSON.
Options can be set using record syntax on defaultOptions
with the fields
below.
data SumEncoding #
Specifies how to encode constructors of a sum datatype.
TaggedObject | A constructor will be encoded to an object with a field
|
UntaggedValue | Constructor names won't be encoded. Instead only the contents of the constructor will be encoded as if the type had a single constructor. JSON encodings have to be disjoint for decoding to work properly. When decoding, constructors are tried in the order of definition. If some encodings overlap, the first one defined will succeed. Note: Nullary constructors are encoded as strings (using
Note: Only the last error is kept when decoding, so in the case of malformed JSON, only an error for the last constructor will be reported. |
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
|
Instances
Eq SumEncoding | |
Defined in Data.Aeson.Types.Internal (==) :: SumEncoding -> SumEncoding -> Bool # (/=) :: SumEncoding -> SumEncoding -> Bool # | |
Show SumEncoding | |
Defined in Data.Aeson.Types.Internal showsPrec :: Int -> SumEncoding -> ShowS # show :: SumEncoding -> String # showList :: [SumEncoding] -> ShowS # |
defaultOptions :: Options Source #
Note that This default set of options is distinct from that in
the aeson
package.
defaultOptionsDropLower :: Int -> Options Source #
This generates a default set of options. The parameter represents the number of characters that must be dropped from the Haskell field names. The first letter of the field is then converted to lowercase, ie:
data Foo = Foo { _fooBarQux :: Int } $(deriveBoth (defaultOptionsDropLower 4) ''Foo)
Will be encoded as:
{"barQux"=12}
Template haskell functions
deriveElmDef :: Options -> Name -> Q [Dec] Source #
Just derive the elm-bridge
definitions for generating the
serialization/deserialization code. It must be kept synchronized with
the Haskell code manually.
deriveBoth :: Options -> Name -> Q [Dec] Source #
Equivalent to running both deriveJSON
and deriveElmDef
with the
same options, so as to ensure the code on the Haskell and Elm size is
synchronized.