Module

Data.Codec.Argonaut.Sum

Package
purescript-codec-argonaut
Repository
garyb/purescript-codec-argonaut

#enumSum Source

enumSum :: forall a. (a -> String) -> (String -> Maybe a) -> JsonCodec 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.

#taggedSum Source

taggedSum :: forall tag a. String -> (tag -> String) -> (String -> Maybe tag) -> (tag -> Either a (Json -> Either JsonDecodeError a)) -> (a -> Tuple tag (Maybe Json)) -> JsonCodec 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 second argument maps a string back to a tag value during decoding.
  • The third argument returns either a constant value or a decoder function based on a tag value.
  • The fourth argument returns a tag value and optional encoded value to store for a constructor of the sum.