composite-aeson-0.8.2.2: JSON for Vinyl records
Safe HaskellSafe-Inferred
LanguageHaskell2010

Composite.Aeson.Record

Synopsis

Documentation

newtype ToJsonField a Source #

Function to encode a single field of a record, possibly choosing to elide the field with Nothing.

Constructors

ToJsonField 

Fields

Instances

Instances details
Contravariant ToJsonField Source # 
Instance details

Defined in Composite.Aeson.Record

Methods

contramap :: (a' -> a) -> ToJsonField a -> ToJsonField a' #

(>$) :: b -> ToJsonField b -> ToJsonField a #

newtype FromJsonField e a Source #

Function to decode a single field of a record.

Constructors

FromJsonField 

Fields

Instances

Instances details
Functor (FromJsonField e) Source # 
Instance details

Defined in Composite.Aeson.Record

Methods

fmap :: (a -> b) -> FromJsonField e a -> FromJsonField e b #

(<$) :: a -> FromJsonField e b -> FromJsonField e a #

data JsonField e a Source #

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.

Constructors

JsonField (a -> Maybe Value) (Text -> Parse e a) 

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.

valField :: forall s a e. JsonFormat e a -> JsonField e (s :-> a) Source #

Specialized type for field so we can specify the Val symbol.

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.

fromField :: Wrapped a => Parse e (Unwrapped a) -> FromJsonField e a Source #

Given a parser for Unwrapped a, produce a FromField e a.

valFromField :: forall s a e. Parse e a -> FromJsonField e (s :-> a) Source #

Specialized type for fromField so we can specify the Val symbol.

fromField' :: Parse e a -> FromJsonField e a Source #

Given a parser for a, produce a FromField e a.

toField :: (Wrapped a', Unwrapped a' ~ a) => (a -> Value) -> ToJsonField a' Source #

Given a parser for Unwrapped a, produce a FromField e a.

toField' :: (a -> Value) -> ToJsonField a Source #

Given a parser for a, produce a ToField a.

defaultField :: (Wrapped a', Unwrapped a' ~ 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 substitutes a default value if the field is missing and never elides the field.

valDefaultField :: forall s a e. a -> JsonFormat e a -> JsonField e (s :-> a) Source #

Specialized type for defaultField so we can specify the Val symbol.

defaultField' :: a -> JsonFormat e a -> JsonField e a Source #

Given a JsonFormat for some type a, produce a JsonField for fields of type a which substitutes a default value if the field is missing and never elides the field.

optionalField :: (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.

valOptionalField :: forall s a e. JsonFormat e a -> JsonField e (s :-> Maybe a) Source #

Specialized type for optionalField so we can specify the Val symbol.

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.

fromOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => Parse e a -> FromJsonField e a' Source #

Given a parser for a, produce a FromField e b where b is a Wrapped around Maybe a.

valFromOptionalField :: forall s a e. Parse e a -> FromJsonField e (s :-> Maybe a) Source #

Specialized type for fromOptionalField so we can specify the Val symbol.

fromOptionalField' :: Parse e a -> FromJsonField e (Maybe a) Source #

Given a parser for a, produce a FromField e (Maybe a) which represents an optional field.

toOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => (a -> Value) -> ToJsonField a' Source #

Given an encoding function for some type a, produce a ToField for fields of type Maybe a which elides the field on Nothing.

toOptionalField' :: (a -> Value) -> ToJsonField (Maybe a) Source #

Given an encoding function for some type a, produce a ToField for fields of type Maybe a which elides the field on Nothing.

defaultValFromOptionalField :: forall s a e. a -> JsonFormat e a -> FromJsonField e (s :-> a) Source #

Given a default a value to use if the field is missing and a parser to use if the field is present, produce a FromJsonField e (s :-> a).

type JsonFormatRecord e rs = Rec (JsonField e) rs Source #

Type of a Vinyl 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 JsonFormatRecord for User might be:

  userFormatRec :: JsonFormatRecord e User
  userFormatRec = field integralJsonFormat
               :& field textJsonFormat
               :& RNil

Or, using the default mappings for each field type:

  userFormatRec :: JsonFormatRecord e User
  userFormatRec = defaultJsonFormatRecord

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 defaultJsonFormatRecord, 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 :: JsonFormatRecord e User
  userFormatRec = over (rlens fId) ('Composite.Aeson.Base.dimapJsonFormat (+10) (subtract 10)) defaultJsonFormatRecord

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 JsonFormatRecord for your case, use recordJsonFormat to make a JsonFormat e (Record '[…]) of it.

type ToJsonFormatRecord rs = Rec ToJsonField rs Source #

Type of a Vinyl record which describes how to map fields of a record from a JSON object.

Once you've produced an appropriate ToJsonFormatRecord for your case, use recordToJson' to make a ToJson (Record '[…]) of it.

type FromJsonFormatRecord e rs = Rec (FromJsonField e) rs Source #

Type of a Vinyl record which describes how to map fields of a record from a JSON object.

'fromJsonFrmaOnce you've produced an appropriate FromJsonFormatRecord for your case, use recordFromJson' to make a FromJson e (Record '[…]) of it.

toJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> ToJsonFormatRecord rs Source #

Given a Rec (JsonField e) rs which describes how to encode or decode a record, produce a Rec ToField rs which describes only how to encode the record.

fromJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> FromJsonFormatRecord e rs Source #

Given a JsonFormatRecord rs which describes how to encode or decode a record, produce a FromJsonFormatRecord rs which describes only how to decode the record.

class DefaultJsonFormatRecord rs Source #

Class to make a JsonFormatRecord with defaultJsonFormat for each field.

Minimal complete definition

defaultJsonFormatRecord

Instances

Instances details
DefaultJsonFormatRecord ('[] :: [TYPE LiftedRep]) Source # 
Instance details

Defined in Composite.Aeson.Record

(KnownSymbol s, DefaultJsonFormat a, DefaultJsonFormatRecord rs) => DefaultJsonFormatRecord ((s :-> a) ': rs) Source # 
Instance details

Defined in Composite.Aeson.Record

defaultJsonFormatRecord :: DefaultJsonFormatRecord rs => JsonFormatRecord e rs Source #

Produce a JsonFormatRecord for a record with fields rs by using the default JsonFormat for each field in rs, as provided by DefaultJsonFormat.

class RecordToJsonObject rs 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

recordToJsonObject

Instances

Instances details
RecordToJsonObject ('[] :: [Type]) Source # 
Instance details

Defined in Composite.Aeson.Record

(KnownSymbol s, RecordToJsonObject rs) => RecordToJsonObject ((s :-> a) ': rs) Source # 
Instance details

Defined in Composite.Aeson.Record

Methods

recordToJsonObject :: Rec ToJsonField ((s :-> a) ': rs) -> Rec Identity ((s :-> a) ': rs) -> Object Source #

recordToJsonObject :: RecordToJsonObject rs => Rec ToJsonField rs -> Rec Identity rs -> Object Source #

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

recordToJson :: RecordToJsonObject rs => Rec ToJsonField 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 . recordToJsonObject fmt

class RecordFromJson rs 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

recordFromJson

Instances

Instances details
RecordFromJson ('[] :: [Type]) Source # 
Instance details

Defined in Composite.Aeson.Record

Methods

recordFromJson :: Rec (FromJsonField e) '[] -> Parse e (Rec Identity '[]) Source #

(KnownSymbol s, RecordFromJson rs) => RecordFromJson ((s :-> a) ': rs) Source # 
Instance details

Defined in Composite.Aeson.Record

Methods

recordFromJson :: Rec (FromJsonField e) ((s :-> a) ': rs) -> Parse e (Rec Identity ((s :-> a) ': rs)) Source #

recordFromJson :: RecordFromJson rs => Rec (FromJsonField 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.

recordJsonFormat :: (RMap rs, RecordToJsonObject rs, RecordFromJson rs) => JsonFormatRecord e rs -> JsonFormat e (Rec Identity rs) Source #

Take a JsonFormatRecord describing how to map a record with field rs to and from JSON and produce a JsonFormat e (Record rs).

See JsonFormatRecord for more.

valMaybeParser :: forall s a e. Parse e a -> (FromJsonField e :. Maybe) (s :-> a) Source #

Promote an ABE value parser to one which can be used to parse a field of a sparse JSON record using maybeRecordFromJson.

valMaybeField :: forall s a e. JsonFormat e a -> (FromJsonField e :. Maybe) (s :-> a) Source #

Promote a value parser to one which can be used to parse a field of a sparse JSON record using maybeRecordFromJson.

class MaybeRecordFromJson rs Source #

Class to make a parser for a Rec Maybe rs given a Rec of parsers.

Minimal complete definition

maybeRecordFromJson

Instances

Instances details
MaybeRecordFromJson ('[] :: [Type]) Source # 
Instance details

Defined in Composite.Aeson.Record

(KnownSymbol s, MaybeRecordFromJson rs) => MaybeRecordFromJson ((s :-> a) ': rs) Source # 
Instance details

Defined in Composite.Aeson.Record

Methods

maybeRecordFromJson :: Rec (FromJsonField e :. Maybe) ((s :-> a) ': rs) -> Parse e (Rec Maybe ((s :-> a) ': rs)) Source #