aeson-extra-0.4.1.1: Extra goodies for aeson

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

Data.Aeson.Extra.CollapsedList

Description

Note: the contexts of functions are different with `aeson-1`.

Synopsis

- newtype CollapsedList f a = CollapsedList (f a)
- getCollapsedList :: CollapsedList f a -> f a
- parseCollapsedList :: (FromJSON a, FromJSON1 f, 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'.

Constructors

CollapsedList (f a) |

Instances

Functor f => Functor (CollapsedList f) Source # |
Instance details Defined in Data.Aeson.Extra.CollapsedList Methods fmap :: (a -> b) -> CollapsedList f a -> CollapsedList f b Source # (<$) :: a -> CollapsedList f b -> CollapsedList f a Source #
Foldable f => Foldable (CollapsedList f) Source # |

Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

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

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

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

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

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

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

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

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

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

null :: CollapsedList f a -> Bool Source #

length :: CollapsedList f a -> Int Source #

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

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

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

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

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

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) Source #

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

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

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

(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 Source #

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

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

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

(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) Source # liftParseJSONList :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser [CollapsedList f a] Source #
Eq (f a) => Eq (CollapsedList f a) Source # |
Instance details Defined in Data.Aeson.Extra.CollapsedList Methods (==) :: CollapsedList f a -> CollapsedList f a -> Bool Source # (/=) :: CollapsedList f a -> CollapsedList f a -> Bool Source #
Ord (f a) => Ord (CollapsedList f a) Source # |

Instance details

Defined in Data.Aeson.Extra.CollapsedList

Methods

compare :: CollapsedList f a -> CollapsedList f a -> Ordering Source #

(<) :: CollapsedList f a -> CollapsedList f a -> Bool Source #

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

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

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

max :: CollapsedList f a -> CollapsedList f a -> CollapsedList f a Source #

min :: CollapsedList f a -> CollapsedList f a -> CollapsedList f a Source #

Read (f a) => Read (CollapsedList f a) Source # |
Instance details Defined in Data.Aeson.Extra.CollapsedList Methods readsPrec :: Int -> ReadS (CollapsedList f a) Source # readList :: ReadS [CollapsedList f a] Source # readPrec :: ReadPrec (CollapsedList f a) Source # readListPrec :: ReadPrec [CollapsedList f a] Source #
Show (f a) => Show (CollapsedList f a) Source # |
Instance details Defined in Data.Aeson.Extra.CollapsedList Methods showsPrec :: Int -> CollapsedList f a -> ShowS Source # show :: CollapsedList f a -> String Source # showList :: [CollapsedList f a] -> ShowS Source #
(ToJSON1 f, Foldable f, ToJSON a) => ToJSON (CollapsedList f a) Source # |
Instance details Defined in Data.Aeson.Extra.CollapsedList Methods toJSON :: CollapsedList f a -> Value Source # toEncoding :: CollapsedList f a -> Encoding Source # toJSONList :: [CollapsedList f a] -> Value Source # toEncodingList :: [CollapsedList f a] -> Encoding Source #
(FromJSON1 f, Alternative f, FromJSON a) => FromJSON (CollapsedList f a) Source # |
Instance details Defined in Data.Aeson.Extra.CollapsedList Methods parseJSON :: Value -> Parser (CollapsedList f a) Source # parseJSONList :: Value -> Parser [CollapsedList f a] Source #

getCollapsedList :: CollapsedList f a -> f a Source #

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])