api-tools-0.10.0.1: DSL for generating API boilerplate and docs
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.API.JSONToCBOR

Synopsis

Documentation

serialiseJSONWithSchema :: API -> TypeName -> Value -> ByteString Source #

Serialise a JSON value as a CBOR term in a generic but schema-dependent fashion. This is necessary because the JSON representation carries less information than we need in CBOR (e.g. it lacks a distinction between bytestrings and text).

There is a corner case where this may lose information: if the schema contains a field with type ? json (i.e. Maybe Value), then we have

toJSON Nothing     == Null
toJSON (Just Null) == Null

so serialiseJSONWithSchema cannot distinguish these values, and will use the CBOR-encoding of Nothing for both.

jsonToCBORWithSchema :: API -> TypeName -> Value -> Term Source #

Convert a JSON value into a CBOR term in a generic but schema-dependent fashion.

deserialiseJSONWithSchema :: API -> TypeName -> ByteString -> Value Source #

When a JSON value has been deserialised from CBOR, the representation may need some modifications in order to match the result of toJSON on a Haskell datatype. In particular, Aeson's representation of Maybe does not round-trip (because Nothing is encoded as Null and Just x as toJSON x), so CBOR uses a different representation (as an empty or 1-element list).