@@ -51,14 +51,18 @@ module Data.Aeson.Types.Instances
53import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
53import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
54+import qualified Data.Aeson.Encode.ByteString as E
55+import qualified Data.ByteString.Builder as B
56+import qualified Data.ByteString.Builder.Prim as BP
54import Data.Aeson.Functions
57import Data.Aeson.Functions
58+import Data.Monoid ((<>), mempty)
55import Data.Aeson.Types.Class
59import Data.Aeson.Types.Class
56import Data.Aeson.Types.Internal
60import Data.Aeson.Types.Internal
57import Data.Scientific (Scientific)
61import Data.Scientific (Scientific)
58import qualified Data.Scientific as Scientific (coefficient, base10Exponent, fromFloatDigits, toRealFloat)
62import qualified Data.Scientific as Scientific (coefficient, base10Exponent, fromFloatDigits, toRealFloat)
59import Data.Attoparsec.Number (Number(..))
63import Data.Attoparsec.Number (Number(..))
60import Data.Fixed
64import Data.Fixed
61−import Data.Foldable (toList)
65+import Data.Foldable (Foldable, toList)
62import Data.Functor.Identity (Identity(..))
66import Data.Functor.Identity (Identity(..))
63import Data.Hashable (Hashable(..))
67import Data.Hashable (Hashable(..))
64import Data.Int (Int8, Int16, Int32, Int64)
68import Data.Int (Int8, Int16, Int32, Int64)
@@ -72,6 +76,7 @@ import Data.Traversable (traverse)
72import Data.Vector (Vector)
76import Data.Vector (Vector)
73import Data.Word (Word, Word8, Word16, Word32, Word64)
77import Data.Word (Word, Word8, Word16, Word32, Word64)
74import Foreign.Storable (Storable)
78import Foreign.Storable (Storable)
79+import Prelude hiding (foldr)
75import qualified Data.HashMap.Strict as H
80import qualified Data.HashMap.Strict as H
76import qualified Data.HashSet as HashSet
81import qualified Data.HashSet as HashSet
77import qualified Data.IntMap as IntMap
82import qualified Data.IntMap as IntMap
@@ -88,6 +93,7 @@ import qualified Data.Vector.Primitive as VP
88import qualified Data.Vector.Storable as VS
93import qualified Data.Vector.Storable as VS
89import qualified Data.Vector.Unboxed as VU
94import qualified Data.Vector.Unboxed as VU
90import qualified Data.Vector.Mutable as VM ( unsafeNew, unsafeWrite )
95import qualified Data.Vector.Mutable as VM ( unsafeNew, unsafeWrite )
96+import qualified Prelude as P
92#if MIN_VERSION_time(1,5,0)
98#if MIN_VERSION_time(1,5,0)
93import Data.Time.Format (defaultTimeLocale, dateTimeFmt)
99import Data.Time.Format (defaultTimeLocale, dateTimeFmt)
@@ -99,6 +105,9 @@ instance (ToJSON a) => ToJSON (Identity a) where
99 toJSON (Identity a) = toJSON a
105 toJSON (Identity a) = toJSON a
100 {-# INLINE toJSON #-}
106 {-# INLINE toJSON #-}
108+ toEncoding (Identity a) = toEncoding a
109+ {-# INLINE toEncoding #-}
102instance (FromJSON a) => FromJSON (Identity a) where
111instance (FromJSON a) => FromJSON (Identity a) where
103 parseJSON a = Identity <$> parseJSON a
112 parseJSON a = Identity <$> parseJSON a
104 {-# INLINE parseJSON #-}
113 {-# INLINE parseJSON #-}
@@ -108,6 +117,9 @@ instance (ToJSON a) => ToJSON (Maybe a) where
108 toJSON Nothing = Null
117 toJSON Nothing = Null
109 {-# INLINE toJSON #-}
118 {-# INLINE toJSON #-}
120+ toEncoding (Just a) = toEncoding a
121+ toEncoding Nothing = E.null_
111instance (FromJSON a) => FromJSON (Maybe a) where
123instance (FromJSON a) => FromJSON (Maybe a) where
112 parseJSON Null = pure Nothing
124 parseJSON Null = pure Nothing
113 parseJSON a = Just <$> parseJSON a
125 parseJSON a = Just <$> parseJSON a
@@ -118,6 +130,11 @@ instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
118 toJSON (Right b) = object [right .= b]
130 toJSON (Right b) = object [right .= b]
119 {-# INLINE toJSON #-}
131 {-# INLINE toJSON #-}
133+ toEncoding (Left a) =
134+ B.shortByteString "{\"left\":" <> toEncoding a <> B.char7 '}'
135+ toEncoding (Right a) =
136+ B.shortByteString "{\"right\":" <> toEncoding a <> B.char7 '}'
121instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
138instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
122 parseJSON (Object (H.toList -> [(key, value)]))
139 parseJSON (Object (H.toList -> [(key, value)]))
123 | key == left = Left <$> parseJSON value
140 | key == left = Left <$> parseJSON value
@@ -136,6 +153,8 @@ instance ToJSON Bool where
136 toJSON = Bool
153 toJSON = Bool
137 {-# INLINE toJSON #-}
154 {-# INLINE toJSON #-}
156+ toEncoding v = E.bool v
139instance FromJSON Bool where
158instance FromJSON Bool where
140 parseJSON = withBool "Bool" pure
159 parseJSON = withBool "Bool" pure
141 {-# INLINE parseJSON #-}
160 {-# INLINE parseJSON #-}
@@ -144,6 +163,8 @@ instance ToJSON () where
144 toJSON _ = emptyArray
163 toJSON _ = emptyArray
145 {-# INLINE toJSON #-}
164 {-# INLINE toJSON #-}
166+ toEncoding _ = E.emptyArray_
147instance FromJSON () where
168instance FromJSON () where
148 parseJSON = withArray "()" $ \v ->
169 parseJSON = withArray "()" $ \v ->
149 if V.null v
170 if V.null v
@@ -155,6 +176,8 @@ instance ToJSON [Char] where
155 toJSON = String . T.pack
176 toJSON = String . T.pack
156 {-# INLINE toJSON #-}
177 {-# INLINE toJSON #-}
179+ toEncoding = E.text . T.pack
158instance FromJSON [Char] where
181instance FromJSON [Char] where
159 parseJSON = withText "String" $ pure . T.unpack
182 parseJSON = withText "String" $ pure . T.unpack
160 {-# INLINE parseJSON #-}
183 {-# INLINE parseJSON #-}
@@ -163,6 +186,8 @@ instance ToJSON Char where
163 toJSON = String . T.singleton
186 toJSON = String . T.singleton
164 {-# INLINE toJSON #-}
187 {-# INLINE toJSON #-}
189+ toEncoding = E.text . T.singleton
166instance FromJSON Char where
191instance FromJSON Char where
167 parseJSON = withText "Char" $ \t ->
192 parseJSON = withText "Char" $ \t ->
168 if T.compareLength t 1 == EQ
193 if T.compareLength t 1 == EQ
@@ -174,6 +199,9 @@ instance ToJSON Scientific where
174 toJSON = Number
199 toJSON = Number
175 {-# INLINE toJSON #-}
200 {-# INLINE toJSON #-}
202+ toEncoding v = E.number v
203+ {-# INLINE toEncoding #-}
177instance FromJSON Scientific where
205instance FromJSON Scientific where
178 parseJSON = withScientific "Scientific" pure
206 parseJSON = withScientific "Scientific" pure
179 {-# INLINE parseJSON #-}
207 {-# INLINE parseJSON #-}
@@ -182,6 +210,9 @@ instance ToJSON Double where
182 toJSON = realFloatToJSON
210 toJSON = realFloatToJSON
183 {-# INLINE toJSON #-}
211 {-# INLINE toJSON #-}
213+ toEncoding = realFloatToEncoding
214+ {-# INLINE toEncoding #-}
185instance FromJSON Double where
216instance FromJSON Double where
186 parseJSON = parseRealFloat "Double"
217 parseJSON = parseRealFloat "Double"
187 {-# INLINE parseJSON #-}
218 {-# INLINE parseJSON #-}
@@ -191,6 +222,10 @@ instance ToJSON Number where
191 toJSON (I i) = toJSON i
222 toJSON (I i) = toJSON i
192 {-# INLINE toJSON #-}
223 {-# INLINE toJSON #-}
225+ toEncoding (D d) = toEncoding d
226+ toEncoding (I i) = toEncoding i
227+ {-# INLINE toEncoding #-}
194instance FromJSON Number where
229instance FromJSON Number where
195 parseJSON (Number s) = pure $ scientificToNumber s
230 parseJSON (Number s) = pure $ scientificToNumber s
196 parseJSON Null = pure (D (0/0))
231 parseJSON Null = pure (D (0/0))
@@ -201,6 +236,9 @@ instance ToJSON Float where
201 toJSON = realFloatToJSON
236 toJSON = realFloatToJSON
202 {-# INLINE toJSON #-}
237 {-# INLINE toJSON #-}
239+ toEncoding = realFloatToEncoding
240+ {-# INLINE toEncoding #-}
204instance FromJSON Float where
242instance FromJSON Float where
205 parseJSON = parseRealFloat "Float"
243 parseJSON = parseRealFloat "Float"
206 {-# INLINE parseJSON #-}
244 {-# INLINE parseJSON #-}
@@ -209,7 +247,11 @@ instance ToJSON (Ratio Integer) where
209 toJSON r = object [ "numerator" .= numerator r
247 toJSON r = object [ "numerator" .= numerator r
210 , "denominator" .= denominator r
248 , "denominator" .= denominator r
212− {-# INLINE toJSON #-}
250+
252+ B.shortByteString "{\"numerator\":" <> toEncoding (numerator r) <>
253+ B.shortByteString ",\"denominator\":" <> toEncoding (denominator r) <>
214instance FromJSON (Ratio Integer) where
256instance FromJSON (Ratio Integer) where
215 parseJSON = withObject "Rational" $ \obj ->
257 parseJSON = withObject "Rational" $ \obj ->
@@ -221,6 +263,8 @@ instance HasResolution a => ToJSON (Fixed a) where
221 toJSON = Number . realToFrac
263 toJSON = Number . realToFrac
222 {-# INLINE toJSON #-}
264 {-# INLINE toJSON #-}
266+ toEncoding = E.number . realToFrac
224-- | /WARNING:/ Only parse fixed-precision numbers from trusted input
268-- | /WARNING:/ Only parse fixed-precision numbers from trusted input
225-- since an attacker could easily fill up the memory of the target
269-- since an attacker could easily fill up the memory of the target
226-- system by specifying a scientific number with a big exponent like
270-- system by specifying a scientific number with a big exponent like
@@ -233,6 +277,9 @@ instance ToJSON Int where
233 toJSON = Number . fromIntegral
277 toJSON = Number . fromIntegral
234 {-# INLINE toJSON #-}
278 {-# INLINE toJSON #-}
280+ toEncoding = B.intDec
281+ {-# INLINE toEncoding #-}
236instance FromJSON Int where
283instance FromJSON Int where
237 parseJSON = parseIntegral "Int"
284 parseJSON = parseIntegral "Int"
238 {-# INLINE parseJSON #-}
285 {-# INLINE parseJSON #-}
@@ -241,6 +288,8 @@ instance ToJSON Integer where
241 toJSON = Number . fromInteger
288 toJSON = Number . fromInteger
242 {-# INLINE toJSON #-}
289 {-# INLINE toJSON #-}
291+ toEncoding = B.integerDec
244-- | /WARNING:/ Only parse Integers from trusted input since an
293-- | /WARNING:/ Only parse Integers from trusted input since an
245-- attacker could easily fill up the memory of the target system by
294-- attacker could easily fill up the memory of the target system by
246-- specifying a scientific number with a big exponent like
295-- specifying a scientific number with a big exponent like
@@ -253,6 +302,8 @@ instance ToJSON Int8 where
253 toJSON = Number . fromIntegral
302 toJSON = Number . fromIntegral
254 {-# INLINE toJSON #-}
303 {-# INLINE toJSON #-}
305+ toEncoding = B.int8Dec
256instance FromJSON Int8 where
307instance FromJSON Int8 where
257 parseJSON = parseIntegral "Int8"
308 parseJSON = parseIntegral "Int8"
258 {-# INLINE parseJSON #-}
309 {-# INLINE parseJSON #-}
@@ -261,6 +312,8 @@ instance ToJSON Int16 where
261 toJSON = Number . fromIntegral
312 toJSON = Number . fromIntegral
262 {-# INLINE toJSON #-}
313 {-# INLINE toJSON #-}
315+ toEncoding = B.int16Dec
264instance FromJSON Int16 where
317instance FromJSON Int16 where
265 parseJSON = parseIntegral "Int16"
318 parseJSON = parseIntegral "Int16"
266 {-# INLINE parseJSON #-}
319 {-# INLINE parseJSON #-}
@@ -269,6 +322,8 @@ instance ToJSON Int32 where
269 toJSON = Number . fromIntegral
322 toJSON = Number . fromIntegral
270 {-# INLINE toJSON #-}
323 {-# INLINE toJSON #-}
325+ toEncoding = B.int32Dec
272instance FromJSON Int32 where
327instance FromJSON Int32 where
273 parseJSON = parseIntegral "Int32"
328 parseJSON = parseIntegral "Int32"
274 {-# INLINE parseJSON #-}
329 {-# INLINE parseJSON #-}
@@ -277,6 +332,8 @@ instance ToJSON Int64 where
277 toJSON = Number . fromIntegral
332 toJSON = Number . fromIntegral
278 {-# INLINE toJSON #-}
333 {-# INLINE toJSON #-}
335+ toEncoding = B.int64Dec
280instance FromJSON Int64 where
337instance FromJSON Int64 where
281 parseJSON = parseIntegral "Int64"
338 parseJSON = parseIntegral "Int64"
282 {-# INLINE parseJSON #-}
339 {-# INLINE parseJSON #-}
@@ -285,6 +342,8 @@ instance ToJSON Word where
285 toJSON = Number . fromIntegral
342 toJSON = Number . fromIntegral
286 {-# INLINE toJSON #-}
343 {-# INLINE toJSON #-}
345+ toEncoding = B.wordDec
288instance FromJSON Word where
347instance FromJSON Word where
289 parseJSON = parseIntegral "Word"
348 parseJSON = parseIntegral "Word"
290 {-# INLINE parseJSON #-}
349 {-# INLINE parseJSON #-}
@@ -293,6 +352,8 @@ instance ToJSON Word8 where
293 toJSON = Number . fromIntegral
352 toJSON = Number . fromIntegral
294 {-# INLINE toJSON #-}
353 {-# INLINE toJSON #-}
355+ toEncoding = B.word8Dec
296instance FromJSON Word8 where
357instance FromJSON Word8 where
297 parseJSON = parseIntegral "Word8"
358 parseJSON = parseIntegral "Word8"
298 {-# INLINE parseJSON #-}
359 {-# INLINE parseJSON #-}
@@ -301,6 +362,8 @@ instance ToJSON Word16 where
301 toJSON = Number . fromIntegral
362 toJSON = Number . fromIntegral
302 {-# INLINE toJSON #-}
363 {-# INLINE toJSON #-}
365+ toEncoding = B.word16Dec
304instance FromJSON Word16 where
367instance FromJSON Word16 where
305 parseJSON = parseIntegral "Word16"
368 parseJSON = parseIntegral "Word16"
306 {-# INLINE parseJSON #-}
369 {-# INLINE parseJSON #-}
@@ -309,6 +372,8 @@ instance ToJSON Word32 where
309 toJSON = Number . fromIntegral
372 toJSON = Number . fromIntegral
310 {-# INLINE toJSON #-}
373 {-# INLINE toJSON #-}
375+ toEncoding = B.word32Dec
312instance FromJSON Word32 where
377instance FromJSON Word32 where
313 parseJSON = parseIntegral "Word32"
378 parseJSON = parseIntegral "Word32"
314 {-# INLINE parseJSON #-}
379 {-# INLINE parseJSON #-}
@@ -317,6 +382,8 @@ instance ToJSON Word64 where
317 toJSON = Number . fromIntegral
382 toJSON = Number . fromIntegral
318 {-# INLINE toJSON #-}
383 {-# INLINE toJSON #-}
385+ toEncoding = B.word64Dec
320instance FromJSON Word64 where
387instance FromJSON Word64 where
321 parseJSON = parseIntegral "Word64"
388 parseJSON = parseIntegral "Word64"
322 {-# INLINE parseJSON #-}
389 {-# INLINE parseJSON #-}
@@ -325,6 +392,8 @@ instance ToJSON Text where
325 toJSON = String
392 toJSON = String
326 {-# INLINE toJSON #-}
393 {-# INLINE toJSON #-}
328instance FromJSON Text where
397instance FromJSON Text where
329 parseJSON = withText "Text" pure
398 parseJSON = withText "Text" pure
330 {-# INLINE parseJSON #-}
399 {-# INLINE parseJSON #-}
@@ -333,6 +402,11 @@ instance ToJSON LT.Text where
333 toJSON = String . LT.toStrict
402 toJSON = String . LT.toStrict
334 {-# INLINE toJSON #-}
403 {-# INLINE toJSON #-}
407+ LT.foldrChunks (\x xs -> E.unquoted x <> xs) mempty t <>
336instance FromJSON LT.Text where
410instance FromJSON LT.Text where
337 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
411 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
338 {-# INLINE parseJSON #-}
412 {-# INLINE parseJSON #-}
@@ -341,14 +415,20 @@ instance (ToJSON a) => ToJSON [a] where
341 toJSON = Array . V.fromList . map toJSON
415 toJSON = Array . V.fromList . map toJSON
342 {-# INLINE toJSON #-}
416 {-# INLINE toJSON #-}
418+ toEncoding [] = E.emptyArray_
419+ toEncoding (x:xs) = B.char7 '[' <> toEncoding x <> commas xs <> B.char7 ']'
420+ where commas = P.foldr (\v vs -> B.char7 ',' <> toEncoding v <> vs) mempty
344instance (FromJSON a) => FromJSON [a] where
422instance (FromJSON a) => FromJSON [a] where
345 parseJSON = withArray "[a]" $ mapM parseJSON . V.toList
423 parseJSON = withArray "[a]" $ mapM parseJSON . V.toList
346 {-# INLINE parseJSON #-}
424 {-# INLINE parseJSON #-}
348−instance (ToJSON a) => ToJSON (Seq.Seq a) where
426+instance (Foldable t, ToJSON a) => ToJSON (t a) where
349 toJSON = toJSON . toList
427 toJSON = toJSON . toList
350 {-# INLINE toJSON #-}
428 {-# INLINE toJSON #-}
430+ toEncoding = E.foldable
352instance (FromJSON a) => FromJSON (Seq.Seq a) where
432instance (FromJSON a) => FromJSON (Seq.Seq a) where
353 parseJSON = withArray "Seq a" $ traverse parseJSON . Seq.fromList . V.toList
433 parseJSON = withArray "Seq a" $ traverse parseJSON . Seq.fromList . V.toList
354 {-# INLINE parseJSON #-}
434 {-# INLINE parseJSON #-}
@@ -357,6 +437,16 @@ instance (ToJSON a) => ToJSON (Vector a) where
357 toJSON = Array . V.map toJSON
437 toJSON = Array . V.map toJSON
358 {-# INLINE toJSON #-}
438 {-# INLINE toJSON #-}
440+ toEncoding = encodeVector
442+encodeVector :: (ToJSON a, VG.Vector v a) => v a -> B.Builder
444+ | VG.null xs = E.emptyArray_
445+ | otherwise = B.char7 '[' <> toEncoding (VG.unsafeHead xs) <>
446+ VG.foldr go mempty (VG.unsafeTail xs) <>
448+ where go v b = B.char7 ',' <> toEncoding v <> b
360instance (FromJSON a) => FromJSON (Vector a) where
450instance (FromJSON a) => FromJSON (Vector a) where
361 parseJSON = withArray "Vector a" $ V.mapM parseJSON
451 parseJSON = withArray "Vector a" $ V.mapM parseJSON
362 {-# INLINE parseJSON #-}
452 {-# INLINE parseJSON #-}
@@ -372,18 +462,24 @@ vectorParseJSON s = withArray s $ fmap V.convert . V.mapM parseJSON
372instance (Storable a, ToJSON a) => ToJSON (VS.Vector a) where
462instance (Storable a, ToJSON a) => ToJSON (VS.Vector a) where
373 toJSON = vectorToJSON
463 toJSON = vectorToJSON
465+ toEncoding = encodeVector
375instance (Storable a, FromJSON a) => FromJSON (VS.Vector a) where
467instance (Storable a, FromJSON a) => FromJSON (VS.Vector a) where
376 parseJSON = vectorParseJSON "Data.Vector.Storable.Vector a"
468 parseJSON = vectorParseJSON "Data.Vector.Storable.Vector a"
378instance (VP.Prim a, ToJSON a) => ToJSON (VP.Vector a) where
470instance (VP.Prim a, ToJSON a) => ToJSON (VP.Vector a) where
379 toJSON = vectorToJSON
471 toJSON = vectorToJSON
473+ toEncoding = encodeVector
381instance (VP.Prim a, FromJSON a) => FromJSON (VP.Vector a) where
475instance (VP.Prim a, FromJSON a) => FromJSON (VP.Vector a) where
382 parseJSON = vectorParseJSON "Data.Vector.Primitive.Vector a"
476 parseJSON = vectorParseJSON "Data.Vector.Primitive.Vector a"
384instance (VG.Vector VU.Vector a, ToJSON a) => ToJSON (VU.Vector a) where
478instance (VG.Vector VU.Vector a, ToJSON a) => ToJSON (VU.Vector a) where
385 toJSON = vectorToJSON
479 toJSON = vectorToJSON
481+ toEncoding = encodeVector
387instance (VG.Vector VU.Vector a, FromJSON a) => FromJSON (VU.Vector a) where
483instance (VG.Vector VU.Vector a, FromJSON a) => FromJSON (VU.Vector a) where
388 parseJSON = vectorParseJSON "Data.Vector.Unboxed.Vector a"
484 parseJSON = vectorParseJSON "Data.Vector.Unboxed.Vector a"
@@ -391,6 +487,8 @@ instance (ToJSON a) => ToJSON (Set.Set a) where
391 toJSON = toJSON . Set.toList
487 toJSON = toJSON . Set.toList
392 {-# INLINE toJSON #-}
488 {-# INLINE toJSON #-}
490+ toEncoding = encodeSet Set.minView Set.foldr
394instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
492instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
395 parseJSON = fmap Set.fromList . parseJSON
493 parseJSON = fmap Set.fromList . parseJSON
396 {-# INLINE parseJSON #-}
494 {-# INLINE parseJSON #-}
@@ -399,6 +497,8 @@ instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
399 toJSON = toJSON . HashSet.toList
497 toJSON = toJSON . HashSet.toList
400 {-# INLINE toJSON #-}
498 {-# INLINE toJSON #-}
500+ toEncoding = E.foldable
402instance (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet.HashSet a) where
502instance (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet.HashSet a) where
403 parseJSON = fmap HashSet.fromList . parseJSON
503 parseJSON = fmap HashSet.fromList . parseJSON
404 {-# INLINE parseJSON #-}
504 {-# INLINE parseJSON #-}
@@ -407,6 +507,20 @@ instance ToJSON IntSet.IntSet where
407 toJSON = toJSON . IntSet.toList
507 toJSON = toJSON . IntSet.toList
408 {-# INLINE toJSON #-}
508 {-# INLINE toJSON #-}
510+ toEncoding = encodeSet IntSet.minView IntSet.foldr
512+encodeSet :: (ToJSON a) =>
514+ -> ((a -> B.Builder -> B.Builder) -> B.Builder -> s -> B.Builder)
516+encodeSet minView foldr xs =
518+ Nothing -> E.emptyArray_
519+ Just (m,ys) -> B.char7 '[' <> toEncoding m <>
520+ foldr go mempty ys <>
522+ where go v b = B.char7 ',' <> toEncoding v <> b
410instance FromJSON IntSet.IntSet where
524instance FromJSON IntSet.IntSet where
411 parseJSON = fmap IntSet.fromList . parseJSON
525 parseJSON = fmap IntSet.fromList . parseJSON
412 {-# INLINE parseJSON #-}
526 {-# INLINE parseJSON #-}
@@ -415,6 +529,8 @@ instance ToJSON a => ToJSON (IntMap.IntMap a) where
415 toJSON = toJSON . IntMap.toList
529 toJSON = toJSON . IntMap.toList
416 {-# INLINE toJSON #-}
530 {-# INLINE toJSON #-}
532+ toEncoding = toEncoding . IntMap.toList
418instance FromJSON a => FromJSON (IntMap.IntMap a) where
534instance FromJSON a => FromJSON (IntMap.IntMap a) where
419 parseJSON = fmap IntMap.fromList . parseJSON
535 parseJSON = fmap IntMap.fromList . parseJSON
420 {-# INLINE parseJSON #-}
536 {-# INLINE parseJSON #-}
@@ -423,6 +539,28 @@ instance (ToJSON v) => ToJSON (M.Map Text v) where
423 toJSON = Object . M.foldrWithKey (\k -> H.insert k . toJSON) H.empty
539 toJSON = Object . M.foldrWithKey (\k -> H.insert k . toJSON) H.empty
424 {-# INLINE toJSON #-}
540 {-# INLINE toJSON #-}
542+ toEncoding = encodeMap M.minViewWithKey M.foldrWithKey
544+encodeMap :: (ToJSON k, ToJSON v) =>
545+ (m -> Maybe ((k,v), m))
546+ -> ((k -> v -> B.Builder -> B.Builder) -> B.Builder -> m -> B.Builder)
548+encodeMap minViewWithKey foldrWithKey xs =
549+ case minViewWithKey xs of
550+ Nothing -> BP.primBounded (E.ascii2 ('{','}')) ()
551+ Just ((k,v),ys) -> B.char7 '{' <> encodePair k v <>
552+ foldrWithKey go mempty ys <> B.char7 '}'
553+ where go k v b = B.char7 ',' <> encodePair k v <> b
555+encodeWithKey :: (ToJSON k, ToJSON v) =>
556+ ((k -> v -> Series -> Series) -> Series -> m -> Series)
558+encodeWithKey foldrWithKey xs = E.series '{' '}' . foldrWithKey go mempty $ xs
559+ where go k v c = Value (encodePair k v) <> c
561+encodePair :: (ToJSON k, ToJSON v) => k -> v -> B.Builder
562+encodePair k v = toEncoding k <> B.char7 ':' <> toEncoding v
426instance (FromJSON v) => FromJSON (M.Map Text v) where
564instance (FromJSON v) => FromJSON (M.Map Text v) where
427 parseJSON = withObject "Map Text a" $
565 parseJSON = withObject "Map Text a" $
428 fmap (H.foldrWithKey M.insert M.empty) . traverse parseJSON
566 fmap (H.foldrWithKey M.insert M.empty) . traverse parseJSON
@@ -430,12 +568,16 @@ instance (FromJSON v) => FromJSON (M.Map Text v) where
430instance (ToJSON v) => ToJSON (M.Map LT.Text v) where
568instance (ToJSON v) => ToJSON (M.Map LT.Text v) where
431 toJSON = Object . mapHashKeyVal LT.toStrict toJSON
569 toJSON = Object . mapHashKeyVal LT.toStrict toJSON
571+ toEncoding = encodeMap M.minViewWithKey M.foldrWithKey
433instance (FromJSON v) => FromJSON (M.Map LT.Text v) where
573instance (FromJSON v) => FromJSON (M.Map LT.Text v) where
434 parseJSON = fmap (hashMapKey LT.fromStrict) . parseJSON
574 parseJSON = fmap (hashMapKey LT.fromStrict) . parseJSON
436instance (ToJSON v) => ToJSON (M.Map String v) where
576instance (ToJSON v) => ToJSON (M.Map String v) where
437 toJSON = Object . mapHashKeyVal pack toJSON
577 toJSON = Object . mapHashKeyVal pack toJSON
579+ toEncoding = encodeMap M.minViewWithKey M.foldrWithKey
439instance (FromJSON v) => FromJSON (M.Map String v) where
581instance (FromJSON v) => FromJSON (M.Map String v) where
440 parseJSON = fmap (hashMapKey unpack) . parseJSON
582 parseJSON = fmap (hashMapKey unpack) . parseJSON
@@ -443,24 +585,32 @@ instance (ToJSON v) => ToJSON (H.HashMap Text v) where
443 toJSON = Object . H.map toJSON
585 toJSON = Object . H.map toJSON
444 {-# INLINE toJSON #-}
586 {-# INLINE toJSON #-}
588+ toEncoding = encodeWithKey H.foldrWithKey
446instance (FromJSON v) => FromJSON (H.HashMap Text v) where
590instance (FromJSON v) => FromJSON (H.HashMap Text v) where
447 parseJSON = withObject "HashMap Text a" $ traverse parseJSON
591 parseJSON = withObject "HashMap Text a" $ traverse parseJSON
449instance (ToJSON v) => ToJSON (H.HashMap LT.Text v) where
593instance (ToJSON v) => ToJSON (H.HashMap LT.Text v) where
450 toJSON = Object . mapKeyVal LT.toStrict toJSON
594 toJSON = Object . mapKeyVal LT.toStrict toJSON
596+ toEncoding = encodeWithKey H.foldrWithKey
452instance (FromJSON v) => FromJSON (H.HashMap LT.Text v) where
598instance (FromJSON v) => FromJSON (H.HashMap LT.Text v) where
453 parseJSON = fmap (mapKey LT.fromStrict) . parseJSON
599 parseJSON = fmap (mapKey LT.fromStrict) . parseJSON
455instance (ToJSON v) => ToJSON (H.HashMap String v) where
601instance (ToJSON v) => ToJSON (H.HashMap String v) where
456 toJSON = Object . mapKeyVal pack toJSON
602 toJSON = Object . mapKeyVal pack toJSON
604+ toEncoding = encodeWithKey H.foldrWithKey
458instance (FromJSON v) => FromJSON (H.HashMap String v) where
606instance (FromJSON v) => FromJSON (H.HashMap String v) where
459 parseJSON = fmap (mapKey unpack) . parseJSON
607 parseJSON = fmap (mapKey unpack) . parseJSON
461instance (ToJSON v) => ToJSON (Tree.Tree v) where
609instance (ToJSON v) => ToJSON (Tree.Tree v) where
462 toJSON (Tree.Node root branches) = toJSON (root,branches)
610 toJSON (Tree.Node root branches) = toJSON (root,branches)
612+ toEncoding (Tree.Node root branches) = toEncoding (root,branches)
464instance (FromJSON v) => FromJSON (Tree.Tree v) where
614instance (FromJSON v) => FromJSON (Tree.Tree v) where
465 parseJSON j = uncurry Tree.Node <$> parseJSON j
615 parseJSON j = uncurry Tree.Node <$> parseJSON j
@@ -468,16 +618,22 @@ instance ToJSON Value where
468 toJSON a = a
618 toJSON a = a
469 {-# INLINE toJSON #-}
619 {-# INLINE toJSON #-}
621+ toEncoding = E.encodeToBuilder
471instance FromJSON Value where
623instance FromJSON Value where
472 parseJSON a = pure a
624 parseJSON a = pure a
473 {-# INLINE parseJSON #-}
625 {-# INLINE parseJSON #-}
475instance ToJSON DotNetTime where
627instance ToJSON DotNetTime where
476− toJSON (DotNetTime t) =
628+ toJSON = toJSON . dotNetTime
477− String (pack (secs ++ formatMillis t ++ ")/"))
478− where secs = formatTime defaultTimeLocale "/Date(%s" t
479 {-# INLINE toJSON #-}
629 {-# INLINE toJSON #-}
631+ toEncoding = toEncoding . dotNetTime
633+dotNetTime :: DotNetTime -> String
634+dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
635+ where secs = formatTime defaultTimeLocale "/Date(%s" t
481instance FromJSON DotNetTime where
637instance FromJSON DotNetTime where
482 parseJSON = withText "DotNetTime" $ \t ->
638 parseJSON = withText "DotNetTime" $ \t ->
483 let (s,m) = T.splitAt (T.length t - 5) t
639 let (s,m) = T.splitAt (T.length t - 5) t
@@ -488,12 +644,16 @@ instance FromJSON DotNetTime where
488 {-# INLINE parseJSON #-}
644 {-# INLINE parseJSON #-}
490instance ToJSON ZonedTime where
646instance ToJSON ZonedTime where
647+ toJSON = toJSON . zonedTime
649+ toEncoding = toEncoding . zonedTime
651+zonedTime :: ZonedTime -> String
491− toJSON t = String $ pack $ formatTime defaultTimeLocale format t
652+zonedTime t = formatTime defaultTimeLocale format t
493− format = "%FT%T." ++ formatMillis t ++ tzFormat
654+ format = "%FT%T." ++ formatMillis t ++ tzFormat
495− | 0 == timeZoneMinutes (zonedTimeZone t) = "Z"
655+ tzFormat | timeZoneMinutes (zonedTimeZone t) == 0 = "Z"
496− | otherwise = "%z"
656+ | otherwise = "%z"
498formatMillis :: (FormatTime t) => t -> String
658formatMillis :: (FormatTime t) => t -> String
499formatMillis = take 3 . formatSubseconds
659formatMillis = take 3 . formatSubseconds
@@ -535,11 +695,15 @@ instance FromJSON ZonedTime where
535 parseJSON v = typeMismatch "ZonedTime" v
695 parseJSON v = typeMismatch "ZonedTime" v
537instance ToJSON UTCTime where
697instance ToJSON UTCTime where
538− toJSON t = String $ pack $ formatTime defaultTimeLocale format t
698+ toJSON = toJSON . utcTime
540− format = "%FT%T." ++ formatSubseconds t ++ "Z"
541 {-# INLINE toJSON #-}
699 {-# INLINE toJSON #-}
701+ toEncoding = toEncoding . utcTime
703+utcTime :: UTCTime -> String
704+utcTime t = formatTime defaultTimeLocale format t
705+ where format = "%FT%T." ++ formatSubseconds t ++ "Z"
543instance FromJSON UTCTime where
707instance FromJSON UTCTime where
544 parseJSON = withText "UTCTime" $ \t ->
708 parseJSON = withText "UTCTime" $ \t ->
545 case parseTime defaultTimeLocale "%FT%T%QZ" (unpack t) of
709 case parseTime defaultTimeLocale "%FT%T%QZ" (unpack t) of
@@ -555,6 +719,10 @@ instance (ToJSON a, ToJSON b) => ToJSON (a,b) where
555 return mv
719 return mv
556 {-# INLINE toJSON #-}
720 {-# INLINE toJSON #-}
722+ toEncoding (a,b) = B.char7 '[' <>
723+ toEncoding a <> B.char7 ',' <>
724+ toEncoding b <> B.char7 ']'
558instance (FromJSON a, FromJSON b) => FromJSON (a,b) where
726instance (FromJSON a, FromJSON b) => FromJSON (a,b) where
559 parseJSON = withArray "(a,b)" $ \ab ->
727 parseJSON = withArray "(a,b)" $ \ab ->
560 let n = V.length ab
728 let n = V.length ab
@@ -574,6 +742,11 @@ instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON (a,b,c) where
574 return mv
742 return mv
575 {-# INLINE toJSON #-}
743 {-# INLINE toJSON #-}
745+ toEncoding (a,b,c) = B.char7 '[' <>
746+ toEncoding a <> B.char7 ',' <>
747+ toEncoding b <> B.char7 ',' <>
748+ toEncoding c <> B.char7 ']'
577instance (FromJSON a, FromJSON b, FromJSON c) => FromJSON (a,b,c) where
750instance (FromJSON a, FromJSON b, FromJSON c) => FromJSON (a,b,c) where
578 parseJSON = withArray "(a,b,c)" $ \abc ->
751 parseJSON = withArray "(a,b,c)" $ \abc ->
579 let n = V.length abc
752 let n = V.length abc
@@ -595,6 +768,12 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a,b,c,d) where
595 return mv
768 return mv
596 {-# INLINE toJSON #-}
769 {-# INLINE toJSON #-}
771+ toEncoding (a,b,c,d) = B.char7 '[' <>
772+ toEncoding a <> B.char7 ',' <>
773+ toEncoding b <> B.char7 ',' <>
774+ toEncoding c <> B.char7 ',' <>
775+ toEncoding d <> B.char7 ']'
598instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d) =>
777instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d) =>
599 FromJSON (a,b,c,d) where
778 FromJSON (a,b,c,d) where
600 parseJSON = withArray "(a,b,c,d)" $ \abcd ->
779 parseJSON = withArray "(a,b,c,d)" $ \abcd ->
@@ -620,6 +799,13 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) =>
620 return mv
799 return mv
621 {-# INLINE toJSON #-}
800 {-# INLINE toJSON #-}
802+ toEncoding (a,b,c,d,e) = B.char7 '[' <>
803+ toEncoding a <> B.char7 ',' <>
804+ toEncoding b <> B.char7 ',' <>
805+ toEncoding c <> B.char7 ',' <>
806+ toEncoding d <> B.char7 ',' <>
807+ toEncoding e <> B.char7 ']'
623instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) =>
809instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) =>
624 FromJSON (a,b,c,d,e) where
810 FromJSON (a,b,c,d,e) where
625 parseJSON = withArray "(a,b,c,d,e)" $ \abcde ->
811 parseJSON = withArray "(a,b,c,d,e)" $ \abcde ->
@@ -647,6 +833,14 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) =>
647 return mv
833 return mv
648 {-# INLINE toJSON #-}
834 {-# INLINE toJSON #-}
836+ toEncoding (a,b,c,d,e,f) = B.char7 '[' <>
837+ toEncoding a <> B.char7 ',' <>
838+ toEncoding b <> B.char7 ',' <>
839+ toEncoding c <> B.char7 ',' <>
840+ toEncoding d <> B.char7 ',' <>
841+ toEncoding e <> B.char7 ',' <>
842+ toEncoding f <> B.char7 ']'
650instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
844instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
651 FromJSON f) => FromJSON (a,b,c,d,e,f) where
845 FromJSON f) => FromJSON (a,b,c,d,e,f) where
652 parseJSON = withArray "(a,b,c,d,e,f)" $ \abcdef ->
846 parseJSON = withArray "(a,b,c,d,e,f)" $ \abcdef ->
@@ -676,6 +870,15 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
676 return mv
870 return mv
677 {-# INLINE toJSON #-}
871 {-# INLINE toJSON #-}
873+ toEncoding (a,b,c,d,e,f,g) = B.char7 '[' <>
874+ toEncoding a <> B.char7 ',' <>
875+ toEncoding b <> B.char7 ',' <>
876+ toEncoding c <> B.char7 ',' <>
877+ toEncoding d <> B.char7 ',' <>
878+ toEncoding e <> B.char7 ',' <>
879+ toEncoding f <> B.char7 ',' <>
880+ toEncoding g <> B.char7 ']'
679instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
882instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
680 FromJSON f, FromJSON g) => FromJSON (a,b,c,d,e,f,g) where
883 FromJSON f, FromJSON g) => FromJSON (a,b,c,d,e,f,g) where
681 parseJSON = withArray "(a,b,c,d,e,f,g)" $ \abcdefg ->
884 parseJSON = withArray "(a,b,c,d,e,f,g)" $ \abcdefg ->
@@ -707,6 +910,16 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
707 return mv
910 return mv
708 {-# INLINE toJSON #-}
911 {-# INLINE toJSON #-}
913+ toEncoding (a,b,c,d,e,f,g,h) = B.char7 '[' <>
914+ toEncoding a <> B.char7 ',' <>
915+ toEncoding b <> B.char7 ',' <>
916+ toEncoding c <> B.char7 ',' <>
917+ toEncoding d <> B.char7 ',' <>
918+ toEncoding e <> B.char7 ',' <>
919+ toEncoding f <> B.char7 ',' <>
920+ toEncoding g <> B.char7 ',' <>
921+ toEncoding h <> B.char7 ']'
710instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
923instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
711 FromJSON f, FromJSON g, FromJSON h) =>
924 FromJSON f, FromJSON g, FromJSON h) =>
712 FromJSON (a,b,c,d,e,f,g,h) where
925 FromJSON (a,b,c,d,e,f,g,h) where
@@ -742,6 +955,17 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
742 return mv
955 return mv
743 {-# INLINE toJSON #-}
956 {-# INLINE toJSON #-}
958+ toEncoding (a,b,c,d,e,f,g,h,i) = B.char7 '[' <>
959+ toEncoding a <> B.char7 ',' <>
960+ toEncoding b <> B.char7 ',' <>
961+ toEncoding c <> B.char7 ',' <>
962+ toEncoding d <> B.char7 ',' <>
963+ toEncoding e <> B.char7 ',' <>
964+ toEncoding f <> B.char7 ',' <>
965+ toEncoding g <> B.char7 ',' <>
966+ toEncoding h <> B.char7 ',' <>
967+ toEncoding i <> B.char7 ']'
745instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
969instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
746 FromJSON f, FromJSON g, FromJSON h, FromJSON i) =>
970 FromJSON f, FromJSON g, FromJSON h, FromJSON i) =>
747 FromJSON (a,b,c,d,e,f,g,h,i) where
971 FromJSON (a,b,c,d,e,f,g,h,i) where
@@ -780,6 +1004,18 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
780 return mv
1004 return mv
781 {-# INLINE toJSON #-}
1005 {-# INLINE toJSON #-}
1007+ toEncoding (a,b,c,d,e,f,g,h,i,j) = B.char7 '[' <>
1008+ toEncoding a <> B.char7 ',' <>
1009+ toEncoding b <> B.char7 ',' <>
1010+ toEncoding c <> B.char7 ',' <>
1011+ toEncoding d <> B.char7 ',' <>
1012+ toEncoding e <> B.char7 ',' <>
1013+ toEncoding f <> B.char7 ',' <>
1014+ toEncoding g <> B.char7 ',' <>
1015+ toEncoding h <> B.char7 ',' <>
1016+ toEncoding i <> B.char7 ',' <>
1017+ toEncoding j <> B.char7 ']'
783instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1019instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
784 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) =>
1020 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) =>
785 FromJSON (a,b,c,d,e,f,g,h,i,j) where
1021 FromJSON (a,b,c,d,e,f,g,h,i,j) where
@@ -820,6 +1056,19 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
820 return mv
1056 return mv
821 {-# INLINE toJSON #-}
1057 {-# INLINE toJSON #-}
1059+ toEncoding (a,b,c,d,e,f,g,h,i,j,k) = B.char7 '[' <>
1060+ toEncoding a <> B.char7 ',' <>
1061+ toEncoding b <> B.char7 ',' <>
1062+ toEncoding c <> B.char7 ',' <>
1063+ toEncoding d <> B.char7 ',' <>
1064+ toEncoding e <> B.char7 ',' <>
1065+ toEncoding f <> B.char7 ',' <>
1066+ toEncoding g <> B.char7 ',' <>
1067+ toEncoding h <> B.char7 ',' <>
1068+ toEncoding i <> B.char7 ',' <>
1069+ toEncoding j <> B.char7 ',' <>
1070+ toEncoding k <> B.char7 ']'
823instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1072instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
824 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1073 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
825 FromJSON k) =>
1074 FromJSON k) =>
@@ -863,6 +1112,20 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
863 return mv
1112 return mv
864 {-# INLINE toJSON #-}
1113 {-# INLINE toJSON #-}
1115+ toEncoding (a,b,c,d,e,f,g,h,i,j,k,l) = B.char7 '[' <>
1116+ toEncoding a <> B.char7 ',' <>
1117+ toEncoding b <> B.char7 ',' <>
1118+ toEncoding c <> B.char7 ',' <>
1119+ toEncoding d <> B.char7 ',' <>
1120+ toEncoding e <> B.char7 ',' <>
1121+ toEncoding f <> B.char7 ',' <>
1122+ toEncoding g <> B.char7 ',' <>
1123+ toEncoding h <> B.char7 ',' <>
1124+ toEncoding i <> B.char7 ',' <>
1125+ toEncoding j <> B.char7 ',' <>
1126+ toEncoding k <> B.char7 ',' <>
1127+ toEncoding l <> B.char7 ']'
866instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1129instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
867 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1130 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
868 FromJSON k, FromJSON l) =>
1131 FromJSON k, FromJSON l) =>
@@ -909,6 +1172,21 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
909 return mv
1172 return mv
910 {-# INLINE toJSON #-}
1173 {-# INLINE toJSON #-}
1175+ toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m) = B.char7 '[' <>
1176+ toEncoding a <> B.char7 ',' <>
1177+ toEncoding b <> B.char7 ',' <>
1178+ toEncoding c <> B.char7 ',' <>
1179+ toEncoding d <> B.char7 ',' <>
1180+ toEncoding e <> B.char7 ',' <>
1181+ toEncoding f <> B.char7 ',' <>
1182+ toEncoding g <> B.char7 ',' <>
1183+ toEncoding h <> B.char7 ',' <>
1184+ toEncoding i <> B.char7 ',' <>
1185+ toEncoding j <> B.char7 ',' <>
1186+ toEncoding k <> B.char7 ',' <>
1187+ toEncoding l <> B.char7 ',' <>
1188+ toEncoding m <> B.char7 ']'
912instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1190instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
913 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1191 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
914 FromJSON k, FromJSON l, FromJSON m) =>
1192 FromJSON k, FromJSON l, FromJSON m) =>
@@ -957,6 +1235,22 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
957 return mv
1235 return mv
958 {-# INLINE toJSON #-}
1236 {-# INLINE toJSON #-}
1238+ toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m,n) = B.char7 '[' <>
1239+ toEncoding a <> B.char7 ',' <>
1240+ toEncoding b <> B.char7 ',' <>
1241+ toEncoding c <> B.char7 ',' <>
1242+ toEncoding d <> B.char7 ',' <>
1243+ toEncoding e <> B.char7 ',' <>
1244+ toEncoding f <> B.char7 ',' <>
1245+ toEncoding g <> B.char7 ',' <>
1246+ toEncoding h <> B.char7 ',' <>
1247+ toEncoding i <> B.char7 ',' <>
1248+ toEncoding j <> B.char7 ',' <>
1249+ toEncoding k <> B.char7 ',' <>
1250+ toEncoding l <> B.char7 ',' <>
1251+ toEncoding m <> B.char7 ',' <>
1252+ toEncoding n <> B.char7 ']'
960instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1254instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
961 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1255 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
962 FromJSON k, FromJSON l, FromJSON m, FromJSON n) =>
1256 FromJSON k, FromJSON l, FromJSON m, FromJSON n) =>
@@ -1007,6 +1301,23 @@ instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1007 return mv
1301 return mv
1008 {-# INLINE toJSON #-}
1302 {-# INLINE toJSON #-}
1304+ toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) = B.char7 '[' <>
1305+ toEncoding a <> B.char7 ',' <>
1306+ toEncoding b <> B.char7 ',' <>
1307+ toEncoding c <> B.char7 ',' <>
1308+ toEncoding d <> B.char7 ',' <>
1309+ toEncoding e <> B.char7 ',' <>
1310+ toEncoding f <> B.char7 ',' <>
1311+ toEncoding g <> B.char7 ',' <>
1312+ toEncoding h <> B.char7 ',' <>
1313+ toEncoding i <> B.char7 ',' <>
1314+ toEncoding j <> B.char7 ',' <>
1315+ toEncoding k <> B.char7 ',' <>
1316+ toEncoding l <> B.char7 ',' <>
1317+ toEncoding m <> B.char7 ',' <>
1318+ toEncoding n <> B.char7 ',' <>
1319+ toEncoding o <> B.char7 ']'
1010instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1321instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1011 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1322 FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1012 FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) =>
1323 FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) =>
@@ -1038,6 +1349,8 @@ instance ToJSON a => ToJSON (Dual a) where
1038 toJSON = toJSON . getDual
1349 toJSON = toJSON . getDual
1039 {-# INLINE toJSON #-}
1350 {-# INLINE toJSON #-}
1352+ toEncoding = toEncoding . getDual
1041instance FromJSON a => FromJSON (Dual a) where
1354instance FromJSON a => FromJSON (Dual a) where
1042 parseJSON = fmap Dual . parseJSON
1355 parseJSON = fmap Dual . parseJSON
1043 {-# INLINE parseJSON #-}
1356 {-# INLINE parseJSON #-}
@@ -1046,6 +1359,8 @@ instance ToJSON a => ToJSON (First a) where
1046 toJSON = toJSON . getFirst
1359 toJSON = toJSON . getFirst
1047 {-# INLINE toJSON #-}
1360 {-# INLINE toJSON #-}
1362+ toEncoding = toEncoding . getFirst
1049instance FromJSON a => FromJSON (First a) where
1364instance FromJSON a => FromJSON (First a) where
1050 parseJSON = fmap First . parseJSON
1365 parseJSON = fmap First . parseJSON
1051 {-# INLINE parseJSON #-}
1366 {-# INLINE parseJSON #-}
@@ -1054,6 +1369,8 @@ instance ToJSON a => ToJSON (Last a) where
1054 toJSON = toJSON . getLast
1369 toJSON = toJSON . getLast
1055 {-# INLINE toJSON #-}
1370 {-# INLINE toJSON #-}
1372+ toEncoding = toEncoding . getLast
1057instance FromJSON a => FromJSON (Last a) where
1374instance FromJSON a => FromJSON (Last a) where
1058 parseJSON = fmap Last . parseJSON
1375 parseJSON = fmap Last . parseJSON
1059 {-# INLINE parseJSON #-}
1376 {-# INLINE parseJSON #-}
@@ -1176,6 +1493,12 @@ realFloatToJSON d
1176 | otherwise = Number $ Scientific.fromFloatDigits d
1493 | otherwise = Number $ Scientific.fromFloatDigits d
1177{-# INLINE realFloatToJSON #-}
1494{-# INLINE realFloatToJSON #-}
1496+realFloatToEncoding :: RealFloat a => a -> Encoding
1497+realFloatToEncoding d
1498+ | isNaN d || isInfinite d = E.null_
1499+ | otherwise = toEncoding (Scientific.fromFloatDigits d)
1500+{-# INLINE realFloatToEncoding #-}
1179scientificToNumber :: Scientific -> Number
1502scientificToNumber :: Scientific -> Number
1180scientificToNumber s
1503scientificToNumber s
1181 | e < 0 = D $ Scientific.toRealFloat s
1504 | e < 0 = D $ Scientific.toRealFloat s