Module

Data.Codec.Argonaut.Compat

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

Codecs that are compatible with purescript-argonaut-codecs.

#foreignObject Source

foreignObject :: forall a. JsonCodec a -> JsonCodec (Object a)

A codec for StrMap values.

Encodes as a JSON object with the keys as properties.

encode (foreignObject int) (Foreign.Object.fromFoldable [Tuple "a" 1, Tuple "b" 2]) == "{ \"a\": 1, \"b\": 2}"

#maybe Source

maybe :: forall a. JsonCodec a -> JsonCodec (Maybe a)

A codec for Maybe values.

Encodes and decodes Nothing as null

Note: this codec cannot represent nested Maybe values in a lossless manner.

Re-exports from Data.Codec.Argonaut.Common

#JsonCodec Source

type JsonCodec a = Codec' (Either JsonDecodeError) Json a

Codec type for Json values.

#JPropCodec Source

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

Codec type for JObject prop/value pairs.

#JIndexedCodec Source

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

Codec type for specifically indexed JArray elements.

#Codec' Source

type Codec' :: (Type -> Type) -> Type -> Type -> Typetype Codec' m a b = Codec m a a b b

#Codec Source

data Codec :: (Type -> Type) -> Type -> Type -> Type -> Type -> Typedata Codec m a b c d

Constructors

Instances

#void Source

void :: JsonCodec Void

A codec for Void values.

#tuple Source

tuple :: forall a b. JsonCodec a -> JsonCodec b -> JsonCodec (Tuple a b)

A codec for Tuple values.

Encodes as a two-element array in JSON.

#string Source

string :: JsonCodec String

A codec for String values in Json.

#strMap Source

strMap :: forall a. JsonCodec a -> JsonCodec (Map String a)

A codec for Map values which have string keys.

Encodes as an object in JSON.

#set Source

set :: forall a. Ord a => JsonCodec a -> JsonCodec (Set a)

A codec for Set values.

Encodes as an array in JSON.

#recordPropOptional Source

