Data.Codec.JSON
- Package
- purescript-codec-json
- Repository
- garyb/purescript-codec-json
#encode Source
encode :: forall a b c d. Codec (Except DecodeError) a b c d -> c -> bEncodes a value as JSON using the specified code.
#decode Source
decode :: forall a b c d. Codec (Except DecodeError) a b c d -> a -> Either DecodeError dTries to decode JSON to a value using the specified code.
#IndexedCodec Source
type IndexedCodec a = Codec (Except DecodeError) JArray (List JSON) a aCodec type for specifically indexed JArray elements.
#indexedArray Source
indexedArray :: forall a. IndexedCodec a -> Codec aA codec for types that are encoded as an array with a specific layout.
For example, if we'd like to encode a Person as a 2-element array, like
["Rashida", 37], we could write the following codec:
import Data.Codec.JSON ((~))
import Data.Codec.JSON as CJ
type Person = { name ∷ String, age ∷ Int }
codecPerson ∷ CJ.Codec Person
codecPerson = CJ.indexedArray $
{ name: _, age: _ }
<$> _.name ~ CJ.index 0 CJ.string
<*> _.age ~ CJ.index 1 CJ.int
#index Source
index :: forall a. Int -> Codec a -> IndexedCodec aA codec for an item in an indexedArray.
#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
import Type.Proxy (Proxy(..))
type Person = { name ∷ String, age ∷ Int }
codecPerson ∷ CJ.Codec Person
codecPerson =
CJ.object $ CJ.record
# CJ.recordProp (Proxy :: _ "name") CJ.string
# CJ.recordProp (Proxy :: _ "age") CJ.int
See also Data.Codec.JSON.Record.object for a more commonly useful
version of this function.
#recordPropOptional Source
recordPropOptional :: forall p a r r'. IsSymbol p => Cons p (Maybe a) r r' => Proxy p -> 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.
#nullable Source
nullable :: forall a. Codec a -> Codec (Maybe a)A codec for JSON values that can be null or some other value.
This should not be used if an accurate representation of nested Maybe values is required, as
values like Just Nothing cannot be encoded. For nested Maybes consider using
Data.Codec.JSON.Common.maybe instead.
#prismaticCodec Source
prismaticCodec :: forall a b. String -> (a -> Maybe b) -> (b -> a) -> Codec a -> Codec bAdapts an existing codec with a pair of functions to allow a value to be
further refined. If the inner decoder fails an UnexpectedValue error will
be raised for JSON input.
This function is named as such as the pair of functions it accepts
correspond with the preview and review functions of a Prism-style lens.
An example of this would be a codec for Data.String.NonEmpty.NonEmptyString:
nonEmptyString ∷ CJ.Codec NES.NonEmptyString
nonEmptyString = CJ.prismaticCodec "NonEmptyString" NES.fromString NES.toString CJ.string
Another example might be to handle a mapping from a small sum type to strings:
data Direction = North | South | West | East
directionCodec :: Codec Direction
directionCodec = CJ.prismaticCodec "Direction" dec enc string
where
dec = case _ of
"N" -> Just North
"S" -> Just South
"W" -> Just West
"E" -> Just East
_ -> Nothing
enc = case _ of
North -> "N"
South -> "S"
West -> "W"
East -> "E"
Although for this latter case there are some other options too, in the form
of Data.Codec.JSON.Generic.nullarySum and Data.Codec.JSON.Sum.enumSum.
Re-exports from Codec.JSON.DecodeError
#DecodeError Source
newtype DecodeErrorType for failures while decoding, a path to the point in the JSON that failure occurred, a message describing the problem, and a list of further causes for the failure.
Constructors
Instances
Re-exports from Data.Codec
#identity Source
identity :: forall m a. Applicative m => Codec m a a a a#(~) Source
Operator alias for Data.Profunctor.lcmap (left-associative / precedence 5)
Codec is defined as a Profunctor so that lcmap can be used to target
specific fields when defining a codec for a product type. This operator
is a convenience for that:
tupleCodec =
Tuple
<$> fst ~ fstCodec
<*> snd ~ sndCodec