Module

Codec.Json.Unidirectional.Value

Package
purescript-json-codecs
Repository
jordanmartinez/purescript-json-codecs

Unidirectional, value-based JSON codecs. This module should be imported using a qualified J or Json alias:

import Codec.Json.Unidirectional.Value as J
import Codec.Json.Unidirectional.Value as Json

thereby causing to* and from* code to read like so:

  • J.fromInt/Json.fromInt, which reads "encode an Int to Json"
  • J.toInt/Json.toInt, which reads "decode JSON to an Int"

#DecodeError Source

data DecodeError

An error type that

  • includes path-to-json information
  • allows for custom decode messages

Constructors

Instances

#printDecodeError Source

printDecodeError :: DecodeError -> String

Pretty-prints the decode error over a multi-line string.

#coerce1 Source

coerce1 :: forall n a. Coercible n a => (a -> n) -> Either DecodeError a -> Either DecodeError n

#FromPrimitive Source

class FromPrimitive a  where

Indicates which values are primitive JSON values that can be encoded via unsafeCoerce.

Members

Instances

#AllPrimitive Source

class AllPrimitive :: RowList Type -> Constraintclass AllPrimitive rl 

Utility class that distinguishes which records are primitive and which are not. Used in FromPrimitive.

Instances

#fromVoid Source

#fromJNull Source

#fromNull Source

fromNull :: forall a. a -> Json

#fromUnit Source

#toNullDefaultOrA Source

toNullDefaultOrA :: forall a. a -> (Json -> Either DecodeError a) -> Json -> Either DecodeError a

#fromNullNothingOrJust Source

fromNullNothingOrJust :: forall a. (a -> Json) -> Maybe a -> Json

#toNullNothingOrJust Source

#fromNullable Source

fromNullable :: forall a. (a -> Json) -> Nullable a -> Json

#toNullable Source

#fromBoolean Source

#fromNumber Source

#fromInt Source

#fromString Source

#fromChar Source

#fromCodePoint Source

#fromJArray Source

#underIndex Source

#fromArray Source

fromArray :: forall a. (a -> Json) -> Array a -> Json

#toArray Source

toArray :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (Array a)

#fromArray2 Source

#toArray2 Source

toArray2 :: forall a b x. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> (a -> b -> x) -> Json -> Either DecodeError x

#fromArray3 Source

#toArray3 Source

toArray3 :: forall a b c x. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> (Json -> Either DecodeError c) -> (a -> b -> c -> x) -> Json -> Either DecodeError x

#fromArray4 Source

fromArray4 :: Json -> Json -> Json -> Json -> Json

#toArray4 Source

toArray4 :: forall a b c d x. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> (Json -> Either DecodeError c) -> (Json -> Either DecodeError d) -> (a -> b -> c -> d -> x) -> Json -> Either DecodeError x

#fromArray5 Source

fromArray5 :: Json -> Json -> Json -> Json -> Json -> Json

#toArray5 Source

toArray5 :: forall a b c d e x. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> (Json -> Either DecodeError c) -> (Json -> Either DecodeError d) -> (Json -> Either DecodeError e) -> (a -> b -> c -> d -> e -> x) -> Json -> Either DecodeError x

#fromNonEmptyArray Source

fromNonEmptyArray :: forall a. (a -> Json) -> NonEmptyArray a -> Json

#toNonEmptyArray Source

#underKey Source

#fromObject Source

fromObject :: forall a. (a -> Json) -> Object a -> Json

#toObject Source

#fromObjSingleton Source

#toObjSingleton Source

#fromIdentity Source

fromIdentity :: forall a. (a -> Json) -> Identity a -> Json

#toIdentity Source

#fromMaybeTagged Source

fromMaybeTagged :: forall a. (a -> Json) -> Maybe a -> Json

#toMaybeTagged Source

#fromEitherTagged Source

fromEitherTagged :: forall a b. (a -> Json) -> (b -> Json) -> Either a b -> Json

#toEitherTagged Source

#fromEitherSingle Source

fromEitherSingle :: forall a b. (a -> Json) -> (b -> Json) -> Either a b -> Json

#toEitherSingle Source

#fromTuple Source

fromTuple :: forall a b. (a -> Json) -> (b -> Json) -> Tuple a b -> Json

