Module

Data.Codec.Argonaut

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

#JsonCodec Source

type JsonCodec a = BasicCodec (Either JsonDecodeError) Json a

Codec type for Json values.

#printJsonDecodeError Source

printJsonDecodeError :: JsonDecodeError -> String

Prints a JsonDecodeError as a somewhat readable error message.

#json Source

json :: JsonCodec Json

The "identity codec" for Json values.

#null Source

null :: JsonCodec Unit

A codec for null values in Json.

#boolean Source

boolean :: JsonCodec Boolean

A codec for Boolean values in Json.

#number Source

number :: JsonCodec Number

A codec for Number values in Json.

#int Source

int :: JsonCodec Int

A codec for Int values in Json.

#string Source

string :: JsonCodec String

A codec for String values in Json.

#char Source

char :: JsonCodec Char

A codec for Char values in Json.

#jarray Source

jarray :: JsonCodec (Array Json)

A codec for Array Json values in Json. This does not decode the values of the array, for that use array for a general array decoder, or indexedArray with index to decode fixed length array encodings.

#jobject Source

jobject :: JsonCodec (Object Json)

A codec for JObject values in Json.

#void Source

void :: JsonCodec Void

A codec for Void values.

#array Source

array :: forall a. JsonCodec a -> JsonCodec (Array a)

A codec for Array values.

decodeIntArray ∷ Json → Either JsonDecodeError (Array Int)
decodeIntArray = decode (array int)

#JIndexedCodec Source

type JIndexedCodec a = Codec (Either JsonDecodeError) (Array Json) (List Json) a a

Codec type for specifically indexed JArray elements.

#indexedArray Source

indexedArray :: forall a. String -> JIndexedCodec a -> JsonCodec a

A codec for types that are encoded as an array with a specific layout.

For example, given that we'd like to encode a Person as a 2-element array, like so [ "Karl", 25 ], we could write the following codec:

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

JA.indexedArray "Test Object" $
  { name: _, age: _ }
    <$> _.name ~ index 0 JA.string
    <*> _.age ~ index 1 JA.int

#index Source

index :: forall a. Int -> JsonCodec a -> JIndexedCodec a

A codec for an item in an indexedArray.

#JPropCodec Source

type JPropCodec a = Codec (Either JsonDecodeError) (Object Json) (List (Tuple String Json)) a a

Codec type for JObject prop/value pairs.

#object Source

object :: forall a. String -> JPropCodec a -> JsonCodec a

A codec for objects that are encoded with specific properties.

#prop Source

prop :: forall a. String -> JsonCodec a -> JPropCodec a

A codec for a property of an object.

#record Source

record :: JPropCodec {  }

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:

myRecordCodec =
  object "MyRecord" $ record
    # recordProp (SProxy :: SProxy "tag") tagCodec
    # recordProp (SProxy :: SProxy "value") valueCodec

#recordProp Source

recordProp :: forall p a r r'. IsSymbol p => Cons p a r r' => SProxy p -> JsonCodec a -> JPropCodec ({  | r }) -> JPropCodec ({  | 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.

#fix Source

fix :: forall a. (JsonCodec a -> JsonCodec a) -> JsonCodec a

Helper function for defining recursive codecs.

#prismaticCodec Source

prismaticCodec :: forall a b. (a -> Maybe b) -> (b -> a) -> JsonCodec a -> JsonCodec b

Adapts 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 view functions of a Prism-style lens.

For example, in order to parse a mapping from an enum to strings, which doesn't match up nicely with Data.Codec.Argonaut.Sum.enumSum we can use prismaticCodec:

data Direction = North | South | West | East

directionCodec :: JsonCodec Direction
directionCodec = prismaticCodec 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"

Re-exports from Data.Codec

#encode Source

encode :: forall m a b c d. Codec m a b c d -> c -> b

#decode Source

decode :: forall m a b c d. Codec m a b c d -> a -> m d

#(~) Source

Operator alias for Data.Profunctor.lcmap (left-associative / precedence 5)

GCodec is defined as a Profunctor so that lmap 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

#(<~<) Source

Operator alias for Data.Codec.composeCodec (right-associative / precedence 8)