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
Constructors
Instances
#printDecodeError Source
printDecodeError :: DecodeError -> String
Pretty-prints the decode error over a multi-line string.
#printDecodeError' Source
printDecodeError' :: String -> DecodeError -> 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
fromPrimitive :: a -> Json
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 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.
#FromProp Source
newtype FromProp :: Int -> Type -> Type
newtype 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
ToOrdering (FromProp insertionOrder a) insertionOrder
(FromRecordObj codecTail (Record cRest) (Record vRest), IsSymbol sym, Cons sym (FromProp i a) cRest codecs, Cons sym a vRest values) => FromRecordObj (Cons sym (FromProp i a) codecTail) (Record codecs) (Record values)
#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) -> 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 :: forall a. String -> (a -> Json) -> FromPropDefaultOrder a
#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
#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)
#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))
#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)
#FromRecordObj Source
class FromRecordObj :: RowList Type -> Type -> Type -> Constraint
class FromRecordObj codecsRL codecs values | codecsRL -> codecs values where
Members
fromRecordObj :: Proxy codecsRL -> codecs -> values -> Object Json
Instances
FromRecordObj Nil (Record ()) (Record ())
(FromRecordObj codecTail (Record cRest) (Record vRest), IsSymbol sym, Cons sym (FromProp i a) cRest codecs, Cons sym a vRest values) => FromRecordObj (Cons sym (FromProp i 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.")))) => FromRecordObj (Cons sym a codecTail) (Record codecs) (Record values)
Shortcut encoder for encoding primitive JSON values.