#toTuple Source

toTuple :: forall a b. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> Json -> Either DecodeError (Tuple a b)

#fromThese Source

fromThese :: forall a b. (a -> Json) -> (b -> Json) -> These a b -> Json

#toThese Source

toThese :: forall a b. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> Json -> Either DecodeError (These a b)

#fromNonEmpty Source

fromNonEmpty :: forall f a. (a -> Json) -> (f a -> Json) -> NonEmpty f a -> Json

#toNonEmpty Source

toNonEmpty :: forall g a. (Json -> Either DecodeError a) -> (Json -> Either DecodeError (g a)) -> (Json -> Either DecodeError (NonEmpty g a))

#fromList Source

fromList :: forall a. (a -> Json) -> List a -> Json

#toList Source

toList :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (List a)

#fromNonEmptyList Source

fromNonEmptyList :: forall a. (a -> Json) -> (NonEmptyList a -> Json)

#toNonEmptyList Source

#fromMap Source

fromMap :: forall k v. (k -> Json) -> (v -> Json) -> Map k v -> Json

#toMap Source

toMap :: forall k v. Ord k => (Json -> Either DecodeError k) -> (Json -> Either DecodeError v) -> Json -> Either DecodeError (Map k v)

#fromSet Source

fromSet :: forall a. (a -> Json) -> Set a -> Json

#toSet Source

toSet :: forall a. Ord a => (Json -> Either DecodeError a) -> Json -> Either DecodeError (Set a)

#fromNonEmptySet Source

fromNonEmptySet :: forall a. (a -> Json) -> NonEmptySet a -> Json

#toNonEmptySet Source

#fromRecord Source

fromRecord :: forall codecs values alphaSortedCodecsRl insertOrderThenAlphaSortedCodecsRL. RowToList codecs alphaSortedCodecsRl => IntThenAlphaSortedRowList alphaSortedCodecsRl insertOrderThenAlphaSortedCodecsRL => FromRecordObj insertOrderThenAlphaSortedCodecsRL (Record codecs) (Record values) => Record codecs -> Record values -> Json

All labels must have a value of type FromProp a, which can be obtained via functions like fromRequired* and fromOption* or by defining a value yourself. Otherwise, you will get a compiler error:

fromRecord
  { label: fromRequired $ fromInt 
  , psName: fromRequiredRename "jsonName" fromInt
  , optionalDisappears: fromOption $ fromInt
  , optionalStillThere: fromOption $ fromInt
  }
  { label: 1
  , psName: Just 2
  , optionalDisappears: Nothing
  , optionalStillThere: Just 3
  }

produces the following JSON

{ "label": 1
, "jsonName": 2
, "optionalStillThere": 3
}

#toRecord Source

toRecord :: forall codecs values codecsRL. RowToList codecs codecsRL => ToRecordObj codecsRL (Record codecs) (Record values) => Record codecs -> Json -> Either DecodeError (Record values)

All labels must have a value of type ToProp a, which can be obtained via functions like toRequired* and toOption* or by defining a value yourself. Otherwise, you will get a compiler error:

The following JSON and codec...

toRecord
  { label: toRequired toInt 
  , psName: toRequiredRename "jsonName" toInt
  , optionalAppears: toOption toInt
  , optionalAlwaysThere: toOption toInt
  }
  $ either (\_ -> unsafeCrashWith "error") identity
  $ jsonParser
  """{ "label": 1
     , "jsonName": 2
     , "optionalAlwaysThere": 3
     }"""

...produces the following value

  { label: 1
  , psName: Just 2
  , optionalAppears: Nothing
  , optionalAlwaysThere: Just 3
  }

#fromRecordN Source

fromRecordN :: forall n codecs values alphaSortedCodecsRl insertOrderThenAlphaSortedCodecsRL. RowToList codecs alphaSortedCodecsRl => IntThenAlphaSortedRowList alphaSortedCodecsRl insertOrderThenAlphaSortedCodecsRL => FromRecordObj insertOrderThenAlphaSortedCodecsRL (Record codecs) (Record values) => Newtype n (Record values) => (Record values -> n) -> Record codecs -> n -> Json

Same as fromRecord but handles the Newtype for you so you don't need to add a type annotation to help type inference.

