@@ -45,8 +45,9 @@ module Data.Aeson.Types.Class
4545 -- * Functions needed for documentation
4646 , typeMismatch
4747 -- * Encoding functions
48− , list
48+ , listEncoding
4949 , listValue
50+ , listParser
5051 ) where
5152
5253import Data.Aeson.Types.Internal
@@ -408,9 +409,7 @@ typeMismatch expected actual =
408409class FromJSON1 f where
409410 liftParseJSON :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser (f a)
410411 liftParseJSONList :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser [f a]
411− liftParseJSONList f g v = case v of
412− Array vals -> fmap V.toList (V.mapM (liftParseJSON f g) vals)
413− _ -> typeMismatch "[a]" v
412+ liftParseJSONList f g v = listParser (liftParseJSON f g) v
414413
415414-- | Lift the standard 'parseJSON' function through the type constructor.
416415parseJSON1 :: (FromJSON1 f, FromJSON a) => Value -> Parser (f a)
@@ -426,7 +425,7 @@ class ToJSON1 f where
426425 -- | Unfortunately there cannot be a default implementation of 'liftToEncoding'.
427426 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
428427 liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [f a] -> Encoding
429− liftToEncodingList f g = list (liftToEncoding f g)
428+ liftToEncodingList f g = listEncoding (liftToEncoding f g)
430429
431430-- | Lift the standard 'toJSON' function through the type constructor.
432431toJSON1 :: (ToJSON1 f, ToJSON a) => f a -> Value
@@ -470,7 +469,7 @@ class ToJSON2 f where
470469
471470 liftToEncoding2 :: (a -> Encoding) -> ([a] -> Encoding) -> (b -> Encoding) -> ([b] -> Encoding) -> f a b -> Encoding
472471 liftToEncodingList2 :: (a -> Encoding) -> ([a] -> Encoding) -> (b -> Encoding) -> ([b] -> Encoding) -> [f a b] -> Encoding
473− liftToEncodingList2 fa ga fb gb = list (liftToEncoding2 fa ga fb gb)
472+ liftToEncodingList2 fa ga fb gb = listEncoding (liftToEncoding2 fa ga fb gb)
474473
475474-- | Lift the standard 'toJSON' function through the type constructor.
476475toJSON2 :: (ToJSON2 f, ToJSON a, ToJSON b) => f a b -> Value
@@ -482,12 +481,19 @@ toEncoding2 :: (ToJSON2 f, ToJSON a, ToJSON b) => f a b -> Encoding
482481toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingList
483482{-# INLINE toEncoding2 #-}
484483
485−list :: (a -> Encoding) -> [a] -> Encoding
486−list _ [] = emptyArray_
487−list to (x:xs) = Encoding $
484+listEncoding :: (a -> Encoding) -> [a] -> Encoding
485+listEncoding _ [] = emptyArray_
486+listEncoding to (x:xs) = Encoding $
488487 B.char7 '[' <> fromEncoding (to x) <> commas xs <> B.char7 ']'
489488 where commas = foldr (\v vs -> B.char7 ',' <> fromEncoding (to v) <> vs) mempty
490−{-# INLINE list #-}
489+{-# INLINE listEncoding #-}
491490
492491listValue :: (a -> Value) -> [a] -> Value
493492listValue f = Array . V.fromList . map f
493+{-# INLINE listValue #-}
494+
495+listParser :: (Value -> Parser a) -> Value -> Parser [a]
496+listParser f (Array xs) = fmap V.toList (V.mapM f xs)
497+listParser _ v = typeMismatch "[a]" v
498+
499+