@@ -45,8 +45,9 @@ module Data.Aeson.Types.Class
45 -- * Functions needed for documentation
45 -- * Functions needed for documentation
46 , typeMismatch
46 , typeMismatch
47 -- * Encoding functions
47 -- * Encoding functions
48− , list
48+ , listEncoding
49 , listValue
49 , listValue
52import Data.Aeson.Types.Internal
53import Data.Aeson.Types.Internal
@@ -408,9 +409,7 @@ typeMismatch expected actual =
408class FromJSON1 f where
409class FromJSON1 f where
409 liftParseJSON :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser (f a)
410 liftParseJSON :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser (f a)
410 liftParseJSONList :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser [f a]
411 liftParseJSONList :: (Value -> Parser a) -> (Value -> Parser [a]) -> Value -> Parser [f a]
411− liftParseJSONList f g v = case v of
412+ liftParseJSONList f g v = listParser (liftParseJSON f g) v
412− Array vals -> fmap V.toList (V.mapM (liftParseJSON f g) vals)
413− _ -> typeMismatch "[a]" v
415-- | Lift the standard 'parseJSON' function through the type constructor.
414-- | Lift the standard 'parseJSON' function through the type constructor.
416parseJSON1 :: (FromJSON1 f, FromJSON a) => Value -> Parser (f a)
415parseJSON1 :: (FromJSON1 f, FromJSON a) => Value -> Parser (f a)
@@ -426,7 +425,7 @@ class ToJSON1 f where
426 -- | Unfortunately there cannot be a default implementation of 'liftToEncoding'.
425 -- | Unfortunately there cannot be a default implementation of 'liftToEncoding'.
427 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
426 liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> f a -> Encoding
428 liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [f a] -> Encoding
427 liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [f a] -> Encoding
429− liftToEncodingList f g = list (liftToEncoding f g)
428+ liftToEncodingList f g = listEncoding (liftToEncoding f g)
431-- | Lift the standard 'toJSON' function through the type constructor.
430-- | Lift the standard 'toJSON' function through the type constructor.
432toJSON1 :: (ToJSON1 f, ToJSON a) => f a -> Value
431toJSON1 :: (ToJSON1 f, ToJSON a) => f a -> Value
@@ -470,7 +469,7 @@ class ToJSON2 f where
471 liftToEncoding2 :: (a -> Encoding) -> ([a] -> Encoding) -> (b -> Encoding) -> ([b] -> Encoding) -> f a b -> Encoding
470 liftToEncoding2 :: (a -> Encoding) -> ([a] -> Encoding) -> (b -> Encoding) -> ([b] -> Encoding) -> f a b -> Encoding
472 liftToEncodingList2 :: (a -> Encoding) -> ([a] -> Encoding) -> (b -> Encoding) -> ([b] -> Encoding) -> [f a b] -> Encoding
471 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)
475-- | Lift the standard 'toJSON' function through the type constructor.
474-- | Lift the standard 'toJSON' function through the type constructor.
476toJSON2 :: (ToJSON2 f, ToJSON a, ToJSON b) => f a b -> Value
475toJSON2 :: (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
482toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingList
481toEncoding2 = liftToEncoding2 toEncoding toEncodingList toEncoding toEncodingList
483{-# INLINE toEncoding2 #-}
482{-# INLINE toEncoding2 #-}
485−list :: (a -> Encoding) -> [a] -> Encoding
484+listEncoding :: (a -> Encoding) -> [a] -> Encoding
486−list _ [] = emptyArray_
485+listEncoding _ [] = emptyArray_
487−list to (x:xs) = Encoding $
486+listEncoding to (x:xs) = Encoding $
488 B.char7 '[' <> fromEncoding (to x) <> commas xs <> B.char7 ']'
487 B.char7 '[' <> fromEncoding (to x) <> commas xs <> B.char7 ']'
489 where commas = foldr (\v vs -> B.char7 ',' <> fromEncoding (to v) <> vs) mempty
488 where commas = foldr (\v vs -> B.char7 ',' <> fromEncoding (to v) <> vs) mempty
490−{-# INLINE list #-}
489+{-# INLINE listEncoding #-}
492listValue :: (a -> Value) -> [a] -> Value
491listValue :: (a -> Value) -> [a] -> Value
493listValue f = Array . V.fromList . map f
492listValue f = Array . V.fromList . map f
493+{-# INLINE listValue #-}
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