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.
- class FromJSON a where
- class ToJSON a where
- class GFromJSON f where
- gParseJSON :: Options -> Value -> Parser (f a)
- 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 {}
- 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
Generic JSON classes
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 | |
|
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"