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

Description

More or less useful newtypes for writing FromJSON & ToJSON instances

Synopsis

Strict encoding

encodeStrict :: ToJSON a => a -> ByteString Source #

Like encode, but produces strict ByteString.

Since: 0.2.3.0

Symbol tag

data SymTag (s :: Symbol) 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

Instances details
Bounded (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

Methods

minBound :: SymTag s #

maxBound :: SymTag s #

Enum (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

Methods

succ :: SymTag s -> SymTag s #

pred :: SymTag s -> SymTag s #

toEnum :: Int -> SymTag s #

fromEnum :: SymTag s -> Int #

enumFrom :: SymTag s -> [SymTag s] #

enumFromThen :: SymTag s -> SymTag s -> [SymTag s] #

enumFromTo :: SymTag s -> SymTag s -> [SymTag s] #

enumFromThenTo :: SymTag s -> SymTag s -> SymTag s -> [SymTag s] #

Eq (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

Methods

(==) :: SymTag s -> SymTag s -> Bool #

(/=) :: SymTag s -> SymTag s -> Bool #

Ord (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

Methods

compare :: SymTag s -> SymTag s -> Ordering #

(<) :: SymTag s -> SymTag s -> Bool #

(<=) :: SymTag s -> SymTag s -> Bool #

(>) :: SymTag s -> SymTag s -> Bool #

(>=) :: SymTag s -> SymTag s -> Bool #

max :: SymTag s -> SymTag s -> SymTag s #

min :: SymTag s -> SymTag s -> SymTag s #

Read (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

Show (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

Methods

showsPrec :: Int -> SymTag s -> ShowS #

show :: SymTag s -> String #

showList :: [SymTag s] -> ShowS #

KnownSymbol s => ToJSON (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

KnownSymbol s => FromJSON (SymTag s) Source # 
Instance details

Defined in Data.Aeson.Extra.SymTag

NFData (SymTag s) Source #

Since: 0.4.1.0

Instance details

Defined in Data.Aeson.Extra.SymTag

Methods

rnf :: SymTag s -> () #

Singleton object

newtype SingObject (s :: Symbol) 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

Instances details
Functor (SingObject s) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

fmap :: (a -> b) -> SingObject s a -> SingObject s b #

(<$) :: a -> SingObject s b -> SingObject s a #

Foldable (SingObject s) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

fold :: Monoid m => SingObject s m -> m #

foldMap :: Monoid m => (a -> m) -> SingObject s a -> m #

foldMap' :: Monoid m => (a -> m) -> SingObject s a -> m #

foldr :: (a -> b -> b) -> b -> SingObject s a -> b #

foldr' :: (a -> b -> b) -> b -> SingObject s a -> b #

foldl :: (b -> a -> b) -> b -> SingObject s a -> b #

foldl' :: (b -> a -> b) -> b -> SingObject s a -> b #

foldr1 :: (a -> a -> a) -> SingObject s a -> a #

foldl1 :: (a -> a -> a) -> SingObject s a -> a #

toList :: SingObject s a -> [a] #

null :: SingObject s a -> Bool #

length :: SingObject s a -> Int #

elem :: Eq a => a -> SingObject s a -> Bool #

maximum :: Ord a => SingObject s a -> a #

minimum :: Ord a => SingObject s a -> a #

sum :: Num a => SingObject s a -> a #

product :: Num a => SingObject s a -> a #

Traversable (SingObject s) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

traverse :: Applicative f => (a -> f b) -> SingObject s a -> f (SingObject s b) #

sequenceA :: Applicative f => SingObject s (f a) -> f (SingObject s a) #

mapM :: Monad m => (a -> m b) -> SingObject s a -> m (SingObject s b) #

sequence :: Monad m => SingObject s (m a) -> m (SingObject s a) #

KnownSymbol s => ToJSON1 (SingObject s) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

liftToJSON :: (a -> Value) -> ([a] -> Value) -> SingObject s a -> Value #

liftToJSONList :: (a -> Value) -> ([a] -> Value) -> [SingObject s a] -> Value #

liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> SingObject s a -> Encoding #

liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [SingObject s a] -> Encoding #

KnownSymbol s => FromJSON1 (SingObject s) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

liftParseJSON :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser (SingObject s a) #

liftParseJSONList :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser [SingObject s a] #

Eq a => Eq (SingObject s a) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

(==) :: SingObject s a -> SingObject s a -> Bool #

(/=) :: SingObject s a -> SingObject s a -> Bool #

Ord a => Ord (SingObject s a) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

compare :: SingObject s a -> SingObject s a -> Ordering #

(<) :: SingObject s a -> SingObject s a -> Bool #

(<=) :: SingObject s a -> SingObject s a -> Bool #

(>) :: SingObject s a -> SingObject s a -> Bool #

(>=) :: SingObject s a -> SingObject s a -> Bool #

max :: SingObject s a -> SingObject s a -> SingObject s a #

min :: SingObject s a -> SingObject s a -> SingObject s a #

Read a => Read (SingObject s a) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Show a => Show (SingObject s a) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

showsPrec :: Int -> SingObject s a -> ShowS #

show :: SingObject s a -> String #

showList :: [SingObject s a] -> ShowS #

(KnownSymbol s, ToJSON a) => ToJSON (SingObject s a) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

(KnownSymbol s, FromJSON a) => FromJSON (SingObject s a) Source # 
Instance details

Defined in Data.Aeson.Extra.SingObject

NFData a => NFData (SingObject s a) Source #

Since: 0.4.1.0

Instance details

Defined in Data.Aeson.Extra.SingObject

Methods

rnf :: SingObject s a -> () #

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

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

Algebra

data ValueF a Source #

An algebra of Value

Since: aeson-extra-0.3.1.0

Instances

Instances details
Functor ValueF Source # 
Instance details

Defined in Data.Aeson.Extra.Recursive

Methods

fmap :: (a -> b) -> ValueF a -> ValueF b #

(<$) :: a -> ValueF b -> ValueF a #

Foldable ValueF Source # 
Instance details

Defined in Data.Aeson.Extra.Recursive

Methods

fold :: Monoid m => ValueF m -> m #

foldMap :: Monoid m => (a -> m) -> ValueF a -> m #

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

foldr :: (a -> b -> b) -> b -> ValueF a -> b #

foldr' :: (a -> b -> b) -> b -> ValueF a -> b #

foldl :: (b -> a -> b) -> b -> ValueF a -> b #

foldl' :: (b -> a -> b) -> b -> ValueF a -> b #

foldr1 :: (a -> a -> a) -> ValueF a -> a #

foldl1 :: (a -> a -> a) -> ValueF a -> a #

toList :: ValueF a -> [a] #

null :: ValueF a -> Bool #

length :: ValueF a -> Int #

elem :: Eq a => a -> ValueF a -> Bool #

maximum :: Ord a => ValueF a -> a #

minimum :: Ord a => ValueF a -> a #

sum :: Num a => ValueF a -> a #

product :: Num a => ValueF a -> a #

Traversable ValueF Source # 
Instance details

Defined in Data.Aeson.Extra.Recursive

Methods

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

sequenceA :: Applicative f => ValueF (f a) -> f (ValueF a) #

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

sequence :: Monad m => ValueF (m a) -> m (ValueF a) #

Eq a => Eq (ValueF a) Source # 
Instance details

Defined in Data.Aeson.Extra.Recursive

Methods

(==) :: ValueF a -> ValueF a -> Bool #

(/=) :: ValueF a -> ValueF a -> Bool #

Data a => Data (ValueF a) Source # 
Instance details

Defined in Data.Aeson.Extra.Recursive

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> ValueF a -> c (ValueF a) #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (ValueF a) #

toConstr :: ValueF a -> Constr #

dataTypeOf :: ValueF a -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (ValueF a)) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (ValueF a)) #

gmapT :: (forall b. Data b => b -> b) -> ValueF a -> ValueF a #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> ValueF a -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> ValueF a -> r #

gmapQ :: (forall d. Data d => d -> u) -> ValueF a -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> ValueF a -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> ValueF a -> m (ValueF a) #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> ValueF a -> m (ValueF a) #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> ValueF a -> m (ValueF a) #

Read a => Read (ValueF a) Source # 
Instance details

Defined in Data.Aeson.Extra.Recursive

Show a => Show (ValueF a) Source # 
Instance details

Defined in Data.Aeson.Extra.Recursive

Methods

showsPrec :: Int -> ValueF a -> ShowS #

show :: ValueF a -> String #

showList :: [ValueF a] -> ShowS #

type ObjectF a = HashMap Text a Source #

A JSON "object" (key/value map).

Since: aeson-extra-0.3.1.0

type ArrayF a = Vector a Source #

A JSON "array" (sequence).

Since: aeson-extra-0.3.1.0

Merge

merge :: (forall a. (a -> a -> a) -> ValueF a -> ValueF a -> ValueF a) -> Value -> Value -> Value Source #

Generic merge.

For example see lodashMerge.

Since: aeson-extra-0.3.1.0

lodashMerge :: Value -> Value -> Value Source #

Example of using merge. see https://lodash.com/docs#merge:

Note: not tested against JavaScript lodash, so may disagree in the results.

Since: 0.4.1.0

Stream

streamDecode :: forall a. FromJSON a => ByteString -> ([a], Maybe String) Source #

Lazyly parse ByteString with top-level JSON array.

Note: inspecting result's second field will force the list!

let ~(values, err) = streamDecode bs
traverse_ processValue values
maybe (pure ()) printError err

Since: 0.3.2.0

Template Haskell

mkValue :: String -> Q Exp Source #

Create a Value from string representation.

This is useful in tests.

Since: aeson-extra-0.3.1.0

mkValue' :: String -> Q Exp Source #

Like mkValue, but replace single quotes with double quotes before.

> $(mkValue' "{'a': 2 }")
Object (fromList [("a",Number 2.0)])

Since: aeson-extra-0.3.1.0