aeson-extra-0.2.2.0: Extra goodies for aeson

Copyright(C) 2015 Oleg Grenrus
LicenseBSD3
MaintainerOleg Grenrus <oleg.grenrus@iki.fi>
Safe HaskellNone
LanguageHaskell2010

Data.Aeson.Extra

Contents

Description

More or less useful newtypes for writing FromJSON & ToJSON instances

Synopsis

Generic maps

newtype M a Source

A wrapper type to parse arbitrary maps

λ > decode "{\"1\": 1, \"2\": 2}" :: Maybe (M (H.HashMap Int Int))
Just (M {getMap = fromList [(1,1),(2,2)]})

Constructors

M 

Fields

getMap :: a
 

Instances

Functor M 
Foldable M 
Traversable M 
Eq a => Eq (M a) 
Ord a => Ord (M a) 
Read a => Read (M a) 
Show a => Show (M a) 
ToJSONMap m k v => ToJSON (M m) 
FromJSONMap m k v => FromJSON (M m) 

class FromJSONMap m k v | m -> k v where Source

Instances

(Eq k, Hashable k, FromJSONKey k, FromJSON v) => FromJSONMap (HashMap k v) k v 
(Ord k, FromJSONKey k, FromJSON v) => FromJSONMap (Map k v) k v 

class ToJSONMap m k v | m -> k v where Source

Instances

(ToJSONKey k, ToJSON v) => ToJSONMap (HashMap k v) k v 
(ToJSONKey k, ToJSON v) => ToJSONMap (Map k v) k v 

Symbol tag

data SymTag s Source

Singleton string encoded and decoded as ifself.

λ> encode (SymTag :: SymTag "foobar")
"\"foobar\""
decode "\"foobar\"" :: Maybe (SymTag "foobar")
Just SymTag
decode "\"foobar\"" :: Maybe (SymTag "barfoo")
Nothing

Available with: base >=4.7

Constructors

SymTag 

Instances

Singleton object

newtype SingObject s a Source

Singleton value object

λ > decode "{\"value\": 42 }" :: Maybe (SingObject "value" Int)
Just (SingObject 42)
λ > encode (SingObject 42 :: SingObject "value" Int)
"{\"value\":42}"

Available with: base >=4.7

Constructors

SingObject a 

Instances

CollapsedList

newtype CollapsedList f a Source

Collapsed list, singleton is represented as the value itself in JSON encoding.

λ > decode "null" :: Maybe (CollapsedList [Int] Int)
Just (CollapsedList [])
λ > decode "42" :: Maybe (CollapsedList [Int] Int)
Just (CollapsedList [42])
λ > decode "[1, 2, 3]" :: Maybe (CollapsedList [Int] Int)
Just (CollapsedList [1,2,3])
λ > encode (CollapsedList ([] :: [Int]))
"null"
λ > encode (CollapsedList ([42] :: [Int]))
"42"
λ > encode (CollapsedList ([1, 2, 3] :: [Int]))
"[1,2,3]"

Documentation rely on f Alternative instance behaving like lists'.

Constructors

CollapsedList (f a) 

Instances

Functor f => Functor (CollapsedList f) 
Foldable f => Foldable (CollapsedList f) 
Traversable f => Traversable (CollapsedList f) 
Eq (f a) => Eq (CollapsedList f a) 
Ord (f a) => Ord (CollapsedList f a) 
Read (f a) => Read (CollapsedList f a) 
Show (f a) => Show (CollapsedList f a) 
(ToJSON a, ToJSON (f a), Foldable f) => ToJSON (CollapsedList f a) 
(FromJSON a, FromJSON (f a), Alternative f) => FromJSON (CollapsedList f a) 

parseCollapsedList :: (FromJSON a, FromJSON (f a), Alternative f) => Object -> Text -> Parser (f a) Source

Parses possibly collapsed array value from the object's field.

λ > newtype V = V [Int] deriving (Show)
λ > instance FromJSON V where parseJSON = withObject "V" $ \obj -> V <$> parseCollapsedList obj "value"
λ > decode "{}" :: Maybe V
Just (V [])
λ > decode "{\"value\": null}" :: Maybe V
Just (V [])
λ > decode "{\"value\": 42}" :: Maybe V
Just (V [42])
λ > decode "{\"value\": [1, 2, 3, 4]}" :: Maybe V
Just (V [1,2,3,4])

UTCTime

newtype U Source

A type to parse UTCTime

FromJSON instance accepts for example:

2015-09-07T08:16:40.807Z
2015-09-07 11:16:40.807 +03:00

Latter format is accepted by aeson staring from version 0.10.0.0.

See https://github.com/bos/aeson/blob/4667ef1029a373cf4510f7deca147c357c6d8947/Data/Aeson/Parser/Time.hs#L150

Since: aeson-extra-0.2.2.0

Constructors

U 

Fields

getU :: UTCTime
 

Instances

newtype Z Source

A type to parse ZonedTime

Since: aeson-extra-0.2.2.0

Constructors

Z 

Fields

getZ :: ZonedTime
 

Instances

Re-exports