Copyright | (C) 2015-2016 Oleg Grenrus |
---|---|
License | BSD3 |
Maintainer | Oleg Grenrus <oleg.grenrus@iki.fi> |
Safe Haskell | None |
Language | Haskell2010 |
Note: the contexts of functions are different with aeson-1
.
- 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)
Documentation
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'.
CollapsedList (f a) |
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])