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
  • can accumulate errors at the same path location

Constructors

Instances

#accumulateErrors Source

accumulateErrors :: DecodeError -> DecodeError -> DecodeError

Prefer to use altAccumulate or altAccumulateLazy. The first error arg is assumed to have happened before the second error arg.

#altAccumulate Source

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

Tries the first codec. If it fails, tries the second codec. If it fails, errors from both are accumulated. Succeeds if either of the two codecs succeed.

#altAccumulateLazy Source

altAccumulateLazy :: forall a. Either DecodeError a -> (Unit -> Either DecodeError a) -> Either DecodeError a

Tries the first codec. If it fails, tries the second codec. If it fails, errors from both are accumulated. Succeeds if either of the two codecs succeed.

#printDecodeError Source

printDecodeError :: DecodeError -> String

Pretty-prints the decode error over a multi-line string. Assumes that all keys, hints, and decode error messages are single-line Strings. Indents using two spaces.

#unsafePrintDecodeError Source

unsafePrintDecodeError :: Boolean -> Int -> String -> String -> DecodeError -> String

Unsafe because no checking is done on the Int arg to determine if it's >=1 nor whether applyIndent makes sense in the given context.

Fully control whether we should apply the indent to the next line of content, how much to indent each error in AccumulateError, and what to use as a "tab"-like string sequence. Assumes that all keys, hints, and decode error messages are single-line Strings.

#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 codecsRL. RowToList codecs codecsRL => FromRecordPropArray codecsRL (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 codecsRL. RowToList codecs codecsRL => FromRecordPropArray codecsRL (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 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.

  • key: on Nothing, the label associated with the record is used; on Just, the provided key is used
  • insertionOrder: controls the order of the keys' appearance in the JSON object with lower values (e.g. 1) appearing before larger values (e.g. 100). When order is the same, reverts to alphabetical ordering. Key order is computed via fromMaybe top insertionOrder.
  • value: the JSON value to use for the key

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) -> FromProp a

#fromRequired' Source

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

#toRequired Source

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

#fromRequiredRename Source

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

#fromRequiredRename' Source

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

#toRequiredRename Source

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

#fromOption Source

fromOption :: forall a. (a -> Json) -> FromProp (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 a. Int -> (a -> Json) -> FromProp (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) -> FromProp (Maybe a)

#fromOptionRename' Source

fromOptionRename' :: forall a. Int -> String -> (a -> Json) -> FromProp (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) -> FromProp (Array a)

#fromOptionArray' Source

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

#toOptionArray Source

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

#fromOptionAssocArray Source

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

#fromOptionAssocArray' Source

fromOptionAssocArray' :: forall a b. Int -> (a -> String) -> (b -> Json) -> FromProp (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

#FromRecordPropArray Source

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

Members

Instances