elm-bridge-0.2.2.1: Derive Elm types from Haskell types

Safe HaskellNone
LanguageHaskell2010

Elm.Derive

Contents

Description

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

Options

data Options :: *

Options that specify how to encode/decode your datatype to/from JSON.

Constructors

Options 

Fields

fieldLabelModifier :: String -> String

Function applied to field labels. Handy for removing common record prefixes for example.

constructorTagModifier :: String -> String

Function applied to constructor tags which could be handy for lower-casing them for example.

allNullaryToStringTag :: Bool

If True the constructors of a datatype, with all nullary constructors, will be encoded to just a string with the constructor tag. If False the encoding will always follow the sumEncoding.

omitNothingFields :: Bool

If True record fields with a Nothing value will be omitted from the resulting object. If False the resulting object will include those fields mapping to null.

sumEncoding :: SumEncoding

Specifies how to encode constructors of a sum datatype.

unwrapUnaryRecords :: Bool

Hide the field name when a record constructor has only one field, like a newtype.

Instances

data SumEncoding :: *

Specifies how to encode constructors of a sum datatype.

Constructors

TaggedObject

A constructor will be encoded to an object with a field tagFieldName which specifies the constructor tag (modified by the constructorTagModifier). If the constructor is a record the encoded record fields will be unpacked into this object. So make sure that your record doesn't have a field with the same label as the tagFieldName. Otherwise the tag gets overwritten by the encoded value of that field! If the constructor is not a record the encoded constructor contents will be stored under the contentsFieldName field.

ObjectWithSingleField

A constructor will be encoded to an object with a single field named after the constructor tag (modified by the constructorTagModifier) which maps to the encoded contents of the constructor.

TwoElemArray

A constructor will be encoded to a 2-element array where the first element is the tag of the constructor (modified by the constructorTagModifier) and the second element the encoded contents of the constructor.

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.