@@ -47,9 +47,7 @@ module Data.Aeson.Types.Instances
4747 , (.:)
4848 , (.:?)
4949 , (.!=)
50− , builder
5150 , series
52− , foldable
5351 , tuple
5452 , (>*<)
5553 , typeMismatch
@@ -60,14 +58,14 @@ import qualified Data.Aeson.Encode.Builder as E
6058import qualified Data.ByteString.Builder as B
6159import Data.Aeson.Functions
6260import Data.Monoid ((<>), mempty)
61+import Data.Aeson.Encode.Functions (brackets, builder, foldable, list)
6362import Data.Aeson.Types.Class
6463import Data.Aeson.Types.Internal
6564import Data.Scientific (Scientific)
6665import qualified Data.Scientific as Scientific (coefficient, base10Exponent, fromFloatDigits, toRealFloat)
6766import Data.Attoparsec.Number (Number(..))
6867import Data.Fixed
69−import Data.Foldable (Foldable, foldMap, toList)
70−import Data.ByteString.Builder.Prim (primBounded)
68+import Data.Foldable (Foldable, toList)
7169import Data.Functor.Identity (Identity(..))
7270import Data.Hashable (Hashable(..))
7371import Data.Int (Int8, Int16, Int32, Int64)
@@ -98,7 +96,6 @@ import qualified Data.Vector.Primitive as VP
9896import qualified Data.Vector.Storable as VS
9997import qualified Data.Vector.Unboxed as VU
10098import qualified Data.Vector.Mutable as VM ( unsafeNew, unsafeWrite )
101−import qualified Prelude as P
10299
103100#if MIN_VERSION_time(1,5,0)
104101import Data.Time.Format (defaultTimeLocale, dateTimeFmt)
@@ -143,10 +140,6 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
143140 toEncoding (Right a) = Encoding $
144141 B.shortByteString "{\"right\":" <> builder a <> B.char7 '}'
145142
146−builder :: ToJSON a => a -> B.Builder
147−builder = fromEncoding . toEncoding
148−{-# INLINE builder #-}
149−
150143instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
151144 parseJSON (Object (H.toList -> [(key, value)]))
152145 | key == left = Left <$> parseJSON value
@@ -427,10 +420,7 @@ instance (ToJSON a) => ToJSON [a] where
427420 toJSON = Array . V.fromList . map toJSON
428421 {-# INLINE toJSON #-}
429422
430− toEncoding [] = E.emptyArray_
431− toEncoding (x:xs) = Encoding $
432− B.char7 '[' <> builder x <> commas xs <> B.char7 ']'
433− where commas = P.foldr (\v vs -> B.char7 ',' <> builder v <> vs) mempty
423+ toEncoding xs = list xs
434424
435425instance (FromJSON a) => FromJSON [a] where
436426 parseJSON = withArray "[a]" $ Tr.sequence .
@@ -453,15 +443,6 @@ instance (ToJSON a) => ToJSON (Vector a) where
453443
454444 toEncoding = encodeVector
455445
456−-- | Encode a 'Foldable' as a JSON array.
457−foldable :: (Foldable t, ToJSON a) => t a -> Encoding
458−foldable = brackets '[' ']' . foldMap (Value . toEncoding)
459−
460−brackets :: Char -> Char -> Series -> Encoding
461−brackets begin end (Value v) = Encoding $
462− B.char7 begin <> fromEncoding v <> B.char7 end
463−brackets begin end Empty = Encoding (primBounded (E.ascii2 (begin,end)) ())
464−
465446encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
466447encodeVector xs
467448 | VG.null xs = E.emptyArray_
@@ -575,18 +556,18 @@ encodeMap minViewWithKey foldrWithKey xs =
575556 case minViewWithKey xs of
576557 Nothing -> E.emptyObject_
577558 Just ((k,v),ys) -> Encoding $
578− B.char7 '{' <> encodePair k v <>
559+ B.char7 '{' <> encodeKV k v <>
579560 foldrWithKey go mempty ys <> B.char7 '}'
580− where go k v b = B.char7 ',' <> encodePair k v <> b
561+ where go k v b = B.char7 ',' <> encodeKV k v <> b
581562
582563encodeWithKey :: (ToJSON k, ToJSON v) =>
583564 ((k -> v -> Series -> Series) -> Series -> m -> Series)
584565 -> m -> Encoding
585566encodeWithKey foldrWithKey = brackets '{' '}' . foldrWithKey go mempty
586− where go k v c = Value (Encoding $ encodePair k v) <> c
567+ where go k v c = Value (Encoding $ encodeKV k v) <> c
587568
588−encodePair :: (ToJSON k, ToJSON v) => k -> v -> B.Builder
589−encodePair k v = builder k <> B.char7 ':' <> builder v
569+encodeKV :: (ToJSON k, ToJSON v) => k -> v -> B.Builder
570+encodeKV k v = builder k <> B.char7 ':' <> builder v
590571
591572instance (FromJSON v) => FromJSON (M.Map Text v) where
592573 parseJSON = withObject "Map Text a" $