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}"
Re-exports from Data.Codec.Argonaut
#JsonDecodeError Source
data JsonDecodeError
Error type for failures while decoding.
Constructors
TypeMismatch String
UnexpectedValue Json
AtIndex Int JsonDecodeError
AtKey String JsonDecodeError
Named String JsonDecodeError
MissingValue
Instances
#JsonCodec Source
type JsonCodec a = BasicCodec (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.
#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.
#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.
#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
.
#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 }
#(~) Source
Operator alias for Data.Profunctor.lcmap (left-associative / precedence 5)
GCodec
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