#toRecordN Source

toRecordN :: forall n codecs values codecsRL. RowToList codecs codecsRL => ToRecordObj codecsRL (Record codecs) (Record values) => Newtype n (Record values) => (Record values -> n) -> Record codecs -> Json -> Either DecodeError n

Same as toRecord but handles the Newtype for you.

#FromProp Source

newtype FromProp :: Int -> Type -> Typenewtype FromProp insertionOrder a

Converts a value associated with the record label into a JSON value associated with the given label in the JSON object.

On Nothing, the key-value pair is not added to the JSON object. On Just, both the JSON-encoded value and the key to use in the JSON object is provided.

The Just-wrapped Tuple is a key-value pair. For the key, Nothing means the label associated with the record is used while using Just means the provided key is used (e.g. renaming).

Constructors

Instances

#ToProp Source

newtype ToProp a

Converts a value in a JSON object into a value associated with the record label.

Explanation of arguments

  • String -> Maybe Json: Looks up the provided key in the object. Implemented via \str -> Object.lookup str obj
  • String -> the label of the record

Constructors

Instances

#toStatic Source

toStatic :: forall a. a -> ToProp a

#fromRequired Source

fromRequired :: forall a. (a -> Json) -> FromPropDefaultOrder a

#fromRequired' Source

fromRequired' :: forall @i a. (a -> Json) -> FromProp i a

#toRequired Source

toRequired :: forall a. (Json -> Either DecodeError a) -> ToProp a

#fromRequiredRename Source

#fromRequiredRename' Source

fromRequiredRename' :: forall @i a. String -> (a -> Json) -> FromProp i a

#toRequiredRename Source

toRequiredRename :: forall a. String -> (Json -> Either DecodeError a) -> ToProp a

#fromOption Source

fromOption :: forall a. (a -> Json) -> FromPropDefaultOrder (Maybe a)

If Nothing, does not add the coressponding key If Just, adds the key and the encoded value to the JObject

#fromOption' Source

fromOption' :: forall @i a. (a -> Json) -> FromProp i (Maybe a)

#toOption Source

toOption :: forall a. (Json -> Either DecodeError a) -> ToProp (Maybe a)

Succeeds with Nothing if key wasn't found or with Just if key was found and value was succesfully tod.

#fromOptionRename Source

fromOptionRename :: forall a. String -> (a -> Json) -> FromPropDefaultOrder (Maybe a)

#fromOptionRename' Source

fromOptionRename' :: forall @i a. String -> (a -> Json) -> FromProp i (Maybe a)

#toOptionRename Source

toOptionRename :: forall a. String -> (Json -> Either DecodeError a) -> ToProp (Maybe a)

#toOptionDefault Source

toOptionDefault :: forall a. a -> (Json -> Either DecodeError a) -> ToProp a

#toOptionDefaultRename Source

toOptionDefaultRename :: forall a. String -> a -> (Json -> Either DecodeError a) -> ToProp a

#fromOptionArray Source

fromOptionArray :: forall a. (a -> Json) -> FromPropDefaultOrder (Array a)

#fromOptionArray' Source

fromOptionArray' :: forall @i a. (a -> Json) -> FromProp i (Array a)

#toOptionArray Source

toOptionArray :: forall a. (Json -> Either DecodeError a) -> ToProp (Array a)

#fromOptionAssocArray Source

fromOptionAssocArray :: forall a b. (a -> String) -> (b -> Json) -> FromPropDefaultOrder (Array (Tuple a b))

#fromOptionAssocArray' Source

fromOptionAssocArray' :: forall @i a b. (a -> String) -> (b -> Json) -> FromProp i (Array (Tuple a b))

#toOptionAssocArray Source

toOptionAssocArray :: forall a b. (String -> Either DecodeError a) -> (Json -> Either DecodeError b) -> ToProp (Array (Tuple a b))

#ToRecordObj Source

class ToRecordObj :: RowList Type -> Type -> Type -> Constraintclass ToRecordObj codecsRL codecs values | codecsRL -> codecs values where

Members

Instances

#FromRecordObj Source

class FromRecordObj :: RowList Type -> Type -> Type -> Constraintclass FromRecordObj codecsRL codecs values | codecsRL -> codecs values where

Members

Instances