aeson-0.1.0.0: Fast JSON parsing and generation

Data.Aeson.Types

Contents

Synopsis

Core JSON types

data Value Source

A JSON value represented as a Haskell value.

Type conversion

class FromJSON a whereSource

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 use empty to fail.
   fromJSON _          = empty

Methods

fromJSON :: Alternative f => Value -> f aSource

class ToJSON a whereSource

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]

Methods

toJSON :: a -> ValueSource

Constructors and accessors

(.=) :: ToJSON a => Text -> a -> ObjectSource

Construct an Object from a key and a value.

(.:) :: (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.

object :: [Object] -> ValueSource

Create a Value from a list of Objects. If duplicate keys arise, earlier keys and their associated values win.