aeson-extra-0.2.2.0: Extra goodies for aeson

Copyright | (C) 2015 Oleg Grenrus
License | BSD3
Maintainer | Oleg Grenrus <oleg.grenrus@iki.fi>
Safe Haskell | None
Language | Haskell2010

Data.Aeson.Extra

Contents

- Generic maps
- Symbol tag
- Singleton object
- CollapsedList
- UTCTime
- Re-exports

Description

More or less useful newtypes for writing `FromJSON` & `ToJSON` instances

Synopsis

- newtype M a = M {
  - getMap :: a
  }
- class FromJSONKey a where
  - parseJSONKey :: Text -> Parser a
- parseIntegralJSONKey :: Integral a => Text -> Parser a
- class FromJSONMap m k v | m -> k v where
  - parseJSONMap :: HashMap Text Value -> Parser m
- class ToJSONKey a where
  - toJSONKey :: a -> Text
- class ToJSONMap m k v | m -> k v where
  - toJSONMap :: m -> HashMap Text Value
- data SymTag s = SymTag
- newtype SingObject s a = SingObject a
- mkSingObject :: Proxy s -> a -> SingObject s a
- getSingObject :: Proxy s -> SingObject s a -> a
- newtype CollapsedList f a = CollapsedList (f a)
- getCollapsedList :: CollapsedList f a -> f a
- parseCollapsedList :: (FromJSON a, FromJSON (f a), Alternative f) => Object -> Text -> Parser (f a)
- newtype U = U {
  - getU :: UTCTime
  }
- newtype Z = Z {
  - getZ :: ZonedTime
  }
- module Data.Aeson.Compat

# 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 Source |
Foldable M Source |
Traversable M Source |
Eq a => Eq (M a) Source |
Ord a => Ord (M a) Source |
Read a => Read (M a) Source |
Show a => Show (M a) Source |
ToJSONMap m k v => ToJSON (M m) Source |
FromJSONMap m k v => FromJSON (M m) Source |

class FromJSONKey a where Source

Methods

parseJSONKey :: Text -> Parser a Source

Instances

FromJSONKey Int Source |
FromJSONKey Integer Source |
FromJSONKey String Source |
FromJSONKey Text Source |
FromJSONKey Text Source |

parseIntegralJSONKey :: Integral a => Text -> Parser a Source

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

Methods

parseJSONMap :: HashMap Text Value -> Parser m Source

Instances

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

class ToJSONKey a where Source

Methods

toJSONKey :: a -> Text Source

Instances

ToJSONKey Int Source |
ToJSONKey Integer Source |
ToJSONKey String Source |
ToJSONKey Text Source |
ToJSONKey Text Source |

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

Methods

toJSONMap :: m -> HashMap Text Value Source

Instances

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

# 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

Bounded (SymTag s) Source |
Enum (SymTag s) Source |
Eq (SymTag s) Source |
Ord (SymTag s) Source |
Read (SymTag s) Source |
Show (SymTag s) Source |
KnownSymbol s => ToJSON (SymTag s) Source |
KnownSymbol s => FromJSON (SymTag s) Source |

# 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

Functor (SingObject s) Source |
Foldable (SingObject s) Source |
Traversable (SingObject s) Source |
Eq a => Eq (SingObject s a) Source |
Ord a => Ord (SingObject s a) Source |
Read a => Read (SingObject s a) Source |
Show a => Show (SingObject s a) Source |
(KnownSymbol s, ToJSON a) => ToJSON (SingObject s a) Source |
(KnownSymbol s, FromJSON a) => FromJSON (SingObject s a) Source |

mkSingObject :: Proxy s -> a -> SingObject s a Source

getSingObject :: Proxy s -> SingObject s a -> a Source

# 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) Source |
Foldable f => Foldable (CollapsedList f) Source |
Traversable f => Traversable (CollapsedList f) Source |
Eq (f a) => Eq (CollapsedList f a) Source |
Ord (f a) => Ord (CollapsedList f a) Source |
Read (f a) => Read (CollapsedList f a) Source |
Show (f a) => Show (CollapsedList f a) Source |
(ToJSON a, ToJSON (f a), Foldable f) => ToJSON (CollapsedList f a) Source |
(FromJSON a, FromJSON (f a), Alternative f) => FromJSON (CollapsedList f a) Source |

getCollapsedList :: CollapsedList f a -> f a Source

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

Eq U Source |
Ord U Source |
Read U Source |
Show U Source |
ToJSON U Source |
FromJSON U Source |

newtype Z Source

A type to parse `ZonedTime`

Since: aeson-extra-0.2.2.0

Constructors

Z |

Fields

getZ :: ZonedTime

Instances

Read Z Source |
Show Z Source |
ToJSON Z Source |
FromJSON Z Source |

# Re-exports

module Data.Aeson.Compat