recordPropOptional :: forall p a r r'. IsSymbol p => Cons p (Maybe a) r r' => Proxy p -> JsonCodec a -> JPropCodec (Record r) -> JPropCodec (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.

#recordProp Source

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

#record Source

record :: JPropCodec (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.Argonaut as CA
import Type.Proxy (Proxy(..))

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

codecPerson ∷ CA.JsonCodec Person
codecPerson =
  CA.object "Person" $ CA.record
    # CA.recordProp (Proxy :: _ "name") CA.string
    # CA.recordProp (Proxy :: _ "age") CA.int

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

#prop Source

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

A codec for a property of an object.

#prismaticCodec Source

prismaticCodec :: forall a b. String -> (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 review functions of a Prism-style lens.

An example of this would be a codec for Data.String.NonEmpty.NonEmptyString:

nonEmptyString ∷ CA.JsonCodec NES.NonEmptyString
nonEmptyString = CA.prismaticCodec "NonEmptyString" NES.fromString NES.toString CA.string

Another example might be to handle a mapping from a small sum type to strings:

data Direction = North | South | West | East

directionCodec :: JsonCodec Direction
directionCodec = CA.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.Argonaut.Generic.nullarySum and Data.Codec.Argonaut.Sum.enumSum.

#printJsonDecodeError Source

printJsonDecodeError :: JsonDecodeError -> String

Prints a JsonDecodeError as a somewhat readable error message.

#object Source

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

A codec for objects that are encoded with specific properties.

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

#number Source

number :: JsonCodec Number

A codec for Number values in Json.

#null Source

null :: JsonCodec Unit

A codec for null values in Json.

#nonEmptyString Source

nonEmptyString :: JsonCodec NonEmptyString

A codec for NonEmptyString values.

Encodes as the standard type in JSON, but will fail to decode if the string is empty.

#nonEmptySet Source

nonEmptySet :: forall a. Ord a => JsonCodec a -> JsonCodec (NonEmptySet a)

A codec for NonEmptySet values.

Encodes as an array in JSON.

#nonEmptyList Source

nonEmptyList :: forall a. JsonCodec a -> JsonCodec (NonEmptyList a)

A codec for NonEmptyList values.

Encodes as an array in JSON.

#nonEmptyArray Source

nonEmptyArray :: forall a. JsonCodec a -> JsonCodec (NonEmptyArray a)

A codec for NonEmptyArray values.

Encodes as the standard type in JSON, but will fail to decode if the array is empty.

#named Source

named :: forall a. String -> JsonCodec a -> JsonCodec a

A codec for introducing names into error messages - useful when definiting a codec for a type synonym for a record, for instance.

#map Source

map :: forall a b. Ord a => JsonCodec a -> JsonCodec b -> JsonCodec (Map a b)

A codec for Map values.

Encodes as an array of two-element key/value arrays in JSON.

#list Source

list :: forall a. JsonCodec a -> JsonCodec (List a)

A codec for List values.

Encodes as an array in JSON.

#json Source

json :: JsonCodec Json

The "identity codec" for Json values.

#jobject Source

jobject :: JsonCodec (Object Json)

A codec for JObject 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.

#int Source

int :: JsonCodec Int

A codec for Int values in Json.

#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, 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 ((~))
import Data.Codec.Argonaut as CA

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

codecPerson ∷ CA.JsonCodec Person
codecPerson = CA.indexedArray "Test Object" $
  { name: _, age: _ }
    <$> _.name ~ CA.index 0 CA.string
    <*> _.age ~ CA.index 1 CA.int

#index Source

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

A codec for an item in an indexedArray.

#identity Source

identity :: forall m a. Applicative m => Codec m a a a a

#hoist Source

hoist :: forall m m' a b c d. (m ~> m') -> Codec m a b c d -> Codec m' a b c d

#fix Source

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

Helper function for defining recursive codecs in situations where the codec definition causes a "The value of <codec> is undefined here" error.

import Data.Codec.Argonaut as CA
import Data.Codec.Argonaut.Common as CAC
import Data.Codec.Argonaut.Record as CAR
import Data.Maybe (Maybe)
import Data.Newtype (class Newtype)
import Data.Profunctor (wrapIso)

newtype IntList = IntList { cell ∷ Int, rest ∷ Maybe IntList }

derive instance newtypeLoopyList ∷ Newtype IntList _

codecIntList ∷ CA.JsonCodec IntList
codecIntList =
  CA.fix \codec →
    wrapIso IntList $
      CAR.object "IntList" { cell: CA.int, rest: CAC.maybe codec }

#encode Source

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

#either Source

either :: forall a b. JsonCodec a -> JsonCodec b -> JsonCodec (Either a b)

A codec for Either values.

#decode Source

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

#coercible Source

coercible :: forall a b. Coercible a b => String -> JsonCodec a -> JsonCodec b

A codec for types that can be safely coerced.

Accepts the name of the target type as an argument to improve error messaging when the inner codec fails.

#codec' Source

codec' :: forall m a b. (a -> m b) -> (b -> a) -> Codec' m a b

#codec Source

codec :: forall m a b c. (a -> m c) -> (c -> b) -> Codec m a b c c

#codePoint Source

codePoint :: JsonCodec CodePoint

A codec for Codepoint values in Json.

#char Source

char :: JsonCodec Char

A codec for Char values in Json.

#boolean Source

boolean :: JsonCodec Boolean

A codec for Boolean values in Json.

#array Source

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

A codec for arbitrary length Arrays where every item in the array shares the same type.

import Data.Codec.Argonaut as CA

codecIntArray ∷ CA.JsonCodec (Array Int)
codecIntArray = CA.array CA.int

#(~) 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

#(>~>) Source

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

#(<~<) Source

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