Safe Haskell | None |
---|---|
Language | Haskell2010 |
- newtype ToJson a = ToJson {}
- newtype FromJson e a = FromJson {
- unFromJson :: Parse e a
- data JsonProfunctor e a b = JsonProfunctor (a -> Value) (Parse e b)
- _JsonProfunctor :: Iso (JsonProfunctor e a b) (JsonProfunctor e' a' b') (ToJson a, FromJson e b) (ToJson a', FromJson e' b')
- newtype JsonFormat e a = JsonFormat {
- unJsonFormat :: JsonProfunctor e a a
- toJsonWithFormat :: JsonFormat e a -> a -> Value
- fromJsonWithFormat :: JsonFormat e a -> Parse e a
- parseJsonWithFormat :: (e -> Text) -> JsonFormat e a -> Value -> Parser a
- parseJsonWithFormat' :: JsonFormat Void a -> Value -> Parser a
- dimapJsonFormat :: (b -> a) -> (a -> b) -> JsonFormat e a -> JsonFormat e b
- jsonFormatWithIso :: AnIso' b a -> JsonFormat e a -> JsonFormat e b
- wrapJsonFormat :: JsonFormat e a -> (a -> Either e b) -> (b -> a) -> JsonFormat e b
- jsonFormatWithoutCustomError :: Show e => JsonFormat e a -> JsonFormat e' a
- wrappedJsonFormat :: Wrapped a => JsonFormat e (Unwrapped a) -> JsonFormat e a
Documentation
Type of functions which take a value a
and convert it to an Value
.
Wrapper around a function of type a -> Aeson.Value
.
Doesn't currently include support for the newer Aeson Encoding machinery, but should.
Type of parsers which might emit some custom error of type e
and produce a value of type a
on success.
a
is the type of value that can be parsed from JSON using this profunctor, and e
is the type of custom error that can be produced when the JSON is
unacceptable. If your parser doesn't produce any custom errors, leave this type polymorphic.
Wrapper about an aeson-better-errors
Parse
e
a
.
FromJson | |
|
data JsonProfunctor e a b Source #
Type of profunctors which produce and consume JSON, a composition of ToJson
and FromJson
.
a
is the type of value that can be converted to Value
using this profunctor.
b
is the type of value that can be parsed from JSON using this profunctor, and e
is the type of custom error that can be produced when the JSON is
unacceptable. If your parser doesn't produce any custom errors, leave this type polymorphic.
Profunctors must have two type parameters a
and b
so this type has two, but JsonProfunctor
s with different types aren't useful for JSON processing
directly. See JsonFormat
for a wrapper which fixes the two types.
Doesn't currently include support for the newer Aeson Encoding machinery, but should.
JsonProfunctor (a -> Value) (Parse e b) |
_JsonProfunctor :: Iso (JsonProfunctor e a b) (JsonProfunctor e' a' b') (ToJson a, FromJson e b) (ToJson a', FromJson e' b') Source #
Observe that a JsonProfunctor
is isomorphic to a pair with a ToJson
and FromJson
.
newtype JsonFormat e a Source #
Wrapper around JsonProfunctor
for use in JSON processing when the profunctor represents a bijection between JSON and a single type a
, i.e. for
JsonProfunctor e a a
.
JsonFormat | |
|
Wrapped (JsonFormat e0 a0) Source # | |
(~) * (JsonFormat e0 a0) t0 => Rewrapped (JsonFormat e1 a1) t0 Source # | |
type Unwrapped (JsonFormat e0 a0) Source # | |
toJsonWithFormat :: JsonFormat e a -> a -> Value Source #
Given a JsonFormat
for a
, convert a value of a
into an Value
.
fromJsonWithFormat :: JsonFormat e a -> Parse e a Source #
Given a JsonFormat
for a
which can produce custom errors of type e
, yield an aeson-better-errors
Parse
which can be used to consume JSON.
parseJsonWithFormat :: (e -> Text) -> JsonFormat e a -> Value -> Parser a Source #
Given a JsonFormat
for a
which produces custom errors of type e
and some function to format those errors as messages, produce an Aeson parser function
Value -> Parser a
.
parseJsonWithFormat' :: JsonFormat Void a -> Value -> Parser a Source #
Given a JsonFormat
for a
which doesn't produce custom errors, produce an Aeson parser function Value -> Parser a
.
dimapJsonFormat :: (b -> a) -> (a -> b) -> JsonFormat e a -> JsonFormat e b Source #
Wrap a JsonFormat
for type a
in a pair of functions representing an isomorphism between a
and b
to produce a new JsonFormat
for b
.
jsonFormatWithIso :: AnIso' b a -> JsonFormat e a -> JsonFormat e b Source #
Wrap a JsonFormat
for type a
in an isomorphism to produce a new JsonFormat
for b
.
wrapJsonFormat :: JsonFormat e a -> (a -> Either e b) -> (b -> a) -> JsonFormat e b Source #
Given a
and a pair of functions JsonFormat
e ab -> a
and a -> Either e b
, produce a
.JsonFormat
e b
This is for the common case of a newtype
wrapper which asserts some kind of validation has been done, e.g.:
newtype MyType = MyType { unMyType :: Int } mkMyType :: Int -> Either Text MyType mkMyType i | i <= 0 = Left "must be positive!" | otherwise = Right (MyType i) myTypeJsonFormat :: JsonFormat e MyType myTypeJsonFormat = wrapJsonFormat intJsonFormat mkMyType unMyType
jsonFormatWithoutCustomError :: Show e => JsonFormat e a -> JsonFormat e' a Source #
Take a JsonFormat
which produces some Show
-able custom error and convert any custom errors into Aeson fail
style errors. Since the custom errors
are never generated by the resulting JsonFormat
, any custom error type can be assumed.
This is commonly used to take a more specific
and make it a more generic JsonFormat
MyError MyType
, e.g. to be used as a
JsonFormat
e MyTypedefaultJsonFormat
.
wrappedJsonFormat :: Wrapped a => JsonFormat e (Unwrapped a) -> JsonFormat e a Source #
Given a format for the value type inside some wrapper type a
which instances Wrapped
, produce a format which works on the wrapper type.