@@ -107,8 +107,6 @@ import qualified Data.Set as Set
107import qualified Data.Text as T
107import qualified Data.Text as T
108import qualified Data.Text.Encoding as T
108import qualified Data.Text.Encoding as T
109import qualified Data.Text.Lazy as LT
109import qualified Data.Text.Lazy as LT
110−import qualified Data.Text.Lazy.Builder as LTB
111−import qualified Data.Text.Lazy.Builder.Int as LTBI
112import qualified Data.Tree as Tree
110import qualified Data.Tree as Tree
113import qualified Data.Vector as V
111import qualified Data.Vector as V
114import qualified Data.Vector.Generic as VG
112import qualified Data.Vector.Generic as VG
@@ -134,12 +132,6 @@ realFloatToJSON d
134 | otherwise = Number $ Scientific.fromFloatDigits d
132 | otherwise = Number $ Scientific.fromFloatDigits d
135{-# INLINE realFloatToJSON #-}
133{-# INLINE realFloatToJSON #-}
137−realFloatToEncoding :: RealFloat a => a -> Encoding
138−realFloatToEncoding d
139− | isNaN d || isInfinite d = Encoding EB.null_
140− | otherwise = toEncoding (Scientific.fromFloatDigits d)
141−{-# INLINE realFloatToEncoding #-}
143-------------------------------------------------------------------------------
135-------------------------------------------------------------------------------
144-- Generics
136-- Generics
145-------------------------------------------------------------------------------
137-------------------------------------------------------------------------------
@@ -1139,9 +1131,15 @@ instance ToJSON Bool where
1139 toJSON = Bool
1131 toJSON = Bool
1140 {-# INLINE toJSON #-}
1132 {-# INLINE toJSON #-}
1142− toEncoding = Encoding . EB.bool
1134+ toEncoding = E.bool
1143 {-# INLINE toEncoding #-}
1135 {-# INLINE toEncoding #-}
1137+instance ToJSONKey Bool where
1138+ toJSONKey = ToJSONKeyText
1139+ ( (\x -> if x then "true" else "false")
1140+ , (\x -> toEncoding $ if x then ("true" :: Text) else "false")
1146instance ToJSON Ordering where
1144instance ToJSON Ordering where
1147 toJSON = toJSON . orderingToText
1145 toJSON = toJSON . orderingToText
@@ -1179,9 +1177,13 @@ instance ToJSON Double where
1179 toJSON = realFloatToJSON
1177 toJSON = realFloatToJSON
1180 {-# INLINE toJSON #-}
1178 {-# INLINE toJSON #-}
1182− toEncoding = realFloatToEncoding
1180+ toEncoding = E.double
1183 {-# INLINE toEncoding #-}
1181 {-# INLINE toEncoding #-}
1183+instance ToJSONKey Double where
1184+ toJSONKey = toJSONKeyTextEnc E.doubleText
1185+ {-# INLINE toJSONKey #-}
1186instance ToJSON Number where
1188instance ToJSON Number where
1187 toJSON (D d) = toJSON d
1189 toJSON (D d) = toJSON d
@@ -1197,20 +1199,23 @@ instance ToJSON Float where
1197 toJSON = realFloatToJSON
1199 toJSON = realFloatToJSON
1198 {-# INLINE toJSON #-}
1200 {-# INLINE toJSON #-}
1200− toEncoding = realFloatToEncoding
1202+ toEncoding = E.float
1201 {-# INLINE toEncoding #-}
1203 {-# INLINE toEncoding #-}
1205+instance ToJSONKey Float where
1206+ toJSONKey = toJSONKeyTextEnc E.floatText
1207+ {-# INLINE toJSONKey #-}
1204−instance ToJSON (Ratio Integer) where
1210+instance (ToJSON a, Integral a) => ToJSON (Ratio a) where
1205 toJSON r = object [ "numerator" .= numerator r
1211 toJSON r = object [ "numerator" .= numerator r
1206 , "denominator" .= denominator r
1212 , "denominator" .= denominator r
1208 {-# INLINE toJSON #-}
1214 {-# INLINE toJSON #-}
1210− toEncoding r = Encoding $
1216+ toEncoding r = E.pairs $
1211− B.shortByteString "{\"numerator\":" <> builder (numerator r) <>
1217+ "numerator" .= numerator r <>
1212− B.shortByteString ",\"denominator\":" <> builder (denominator r) <>
1218+ "denominator" .= denominator r
1214 {-# INLINE toEncoding #-}
1219 {-# INLINE toEncoding #-}
@@ -1218,24 +1223,37 @@ instance HasResolution a => ToJSON (Fixed a) where
1218 toJSON = Number . realToFrac
1223 toJSON = Number . realToFrac
1219 {-# INLINE toJSON #-}
1224 {-# INLINE toJSON #-}
1221− toEncoding = Encoding . EB.number . realToFrac
1226+ toEncoding = E.scientific . realToFrac
1222 {-# INLINE toEncoding #-}
1227 {-# INLINE toEncoding #-}
1229+instance HasResolution a => ToJSONKey (Fixed a) where
1230+ toJSONKey = toJSONKeyTextEnc (E.scientificText . realToFrac)
1231+ {-# INLINE toJSONKey #-}
1225instance ToJSON Int where
1234instance ToJSON Int where
1226 toJSON = Number . fromIntegral
1235 toJSON = Number . fromIntegral
1227 {-# INLINE toJSON #-}
1236 {-# INLINE toJSON #-}
1229− toEncoding = Encoding . B.intDec
1238+ toEncoding = E.int
1230 {-# INLINE toEncoding #-}
1239 {-# INLINE toEncoding #-}
1241+instance ToJSONKey Int where
1242+ toJSONKey = toJSONKeyTextEnc E.intText
1243+ {-# INLINE toJSONKey #-}
1232instance ToJSON Integer where
1246instance ToJSON Integer where
1233 toJSON = Number . fromInteger
1247 toJSON = Number . fromInteger
1234 {-# INLINE toJSON #-}
1248 {-# INLINE toJSON #-}
1236− toEncoding = Encoding . B.integerDec
1250+ toEncoding = E.integer
1237 {-# INLINE toEncoding #-}
1251 {-# INLINE toEncoding #-}
1253+instance ToJSONKey Integer where
1254+ toJSONKey = toJSONKeyTextEnc E.integerText
1255+ {-# INLINE toJSONKey #-}
1240instance ToJSON Natural where
1258instance ToJSON Natural where
1241 toJSON = toJSON . toInteger
1259 toJSON = toJSON . toInteger
@@ -1244,84 +1262,130 @@ instance ToJSON Natural where
1244 toEncoding = toEncoding . toInteger
1262 toEncoding = toEncoding . toInteger
1245 {-# INLINE toEncoding #-}
1263 {-# INLINE toEncoding #-}
1265+instance ToJSONKey Natural where
1266+ toJSONKey = toJSONKeyTextEnc (E.integerText . toInteger)
1267+ {-# INLINE toJSONKey #-}
1248instance ToJSON Int8 where
1270instance ToJSON Int8 where
1249 toJSON = Number . fromIntegral
1271 toJSON = Number . fromIntegral
1250 {-# INLINE toJSON #-}
1272 {-# INLINE toJSON #-}
1252− toEncoding = Encoding . B.int8Dec
1274+ toEncoding = E.int8
1253 {-# INLINE toEncoding #-}
1275 {-# INLINE toEncoding #-}
1277+instance ToJSONKey Int8 where
1278+ toJSONKey = toJSONKeyTextEnc E.int8Text
1279+ {-# INLINE toJSONKey #-}
1256instance ToJSON Int16 where
1282instance ToJSON Int16 where
1257 toJSON = Number . fromIntegral
1283 toJSON = Number . fromIntegral
1258 {-# INLINE toJSON #-}
1284 {-# INLINE toJSON #-}
1260− toEncoding = Encoding . B.int16Dec
1286+ toEncoding = E.int16
1261 {-# INLINE toEncoding #-}
1287 {-# INLINE toEncoding #-}
1289+instance ToJSONKey Int16 where
1290+ toJSONKey = toJSONKeyTextEnc E.int16Text
1291+ {-# INLINE toJSONKey #-}
1264instance ToJSON Int32 where
1294instance ToJSON Int32 where
1265 toJSON = Number . fromIntegral
1295 toJSON = Number . fromIntegral
1266 {-# INLINE toJSON #-}
1296 {-# INLINE toJSON #-}
1268− toEncoding = Encoding . B.int32Dec
1298+ toEncoding = E.int32
1269 {-# INLINE toEncoding #-}
1299 {-# INLINE toEncoding #-}
1301+instance ToJSONKey Int32 where
1302+ toJSONKey = toJSONKeyTextEnc E.int32Text
1303+ {-# INLINE toJSONKey #-}
1272instance ToJSON Int64 where
1306instance ToJSON Int64 where
1273 toJSON = Number . fromIntegral
1307 toJSON = Number . fromIntegral
1274 {-# INLINE toJSON #-}
1308 {-# INLINE toJSON #-}
1276− toEncoding = Encoding . B.int64Dec
1310+ toEncoding = E.int64
1277 {-# INLINE toEncoding #-}
1311 {-# INLINE toEncoding #-}
1313+instance ToJSONKey Int64 where
1314+ toJSONKey = toJSONKeyTextEnc E.int64Text
1315+ {-# INLINE toJSONKey #-}
1280instance ToJSON Word where
1318instance ToJSON Word where
1281 toJSON = Number . fromIntegral
1319 toJSON = Number . fromIntegral
1282 {-# INLINE toJSON #-}
1320 {-# INLINE toJSON #-}
1284− toEncoding = Encoding . B.wordDec
1322+ toEncoding = E.word
1285 {-# INLINE toEncoding #-}
1323 {-# INLINE toEncoding #-}
1325+instance ToJSONKey Word where
1326+ toJSONKey = toJSONKeyTextEnc E.wordText
1327+ {-# INLINE toJSONKey #-}
1288instance ToJSON Word8 where
1330instance ToJSON Word8 where
1289 toJSON = Number . fromIntegral
1331 toJSON = Number . fromIntegral
1290 {-# INLINE toJSON #-}
1332 {-# INLINE toJSON #-}
1292− toEncoding = Encoding . B.word8Dec
1334+ toEncoding = E.word8
1293 {-# INLINE toEncoding #-}
1335 {-# INLINE toEncoding #-}
1337+instance ToJSONKey Word8 where
1338+ toJSONKey = toJSONKeyTextEnc E.word8Text
1339+ {-# INLINE toJSONKey #-}
1296instance ToJSON Word16 where
1342instance ToJSON Word16 where
1297 toJSON = Number . fromIntegral
1343 toJSON = Number . fromIntegral
1298 {-# INLINE toJSON #-}
1344 {-# INLINE toJSON #-}
1300− toEncoding = Encoding . B.word16Dec
1346+ toEncoding = E.word16
1301 {-# INLINE toEncoding #-}
1347 {-# INLINE toEncoding #-}
1349+instance ToJSONKey Word16 where
1350+ toJSONKey = toJSONKeyTextEnc E.word16Text
1351+ {-# INLINE toJSONKey #-}
1304instance ToJSON Word32 where
1354instance ToJSON Word32 where
1305 toJSON = Number . fromIntegral
1355 toJSON = Number . fromIntegral
1306 {-# INLINE toJSON #-}
1356 {-# INLINE toJSON #-}
1308− toEncoding = Encoding . B.word32Dec
1358+ toEncoding = E.word32
1309 {-# INLINE toEncoding #-}
1359 {-# INLINE toEncoding #-}
1361+instance ToJSONKey Word32 where
1362+ toJSONKey = toJSONKeyTextEnc E.word32Text
1363+ {-# INLINE toJSONKey #-}
1311instance ToJSON Word64 where
1366instance ToJSON Word64 where
1312 toJSON = Number . fromIntegral
1367 toJSON = Number . fromIntegral
1313 {-# INLINE toJSON #-}
1368 {-# INLINE toJSON #-}
1315− toEncoding = Encoding . B.word64Dec
1370+ toEncoding = E.word64
1316 {-# INLINE toEncoding #-}
1371 {-# INLINE toEncoding #-}
1373+instance ToJSONKey Word64 where
1374+ toJSONKey = toJSONKeyTextEnc E.word64Text
1375+ {-# INLINE toJSONKey #-}
1318instance ToJSON Text where
1378instance ToJSON Text where
1319 toJSON = String
1379 toJSON = String
1320 {-# INLINE toJSON #-}
1380 {-# INLINE toJSON #-}
1322− toEncoding = Encoding . EB.text
1382+ toEncoding = E.text
1323 {-# INLINE toEncoding #-}
1383 {-# INLINE toEncoding #-}
1385+instance ToJSONKey Text where
1386+ toJSONKey = ToJSONKeyText (id, toEncoding)
1387+ {-# INLINE toJSONKey #-}
1325instance ToJSON LT.Text where
1389instance ToJSON LT.Text where
1326 toJSON = String . LT.toStrict
1390 toJSON = String . LT.toStrict
1327 {-# INLINE toJSON #-}
1391 {-# INLINE toJSON #-}
@@ -1365,9 +1429,12 @@ instance ToJSON Scientific where
1365 toJSON = Number
1429 toJSON = Number
1366 {-# INLINE toJSON #-}
1430 {-# INLINE toJSON #-}
1368− toEncoding = Encoding . EB.number
1432+ toEncoding = E.scientific
1369 {-# INLINE toEncoding #-}
1433 {-# INLINE toEncoding #-}
1435+instance ToJSONKey Scientific where
1436+ toJSONKey = toJSONKeyTextEnc E.scientificText
1371-------------------------------------------------------------------------------
1438-------------------------------------------------------------------------------
1373-------------------------------------------------------------------------------
1440-------------------------------------------------------------------------------
@@ -1669,7 +1736,7 @@ instance ToJSON NominalDiffTime where
1669 toJSON = Number . realToFrac
1736 toJSON = Number . realToFrac
1670 {-# INLINE toJSON #-}
1737 {-# INLINE toJSON #-}
1672− toEncoding = Encoding . EB.number . realToFrac
1739+ toEncoding = Encoding . EB.scientific . realToFrac
1673 {-# INLINE toEncoding #-}
1740 {-# INLINE toEncoding #-}
1675-------------------------------------------------------------------------------
1742-------------------------------------------------------------------------------
@@ -1755,21 +1822,14 @@ instance ToJSONKey b => ToJSONKey (Tagged a b) where
1755-- Instances for converting t map keys
1822-- Instances for converting t map keys
1756-------------------------------------------------------------------------------
1823-------------------------------------------------------------------------------
1758−instance ToJSONKey Text where
1825+-- | TODO: Move ToJSONKEyFunction to .Types(.Internal),
1759− toJSONKey = ToJSONKeyText (id,toEncoding)
1826+-- export and document this function from there
1760−
1827+toJSONKeyTextEnc :: (a -> Encoding) -> ToJSONKeyFunction a
1761−
1828+toJSONKeyTextEnc e = ToJSONKeyText
1762−instance ToJSONKey Bool where
1829+ -- TODO: dropAround is also used in stringEncoding, which is unfortunate atm
1763− toJSONKey = ToJSONKeyText
1830+ ( T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . B.toLazyByteString . fromEncoding . e
1764− ( (\x -> if x then "true" else "false")
1831+ , e
1765− , (\x -> Encoding $ if x then "\"true\"" else "\"false\"")
1768−instance ToJSONKey Int where
1769− toJSONKey = ToJSONKeyText
1770− ( LT.toStrict . LTB.toLazyText . LTBI.decimal
1771− , \x -> Encoding $ B.char7 '"' <> fromEncoding (toEncoding x) <> B.char7 '"'
1774instance (ToJSON a, ToJSON b) => ToJSONKey (a,b)
1834instance (ToJSON a, ToJSON b) => ToJSONKey (a,b)
1775instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)
1835instance (ToJSON a, ToJSON b, ToJSON c) => ToJSONKey (a,b,c)