aeson-options-0.1.0: Options to derive FromJSON/ToJSON instances

Safe HaskellNone
LanguageHaskell2010

Data.Aeson.Options

Contents

Description

Options used to derive FromJSON/ToJSON instance. These options generally comply to our style regarding names. Of course sometimes they don't fit one's needs, so treat them as just sensible defaults.

Synopsis

Custom options

defaultOptions :: Options Source #

These options do the following transformations: 1. Names of field records are assumed to be camelCased, camel part is removed, Cased part is converted to cased. So camelCased becomes cased. Also all punctuation symbols are dropped before doing it. 2. Constructors are assumed to start with some capitalized prefix (which finished right before the last capital letter). This prefix is dropped and then the first letter is lowercased.

leaveTagOptions :: Options Source #

These options are the same as defaultOptions, but they don't modify constructor tags.

defaultOptionsPS :: Options Source #

Options used for communication with PureScript by default.

stripTypeOptions :: forall a. Typeable a => Options Source #

Options to strip type name from the field names. See genericParseJSONStripType and genericToJSONStripType for examples.

Generic functions

genericParseJSONStripType :: forall a. (Typeable a, Generic a, GFromJSON Zero (Rep a)) => Value -> Parser a Source #

Allows to create FromJSON instance that strips the data type name prefix from every field. Doesn't change name of the fields that doesn't start with the type name.

>>> data Foo = Foo { fooBar :: String, fooQuux :: Int } deriving (Generic, Show)
>>> instance FromJSON Foo where parseJSON = genericParseJSONStripType
>>> decode @Foo "{ \"bar\": \"test\", \"quux\": 42 }"
Just (Foo {fooBar = "test", fooQuux = 42})

genericToJSONStripType :: forall a. (Typeable a, Generic a, GToJSON Zero (Rep a)) => a -> Value Source #

Allows to create ToJSON instance that strips the data type name prefix from every field. Doesn't change name of the fields that doesn't start with the type name.

>>> data Foo = Foo { fooBar :: String, fooQuux :: Int } deriving (Generic, Show)
>>> instance ToJSON Foo where toJSON = genericToJSONStripType
>>> encode $ Foo { fooBar = "test", fooQuux = 42 }
"{\"quux\":42,\"bar\":\"test\"}"