@@ -61,7 +61,7 @@ import Data.Aeson.Functions (mapHashKeyVal, mapKeyVal)
61import Data.Aeson.Types.Generic
61import Data.Aeson.Types.Generic
62import Data.Aeson.Types.Internal
62import Data.Aeson.Types.Internal
64−import qualified Data.Aeson.Encode.Builder as EB
64+-- We need internal here for generic deriving
65import qualified Data.Aeson.Encoding.Internal as E
65import qualified Data.Aeson.Encoding.Internal as E
67import Control.Applicative (Const (..))
67import Control.Applicative (Const (..))
@@ -92,7 +92,7 @@ import Foreign.Storable (Storable)
92import GHC.Generics
92import GHC.Generics
93import Numeric.Natural (Natural)
93import Numeric.Natural (Natural)
95−import qualified Data.ByteString.Builder as B
95+import qualified Data.ByteString.Builder as B (toLazyByteString)
96import qualified Data.ByteString.Lazy as L
96import qualified Data.ByteString.Lazy as L
97import qualified Data.DList as DList
97import qualified Data.DList as DList
98import qualified Data.HashMap.Strict as H
98import qualified Data.HashMap.Strict as H
@@ -115,9 +115,6 @@ import qualified Data.Vector.Primitive as VP
115import qualified Data.Vector.Storable as VS
115import qualified Data.Vector.Storable as VS
116import qualified Data.Vector.Unboxed as VU
116import qualified Data.Vector.Unboxed as VU
118−builder :: ToJSON a => a -> B.Builder
119−builder = fromEncoding . toEncoding
121toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
118toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
122toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
119toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
123 mv <- VM.unsafeNew 2
120 mv <- VM.unsafeNew 2
@@ -278,7 +275,7 @@ class ToJSON a where
280 toEncoding :: a -> Encoding
277 toEncoding :: a -> Encoding
281− toEncoding = E.Encoding . EB.encodeToBuilder . toJSON
278+ toEncoding = E.value . toJSON
282 {-# INLINE toEncoding #-}
279 {-# INLINE toEncoding #-}
284 toJSONList :: [a] -> Value
281 toJSONList :: [a] -> Value
@@ -299,7 +296,7 @@ class KeyValue kv where
299 infixr 8 .=
296 infixr 8 .=
301instance KeyValue Series where
298instance KeyValue Series where
302− name .= value = E.Value $ E.text name <> E.colon <> toEncoding value
299+ name .= value = E.pair name (toEncoding value)
303 {-# INLINE (.=) #-}
300 {-# INLINE (.=) #-}
305instance KeyValue Pair where
302instance KeyValue Pair where
@@ -1192,7 +1189,7 @@ instance ToJSON1 Maybe where
1192 {-# INLINE liftToJSON #-}
1189 {-# INLINE liftToJSON #-}
1194 liftToEncoding t _ (Just a) = t a
1191 liftToEncoding t _ (Just a) = t a
1195− liftToEncoding _ _ Nothing = Encoding EB.null_
1192+ liftToEncoding _ _ Nothing = E.null_
1196 {-# INLINE liftToEncoding #-}
1193 {-# INLINE liftToEncoding #-}
1198instance (ToJSON a) => ToJSON (Maybe a) where
1195instance (ToJSON a) => ToJSON (Maybe a) where
@@ -1208,15 +1205,9 @@ instance ToJSON2 Either where
1208 liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
1205 liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
1209 {-# INLINE liftToJSON2 #-}
1206 {-# INLINE liftToJSON2 #-}
1211− liftToEncoding2 toA _ _toB _ (Left a) =
1208+ liftToEncoding2 toA _ _toB _ (Left a) = E.pairs $ E.pair "Left" $ toA a
1212− Encoding (B.shortByteString "{\"Left\":")
1214− <> Encoding (B.char7 '}')
1216− liftToEncoding2 _toA _ toB _ (Right b) =
1210+ liftToEncoding2 _toA _ toB _ (Right b) = E.pairs $ E.pair "Right" $ toB b
1217− Encoding (B.shortByteString "{\"Right\":")
1219− <> Encoding (B.char7 '}')
1220 {-# INLINE liftToEncoding2 #-}
1211 {-# INLINE liftToEncoding2 #-}
1222instance (ToJSON a) => ToJSON1 (Either a) where
1213instance (ToJSON a) => ToJSON1 (Either a) where
@@ -1277,10 +1268,10 @@ instance ToJSON Char where
1277 toJSONList = String . T.pack
1268 toJSONList = String . T.pack
1278 {-# INLINE toJSONList #-}
1269 {-# INLINE toJSONList #-}
1280− toEncoding = Encoding . EB.string . (:[])
1271+ toEncoding = E.string . (:[])
1281 {-# INLINE toEncoding #-}
1272 {-# INLINE toEncoding #-}
1283− toEncodingList = Encoding . EB.string
1274+ toEncodingList = E.string
1284 {-# INLINE toEncodingList #-}
1275 {-# INLINE toEncodingList #-}
@@ -1501,9 +1492,7 @@ instance ToJSON LT.Text where
1501 toJSON = String . LT.toStrict
1492 toJSON = String . LT.toStrict
1502 {-# INLINE toJSON #-}
1493 {-# INLINE toJSON #-}
1504− toEncoding t = Encoding $
1495+ toEncoding = E.lazyText
1506− LT.foldrChunks (\x xs -> EB.unquoted x <> xs) (B.char7 '"') t
1507 {-# INLINE toEncoding #-}
1496 {-# INLINE toEncoding #-}
1509instance ToJSON Version where
1498instance ToJSON Version where
@@ -1513,7 +1502,6 @@ instance ToJSON Version where
1513 toEncoding = toEncoding . showVersion
1502 toEncoding = toEncoding . showVersion
1514 {-# INLINE toEncoding #-}
1503 {-# INLINE toEncoding #-}
1517-------------------------------------------------------------------------------
1505-------------------------------------------------------------------------------
1518-- semigroups NonEmpty
1506-- semigroups NonEmpty
1519-------------------------------------------------------------------------------
1507-------------------------------------------------------------------------------
@@ -1686,13 +1674,7 @@ instance ToJSON1 Vector where
1686 liftToJSON t _ = Array . V.map t
1674 liftToJSON t _ = Array . V.map t
1687 {-# INLINE liftToJSON #-}
1675 {-# INLINE liftToJSON #-}
1689− liftToEncoding t _ xs
1677+ liftToEncoding t _ = listEncoding t . V.toList
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)
1695− go v b = B.char7 ',' <> fromEncoding (t v) <> b
1696 {-# INLINE liftToEncoding #-}
1678 {-# INLINE liftToEncoding #-}
1698instance (ToJSON a) => ToJSON (Vector a) where
1680instance (ToJSON a) => ToJSON (Vector a) where
@@ -1703,12 +1685,7 @@ instance (ToJSON a) => ToJSON (Vector a) where
1703 {-# INLINE toEncoding #-}
1685 {-# INLINE toEncoding #-}
1705encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1687encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
1706−encodeVector xs
1688+encodeVector = listEncoding toEncoding . VG.toList
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
1712{-# INLINE encodeVector #-}
1689{-# INLINE encodeVector #-}
1714vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
1691vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
@@ -1787,7 +1764,7 @@ instance ToJSON Value where
1787 toJSON a = a
1764 toJSON a = a
1788 {-# INLINE toJSON #-}
1765 {-# INLINE toJSON #-}
1790− toEncoding = Encoding . EB.encodeToBuilder
1767+ toEncoding = E.value
1791 {-# INLINE toEncoding #-}
1768 {-# INLINE toEncoding #-}
1793instance ToJSON DotNetTime where
1770instance ToJSON DotNetTime where
@@ -1799,55 +1776,67 @@ dotNetTime :: DotNetTime -> String
1799dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1776dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
1800 where secs = formatTime defaultTimeLocale "/Date(%s" t
1777 where secs = formatTime defaultTimeLocale "/Date(%s" t
1779+formatMillis :: (FormatTime t) => t -> String
1780+formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1802-------------------------------------------------------------------------------
1782-------------------------------------------------------------------------------
1804-------------------------------------------------------------------------------
1784-------------------------------------------------------------------------------
1806instance ToJSON Day where
1786instance ToJSON Day where
1807− toJSON = stringEncoding
1787+ toJSON = stringEncoding . E.day
1808− toEncoding z = Encoding (EB.quote $ EB.day z)
1788+ toEncoding = E.day
1790+instance ToJSONKey Day where
1791+ toJSONKey = toJSONKeyTextEnc E.day
1811instance ToJSON TimeOfDay where
1794instance ToJSON TimeOfDay where
1812− toJSON = stringEncoding
1795+ toJSON = stringEncoding . E.timeOfDay
1813− toEncoding z = Encoding (EB.quote $ EB.timeOfDay z)
1796+ toEncoding = E.timeOfDay
1798+instance ToJSONKey TimeOfDay where
1799+ toJSONKey = toJSONKeyTextEnc E.timeOfDay
1816instance ToJSON LocalTime where
1802instance ToJSON LocalTime where
1817− toJSON = stringEncoding
1803+ toJSON = stringEncoding . E.localTime
1818− toEncoding z = Encoding (EB.quote $ EB.localTime z)
1804+ toEncoding = E.localTime
1806+instance ToJSONKey LocalTime where
1807+ toJSONKey = toJSONKeyTextEnc E.localTime
1820instance ToJSON ZonedTime where
1810instance ToJSON ZonedTime where
1821− toJSON = stringEncoding
1811+ toJSON = stringEncoding . E.zonedTime
1812+ toEncoding = E.zonedTime
1823− toEncoding z = Encoding (EB.quote $ EB.zonedTime z)
1814+instance ToJSONKey ZonedTime where
1815+ toJSONKey = toJSONKeyTextEnc E.zonedTime
1825−formatMillis :: (FormatTime t) => t -> String
1826−formatMillis = take 3 . formatTime defaultTimeLocale "%q"
1828instance ToJSON UTCTime where
1818instance ToJSON UTCTime where
1829− toJSON = stringEncoding
1819+ toJSON = stringEncoding . E.utcTime
1820+ toEncoding = E.utcTime
1831− toEncoding t = Encoding (EB.quote $ EB.utcTime t)
1822+instance ToJSONKey UTCTime where
1823+ toJSONKey = toJSONKeyTextEnc E.utcTime
1833-- | Encode something t a JSON string.
1825-- | Encode something t a JSON string.
1835−-- TODO: remove me, this is a hack
1836−stringEncoding :: (ToJSON a) => a -> Value
1826+stringEncoding :: Encoding -> Value
1837stringEncoding = String
1827stringEncoding = String
1838 . T.dropAround (== '"')
1828 . T.dropAround (== '"')
1839 . T.decodeLatin1
1829 . T.decodeLatin1
1840 . L.toStrict
1830 . L.toStrict
1841 . B.toLazyByteString
1831 . B.toLazyByteString
1842 . fromEncoding
1832 . fromEncoding
1844{-# INLINE stringEncoding #-}
1833{-# INLINE stringEncoding #-}
1846instance ToJSON NominalDiffTime where
1835instance ToJSON NominalDiffTime where
1847 toJSON = Number . realToFrac
1836 toJSON = Number . realToFrac
1848 {-# INLINE toJSON #-}
1837 {-# INLINE toJSON #-}
1850− toEncoding = Encoding . EB.scientific . realToFrac
1839+ toEncoding = E.scientific . realToFrac
1851 {-# INLINE toEncoding #-}
1840 {-# INLINE toEncoding #-}
1853-------------------------------------------------------------------------------
1842-------------------------------------------------------------------------------
@@ -1906,7 +1895,7 @@ instance ToJSON (Proxy a) where
1906 toJSON _ = Null
1895 toJSON _ = Null
1907 {-# INLINE toJSON #-}
1896 {-# INLINE toJSON #-}
1909− toEncoding _ = Encoding EB.null_
1898+ toEncoding _ = E.null_
1910 {-# INLINE toEncoding #-}
1899 {-# INLINE toEncoding #-}