/
Instances.hs
1662 lines (1410 loc) · 54.9 KB
/
Instances.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE CPP, BangPatterns, DeriveDataTypeable, FlexibleContexts,
FlexibleInstances, GeneralizedNewtypeDeriving,
OverloadedStrings, UndecidableInstances,
ViewPatterns #-}
{-# LANGUAGE DefaultSignatures #-}
#include "overlapping-compat.h"
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- TODO: Drop this when we remove support for Data.Attoparsec.Number
{-# OPTIONS_GHC -fno-warn-deprecations #-}
-- |
-- Module: Data.Aeson.Types.Instances
-- Copyright: (c) 2011-2015 Bryan O'Sullivan
-- (c) 2011 MailRank, Inc.
-- License: Apache
-- Maintainer: Bryan O'Sullivan <bos@serpentine.com>
-- Stability: experimental
-- Portability: portable
--
-- Types for working with JSON data.
module Data.Aeson.Types.Instances
(
-- * Type classes
-- ** Core JSON classes
FromJSON(..)
, ToJSON(..)
, KeyValue(..)
-- ** Generic JSON classes
, GFromJSON(..)
, GToJSON(..)
, genericToJSON
, genericToEncoding
, genericParseJSON
-- * Types
, DotNetTime(..)
-- * Inspecting @'Value's@
, withObject
, withText
, withArray
, withNumber
, withScientific
, withBool
-- * Functions
, fromJSON
, ifromJSON
, (.:)
, (.:?)
, (.!=)
, tuple
, (>*<)
, typeMismatch
) where
import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable, list)
import Data.Aeson.Functions (hashMapKey, mapHashKeyVal, mapKey, mapKeyVal)
import Data.Aeson.Types.Class
import Data.Aeson.Types.Internal
import Data.Attoparsec.Number (Number(..))
import Data.Fixed (Fixed, HasResolution)
import Data.Foldable (toList)
import Data.Functor.Identity (Identity(..))
import Data.Hashable (Hashable(..))
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Monoid (Dual(..), First(..), Last(..))
import Data.Ratio (Ratio, (%), numerator, denominator)
import Data.Scientific (Scientific)
import Data.Text (Text, pack, unpack)
import Data.Time (Day, LocalTime, NominalDiffTime, UTCTime, ZonedTime)
import Data.Time.Format (FormatTime, formatTime, parseTime)
import Data.Traversable as Tr (sequence)
import Data.Vector (Vector)
import Data.Word (Word8, Word16, Word32, Word64)
import Data.Version (Version, showVersion, parseVersion)
import Text.ParserCombinators.ReadP (readP_to_S)
import Foreign.Storable (Storable)
import Prelude hiding (foldr)
import qualified Data.Aeson.Encode.Builder as E
import qualified Data.Aeson.Parser.Time as Time
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Lazy as L
import qualified Data.HashMap.Strict as H
import qualified Data.HashSet as HashSet
import qualified Data.IntMap as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Map as M
import qualified Data.Scientific as Scientific
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as LT
import qualified Data.Tree as Tree
import qualified Data.Vector as V
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Mutable as VM (unsafeNew, unsafeWrite)
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Unboxed as VU
#if MIN_VERSION_base(4,8,0)
import Numeric.Natural
#else
import Control.Applicative ((<$>), (<*>), pure)
import Data.Foldable (Foldable)
import Data.Monoid (mempty)
import Data.Traversable as Tr (traverse)
import Data.Word (Word)
#endif
#if MIN_VERSION_time(1,5,0)
import Data.Time.Format (defaultTimeLocale)
#else
import System.Locale (defaultTimeLocale)
#endif
parseIndexedJSON :: FromJSON a => Int -> Value -> Parser a
parseIndexedJSON idx value = parseJSON value <?> Index idx
instance (ToJSON a) => ToJSON (Identity a) where
toJSON (Identity a) = toJSON a
{-# INLINE toJSON #-}
toEncoding (Identity a) = toEncoding a
{-# INLINE toEncoding #-}
instance (FromJSON a) => FromJSON (Identity a) where
parseJSON a = Identity <$> parseJSON a
{-# INLINE parseJSON #-}
instance (ToJSON a) => ToJSON (Maybe a) where
toJSON (Just a) = toJSON a
toJSON Nothing = Null
{-# INLINE toJSON #-}
toEncoding (Just a) = toEncoding a
toEncoding Nothing = Encoding E.null_
{-# INLINE toEncoding #-}
instance (FromJSON a) => FromJSON (Maybe a) where
parseJSON Null = pure Nothing
parseJSON a = Just <$> parseJSON a
{-# INLINE parseJSON #-}
instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
toJSON (Left a) = object [left .= a]
toJSON (Right b) = object [right .= b]
{-# INLINE toJSON #-}
toEncoding (Left a) = Encoding $
B.shortByteString "{\"Left\":" <> builder a <> B.char7 '}'
toEncoding (Right a) = Encoding $
B.shortByteString "{\"Right\":" <> builder a <> B.char7 '}'
{-# INLINE toEncoding #-}
instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
parseJSON (Object (H.toList -> [(key, value)]))
| key == left = Left <$> parseJSON value
| key == right = Right <$> parseJSON value
parseJSON _ = fail $
"expected an object with a single property " ++
"where the property key should be either " ++
"\"Left\" or \"Right\""
{-# INLINE parseJSON #-}
left, right :: Text
left = "Left"
right = "Right"
instance ToJSON Bool where
toJSON = Bool
{-# INLINE toJSON #-}
toEncoding = Encoding . E.bool
{-# INLINE toEncoding #-}
instance FromJSON Bool where
parseJSON = withBool "Bool" pure
{-# INLINE parseJSON #-}
instance ToJSON Ordering where
toJSON = toJSON . orderingToText
toEncoding = toEncoding . orderingToText
orderingToText :: Ordering -> T.Text
orderingToText o = case o of
LT -> "LT"
EQ -> "EQ"
GT -> "GT"
instance FromJSON Ordering where
parseJSON = withText "Ordering" $ \s ->
case s of
"LT" -> return LT
"EQ" -> return EQ
"GT" -> return GT
_ -> fail "Parsing Ordering value failed: expected \"LT\", \"EQ\", or \"GT\""
instance ToJSON () where
toJSON _ = emptyArray
{-# INLINE toJSON #-}
toEncoding _ = E.emptyArray_
{-# INLINE toEncoding #-}
instance FromJSON () where
parseJSON = withArray "()" $ \v ->
if V.null v
then pure ()
else fail "Expected an empty array"
{-# INLINE parseJSON #-}
instance ToJSON [Char] where
toJSON = String . T.pack
{-# INLINE toJSON #-}
toEncoding = Encoding . E.string
{-# INLINE toEncoding #-}
instance FromJSON [Char] where
parseJSON = withText "String" $ pure . T.unpack
{-# INLINE parseJSON #-}
instance ToJSON Char where
toJSON = String . T.singleton
{-# INLINE toJSON #-}
toEncoding = Encoding . E.string . (:[])
{-# INLINE toEncoding #-}
instance FromJSON Char where
parseJSON = withText "Char" $ \t ->
if T.compareLength t 1 == EQ
then pure $ T.head t
else fail "Expected a string of length 1"
{-# INLINE parseJSON #-}
instance ToJSON Scientific where
toJSON = Number
{-# INLINE toJSON #-}
toEncoding = Encoding . E.number
{-# INLINE toEncoding #-}
instance FromJSON Scientific where
parseJSON = withScientific "Scientific" pure
{-# INLINE parseJSON #-}
instance ToJSON Double where
toJSON = realFloatToJSON
{-# INLINE toJSON #-}
toEncoding = realFloatToEncoding
{-# INLINE toEncoding #-}
instance FromJSON Double where
parseJSON = parseRealFloat "Double"
{-# INLINE parseJSON #-}
instance ToJSON Number where
toJSON (D d) = toJSON d
toJSON (I i) = toJSON i
{-# INLINE toJSON #-}
toEncoding (D d) = toEncoding d
toEncoding (I i) = toEncoding i
{-# INLINE toEncoding #-}
instance FromJSON Number where
parseJSON (Number s) = pure $ scientificToNumber s
parseJSON Null = pure (D (0/0))
parseJSON v = typeMismatch "Number" v
{-# INLINE parseJSON #-}
instance ToJSON Float where
toJSON = realFloatToJSON
{-# INLINE toJSON #-}
toEncoding = realFloatToEncoding
{-# INLINE toEncoding #-}
instance FromJSON Float where
parseJSON = parseRealFloat "Float"
{-# INLINE parseJSON #-}
instance ToJSON (Ratio Integer) where
toJSON r = object [ "numerator" .= numerator r
, "denominator" .= denominator r
]
{-# INLINE toJSON #-}
toEncoding r = Encoding $
B.shortByteString "{\"numerator\":" <> builder (numerator r) <>
B.shortByteString ",\"denominator\":" <> builder (denominator r) <>
B.char7 '}'
{-# INLINE toEncoding #-}
instance FromJSON (Ratio Integer) where
parseJSON = withObject "Rational" $ \obj ->
(%) <$> obj .: "numerator"
<*> obj .: "denominator"
{-# INLINE parseJSON #-}
instance HasResolution a => ToJSON (Fixed a) where
toJSON = Number . realToFrac
{-# INLINE toJSON #-}
toEncoding = Encoding . E.number . realToFrac
{-# INLINE toEncoding #-}
-- | /WARNING:/ Only parse fixed-precision numbers from trusted input
-- since an attacker could easily fill up the memory of the target
-- system by specifying a scientific number with a big exponent like
-- @1e1000000000@.
instance HasResolution a => FromJSON (Fixed a) where
parseJSON = withScientific "Fixed" $ pure . realToFrac
{-# INLINE parseJSON #-}
instance ToJSON Int where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.intDec
{-# INLINE toEncoding #-}
instance FromJSON Int where
parseJSON = parseIntegral "Int"
{-# INLINE parseJSON #-}
instance ToJSON Integer where
toJSON = Number . fromInteger
{-# INLINE toJSON #-}
toEncoding = Encoding . B.integerDec
{-# INLINE toEncoding #-}
-- | /WARNING:/ Only parse Integers from trusted input since an
-- attacker could easily fill up the memory of the target system by
-- specifying a scientific number with a big exponent like
-- @1e1000000000@.
instance FromJSON Integer where
parseJSON = withScientific "Integral" $ pure . truncate
{-# INLINE parseJSON #-}
#if MIN_VERSION_base(4,8,0)
instance ToJSON Natural where
toJSON = toJSON . toInteger
{-# INLINE toJSON #-}
toEncoding = toEncoding . toInteger
{-# INLINE toEncoding #-}
instance FromJSON Natural where
parseJSON = withScientific "Natural" $ \s ->
if Scientific.coefficient s < 0
then fail $ "Expected a Natural number but got the negative number: " <> show s
else pure $ truncate s
#endif
instance ToJSON Int8 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.int8Dec
{-# INLINE toEncoding #-}
instance FromJSON Int8 where
parseJSON = parseIntegral "Int8"
{-# INLINE parseJSON #-}
instance ToJSON Int16 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.int16Dec
{-# INLINE toEncoding #-}
instance FromJSON Int16 where
parseJSON = parseIntegral "Int16"
{-# INLINE parseJSON #-}
instance ToJSON Int32 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.int32Dec
{-# INLINE toEncoding #-}
instance FromJSON Int32 where
parseJSON = parseIntegral "Int32"
{-# INLINE parseJSON #-}
instance ToJSON Int64 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.int64Dec
{-# INLINE toEncoding #-}
instance FromJSON Int64 where
parseJSON = parseIntegral "Int64"
{-# INLINE parseJSON #-}
instance ToJSON Word where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.wordDec
{-# INLINE toEncoding #-}
instance FromJSON Word where
parseJSON = parseIntegral "Word"
{-# INLINE parseJSON #-}
instance ToJSON Word8 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.word8Dec
{-# INLINE toEncoding #-}
instance FromJSON Word8 where
parseJSON = parseIntegral "Word8"
{-# INLINE parseJSON #-}
instance ToJSON Word16 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.word16Dec
{-# INLINE toEncoding #-}
instance FromJSON Word16 where
parseJSON = parseIntegral "Word16"
{-# INLINE parseJSON #-}
instance ToJSON Word32 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.word32Dec
{-# INLINE toEncoding #-}
instance FromJSON Word32 where
parseJSON = parseIntegral "Word32"
{-# INLINE parseJSON #-}
instance ToJSON Word64 where
toJSON = Number . fromIntegral
{-# INLINE toJSON #-}
toEncoding = Encoding . B.word64Dec
{-# INLINE toEncoding #-}
instance FromJSON Word64 where
parseJSON = parseIntegral "Word64"
{-# INLINE parseJSON #-}
instance ToJSON Text where
toJSON = String
{-# INLINE toJSON #-}
toEncoding = Encoding . E.text
{-# INLINE toEncoding #-}
instance FromJSON Text where
parseJSON = withText "Text" pure
{-# INLINE parseJSON #-}
instance ToJSON LT.Text where
toJSON = String . LT.toStrict
{-# INLINE toJSON #-}
toEncoding t = Encoding $
B.char7 '"' <>
LT.foldrChunks (\x xs -> E.unquoted x <> xs) (B.char7 '"') t
{-# INLINE toEncoding #-}
instance FromJSON LT.Text where
parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
{-# INLINE parseJSON #-}
instance OVERLAPPABLE_ (ToJSON a) => ToJSON [a] where
toJSON = Array . V.fromList . map toJSON
{-# INLINE toJSON #-}
toEncoding xs = list xs
{-# INLINE toEncoding #-}
instance OVERLAPPABLE_ (FromJSON a) => FromJSON [a] where
parseJSON = withArray "[a]" $ Tr.sequence .
zipWith parseIndexedJSON [0..] . V.toList
{-# INLINE parseJSON #-}
instance OVERLAPPABLE_ (Foldable t, ToJSON a) => ToJSON (t a) where
toJSON = toJSON . toList
{-# INLINE toJSON #-}
toEncoding = foldable
{-# INLINE toEncoding #-}
instance (FromJSON a) => FromJSON (Seq.Seq a) where
parseJSON = withArray "Seq a" $ traverse parseJSON . Seq.fromList . V.toList
{-# INLINE parseJSON #-}
instance (ToJSON a) => ToJSON (Vector a) where
toJSON = Array . V.map toJSON
{-# INLINE toJSON #-}
toEncoding = encodeVector
{-# INLINE toEncoding #-}
encodeVector :: (ToJSON a, VG.Vector v a) => v a -> Encoding
encodeVector xs
| VG.null xs = E.emptyArray_
| otherwise = Encoding $
B.char7 '[' <> builder (VG.unsafeHead xs) <>
VG.foldr go (B.char7 ']') (VG.unsafeTail xs)
where go v b = B.char7 ',' <> builder v <> b
{-# INLINE encodeVector #-}
instance (FromJSON a) => FromJSON (Vector a) where
parseJSON = withArray "Vector a" $ V.mapM (uncurry parseIndexedJSON) .
V.indexed
{-# INLINE parseJSON #-}
vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
vectorToJSON = Array . V.map toJSON . V.convert
{-# INLINE vectorToJSON #-}
vectorParseJSON :: (FromJSON a, VG.Vector w a) => String -> Value -> Parser (w a)
vectorParseJSON s = withArray s $ fmap V.convert . V.mapM (uncurry parseIndexedJSON) . V.indexed
{-# INLINE vectorParseJSON #-}
instance (Storable a, ToJSON a) => ToJSON (VS.Vector a) where
toJSON = vectorToJSON
{-# INLINE toJSON #-}
toEncoding = encodeVector
{-# INLINE toEncoding #-}
instance (Storable a, FromJSON a) => FromJSON (VS.Vector a) where
parseJSON = vectorParseJSON "Data.Vector.Storable.Vector a"
instance (VP.Prim a, ToJSON a) => ToJSON (VP.Vector a) where
toJSON = vectorToJSON
{-# INLINE toJSON #-}
toEncoding = encodeVector
{-# INLINE toEncoding #-}
instance (VP.Prim a, FromJSON a) => FromJSON (VP.Vector a) where
parseJSON = vectorParseJSON "Data.Vector.Primitive.Vector a"
{-# INLINE parseJSON #-}
instance (VG.Vector VU.Vector a, ToJSON a) => ToJSON (VU.Vector a) where
toJSON = vectorToJSON
{-# INLINE toJSON #-}
toEncoding = encodeVector
{-# INLINE toEncoding #-}
instance (VG.Vector VU.Vector a, FromJSON a) => FromJSON (VU.Vector a) where
parseJSON = vectorParseJSON "Data.Vector.Unboxed.Vector a"
{-# INLINE parseJSON #-}
instance (ToJSON a) => ToJSON (Set.Set a) where
toJSON = toJSON . Set.toList
{-# INLINE toJSON #-}
toEncoding = encodeSet Set.minView Set.foldr
{-# INLINE toEncoding #-}
instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
parseJSON = fmap Set.fromList . parseJSON
{-# INLINE parseJSON #-}
instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
toJSON = toJSON . HashSet.toList
{-# INLINE toJSON #-}
toEncoding = foldable
{-# INLINE toEncoding #-}
instance (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet.HashSet a) where
parseJSON = fmap HashSet.fromList . parseJSON
{-# INLINE parseJSON #-}
instance ToJSON IntSet.IntSet where
toJSON = toJSON . IntSet.toList
{-# INLINE toJSON #-}
toEncoding = encodeSet IntSet.minView IntSet.foldr
{-# INLINE toEncoding #-}
encodeSet :: (ToJSON a) =>
(s -> Maybe (a, s))
-> ((a -> B.Builder -> B.Builder) -> B.Builder -> s -> B.Builder)
-> s -> Encoding
encodeSet minView foldr xs =
case minView xs of
Nothing -> E.emptyArray_
Just (m,ys) -> Encoding $
B.char7 '[' <> builder m <> foldr go (B.char7 ']') ys
where go v b = B.char7 ',' <> builder v <> b
{-# INLINE encodeSet #-}
instance FromJSON IntSet.IntSet where
parseJSON = fmap IntSet.fromList . parseJSON
{-# INLINE parseJSON #-}
instance ToJSON a => ToJSON (IntMap.IntMap a) where
toJSON = toJSON . IntMap.toList
{-# INLINE toJSON #-}
toEncoding = toEncoding . IntMap.toList
{-# INLINE toEncoding #-}
instance FromJSON a => FromJSON (IntMap.IntMap a) where
parseJSON = fmap IntMap.fromList . parseJSON
{-# INLINE parseJSON #-}
instance (ToJSON v) => ToJSON (M.Map Text v) where
toJSON = Object . M.foldrWithKey (\k -> H.insert k . toJSON) H.empty
{-# INLINE toJSON #-}
toEncoding = encodeMap M.minViewWithKey M.foldrWithKey
{-# INLINE toEncoding #-}
encodeMap :: (ToJSON k, ToJSON v) =>
(m -> Maybe ((k,v), m))
-> ((k -> v -> B.Builder -> B.Builder) -> B.Builder -> m -> B.Builder)
-> m -> Encoding
encodeMap minViewWithKey foldrWithKey xs =
case minViewWithKey xs of
Nothing -> E.emptyObject_
Just ((k,v),ys) -> Encoding $
B.char7 '{' <> encodeKV k v <>
foldrWithKey go (B.char7 '}') ys
where go k v b = B.char7 ',' <> encodeKV k v <> b
{-# INLINE encodeMap #-}
encodeWithKey :: (ToJSON k, ToJSON v) =>
((k -> v -> Series -> Series) -> Series -> m -> Series)
-> m -> Encoding
encodeWithKey foldrWithKey = brackets '{' '}' . foldrWithKey go mempty
where go k v c = Value (Encoding $ encodeKV k v) <> c
{-# INLINE encodeWithKey #-}
encodeKV :: (ToJSON k, ToJSON v) => k -> v -> B.Builder
encodeKV k v = builder k <> B.char7 ':' <> builder v
{-# INLINE encodeKV #-}
instance (FromJSON v) => FromJSON (M.Map Text v) where
parseJSON = withObject "Map Text a" $
fmap (H.foldrWithKey M.insert M.empty) . H.traverseWithKey (\k v -> parseJSON v <?> Key k)
instance (ToJSON v) => ToJSON (M.Map LT.Text v) where
toJSON = Object . mapHashKeyVal LT.toStrict toJSON
{-# INLINE toJSON #-}
toEncoding = encodeMap M.minViewWithKey M.foldrWithKey
{-# INLINE toEncoding #-}
instance (FromJSON v) => FromJSON (M.Map LT.Text v) where
parseJSON = fmap (hashMapKey LT.fromStrict) . parseJSON
{-# INLINE parseJSON #-}
instance (ToJSON v) => ToJSON (M.Map String v) where
toJSON = Object . mapHashKeyVal pack toJSON
{-# INLINE toJSON #-}
toEncoding = encodeMap M.minViewWithKey M.foldrWithKey
{-# INLINE toEncoding #-}
instance (FromJSON v) => FromJSON (M.Map String v) where
parseJSON = fmap (hashMapKey unpack) . parseJSON
{-# INLINE parseJSON #-}
instance (ToJSON v) => ToJSON (H.HashMap Text v) where
toJSON = Object . H.map toJSON
{-# INLINE toJSON #-}
toEncoding = encodeWithKey H.foldrWithKey
{-# INLINE toEncoding #-}
instance (FromJSON v) => FromJSON (H.HashMap Text v) where
parseJSON = withObject "HashMap Text a" $ H.traverseWithKey (\k v -> parseJSON v <?> Key k)
{-# INLINE parseJSON #-}
instance (ToJSON v) => ToJSON (H.HashMap LT.Text v) where
toJSON = Object . mapKeyVal LT.toStrict toJSON
{-# INLINE toJSON #-}
toEncoding = encodeWithKey H.foldrWithKey
{-# INLINE toEncoding #-}
instance (FromJSON v) => FromJSON (H.HashMap LT.Text v) where
parseJSON = fmap (mapKey LT.fromStrict) . parseJSON
{-# INLINE parseJSON #-}
instance (ToJSON v) => ToJSON (H.HashMap String v) where
toJSON = Object . mapKeyVal pack toJSON
{-# INLINE toJSON #-}
toEncoding = encodeWithKey H.foldrWithKey
{-# INLINE toEncoding #-}
instance (FromJSON v) => FromJSON (H.HashMap String v) where
parseJSON = fmap (mapKey unpack) . parseJSON
{-# INLINE parseJSON #-}
instance (ToJSON v) => ToJSON (Tree.Tree v) where
toJSON (Tree.Node root branches) = toJSON (root,branches)
{-# INLINE toJSON #-}
toEncoding (Tree.Node root branches) = toEncoding (root,branches)
{-# INLINE toEncoding #-}
instance (FromJSON v) => FromJSON (Tree.Tree v) where
parseJSON j = uncurry Tree.Node <$> parseJSON j
{-# INLINE parseJSON #-}
instance ToJSON Value where
toJSON a = a
{-# INLINE toJSON #-}
toEncoding = Encoding . E.encodeToBuilder
{-# INLINE toEncoding #-}
instance FromJSON Value where
parseJSON a = pure a
{-# INLINE parseJSON #-}
instance ToJSON DotNetTime where
toJSON = toJSON . dotNetTime
toEncoding = toEncoding . dotNetTime
dotNetTime :: DotNetTime -> String
dotNetTime (DotNetTime t) = secs ++ formatMillis t ++ ")/"
where secs = formatTime defaultTimeLocale "/Date(%s" t
instance FromJSON DotNetTime where
parseJSON = withText "DotNetTime" $ \t ->
let (s,m) = T.splitAt (T.length t - 5) t
t' = T.concat [s,".",m]
in case parseTime defaultTimeLocale "/Date(%s%Q)/" (unpack t') of
Just d -> pure (DotNetTime d)
_ -> fail "could not parse .NET time"
{-# INLINE parseJSON #-}
instance ToJSON Day where
toJSON = stringEncoding
toEncoding z = Encoding (E.quote $ E.day z)
instance FromJSON Day where
parseJSON = withText "Day" (Time.run Time.day)
instance ToJSON LocalTime where
toJSON = stringEncoding
toEncoding z = Encoding (E.quote $ E.localTime z)
instance FromJSON LocalTime where
parseJSON = withText "LocalTime" (Time.run Time.localTime)
instance ToJSON ZonedTime where
toJSON = stringEncoding
toEncoding z = Encoding (E.quote $ E.zonedTime z)
formatMillis :: (FormatTime t) => t -> String
formatMillis = take 3 . formatTime defaultTimeLocale "%q"
instance FromJSON ZonedTime where
parseJSON = withText "ZonedTime" (Time.run Time.zonedTime)
instance ToJSON UTCTime where
toJSON = stringEncoding
toEncoding t = Encoding (E.quote $ E.utcTime t)
-- | Encode something to a JSON string.
stringEncoding :: (ToJSON a) => a -> Value
stringEncoding = String . T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . encode
{-# INLINE stringEncoding #-}
instance FromJSON UTCTime where
parseJSON = withText "UTCTime" (Time.run Time.utcTime)
instance ToJSON NominalDiffTime where
toJSON = Number . realToFrac
{-# INLINE toJSON #-}
toEncoding = Encoding . E.number . realToFrac
{-# INLINE toEncoding #-}
-- | /WARNING:/ Only parse lengths of time from trusted input
-- since an attacker could easily fill up the memory of the target
-- system by specifying a scientific number with a big exponent like
-- @1e1000000000@.
instance FromJSON NominalDiffTime where
parseJSON = withScientific "NominalDiffTime" $ pure . realToFrac
{-# INLINE parseJSON #-}
parseJSONElemAtIndex :: FromJSON a => Int -> Vector Value -> Parser a
parseJSONElemAtIndex idx ary = parseJSON (V.unsafeIndex ary idx) <?> Index idx
tuple :: B.Builder -> Encoding
tuple b = Encoding (B.char7 '[' <> b <> B.char7 ']')
{-# INLINE tuple #-}
(>*<) :: B.Builder -> B.Builder -> B.Builder
a >*< b = a <> B.char7 ',' <> b
{-# INLINE (>*<) #-}
infixr 6 >*<
instance (ToJSON a, ToJSON b) => ToJSON (a,b) where
toJSON (a,b) = Array $ V.create $ do
mv <- VM.unsafeNew 2
VM.unsafeWrite mv 0 (toJSON a)
VM.unsafeWrite mv 1 (toJSON b)
return mv
{-# INLINE toJSON #-}
toEncoding (a,b) = tuple $
builder a >*< builder b
{-# INLINE toEncoding #-}
instance (FromJSON a, FromJSON b) => FromJSON (a,b) where
parseJSON = withArray "(a,b)" $ \ab ->
let n = V.length ab
in if n == 2
then (,) <$> parseJSONElemAtIndex 0 ab
<*> parseJSONElemAtIndex 1 ab
else fail $ "cannot unpack array of length " ++
show n ++ " into a pair"
{-# INLINE parseJSON #-}
instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON (a,b,c) where
toJSON (a,b,c) = Array $ V.create $ do
mv <- VM.unsafeNew 3
VM.unsafeWrite mv 0 (toJSON a)
VM.unsafeWrite mv 1 (toJSON b)
VM.unsafeWrite mv 2 (toJSON c)
return mv
{-# INLINE toJSON #-}
toEncoding (a,b,c) = tuple $
builder a >*<
builder b >*<
builder c
{-# INLINE toEncoding #-}
instance (FromJSON a, FromJSON b, FromJSON c) => FromJSON (a,b,c) where
parseJSON = withArray "(a,b,c)" $ \abc ->
let n = V.length abc
in if n == 3
then (,,) <$> parseJSONElemAtIndex 0 abc
<*> parseJSONElemAtIndex 1 abc
<*> parseJSONElemAtIndex 2 abc
else fail $ "cannot unpack array of length " ++
show n ++ " into a 3-tuple"
{-# INLINE parseJSON #-}
instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a,b,c,d) where
toJSON (a,b,c,d) = Array $ V.create $ do
mv <- VM.unsafeNew 4
VM.unsafeWrite mv 0 (toJSON a)
VM.unsafeWrite mv 1 (toJSON b)
VM.unsafeWrite mv 2 (toJSON c)
VM.unsafeWrite mv 3 (toJSON d)
return mv
{-# INLINE toJSON #-}
toEncoding (a,b,c,d) = tuple $
builder a >*<
builder b >*<
builder c >*<
builder d
{-# INLINE toEncoding #-}
instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d) =>
FromJSON (a,b,c,d) where
parseJSON = withArray "(a,b,c,d)" $ \abcd ->
let n = V.length abcd
in if n == 4
then (,,,) <$> parseJSONElemAtIndex 0 abcd
<*> parseJSONElemAtIndex 1 abcd
<*> parseJSONElemAtIndex 2 abcd
<*> parseJSONElemAtIndex 3 abcd
else fail $ "cannot unpack array of length " ++
show n ++ " into a 4-tuple"
{-# INLINE parseJSON #-}
instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) =>
ToJSON (a,b,c,d,e) where
toJSON (a,b,c,d,e) = Array $ V.create $ do
mv <- VM.unsafeNew 5
VM.unsafeWrite mv 0 (toJSON a)
VM.unsafeWrite mv 1 (toJSON b)
VM.unsafeWrite mv 2 (toJSON c)
VM.unsafeWrite mv 3 (toJSON d)
VM.unsafeWrite mv 4 (toJSON e)
return mv
{-# INLINE toJSON #-}
toEncoding (a,b,c,d,e) = tuple $
builder a >*<
builder b >*<
builder c >*<
builder d >*<
builder e
{-# INLINE toEncoding #-}
instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) =>
FromJSON (a,b,c,d,e) where
parseJSON = withArray "(a,b,c,d,e)" $ \abcde ->
let n = V.length abcde
in if n == 5
then (,,,,) <$> parseJSONElemAtIndex 0 abcde
<*> parseJSONElemAtIndex 1 abcde
<*> parseJSONElemAtIndex 2 abcde
<*> parseJSONElemAtIndex 3 abcde
<*> parseJSONElemAtIndex 4 abcde
else fail $ "cannot unpack array of length " ++
show n ++ " into a 5-tuple"
{-# INLINE parseJSON #-}
instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) =>
ToJSON (a,b,c,d,e,f) where
toJSON (a,b,c,d,e,f) = Array $ V.create $ do
mv <- VM.unsafeNew 6
VM.unsafeWrite mv 0 (toJSON a)
VM.unsafeWrite mv 1 (toJSON b)
VM.unsafeWrite mv 2 (toJSON c)
VM.unsafeWrite mv 3 (toJSON d)
VM.unsafeWrite mv 4 (toJSON e)
VM.unsafeWrite mv 5 (toJSON f)
return mv
{-# INLINE toJSON #-}
toEncoding (a,b,c,d,e,f) = tuple $
builder a >*<
builder b >*<
builder c >*<
builder d >*<
builder e >*<
builder f
{-# INLINE toEncoding #-}
instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
FromJSON f) => FromJSON (a,b,c,d,e,f) where
parseJSON = withArray "(a,b,c,d,e,f)" $ \abcdef ->
let n = V.length abcdef
in if n == 6
then (,,,,,) <$> parseJSONElemAtIndex 0 abcdef
<*> parseJSONElemAtIndex 1 abcdef
<*> parseJSONElemAtIndex 2 abcdef
<*> parseJSONElemAtIndex 3 abcdef
<*> parseJSONElemAtIndex 4 abcdef
<*> parseJSONElemAtIndex 5 abcdef
else fail $ "cannot unpack array of length " ++
show n ++ " into a 6-tuple"
{-# INLINE parseJSON #-}
instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
ToJSON g) => ToJSON (a,b,c,d,e,f,g) where
toJSON (a,b,c,d,e,f,g) = Array $ V.create $ do
mv <- VM.unsafeNew 7
VM.unsafeWrite mv 0 (toJSON a)
VM.unsafeWrite mv 1 (toJSON b)
VM.unsafeWrite mv 2 (toJSON c)
VM.unsafeWrite mv 3 (toJSON d)
VM.unsafeWrite mv 4 (toJSON e)
VM.unsafeWrite mv 5 (toJSON f)
VM.unsafeWrite mv 6 (toJSON g)
return mv
{-# INLINE toJSON #-}
toEncoding (a,b,c,d,e,f,g) = tuple $
builder a >*<
builder b >*<
builder c >*<
builder d >*<
builder e >*<
builder f >*<
builder g
{-# INLINE toEncoding #-}
instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
FromJSON f, FromJSON g) => FromJSON (a,b,c,d,e,f,g) where