Module

Data.Codec.JSON.Sum

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

#taggedSum Source

taggedSum :: forall tag a. String -> (tag -> String) -> (String -> Maybe tag) -> (tag -> Either a (JSON -> Either DecodeError a)) -> (a -> Tuple tag (Maybe JSON)) -> Codec a

A helper for defining JSON codecs for sum types. To ensure exhaustivity there needs to be a mapping to and from a tag type for the type to be encoded.

  • The first argument is the name of the type being decoded, for error message purposes.
  • The second argument maps a tag value to a string to use in the encoding.
  • The third argument maps a string back to a tag value during decoding.
  • The fourth argument returns either a constant value or a decoder function based on a tag value.
  • The fifth argument returns a tag value and optional encoded value to store for a constructor of the sum.

#enumSum Source

enumSum :: forall a. (a -> String) -> (String -> Maybe a) -> Codec a

A helper for defining JSON codecs for "enum" sum types, where every constructor is nullary, and the type will be encoded as a string.