Module

Data.Codec.JSON.Strict

Package
purescript-codec-json
Repository
garyb/purescript-codec-json

#PropCodec Source

type PropCodec a = Codec (StateT ClaimedProps (Except DecodeError)) JObject (List (Tuple String JSON)) a a

Codec type for JObject prop/value pairs.

#ClaimedProps Source

type ClaimedProps = Set String

The set of properties that have been claimed "so far" during parsing of a record. This is used internally to track which properties have been parsed in order to determine which properties are unknown.

#objectStrict Source

objectStrict :: forall a. PropCodec a -> Codec a

A codec for objects that are encoded with specific properties. This codec will fail upon encountering unknown properties in the incoming record. Use Data.Codec.JSON.object for a version that ignores unknown properties.

See also Data.Codec.JSON.Record.objectStrict for a more commonly useful version of this function.

#prop Source

prop :: forall a. String -> Codec a -> PropCodec a

A codec for a property of an object.

#record Source

record :: PropCodec (Record ())

The starting value for a object-record codec. Used with recordProp it provides a convenient method for defining codecs for record types that encode into JSON objects of the same shape.

For example, to encode a record as the JSON object { "name": "Karl", "age": 25 } we would define a codec like this:

import Data.Codec.JSON as CJ

type Person = { name ∷ String, age ∷ Int }

codecPerson ∷ CJ.Codec Person
codecPerson =
  CJ.object $ CJ.record
    # CJ.recordProp @"name" CJ.string
    # CJ.recordProp @"age" CJ.int

See also Data.Codec.JSON.Record.object for a more commonly useful version of this function.

#recordProp Source

recordProp :: forall @p a r r'. IsSymbol p => Cons p a r r' => Codec a -> PropCodec (Record r) -> PropCodec (Record r')

Used with record to define codecs for record types that encode into JSON objects of the same shape. See the comment on record for an example.

#recordPropOptional Source

recordPropOptional :: forall @p a r r'. IsSymbol p => Cons p (Maybe a) r r' => Codec a -> PropCodec (Record r) -> PropCodec (Record r')

Used with record to define an optional field.

This will only decode the property as Nothing if the field does not exist in the object - having a values such as null assigned will need handling separately.

The property will be omitted when encoding and the value is Nothing.