aeson-extra-0.5: Extra goodies for aeson
Copyright(C) 2015-2016 Oleg Grenrus
LicenseBSD3
MaintainerOleg Grenrus <oleg.grenrus@iki.fi>
Safe HaskellNone
LanguageHaskell2010

Data.Aeson.Extra.CollapsedList

Description

 
Synopsis

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'.

Constructors

CollapsedList (f a) 

Instances

Instances details
Functor f => Functor (CollapsedList f) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

fmap :: (a -> b) -> CollapsedList f a -> CollapsedList f b #

(<$) :: a -> CollapsedList f b -> CollapsedList f a #

Foldable f => Foldable (CollapsedList f) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

fold :: Monoid m => CollapsedList f m -> m #

foldMap :: Monoid m => (a -> m) -> CollapsedList f a -> m #

foldMap' :: Monoid m => (a -> m) -> CollapsedList f a -> m #

foldr :: (a -> b -> b) -> b -> CollapsedList f a -> b #

foldr' :: (a -> b -> b) -> b -> CollapsedList f a -> b #

foldl :: (b -> a -> b) -> b -> CollapsedList f a -> b #

foldl' :: (b -> a -> b) -> b -> CollapsedList f a -> b #

foldr1 :: (a -> a -> a) -> CollapsedList f a -> a #

foldl1 :: (a -> a -> a) -> CollapsedList f a -> a #

toList :: CollapsedList f a -> [a] #

null :: CollapsedList f a -> Bool #

length :: CollapsedList f a -> Int #

elem :: Eq a => a -> CollapsedList f a -> Bool #

maximum :: Ord a => CollapsedList f a -> a #

minimum :: Ord a => CollapsedList f a -> a #

sum :: Num a => CollapsedList f a -> a #

product :: Num a => CollapsedList f a -> a #

Traversable f => Traversable (CollapsedList f) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

traverse :: Applicative f0 => (a -> f0 b) -> CollapsedList f a -> f0 (CollapsedList f b) #

sequenceA :: Applicative f0 => CollapsedList f (f0 a) -> f0 (CollapsedList f a) #

mapM :: Monad m => (a -> m b) -> CollapsedList f a -> m (CollapsedList f b) #

sequence :: Monad m => CollapsedList f (m a) -> m (CollapsedList f a) #

(ToJSON1 f, Foldable f) => ToJSON1 (CollapsedList f) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

liftToJSON :: (a -> Value) -> ([a] -> Value) -> CollapsedList f a -> Value #

liftToJSONList :: (a -> Value) -> ([a] -> Value) -> [CollapsedList f a] -> Value #

liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> CollapsedList f a -> Encoding #

liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [CollapsedList f a] -> Encoding #

(FromJSON1 f, Alternative f) => FromJSON1 (CollapsedList f) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

liftParseJSON :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser (CollapsedList f a) #

liftParseJSONList :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser [CollapsedList f a] #

Eq (f a) => Eq (CollapsedList f a) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

(==) :: CollapsedList f a -> CollapsedList f a -> Bool #

(/=) :: CollapsedList f a -> CollapsedList f a -> Bool #

Ord (f a) => Ord (CollapsedList f a) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Read (f a) => Read (CollapsedList f a) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

Show (f a) => Show (CollapsedList f a) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

(ToJSON1 f, Foldable f, ToJSON a) => ToJSON (CollapsedList f a) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

(FromJSON1 f, Alternative f, FromJSON a) => FromJSON (CollapsedList f a) Source # 
Instance details

Defined in Data.Aeson.Extra.CollapsedList

parseCollapsedList :: (FromJSON a, FromJSON1 f, 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])