| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Composite.Aeson.Record
- newtype ToField a = ToField {}
- newtype FromField e a = FromField {
- unFromField :: Text -> Parse e a
- data JsonField e a = JsonField (a -> Maybe Value) (Text -> Parse e a)
- field :: (Wrapped a', Unwrapped a' ~ a) => JsonFormat e a -> JsonField e a'
- field' :: JsonFormat e a -> JsonField e a
- optionalField :: forall e a a'. (Wrapped a', Unwrapped a' ~ Maybe a) => JsonFormat e a -> JsonField e a'
- optionalField' :: JsonFormat e a -> JsonField e (Maybe a)
- type JsonFormatRec e rs = Rec (JsonField e) rs
- class RecToJsonObject rs where
- recToJson :: RecToJsonObject rs => Rec ToField rs -> Rec Identity rs -> Value
- class RecFromJson rs where
- recJsonFormat :: (RecToJsonObject rs, RecFromJson rs) => JsonFormatRec e rs -> JsonFormat e (Rec Identity rs)
- class DefaultJsonFormatRec rs where
Documentation
Function to encode a single field of a record, possibly choosing to elide the field with Nothing.
Instances
newtype FromField e a Source #
Function to decode a single field of a record.
Constructors
| FromField | |
Fields
| |
Descriptor of how to handle a single record field with functions to parse and emit the field which can handle missing fields on parse and elide fields on encode.
field :: (Wrapped a', Unwrapped a' ~ a) => JsonFormat e a -> JsonField e a' Source #
Given a JsonFormat for some type a, produce a JsonField for fields of type a which fails if the field is missing and never elides the field.
field' :: JsonFormat e a -> JsonField e a Source #
Given a JsonFormat for some type a, produce a JsonField for fields of type a which fails if the field is missing and never elides the field.
optionalField :: forall e a a'. (Wrapped a', Unwrapped a' ~ Maybe a) => JsonFormat e a -> JsonField e a' Source #
Given a JsonFormat for some type a, produce a JsonField for fields of type Maybe a which substitutes Nothing for either null or missing field,
and which elides the field on Nothing.
optionalField' :: JsonFormat e a -> JsonField e (Maybe a) Source #
Given a JsonFormat for some type a, produce a JsonField for fields of type Maybe a which substitutes Nothing for either null or missing field,
and which elides the field on Nothing.
type JsonFormatRec e rs = Rec (JsonField e) rs Source #
Type of a Vinyl/Frames record which describes how to map fields of a record to JSON and back.
This record type has the same field names and types as a regular record with Identity but instead of Identity uses 'JsonFormat e'.
For example, given:
type FId = "id" :-> Int type FName = "name" :-> Text type User = '[FId, FName]
A JsonFormatRec for User might be:
userFormatRec ::JsonFormatRece User userFormatRec =fieldintegralJsonFormat:^:fieldtextJsonFormat:^: RNil
Or, using the default mappings for each field type:
userFormatRec ::JsonFormatRece User userFormatRec =defaultJsonFormatRec
Such a record is a first-class value like any other record, so can be composed into larger records, modified, etc. This is particularly useful in
combination with defaultJsonFormatRec, where you can automatically derive a format record for all fields you want defaults for and then extend or
override formats for particular fields, e.g.
fId :: Proxy FId fId = Proxy userFormatRec ::JsonFormatRece User userFormatRec =over(rlensfId) ('Composite.Aeson.Base.dimapJsonFormat (+10) (subtract 10))defaultJsonFormatRec
Would use the same JSON schema as the other examples, but the id field would be encoded in JSON as 10 higher.
Once you've produced an appropriate JsonFormatRec for your case, use recJsonFormat to make a of it.JsonFormat e (Record '[…])
class RecToJsonObject rs where Source #
Helper class which induces over the structure of a record, reflecting the name of each field and applying each ToJson to its corresponding value to
produce JSON.
Minimal complete definition
Instances
| RecToJsonObject ([] *) Source # | |
| (KnownSymbol s, RecToJsonObject rs) => RecToJsonObject ((:) * ((:->) s a) rs) Source # | |
recToJson :: RecToJsonObject rs => Rec ToField rs -> Rec Identity rs -> Value Source #
Given a record of ToField functions for each field in rs, convert an Identity record to JSON. Equivalent to Aeson.Object . recToJsonObject fmt
class RecFromJson rs where Source #
Class which induces over the structure of a record, parsing fields using a record of FromJson and assembling an Identity record.
Minimal complete definition
Instances
| RecFromJson ([] *) Source # | |
| (KnownSymbol s, RecFromJson rs) => RecFromJson ((:) * ((:->) s a) rs) Source # | |
recJsonFormat :: (RecToJsonObject rs, RecFromJson rs) => JsonFormatRec e rs -> JsonFormat e (Rec Identity rs) Source #
Take a JsonFormatRec describing how to map a record with field rs to and from JSON and produce a .JsonFormat e (Record rs)
See JsonFormatRec for more.
class DefaultJsonFormatRec rs where Source #
Class to make a JsonFormatRec with defaultJsonFormat for each field.
Minimal complete definition
Methods
defaultJsonFormatRec :: JsonFormatRec e rs Source #
Produce a JsonFormatRec for a record with fields rs by using the default JsonFormat for each field in rs, as provided by DefaultJsonFormat.
Instances
| DefaultJsonFormatRec ([] *) Source # | |
| (KnownSymbol s, DefaultJsonFormat a, DefaultJsonFormatRec rs) => DefaultJsonFormatRec ((:) * ((:->) s a) rs) Source # | |