- data Value
- type Array = Vector Value
- type Object = Map Text Value
- class FromJSON a where
- fromJSON :: Alternative f => Value -> f a
- class ToJSON a where
- (.=) :: ToJSON a => Text -> a -> Object
- (.:) :: (Alternative f, FromJSON a) => Object -> Text -> f a
- (.:?) :: (Alternative f, FromJSON a) => Object -> Text -> f (Maybe a)
- object :: [Object] -> Value
- encode :: ToJSON a => a -> ByteString
- json :: Parser Value
Core JSON types
A JSON value represented as a Haskell value.
Type conversion
A type that can be converted from JSON, with the possibility of failure.
When writing an instance, use empty
to make a conversion fail,
e.g. if an Object
is missing a required key, or the value is of
the wrong type.
An example type and instance:
data Coord { x :: Double, y :: Double } instance FromJSON Coord where fromJSON (Object
v) = Coord<$>
v.:
"x"<*>
v.:
"y" -- A non-Object
value is of the wrong type, so useempty
to fail. fromJSON _ =empty
fromJSON :: Alternative f => Value -> f aSource
FromJSON Bool | |
FromJSON Double | |
FromJSON Int | |
FromJSON Integer | |
FromJSON ByteString | |
FromJSON ByteString | |
FromJSON Text | |
FromJSON Text | |
FromJSON UTCTime | |
FromJSON Value | |
FromJSON a => FromJSON [a] | |
FromJSON a => FromJSON (Maybe a) | |
FromJSON a => FromJSON (Vector a) | |
(FromJSON a, FromJSON b) => FromJSON (Either a b) | |
(FromJSON a, FromJSON b) => FromJSON (a, b) |
A type that can be converted to JSON.
An example type and instance:
data Coord { x :: Double, y :: Double } instance ToJSON Coord where toJSON (Coord x y) =object
["x".=
x, "y".=
y]
ToJSON Bool | |
ToJSON Double | |
ToJSON Int | |
ToJSON Integer | |
ToJSON ByteString | |
ToJSON ByteString | |
ToJSON Text | |
ToJSON Text | |
ToJSON UTCTime | |
ToJSON Value | |
ToJSON a => ToJSON [a] | |
ToJSON a => ToJSON (Maybe a) | |
ToJSON a => ToJSON (Vector a) | |
(ToJSON a, ToJSON b) => ToJSON (Either a b) | |
(ToJSON a, ToJSON b) => ToJSON (a, b) |
Constructors and accessors
(.:) :: (Alternative f, FromJSON a) => Object -> Text -> f aSource
Retrieve the value associated with the given key of an Object
.
The result is empty
if the key is not present or the value cannot
be converted to the desired type.
This accessor is appropriate if the key and value must be present in an object for it to be valid. If the key and value are optional, use '(.:?)' instead.
(.:?) :: (Alternative f, FromJSON a) => Object -> Text -> f (Maybe a)Source
Retrieve the value associated with the given key of an Object
.
The result is Nothing
if the key is not present, or empty
if
the value cannot be converted to the desired type.
This accessor is most useful if the key and value can be absent from an object without affecting its validity. If the key and value are mandatory, use '(.:?)' instead.
Encoding and parsing
encode :: ToJSON a => a -> ByteStringSource
Efficiently serialize a JSON value as a lazy ByteString
.