Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Data/Aeson/Encode/Functions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ encode = B.toLazyByteString . builder
{-# INLINE encode #-}

-- | Encode a 'Foldable' as a JSON array.
foldable :: (Foldable t, ToJSON a) => t a -> Encoding
foldable = brackets '[' ']' . foldMap (Value . toEncoding)
foldable :: (Foldable t) => (a -> Encoding) -> t a -> Encoding
foldable to = brackets '[' ']' . foldMap (Value . to)
{-# INLINE foldable #-}

list :: (ToJSON a) => [a] -> Encoding
list [] = emptyArray_
list (x:xs) = Encoding $
char7 '[' <> builder x <> commas xs <> char7 ']'
where commas = foldr (\v vs -> char7 ',' <> builder v <> vs) mempty
list :: (a -> Encoding) -> [a] -> Encoding
list _ [] = emptyArray_
list to (x:xs) = Encoding $
char7 '[' <> fromEncoding (to x) <> commas xs <> char7 ']'
where commas = foldr (\v vs -> char7 ',' <> fromEncoding (to v) <> vs) mempty
{-# INLINE list #-}

brackets :: Char -> Char -> Series -> Encoding
Expand Down
11 changes: 11 additions & 0 deletions Data/Aeson/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ module Data.Aeson.Types
, KeyValue(..)
, modifyFailure

-- ** Liftings to unary and binary type constructors
, FromJSON1(..)
, parseJSON1
, FromJSON2(..)
, parseJSON2
, ToJSON1(..)
, toJSON1
, toEncoding1
, ToJSON2(..)
, toJSON2

-- ** Generic JSON classes
, GFromJSON(..)
, GToJSON(..)
Expand Down
70 changes: 70 additions & 0 deletions Data/Aeson/Types/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ module Data.Aeson.Types.Class
-- * Core JSON classes
FromJSON(..)
, ToJSON(..)
-- * Liftings to unary and binary type constructors
, FromJSON1(..)
, parseJSON1
, FromJSON2(..)
, parseJSON2
, ToJSON1(..)
, toJSON1
, toEncoding1
, ToJSON2(..)
, toJSON2
, toEncoding2
-- * Generic JSON classes
, GFromJSON(..)
, GToJSON(..)
Expand Down Expand Up @@ -272,3 +283,62 @@ typeMismatch expected actual =
Number _ -> "Number"
Bool _ -> "Boolean"
Null -> "Null"

-------------------------------------------------------------------------------
-- Lifings of FromJSON and ToJSON to unary and binary type constructors
-------------------------------------------------------------------------------

-- | Lifting of the 'FromJSON' class to unary type constructors.
class FromJSON1 f where
liftParseJSON :: (Value -> Parser a) -> Value -> Parser (f a)

-- | Lift the standard 'parseJSON' function through the type constructor.
parseJSON1 :: (FromJSON1 f, FromJSON a) => Value -> Parser (f a)
parseJSON1 = liftParseJSON parseJSON
{-# INLINE parseJSON1 #-}

-- | Lifting of the 'ToJSON' class to unary type constructors.
class ToJSON1 f where
liftToJSON :: (a -> Value) -> f a -> Value

-- | Unfortunately there cannot be default implementation of 'liftToEncoding'.
liftToEncoding :: (a -> Encoding) -> f a -> Encoding

-- | Lift the standard 'toJSON' function through the type constructor.
toJSON1 :: (ToJSON1 f, ToJSON a) => f a -> Value
toJSON1 = liftToJSON toJSON
{-# INLINE toJSON1 #-}

-- | Lift the standard 'toEncoding' function through the type constructor.
toEncoding1 :: (ToJSON1 f, ToJSON a) => f a -> Encoding
toEncoding1 = liftToEncoding toEncoding
{-# INLINE toEncoding1 #-}


-- | Lifting of the 'FromJSON' class to binary type constructors.
class FromJSON2 f where
liftParseJSON2
:: (Value -> Parser a)
-> (Value -> Parser b)
-> Value -> Parser (f a b)

-- | Lift the standard 'parseJSON' function through the type constructor.
parseJSON2 :: (FromJSON2 f, FromJSON a, FromJSON b) => Value -> Parser (f a b)
parseJSON2 = liftParseJSON2 parseJSON parseJSON
{-# INLINE parseJSON2 #-}

-- | Lifting of the 'ToJSON' class to binary type constructors.
class ToJSON2 f where
liftToJSON2 :: (a -> Value) -> (b -> Value) -> f a b -> Value

liftToEncoding2 :: (a -> Encoding) -> (b -> Encoding) -> f a b -> Encoding

-- | Lift the standard 'toJSON' function through the type constructor.
toJSON2 :: (ToJSON2 f, ToJSON a, ToJSON b) => f a b -> Value
toJSON2 = liftToJSON2 toJSON toJSON
{-# INLINE toJSON2 #-}

-- | Lift the standard 'toEncoding' function through the type constructor.
toEncoding2 :: (ToJSON2 f, ToJSON a, ToJSON b) => f a b -> Encoding
toEncoding2 = liftToEncoding2 toEncoding toEncoding
{-# INLINE toEncoding2 #-}
Loading