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 anInt
toJson
"J.toInt
/Json.toInt
, which reads "decodeJSON
to anInt
"
#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
AtKey String DecodeError
AtIndex Int DecodeError
DecodeError String
AccumulateError (NonEmptyList DecodeError)
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 String
s.
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 String
s.
#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
fromPrimitive :: a -> Json
Shortcut encoder for encoding primitive JSON values.
Instances
FromPrimitive Boolean
FromPrimitive Number
FromPrimitive String
(FromPrimitive a) => FromPrimitive (Array a)
(FromPrimitive a) => FromPrimitive (Object a)
(RowToList rows rl, AllPrimitive rl) => FromPrimitive (Record rows)
(Fail (Beside (Text "Expected a primitive JSON type but got type: ") (Quote a))) => FromPrimitive a
#AllPrimitive Source
class AllPrimitive :: RowList Type -> Constraint
class AllPrimitive rl
Utility class that distinguishes which records are primitive and which are not.
Used in FromPrimitive
.
Instances
AllPrimitive Nil
(AllPrimitive tail, FromPrimitive a) => AllPrimitive (Cons sym a tail)
#toNullDefaultOrA Source
toNullDefaultOrA :: forall a. a -> (Json -> Either DecodeError a) -> Json -> Either DecodeError a
#toNullNothingOrJust Source
toNullNothingOrJust :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (Maybe a)
#toNullable Source
toNullable :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (Nullable a)
#fromBoolean Source
fromBoolean :: Boolean -> Json
#fromNumber Source
fromNumber :: Number -> Json
#fromString Source
fromString :: String -> Json
#fromCodePoint Source
fromCodePoint :: CodePoint -> Json
#underIndex Source
underIndex :: forall a. Int -> (Json -> Either DecodeError a) -> Array Json -> Either DecodeError a
#toArray Source
toArray :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (Array a)
#toArray2 Source
toArray2 :: forall a b x. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> (a -> b -> x) -> Json -> Either DecodeError x
#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
#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
#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
toNonEmptyArray :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (NonEmptyArray a)
#underKey Source
underKey :: forall a. String -> (Json -> Either DecodeError a) -> Object Json -> Either DecodeError a
#toObject Source
toObject :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (Object a)
#toObjSingleton Source
toObjSingleton :: forall a. String -> (Json -> Either DecodeError a) -> Json -> Either DecodeError a
#toIdentity Source
toIdentity :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (Identity a)
#toMaybeTagged Source
toMaybeTagged :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (Maybe a)
#fromEitherTagged Source
fromEitherTagged :: forall a b. (a -> Json) -> (b -> Json) -> Either a b -> Json
#toEitherTagged Source
toEitherTagged :: forall a b. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> Json -> Either DecodeError (Either a b)
#fromEitherSingle Source
fromEitherSingle :: forall a b. (a -> Json) -> (b -> Json) -> Either a b -> Json
#toEitherSingle Source
toEitherSingle :: forall a b. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> Json -> Either DecodeError (Either a b)
#toTuple Source
toTuple :: forall a b. (Json -> Either DecodeError a) -> (Json -> Either DecodeError b) -> Json -> Either DecodeError (Tuple a b)
#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))
#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
toNonEmptyList :: forall a. (Json -> Either DecodeError a) -> Json -> Either DecodeError (NonEmptyList a)
#toMap Source
toMap :: forall k v. Ord k => (Json -> Either DecodeError k) -> (Json -> Either DecodeError v) -> Json -> Either DecodeError (Map k v)
#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
toNonEmptySet :: forall a. Ord a => (Json -> Either DecodeError a) -> Json -> Either DecodeError (NonEmptySet a)
#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.
#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
: onNothing
, the label associated with the record is used; onJust
, the provided key is usedinsertionOrder
: 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 viafromMaybe 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
#fromRequired Source
fromRequired :: forall a. (a -> Json) -> FromProp a
#toRequired Source
toRequired :: forall a. (Json -> Either DecodeError a) -> ToProp 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
#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
#toOptionArray Source
toOptionArray :: forall a. (Json -> Either DecodeError a) -> ToProp (Array a)
#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 -> Constraint
class ToRecordObj codecsRL codecs values | codecsRL -> codecs values where
Members
toRecordObj :: Proxy codecsRL -> codecs -> Object Json -> Either DecodeError values
Instances
ToRecordObj Nil (Record ()) (Record ())
(ToRecordObj codecTail (Record cRest) (Record vRest), IsSymbol sym, Cons sym (ToProp a) cRest codecs, Cons sym a vRest values, Lacks sym vRest) => ToRecordObj (Cons sym (ToProp a) codecTail) (Record codecs) (Record values)
(Fail (Above (Beside (Beside (Text "Expected \'ToProp a\' for label \'") (Text sym)) (Beside (Text "\' but got type: ") (Quote a))) (Above (Text "") (Text "User likely forgot to supply an additional argument or is not using `toRequired*`/`toOption*` variants.")))) => ToRecordObj (Cons sym a codecTail) (Record codecs) (Record values)
#FromRecordPropArray Source
class FromRecordPropArray :: RowList Type -> Type -> Type -> Constraint
class FromRecordPropArray codecsRL codecs values | codecsRL -> codecs values where
Members
fromRecordPropArray :: Proxy codecsRL -> codecs -> values -> Array { insertionOrder :: Int, key :: String, value :: Json }
Instances
FromRecordPropArray Nil (Record ()) (Record ())
(FromRecordPropArray codecTail (Record cRest) (Record vRest), IsSymbol sym, Cons sym (FromProp a) cRest codecs, Cons sym a vRest values) => FromRecordPropArray (Cons sym (FromProp a) codecTail) (Record codecs) (Record values)
(Fail (Above (Beside (Beside (Text "Expected \'FromProp a\' for label \'") (Text sym)) (Beside (Text "\' but got type: ") (Quote a))) (Above (Text "") (Text "User likely forgot to supply an additional argument or is not using `fromRequired*`/`fromOption*` variants.")))) => FromRecordPropArray (Cons sym a codecTail) (Record codecs) (Record values)
- Modules
- Codec.
Json. Unidirectional. Value
Stores a reversed list of errors that happened at the same path. The error at
head
happened AFTER the errors intail
.