on master help sign in
Cleanup primed functions
commitddba9608f6b9d6ca892a4e17e62011bbf58ac61d authorOleg Grenrus <oleg.grenrus@iki.fi> dateTue 29 Mar 2016, 12:12 (11 years ago) parent48e61c1
2 files changed+14−28
Data/Aeson/Encode/Functions.hs+7−21 Data/Aeson/Types/Instances.hs+7−7
Data/Aeson/Encode/Functions.hs+7−21
@@ -7,9 +7,7 @@ module Data.Aeson.Encode.Functions
77 , char7
88 , encode
99 , foldable
10− , foldable'
1110 , list
12− , list'
1311 , pairs
1412 ) where
1513
@@ -39,29 +37,17 @@ encode = B.toLazyByteString . builder
3937{-# INLINE encode #-}
4038
4139-- | Encode a 'Foldable' as a JSON array.
42−foldable :: (Foldable t, ToJSON a) => t a -> Encoding
43−foldable = brackets '[' ']' . foldMap (Value . toEncoding)
40+foldable :: (Foldable t) => (a -> Encoding) -> t a -> Encoding
41+foldable to = brackets '[' ']' . foldMap (Value . to)
4442{-# INLINE foldable #-}
4543
46−-- | Encode a 'Foldable' as a JSON array.
47−foldable' :: (Foldable t) => (a -> Encoding) -> t a -> Encoding
48−foldable' to = brackets '[' ']' . foldMap (Value . to)
49−{-# INLINE foldable' #-}
50−
51−list :: (ToJSON a) => [a] -> Encoding
52−list [] = emptyArray_
53−list (x:xs) = Encoding $
54− char7 '[' <> builder x <> commas xs <> char7 ']'
55− where commas = foldr (\v vs -> char7 ',' <> builder v <> vs) mempty
44+list :: (a -> Encoding) -> [a] -> Encoding
45+list _ [] = emptyArray_
46+list to (x:xs) = Encoding $
47+ char7 '[' <> fromEncoding (to x) <> commas xs <> char7 ']'
48+ where commas = foldr (\v vs -> char7 ',' <> fromEncoding (to v) <> vs) mempty
5649{-# INLINE list #-}
5750
58−list' :: (a -> Encoding) -> [a] -> Encoding
59−list' _ [] = emptyArray_
60−list' e (x:xs) = Encoding $
61− char7 '[' <> fromEncoding (e x) <> commas xs <> char7 ']'
62− where commas = foldr (\v vs -> char7 ',' <> fromEncoding (e v) <> vs) mempty
63−{-# INLINE list' #-}
64−
6551brackets :: Char -> Char -> Series -> Encoding
6652brackets begin end (Value v) = Encoding $
6753 char7 begin <> fromEncoding v <> char7 end
Data/Aeson/Types/Instances.hs+7−7
@@ -79,7 +79,7 @@ module Data.Aeson.Types.Instances
7979import Data.Aeson.Types.Instances.Tuple (tuple, (>*<))
8080
8181import Control.Applicative (Const(..))
82−import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable, foldable', list')
82+import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable, list)
8383import Data.Aeson.Functions (hashMapKey, mapHashKeyVal, mapKey, mapKeyVal)
8484import Data.Aeson.Types.Class
8585import Data.Aeson.Types.Internal
@@ -590,7 +590,7 @@ instance ToJSON1 NonEmpty where
590590 liftToJSON to = liftToJSON to . toList
591591 {-# INLINE liftToJSON #-}
592592
593− liftToEncoding = foldable'
593+ liftToEncoding = foldable
594594 {-# INLINE liftToEncoding #-}
595595
596596instance (ToJSON a) => ToJSON (NonEmpty a) where
@@ -615,7 +615,7 @@ instance ToJSON1 [] where
615615 liftToJSON to = Array . V.fromList . map to
616616 {-# INLINE liftToJSON #-}
617617
618− liftToEncoding = list'
618+ liftToEncoding = list
619619 {-# INLINE liftToEncoding #-}
620620
621621instance (ToJSON a) => ToJSON [a] where
@@ -638,7 +638,7 @@ instance ToJSON1 Seq.Seq where
638638 liftToJSON to = liftToJSON to . toList
639639 {-# INLINE liftToJSON #-}
640640
641− liftToEncoding = foldable'
641+ liftToEncoding = foldable
642642 {-# INLINE liftToEncoding #-}
643643
644644instance (ToJSON a) => ToJSON (Seq.Seq a) where
@@ -751,7 +751,7 @@ instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
751751 toJSON = toJSON . HashSet.toList
752752 {-# INLINE toJSON #-}
753753
754− toEncoding = foldable
754+ toEncoding = foldable toEncoding
755755 {-# INLINE toEncoding #-}
756756
757757instance (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet.HashSet a) where
@@ -826,7 +826,7 @@ instance (ToJSON v, ToJSONKey k) => ToJSON (M.Map k v) where
826826
827827 toEncoding = case toJSONKey of
828828 ToJSONKeyText (_,f) -> encodeMap f M.minViewWithKey M.foldrWithKey
829− ToJSONKeyValue (_,f) -> list' (pairEncoding f) . M.toList
829+ ToJSONKeyValue (_,f) -> list (pairEncoding f) . M.toList
830830 where pairEncoding :: (k -> Encoding) -> (k, v) -> Encoding
831831 pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder b
832832 {-# INLINE toEncoding #-}
@@ -852,7 +852,7 @@ instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
852852
853853 toEncoding = case toJSONKey of
854854 ToJSONKeyText (_,f) -> encodeWithKey f H.foldrWithKey
855− ToJSONKeyValue (_,f) -> list' (pairEncoding f) . H.toList
855+ ToJSONKeyValue (_,f) -> list (pairEncoding f) . H.toList
856856 where pairEncoding :: (k -> Encoding) -> (k, v) -> Encoding
857857 pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder b
858858 {-# INLINE toEncoding #-}