Safe Haskell | None |
---|---|
Language | Haskell2010 |
- type JsonFormatRec e rs = Rec (JsonFormat e) rs
- class RecToJsonObject rs where
- recToJson :: RecToJsonObject rs => Rec ToJson 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
type JsonFormatRec e rs = Rec (JsonFormat 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 ::JsonFormatRec
e User userFormatRec =integralJsonFormat
&:textJsonFormat
&: Nil
Or, using the default mappings for each field type:
userFormatRec ::JsonFormatRec
e 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 ::JsonFormatRec
e User userFormatRec =over
(rlens
fId) ('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.
RecToJsonObject ([] *) Source # | |
(NamedField r, RecToJsonObject rs) => RecToJsonObject ((:) * r rs) Source # | |
recToJson :: RecToJsonObject rs => Rec ToJson rs -> Rec Identity rs -> Value Source #
Given a record of ToJson
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.
RecFromJson ([] *) Source # | |
(NamedField r, RecFromJson rs) => RecFromJson ((:) * r 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.
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
.
DefaultJsonFormatRec ([] *) Source # | |
(NamedField r, DefaultJsonFormat (Unwrapped r), DefaultJsonFormatRec rs) => DefaultJsonFormatRec ((:) * r rs) Source # | |