composite-aeson-0.1.1.0: JSON for Vinyl/Frames records

Safe HaskellNone
LanguageHaskell2010

Composite.Aeson.Record

Synopsis

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 JsonFormat e (Record '[…]) of it.

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

recToJsonObject

Methods

recToJsonObject :: Rec ToJson rs -> Rec Identity rs -> Object Source #

Given a record of ToJson functions for each field in rs, convert an Identity record to Object.

Instances

RecToJsonObject ([] *) Source # 
(NamedField r, RecToJsonObject rs) => RecToJsonObject ((:) * r rs) Source # 

Methods

recToJsonObject :: Rec * ToJson ((* ': r) rs) -> Rec * Identity ((* ': r) rs) -> Object 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.

Minimal complete definition

recFromJson

Methods

recFromJson :: Rec (FromJson e) rs -> Parse e (Rec Identity rs) Source #

Given a record of FromJson parsers for each field in rs, produce an Parse to make an Identity record.

Instances

RecFromJson ([] *) Source # 

Methods

recFromJson :: Rec * (FromJson e) [*] -> Parse e (Rec * Identity [*]) Source #

(NamedField r, RecFromJson rs) => RecFromJson ((:) * r rs) Source # 

Methods

recFromJson :: Rec * (FromJson e) ((* ': r) rs) -> Parse e (Rec * Identity ((* ': 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.

Minimal complete definition

defaultJsonFormatRec

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.