@@ -55,8 +55,8 @@ module Data.Aeson.Types.ToJSON (
5555import Prelude ()
5656import Prelude.Compat
5757
58−import Data.Aeson.Encoding.Internal (Encoding (..), Series, dict, emptyArray_,
59− tuple, (>*<))
58+import Data.Aeson.Encoding.Internal (Encoding, Encoding', Series, dict,
59+ emptyArray_, tuple, (>*<), (><))
6060import Data.Aeson.Internal.Functions (mapHashKeyVal, mapKeyVal)
6161import Data.Aeson.Types.Generic
6262import Data.Aeson.Types.Internal
@@ -92,7 +92,6 @@ import Foreign.Storable (Storable)
9292import GHC.Generics
9393import Numeric.Natural (Natural)
9494
95−import qualified Data.ByteString.Builder as B (toLazyByteString)
9695import qualified Data.ByteString.Lazy as L
9796import qualified Data.DList as DList
9897import qualified Data.HashMap.Strict as H
@@ -423,7 +422,7 @@ class ToJSONKey a where
423422 toJSONKeyList = ToJSONKeyValue (toJSON, toEncoding)
424423
425424data ToJSONKeyFunction a
426− = ToJSONKeyText (a -> Text, a -> Encoding) -- ^ key is encoded to string, produces object
425+ = ToJSONKeyText (a -> Text, a -> Encoding' Text) -- ^ key is encoded to string, produces object
427426 | ToJSONKeyValue (a -> Value, a -> Encoding) -- ^ key is encoded to value, produces array
428427
429428contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
@@ -672,7 +671,7 @@ instance ( AllNullary (a :+: b) allNullary
672671 -- 'allNullaryToStringTag' option is set they are encoded to
673672 -- strings. This distinction is made by 'sumToEncoding':
674673 gToEncoding opts pa te tel
675− = (unTagged :: Tagged allNullary Encoding -> Encoding)
674+ = (unTagged :: Tagged allNullary (Encoding) -> Encoding)
676675 . sumToEncoding opts pa te tel
677676
678677instance (ToJSON1 f, GToEncoding One g) => GToEncoding One (f :.: g) where
@@ -724,7 +723,7 @@ nonAllNullarySumToJSON opts pa tj tjl =
724723class SumToEncoding arity f allNullary where
725724 sumToEncoding :: Options -> Proxy arity
726725 -> (a -> Encoding) -> ([a] -> Encoding)
727− -> f a -> Tagged allNullary Encoding
726+ -> f a -> Tagged allNullary (Encoding)
728727
729728instance ( GetConName f
730729 , TaggedObjectEnc arity f
@@ -818,29 +817,28 @@ instance ( IsRecord a isRecord
818817 , Constructor c
819818 ) => TaggedObjectEnc arity (C1 c a) where
820819 taggedObjectEnc opts pa tagFieldName contentsFieldName te tel v =
821− E.openCurly <>
822− (toEncoding tagFieldName <>
823− E.colon <>
824− toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) <>
825− ((unTagged :: Tagged isRecord Encoding -> Encoding) .
826− taggedObjectEnc' opts pa contentsFieldName te tel . unM1 $ v) <>
827− E.closeCurly
820+ E.wrapObject $
821+ (E.string tagFieldName ><
822+ E.colon ><
823+ toEncoding (constructorTagModifier opts (conName (undefined :: t c a p)))) ><
824+ ((unTagged :: Tagged isRecord (Encoding) -> Encoding) .
825+ taggedObjectEnc' opts pa contentsFieldName te tel . unM1 $ v)
828826
829827class TaggedObjectEnc' arity f isRecord where
830828 taggedObjectEnc' :: Options -> Proxy arity
831829 -> String -> (a -> Encoding) -> ([a] -> Encoding)
832− -> f a -> Tagged isRecord Encoding
830+ -> f a -> Tagged isRecord (Encoding)
833831
834832instance OVERLAPPING_ TaggedObjectEnc' arity U1 False where
835− taggedObjectEnc' _ _ _ _ _ _ = Tagged mempty
833+ taggedObjectEnc' _ _ _ _ _ _ = Tagged E.empty
836834
837835instance (RecordToEncoding arity f) => TaggedObjectEnc' arity f True where
838− taggedObjectEnc' opts pa _ te tel = Tagged . (E.comma <>) . fst
836+ taggedObjectEnc' opts pa _ te tel = Tagged . (E.comma ><) . fst
839837 . recordToEncoding opts pa te tel
840838
841839instance (GToEncoding arity f) => TaggedObjectEnc' arity f False where
842840 taggedObjectEnc' opts pa contentsFieldName te tel =
843− Tagged . (\z -> E.comma <> toEncoding contentsFieldName <> E.colon <> z) .
841+ Tagged . (\z -> E.comma >< toEncoding contentsFieldName >< E.colon >< z) .
844842 gToEncoding opts pa te tel
845843
846844--------------------------------------------------------------------------------
@@ -942,13 +940,13 @@ class ConsToEncoding' arity f isRecord where
942940 consToEncoding' :: Options -> Proxy arity
943941 -> Bool -- ^ Are we a record with one field?
944942 -> (a -> Encoding) -> ([a] -> Encoding)
945− -> f a -> Tagged isRecord Encoding
943+ -> f a -> Tagged isRecord (Encoding)
946944
947945instance ( IsRecord f isRecord
948946 , ConsToEncoding' arity f isRecord
949947 ) => ConsToEncoding arity f where
950948 consToEncoding opts pa te tel =
951− (unTagged :: Tagged isRecord Encoding -> Encoding)
949+ (unTagged :: Tagged isRecord (Encoding) -> (Encoding))
952950 . consToEncoding' opts pa (isUnary (undefined :: f a)) te tel
953951
954952instance (RecordToEncoding arity f) => ConsToEncoding' arity f True where
@@ -999,39 +997,40 @@ class RecordToEncoding arity f where
999997 -- of the field (without the key); 'Nothing' otherwise
1000998 recordToEncoding :: Options -> Proxy arity
1001999 -> (a -> Encoding) -> ([a] -> Encoding)
1002− -> f a -> (Encoding, Maybe Encoding)
1000+ -> f a -> (Encoding, Maybe (Encoding))
10031001
10041002instance ( RecordToEncoding arity a
10051003 , RecordToEncoding arity b
10061004 ) => RecordToEncoding arity (a :*: b) where
10071005 recordToEncoding opts pa te tel (a :*: b) | omitNothingFields opts =
1008− (mconcat $ intersperse E.comma $
1006+ (E.econcat $ intersperse E.comma $
10091007 filter (not . E.nullEncoding)
10101008 [ fst (recordToEncoding opts pa te tel a)
10111009 , fst (recordToEncoding opts pa te tel b) ]
10121010 , Nothing)
10131011 recordToEncoding opts pa te tel (a :*: b) =
1014− (fst (recordToEncoding opts pa te tel a) <> E.comma <>
1012+ (fst (recordToEncoding opts pa te tel a) >< E.comma ><
10151013 fst (recordToEncoding opts pa te tel b),
10161014 Nothing)
10171015
1016+
10181017instance (Selector s, GToEncoding arity a) => RecordToEncoding arity (S1 s a) where
10191018 recordToEncoding = fieldToEncoding
10201019
10211020instance OVERLAPPING_ (Selector s, ToJSON a) =>
10221021 RecordToEncoding arity (S1 s (K1 i (Maybe a))) where
10231022 recordToEncoding opts _ _ _ (M1 k1) | omitNothingFields opts
1024− , K1 Nothing <- k1 = (mempty, Nothing)
1023+ , K1 Nothing <- k1 = (E.empty, Nothing)
10251024 recordToEncoding opts pa te tel m1 = fieldToEncoding opts pa te tel m1
10261025
10271026fieldToEncoding :: (Selector s, GToEncoding arity a)
10281027 => Options -> Proxy arity
10291028 -> (p -> Encoding) -> ([p] -> Encoding)
1030− -> S1 s a p -> (Encoding, Maybe Encoding)
1029+ -> S1 s a p -> (Encoding, Maybe (Encoding))
10311030fieldToEncoding opts pa te tel m1 =
10321031 let keyBuilder = toEncoding (fieldLabelModifier opts $ selName m1)
10331032 valueBuilder = gToEncoding opts pa te tel (unM1 m1)
1034− in (keyBuilder <> E.colon <> valueBuilder, Just valueBuilder)
1033+ in (keyBuilder >< E.colon >< valueBuilder, Just valueBuilder)
10351034
10361035--------------------------------------------------------------------------------
10371036
@@ -1066,21 +1065,20 @@ instance OVERLAPPABLE_ (GToJSON arity a) => WriteProduct arity a where
10661065class EncodeProduct arity f where
10671066 encodeProduct :: Options -> Proxy arity
10681067 -> (a -> Encoding) -> ([a] -> Encoding)
1069− -> f a -> Encoding
1068+ -> f a -> Encoding' E.InArray
10701069
10711070instance ( EncodeProduct arity a
10721071 , EncodeProduct arity b
10731072 ) => EncodeProduct arity (a :*: b) where
10741073 encodeProduct opts pa te tel (a :*: b) | omitNothingFields opts =
1075− mconcat $ intersperse E.comma $
1074+ E.econcat $ intersperse E.comma $
10761075 filter (not . E.nullEncoding)
10771076 [encodeProduct opts pa te tel a, encodeProduct opts pa te tel b]
1078− encodeProduct opts pa te tel (a :*: b) = encodeProduct opts pa te tel a <>
1079− E.comma <>
1077+ encodeProduct opts pa te tel (a :*: b) = encodeProduct opts pa te tel a >*<
10801078 encodeProduct opts pa te tel b
10811079
10821080instance OVERLAPPABLE_ (GToEncoding arity a) => EncodeProduct arity a where
1083− encodeProduct opts = gToEncoding opts
1081+ encodeProduct opts pa te tel a = E.retagEncoding $ gToEncoding opts pa te tel a
10841082
10851083--------------------------------------------------------------------------------
10861084
@@ -1126,12 +1124,12 @@ instance ( GToEncoding arity a
11261124 , Constructor c
11271125 ) => ObjectWithSingleFieldEnc arity (C1 c a) where
11281126 objectWithSingleFieldEnc opts pa te tel v =
1129− E.openCurly <>
1127+ E.openCurly ><
11301128 toEncoding
11311129 (constructorTagModifier opts
1132− (conName (undefined :: t c a p))) <>
1133− E.colon <>
1134− gToEncoding opts pa te tel v <>
1130+ (conName (undefined :: t c a p))) ><
1131+ E.colon ><
1132+ gToEncoding opts pa te tel v ><
11351133 E.closeCurly
11361134
11371135-------------------------------------------------------------------------------
@@ -1235,7 +1233,7 @@ instance ToJSON Bool where
12351233instance ToJSONKey Bool where
12361234 toJSONKey = ToJSONKeyText
12371235 ( (\x -> if x then "true" else "false")
1238− , (\x -> toEncoding $ if x then ("true" :: Text) else "false")
1236+ , (\x -> E.text $ if x then "true"else "false")
12391237 )
12401238
12411239
@@ -1481,7 +1479,7 @@ instance ToJSON Text where
14811479 {-# INLINE toEncoding #-}
14821480
14831481instance ToJSONKey Text where
1484− toJSONKey = ToJSONKeyText (id, toEncoding)
1482+ toJSONKey = ToJSONKeyText (id, E.text)
14851483 {-# INLINE toJSONKey #-}
14861484
14871485instance ToJSON LT.Text where
@@ -1819,13 +1817,12 @@ instance ToJSONKey UTCTime where
18191817 toJSONKey = toJSONKeyTextEnc E.utcTime
18201818
18211819-- | Encode something t a JSON string.
1822−stringEncoding :: Encoding -> Value
1820+stringEncoding :: Encoding' Text -> Value
18231821stringEncoding = String
18241822 . T.dropAround (== '"')
18251823 . T.decodeLatin1
18261824 . L.toStrict
1827− . B.toLazyByteString
1828− . fromEncoding
1825+ . E.encodingToLazyByteString
18291826{-# INLINE stringEncoding #-}
18301827
18311828instance ToJSON NominalDiffTime where
@@ -1920,10 +1917,10 @@ instance ToJSONKey b => ToJSONKey (Tagged a b) where
19201917
19211918-- | TODO: Move ToJSONKEyFunction to .Types(.Internal),
19221919-- export and document this function from there
1923−toJSONKeyTextEnc :: (a -> Encoding) -> ToJSONKeyFunction a
1920+toJSONKeyTextEnc :: (a -> Encoding' Text) -> ToJSONKeyFunction a
19241921toJSONKeyTextEnc e = ToJSONKeyText
19251922 -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
1926− ( T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . B.toLazyByteString . fromEncoding . e
1923+ ( T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . E.encodingToLazyByteString . e
19271924 , e
19281925 )
19291926
@@ -1932,8 +1929,8 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
19321929instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSONKey (a,b,c,d)
19331930
19341931instance ToJSONKey Char where
1935− toJSONKey = ToJSONKeyText (T.singleton, toEncoding)
1936− toJSONKeyList = ToJSONKeyText (T.pack, toEncoding . T.pack)
1932+ toJSONKey = ToJSONKeyText (T.singleton, E.string . (:[]))
1933+ toJSONKeyList = ToJSONKeyText (T.pack, E.text . T.pack)
19371934
19381935instance (ToJSONKey a, ToJSON a) => ToJSONKey [a] where
19391936 toJSONKey = toJSONKeyList