| Copyright | (C) 2015 Oleg Grenrus |
|---|---|
| License | BSD3 |
| Maintainer | Oleg Grenrus <oleg.grenrus@iki.fi> |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Aeson.Extra
- 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
- class ToJSONMap m k v | m -> k v where
- 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 full elem = CollapsedList full
- getCollapsedList :: CollapsedList full elem -> full
- parseCollapsedList :: (FromJSON elem, FromJSON full, ListLike full elem) => Object -> Text -> Parser full
- module Data.Aeson.Compat
Generic maps
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)]})class FromJSONKey a where Source
Methods
parseJSONKey :: Text -> Parser a Source
parseIntegralJSONKey :: Integral a => Text -> Parser a Source
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 Source | |
| (Ord k, FromJSONKey k, FromJSON v) => FromJSONMap (Map k v) k v Source | |
Symbol tag
Singleton string encoded and decoded as ifself.
λ> encode (Sym :: Sym "foobar") "\"foobar\""
decode "\"foobar\"" :: Maybe (Sym "foobar") Just Sym
decode "\"foobar\"" :: Maybe (Sym "barfoo") Nothing
Constructors
| SymTag |
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}"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 full elem 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]"
Constructors
| CollapsedList full |
Instances
| Eq full => Eq (CollapsedList full elem) Source | |
| Ord full => Ord (CollapsedList full elem) Source | |
| Read full => Read (CollapsedList full elem) Source | |
| Show full => Show (CollapsedList full elem) Source | |
| (ToJSON elem, ToJSON full, ListLike full elem) => ToJSON (CollapsedList full elem) Source | |
| (FromJSON elem, FromJSON full, ListLike full elem) => FromJSON (CollapsedList full elem) Source | |
getCollapsedList :: CollapsedList full elem -> full Source
parseCollapsedList :: (FromJSON elem, FromJSON full, ListLike full elem) => Object -> Text -> Parser full 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 <$> collapsedList 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])Re-exports
module Data.Aeson.Compat