Copyright | (c) 2011-2013 Bryan O'Sullivan (c) 2011 MailRank Inc. |
---|---|
License | Apache |
Maintainer | Bryan O'Sullivan <bos@serpentine.com> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell98 |
Types for working with JSON data.
Synopsis
- class FromJSON a where
- class ToJSON a where
- class GFromJSON f where
- class GToJSON f where
- genericToJSON :: (Generic a, GToJSON (Rep a)) => Options -> a -> Value
- genericParseJSON :: (Generic a, GFromJSON (Rep a)) => Options -> Value -> Parser a
- newtype DotNetTime = DotNetTime {
- fromDotNetTime :: UTCTime
- withObject :: String -> (Object -> Parser a) -> Value -> Parser a
- withJSString :: String -> (JSString -> Parser a) -> Value -> Parser a
- withArray :: String -> (JSArray -> Parser a) -> Value -> Parser a
- withDouble :: String -> (Double -> Parser a) -> Value -> Parser a
- withBool :: String -> (Bool -> Parser a) -> Value -> Parser a
- fromJSON :: FromJSON a => Value -> Result a
- (.:) :: FromJSON a => Object -> JSString -> Parser a
- (.:?) :: FromJSON a => Object -> JSString -> Parser (Maybe a)
- (.!=) :: Parser (Maybe a) -> a -> Parser a
- (.=) :: ToJSON a => JSString -> a -> Pair
- typeMismatch :: String -> Value -> Parser a
Type classes
Core JSON classes
class FromJSON a where Source #
parseJSON :: Value -> Parser a Source #
parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a Source #
Instances
Instances
Generic JSON classes
class GFromJSON f where Source #
Instances
GFromJSON (U1 :: * -> *) Source # | |
Defined in JavaScript.JSON.Types.Generic | |
FromJSON a => GFromJSON (K1 i a :: * -> *) Source # | |
Defined in JavaScript.JSON.Types.Generic | |
(AllNullary (a :+: b) allNullary, ParseSum (a :+: b) allNullary) => GFromJSON (a :+: b) Source # | |
Defined in JavaScript.JSON.Types.Generic | |
(FromProduct a, FromProduct b, ProductSize a, ProductSize b) => GFromJSON (a :*: b) Source # | |
Defined in JavaScript.JSON.Types.Generic | |
ConsFromJSON a => GFromJSON (C1 c a) Source # | |
Defined in JavaScript.JSON.Types.Generic | |
GFromJSON a => GFromJSON (M1 i c a) Source # | |
Defined in JavaScript.JSON.Types.Generic |
class GToJSON f where Source #
Instances
GToJSON (U1 :: * -> *) Source # | |
ToJSON a => GToJSON (K1 i a :: * -> *) Source # | |
(AllNullary (a :+: b) allNullary, SumToJSON (a :+: b) allNullary) => GToJSON (a :+: b) Source # | |
(WriteProduct a, WriteProduct b, ProductSize a, ProductSize b) => GToJSON (a :*: b) Source # | |
ConsToJSON a => GToJSON (C1 c a) Source # | |
GToJSON a => GToJSON (M1 i c a) Source # | |
Types
newtype DotNetTime #
A newtype wrapper for UTCTime
that uses the same non-standard
serialization format as Microsoft .NET, whose
System.DateTime
type is by default serialized to JSON as in the following example:
/Date(1302547608878)/
The number represents milliseconds since the Unix epoch.
DotNetTime | |
|
Instances
Inspecting Value
s
Value
swithObject :: String -> (Object -> Parser a) -> Value -> Parser a Source #
withObject expected f value
applies f
to the Object
when value
is an Object
and fails using
otherwise.typeMismatch
expected
withArray :: String -> (JSArray -> Parser a) -> Value -> Parser a Source #
withArray expected f value
applies f
to the Array
when value
is an Array
and fails using
otherwise.typeMismatch
expected
withDouble :: String -> (Double -> Parser a) -> Value -> Parser a Source #
withScientific expected f value
applies f
to the Double
number when value
is a Number
.
and fails using
otherwise.typeMismatch
expected
withBool :: String -> (Bool -> Parser a) -> Value -> Parser a Source #
withBool expected f value
applies f
to the Bool
when value
is a Bool
and fails using
otherwise.typeMismatch
expected
Functions
fromJSON :: FromJSON a => Value -> Result a Source #
Convert a value from JSON, failing if the types do not match.
(.:) :: FromJSON a => Object -> JSString -> Parser a Source #
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.
(.:?) :: FromJSON a => Object -> JSString -> Parser (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.
(.!=) :: Parser (Maybe a) -> a -> Parser a Source #
Helper for use in combination with .:?
to provide default
values for optional JSON object fields.
This combinator is most useful if the key and value can be absent from an object without affecting its validity and we know a default value to assign in that case. If the key and value are mandatory, use '(.:)' instead.
Example usage:
v1 <- o.:?
"opt_field_with_dfl" .!= "default_val" v2 <- o.:
"mandatory_field" v3 <- o.:?
"opt_field2"
:: String | The name of the type you are trying to parse. |
-> Value | The actual value encountered. |
-> Parser a |
Fail parsing due to a type mismatch, with a descriptive message.