Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- newtype ToJsonField a = ToJsonField {
- unToJsonField :: a -> Maybe Value
- newtype FromJsonField e a = FromJsonField {
- unFromJsonField :: 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'
- valField :: forall s a e. JsonFormat e a -> JsonField e (s :-> a)
- field' :: JsonFormat e a -> JsonField e a
- fromField :: Wrapped a => Parse e (Unwrapped a) -> FromJsonField e a
- valFromField :: forall s a e. Parse e a -> FromJsonField e (s :-> a)
- fromField' :: Parse e a -> FromJsonField e a
- toField :: (Wrapped a', Unwrapped a' ~ a) => (a -> Value) -> ToJsonField a'
- toField' :: (a -> Value) -> ToJsonField a
- defaultField :: (Wrapped a', Unwrapped a' ~ a) => a -> JsonFormat e a -> JsonField e a'
- valDefaultField :: forall s a e. a -> JsonFormat e a -> JsonField e (s :-> a)
- defaultField' :: a -> JsonFormat e a -> JsonField e a
- optionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => JsonFormat e a -> JsonField e a'
- valOptionalField :: forall s a e. JsonFormat e a -> JsonField e (s :-> Maybe a)
- optionalField' :: JsonFormat e a -> JsonField e (Maybe a)
- fromOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => Parse e a -> FromJsonField e a'
- valFromOptionalField :: forall s a e. Parse e a -> FromJsonField e (s :-> Maybe a)
- fromOptionalField' :: Parse e a -> FromJsonField e (Maybe a)
- toOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => (a -> Value) -> ToJsonField a'
- toOptionalField' :: (a -> Value) -> ToJsonField (Maybe a)
- defaultValFromOptionalField :: forall s a e. a -> JsonFormat e a -> FromJsonField e (s :-> a)
- type JsonFormatRecord e rs = Rec (JsonField e) rs
- type ToJsonFormatRecord rs = Rec ToJsonField rs
- type FromJsonFormatRecord e rs = Rec (FromJsonField e) rs
- zipJsonFormatRecord :: (RMap rs, RApply rs) => ToJsonFormatRecord rs -> FromJsonFormatRecord e rs -> JsonFormatRecord e rs
- toJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> ToJsonFormatRecord rs
- fromJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> FromJsonFormatRecord e rs
- class DefaultJsonFormatRecord rs
- defaultJsonFormatRecord :: DefaultJsonFormatRecord rs => JsonFormatRecord e rs
- class RecordToJsonObject rs
- recordToJsonObject :: RecordToJsonObject rs => Rec ToJsonField rs -> Rec Identity rs -> Object
- recordToJson :: RecordToJsonObject rs => Rec ToJsonField rs -> Rec Identity rs -> Value
- class RecordFromJson rs
- recordFromJson :: RecordFromJson rs => Rec (FromJsonField e) rs -> Parse e (Rec Identity rs)
- recordJsonFormat :: (RMap rs, RecordToJsonObject rs, RecordFromJson rs) => JsonFormatRecord e rs -> JsonFormat e (Rec Identity rs)
- valMaybeParser :: forall s a e. Parse e a -> (FromJsonField e :. Maybe) (s :-> a)
- valMaybeField :: forall s a e. JsonFormat e a -> (FromJsonField e :. Maybe) (s :-> a)
- class MaybeRecordFromJson rs
- maybeRecordFromJson :: MaybeRecordFromJson rs => Rec (FromJsonField e :. Maybe) rs -> Parse e (Rec Maybe rs)
Documentation
newtype ToJsonField a Source #
Function to encode a single field of a record, possibly choosing to elide the field with Nothing
.
ToJsonField | |
|
Instances
Contravariant ToJsonField Source # | |
Defined in Composite.Aeson.Record 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.
FromJsonField | |
|
Instances
Functor (FromJsonField e) Source # | |
Defined in Composite.Aeson.Record fmap :: (a -> b) -> FromJsonField e a -> FromJsonField e b # (<$) :: a -> FromJsonField e b -> FromJsonField e a # |
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.
fromField :: Wrapped a => Parse e (Unwrapped a) -> FromJsonField e a Source #
Given a parser for
, produce a Unwrapped
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
, produce a Unwrapped
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
where FromField
e bb
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
which represents an optional field.FromField
e (Maybe a)
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
of it.JsonFormat
e (Record '[…])
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
of it.ToJson
(Record '[…])
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
of it.FromJson
e (Record '[…])
zipJsonFormatRecord :: (RMap rs, RApply rs) => ToJsonFormatRecord rs -> FromJsonFormatRecord e rs -> JsonFormatRecord e rs Source #
Zip up a matching FromJsonFormatRecord
and ToJsonFormatRecord
into a JsonFormatRecord
.
Reverse operation of fromJsonFormatRecord
and toJsonFormatRecord
.
toJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> ToJsonFormatRecord rs Source #
fromJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> FromJsonFormatRecord e rs Source #
Given a
which describes how to encode or decode a record, produce a JsonFormatRecord
rs
which describes
only how to decode the record.FromJsonFormatRecord
rs
class DefaultJsonFormatRecord rs Source #
Class to make a JsonFormatRecord
with defaultJsonFormat
for each field.
Instances
DefaultJsonFormatRecord ('[] :: [TYPE LiftedRep]) Source # | |
Defined in Composite.Aeson.Record defaultJsonFormatRecord :: JsonFormatRecord e '[] Source # | |
(KnownSymbol s, DefaultJsonFormat a, DefaultJsonFormatRecord rs) => DefaultJsonFormatRecord ((s :-> a) ': rs) Source # | |
Defined in Composite.Aeson.Record defaultJsonFormatRecord :: JsonFormatRecord e ((s :-> a) ': rs) Source # |
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.
Instances
RecordToJsonObject ('[] :: [Type]) Source # | |
Defined in Composite.Aeson.Record recordToJsonObject :: Rec ToJsonField '[] -> Rec Identity '[] -> Object Source # | |
(KnownSymbol s, RecordToJsonObject rs) => RecordToJsonObject ((s :-> a) ': rs) Source # | |
Defined in Composite.Aeson.Record recordToJsonObject :: Rec ToJsonField ((s :-> a) ': rs) -> Rec Identity ((s :-> a) ': rs) -> Object Source # |
recordToJsonObject :: RecordToJsonObject rs => Rec ToJsonField rs -> Rec Identity rs -> Object Source #
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.
Instances
RecordFromJson ('[] :: [Type]) Source # | |
Defined in Composite.Aeson.Record recordFromJson :: Rec (FromJsonField e) '[] -> Parse e (Rec Identity '[]) Source # | |
(KnownSymbol s, RecordFromJson rs) => RecordFromJson ((s :-> a) ': rs) Source # | |
Defined in Composite.Aeson.Record 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 #
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.
Instances
MaybeRecordFromJson ('[] :: [Type]) Source # | |
Defined in Composite.Aeson.Record maybeRecordFromJson :: Rec (FromJsonField e :. Maybe) '[] -> Parse e (Rec Maybe '[]) Source # | |
(KnownSymbol s, MaybeRecordFromJson rs) => MaybeRecordFromJson ((s :-> a) ': rs) Source # | |
Defined in Composite.Aeson.Record |
maybeRecordFromJson :: MaybeRecordFromJson rs => Rec (FromJsonField e :. Maybe) rs -> Parse e (Rec Maybe rs) Source #