@@ -61,7 +61,7 @@ import Data.Aeson.Functions (mapHashKeyVal, mapKeyVal)
6161import Data.Aeson.Types.Generic
6262import Data.Aeson.Types.Internal
6363
64−import qualified Data.Aeson.Encode.Builder as EB
64+-- We need internal here for generic deriving
6565import qualified Data.Aeson.Encoding.Internal as E
6666
6767import Control.Applicative (Const (..))
@@ -92,7 +92,7 @@ import Foreign.Storable (Storable)
9292import GHC.Generics
9393import Numeric.Natural (Natural)
9494
95−import qualified Data.ByteString.Builder as B
95+import qualified Data.ByteString.Builder as B (toLazyByteString)
9696import qualified Data.ByteString.Lazy as L
9797import qualified Data.DList as DList
9898import qualified Data.HashMap.Strict as H
@@ -115,9 +115,6 @@ import qualified Data.Vector.Primitive as VP
115115import qualified Data.Vector.Storable as VS
116116import qualified Data.Vector.Unboxed as VU
117117
118−builder :: ToJSON a => a -> B.Builder
119−builder = fromEncoding . toEncoding
120−
121118toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
122119toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
123120 mv <- VM.unsafeNew 2
@@ -278,7 +275,7 @@ class ToJSON a where
278275 -- @
279276
280277 toEncoding :: a -> Encoding
281− toEncoding = E.Encoding . EB.encodeToBuilder . toJSON
278+ toEncoding = E.value . toJSON
282279 {-# INLINE toEncoding #-}
283280
284281 toJSONList :: [a] -> Value
@@ -299,7 +296,7 @@ class KeyValue kv where
299296 infixr 8 .=
300297
301298instance KeyValue Series where
302− name .= value = E.Value $ E.text name <> E.colon <> toEncoding value
299+ name .= value = E.pair name (toEncoding value)
303300 {-# INLINE (.=) #-}
304301
305302instance KeyValue Pair where
@@ -1192,7 +1189,7 @@ instance ToJSON1 Maybe where
11921189 {-# INLINE liftToJSON #-}
11931190
11941191 liftToEncoding t _ (Just a) = t a
1195− liftToEncoding _ _ Nothing = Encoding EB.null_
1192+ liftToEncoding _ _ Nothing = E.null_
11961193 {-# INLINE liftToEncoding #-}
11971194
11981195instance (ToJSON a) => ToJSON (Maybe a) where
@@ -1208,15 +1205,9 @@ instance ToJSON2 Either where
12081205 liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
12091206 {-# INLINE liftToJSON2 #-}
12101207
1211− liftToEncoding2 toA _ _toB _ (Left a) =
1212− Encoding (B.shortByteString "{\"Left\":")
1213− <> toA a
1214− <> Encoding (B.char7 '}')
1208+ liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
12151209
1216− liftToEncoding2 _toA _ toB _ (Right b) =
1217− Encoding (B.shortByteString "{\"Right\":")
1218− <> toB b
1219− <> Encoding (B.char7 '}')
1210+ liftToEncoding2 _toA _ toB _ (Right b) = E.pairs $ E.pair "Right" $ toB b
12201211 {-# INLINE liftToEncoding2 #-}
12211212
12221213instance (ToJSON a) => ToJSON1 (Either a) where
@@ -1277,10 +1268,10 @@ instance ToJSON Char where
12771268 toJSONList = String . T.pack
12781269 {-# INLINE toJSONList #-}
12791270
1280− toEncoding = Encoding . EB.string . (:[])
1271+ toEncoding = E.string . (:[])
12811272 {-# INLINE toEncoding #-}
12821273
1283− toEncodingList = Encoding . EB.string
1274+ toEncodingList = E.string
12841275 {-# INLINE toEncodingList #-}
12851276
12861277
@@ -1501,9 +1492,7 @@ instance ToJSON LT.Text where
15011492 toJSON = String . LT.toStrict
15021493 {-# INLINE toJSON #-}
15031494
1504− toEncoding t = Encoding $
1505− B.char7 '"' <>
1506− LT.foldrChunks (\x xs -> EB.unquoted x <> xs) (B.char7 '"') t
1495+ toEncoding = E.lazyText
15071496 {-# INLINE toEncoding #-}
15081497
15091498instance ToJSON Version where
@@ -1513,7 +1502,6 @@ instance ToJSON Version where
15131502 toEncoding = toEncoding . showVersion
15141503 {-# INLINE toEncoding #-}
15151504
1516−
15171505-------------------------------------------------------------------------------
15181506-- semigroups NonEmpty
15191507-------------------------------------------------------------------------------
@@ -1686,13 +1674,7 @@ instance ToJSON1 Vector where
16861674 liftToJSON t _ = Array . V.map t
16871675 {-# INLINE liftToJSON #-}
16881676
1689− liftToEncoding t _ xs
1690− | V.null xs = emptyArray_
1691− | otherwise = Encoding $
1692− B.char7 '[' <> fromEncoding (t (V.unsafeHead xs)) <>
1693− V.foldr go (B.char7 ']') (V.unsafeTail xs)
1694− where
1695− go v b = B.char7 ',' <> fromEncoding (t v) <> b
1677+ liftToEncoding t _ = listEncoding t . V.toList
16961678 {-# INLINE liftToEncoding #-}
16971679
16981680instance (ToJSON a) => ToJSON (Vector a) where
@@ -1703,12 +1685,7 @@ instance (ToJSON a) => ToJSON (Vector a) where
17031685 {-# INLINE toEncoding #-}
17041686
17051687encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1706−encodeVector xs
1707− | VG.null xs = emptyArray_
1708− | otherwise = Encoding $
1709− B.char7 '[' <> builder (VG.unsafeHead xs) <>
1710− VG.foldr go (B.char7 ']') (VG.unsafeTail xs)
1711− where go v b = B.char7 ',' <> builder v <> b
1688+encodeVector = listEncoding toEncoding . VG.toList
17121689{-# INLINE encodeVector #-}
17131690
17141691vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
@@ -1787,7 +1764,7 @@ instance ToJSON Value where
17871764 toJSON a = a
17881765 {-# INLINE toJSON #-}
17891766
1790− toEncoding = Encoding . EB.encodeToBuilder
1767+ toEncoding = E.value
17911768 {-# INLINE toEncoding #-}
17921769
17931770instance ToJSON DotNetTime where
@@ -1799,55 +1776,67 @@ dotNetTime :: DotNetTime -> String
17991776dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
18001777 where secs = formatTime defaultTimeLocale "/Date(%s" t
18011778
1779+formatMillis :: (FormatTime t) => t -> String
1780+formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1781+
18021782-------------------------------------------------------------------------------
18031783-- time
18041784-------------------------------------------------------------------------------
18051785
18061786instance ToJSON Day where
1807− toJSON = stringEncoding
1808− toEncoding z = Encoding (EB.quote $ EB.day z)
1787+ toJSON = stringEncoding . E.day
1788+ toEncoding = E.day
1789+
1790+instance ToJSONKey Day where
1791+ toJSONKey = toJSONKeyTextEnc E.day
18091792
18101793
18111794instance ToJSON TimeOfDay where
1812− toJSON = stringEncoding
1813− toEncoding z = Encoding (EB.quote $ EB.timeOfDay z)
1795+ toJSON = stringEncoding . E.timeOfDay
1796+ toEncoding = E.timeOfDay
1797+
1798+instance ToJSONKey TimeOfDay where
1799+ toJSONKey = toJSONKeyTextEnc E.timeOfDay
18141800
18151801
18161802instance ToJSON LocalTime where
1817− toJSON = stringEncoding
1818− toEncoding z = Encoding (EB.quote $ EB.localTime z)
1803+ toJSON = stringEncoding . E.localTime
1804+ toEncoding = E.localTime
1805+
1806+instance ToJSONKey LocalTime where
1807+ toJSONKey = toJSONKeyTextEnc E.localTime
1808+
18191809
18201810instance ToJSON ZonedTime where
1821− toJSON = stringEncoding
1811+ toJSON = stringEncoding . E.zonedTime
1812+ toEncoding = E.zonedTime
18221813
1823− toEncoding z = Encoding (EB.quote $ EB.zonedTime z)
1814+instance ToJSONKey ZonedTime where
1815+ toJSONKey = toJSONKeyTextEnc E.zonedTime
18241816
1825−formatMillis :: (FormatTime t) => t -> String
1826−formatMillis = take 3 . formatTime defaultTimeLocale "%q"
18271817
18281818instance ToJSON UTCTime where
1829− toJSON = stringEncoding
1819+ toJSON = stringEncoding . E.utcTime
1820+ toEncoding = E.utcTime
18301821
1831− toEncoding t = Encoding (EB.quote $ EB.utcTime t)
1822+instance ToJSONKey UTCTime where
1823+ toJSONKey = toJSONKeyTextEnc E.utcTime
18321824
18331825-- | Encode something t a JSON string.
1834−--
1835−-- TODO: remove me, this is a hack
1836−stringEncoding :: (ToJSON a) => a -> Value
1826+stringEncoding :: Encoding -> Value
18371827stringEncoding = String
18381828 . T.dropAround (== '"')
18391829 . T.decodeLatin1
18401830 . L.toStrict
18411831 . B.toLazyByteString
18421832 . fromEncoding
1843− . toEncoding
18441833{-# INLINE stringEncoding #-}
18451834
18461835instance ToJSON NominalDiffTime where
18471836 toJSON = Number . realToFrac
18481837 {-# INLINE toJSON #-}
18491838
1850− toEncoding = Encoding . EB.scientific . realToFrac
1839+ toEncoding = E.scientific . realToFrac
18511840 {-# INLINE toEncoding #-}
18521841
18531842-------------------------------------------------------------------------------
@@ -1906,7 +1895,7 @@ instance ToJSON (Proxy a) where
19061895 toJSON _ = Null
19071896 {-# INLINE toJSON #-}
19081897
1909− toEncoding _ = Encoding EB.null_
1898+ toEncoding _ = E.null_
19101899 {-# INLINE toEncoding #-}
19111900
19121901