@@ -9,9 +9,6 @@
9{-# LANGUAGE PolyKinds #-}
9{-# LANGUAGE PolyKinds #-}
12−#define NEEDS_INCOHERENT
13−#include "overlapping-compat.h"
15{-# OPTIONS_GHC -fno-warn-orphans #-}
12{-# OPTIONS_GHC -fno-warn-orphans #-}
17-- TODO: Drop this when we remove support for Data.Attoparsec.Number
14-- TODO: Drop this when we remove support for Data.Attoparsec.Number
@@ -38,10 +35,23 @@ module Data.Aeson.Types.Instances
38 -- ** Keys for maps
35 -- ** Keys for maps
39 , ToJSONKey(..)
36 , ToJSONKey(..)
40 , ToJSONKeyFunction(..)
37 , ToJSONKeyFunction(..)
38+ , contramapToJSONKeyFunction
41 , FromJSONKey(..)
39 , FromJSONKey(..)
42 , FromJSONKeyFunction(..)
40 , FromJSONKeyFunction(..)
43 , fromJSONKeyCoerce
41 , fromJSONKeyCoerce
44 , coerceFromJSONKeyFunction
42 , coerceFromJSONKeyFunction
43+ , mapFromJSONKeyFunction
44+ -- ** Liftings to unary and binary type constructors
45 -- ** Generic JSON classes
55 -- ** Generic JSON classes
46 , GFromJSON(..)
56 , GFromJSON(..)
47 , GToJSON(..)
57 , GToJSON(..)
@@ -71,10 +81,16 @@ module Data.Aeson.Types.Instances
73 , typeMismatch
83 , typeMismatch
90+import Data.Aeson.Types.Instances.Tuple (tuple, (>*<))
76import Control.Applicative (Const(..))
92import Control.Applicative (Const(..))
77−import Data.Aeson.Encode.Functions (brackets, builder, encode, foldable, list, list')
93+import Data.Aeson.Encode.Functions (builder, builder', encode, list, encodeWithKey, encodeMap)
78import Data.Aeson.Functions (mapHashKeyVal, mapKey, mapKeyVal)
94import Data.Aeson.Functions (mapHashKeyVal, mapKey, mapKeyVal)
79import Data.Aeson.Types.Class
95import Data.Aeson.Types.Class
80import Data.Aeson.Types.Internal
96import Data.Aeson.Types.Internal
@@ -92,7 +108,7 @@ import Data.Proxy (Proxy(..))
92import Data.Ratio (Ratio, (%), numerator, denominator)
108import Data.Ratio (Ratio, (%), numerator, denominator)
93import Data.Scientific (Scientific)
109import Data.Scientific (Scientific)
94import Data.Tagged (Tagged(..))
110import Data.Tagged (Tagged(..))
95−import Data.Text (Text, pack, unpack)
111+import Data.Text (Text, unpack)
96import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay, UTCTime,
112import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay, UTCTime,
97 ZonedTime)
113 ZonedTime)
98import Data.Time.Format (FormatTime, formatTime, parseTime)
114import Data.Time.Format (FormatTime, formatTime, parseTime)
@@ -112,6 +128,7 @@ import qualified Data.HashMap.Strict as H
112import qualified Data.HashSet as HashSet
128import qualified Data.HashSet as HashSet
113import qualified Data.IntMap as IntMap
129import qualified Data.IntMap as IntMap
114import qualified Data.IntSet as IntSet
130import qualified Data.IntSet as IntSet
131+import qualified Data.List.NonEmpty as NE
115import qualified Data.Map as M
132import qualified Data.Map as M
116import qualified Data.Scientific as Scientific
133import qualified Data.Scientific as Scientific
117import qualified Data.Sequence as Seq
134import qualified Data.Sequence as Seq
@@ -125,10 +142,10 @@ import qualified Data.Text.Lazy.Builder.Int as LTBI
125import qualified Data.Tree as Tree
142import qualified Data.Tree as Tree
126import qualified Data.Vector as V
143import qualified Data.Vector as V
127import qualified Data.Vector.Generic as VG
144import qualified Data.Vector.Generic as VG
128−import qualified Data.Vector.Mutable as VM (unsafeNew, unsafeWrite)
129import qualified Data.Vector.Primitive as VP
145import qualified Data.Vector.Primitive as VP
130import qualified Data.Vector.Storable as VS
146import qualified Data.Vector.Storable as VS
131import qualified Data.Vector.Unboxed as VU
147import qualified Data.Vector.Unboxed as VU
148+import qualified Data.Vector.Mutable as VM
133import Unsafe.Coerce (unsafeCoerce)
150import Unsafe.Coerce (unsafeCoerce)
@@ -145,77 +162,209 @@ import Data.Time.Format (defaultTimeLocale)
145import System.Locale (defaultTimeLocale)
162import System.Locale (defaultTimeLocale)
165+-------------------------------------------------------------------------------
167+-------------------------------------------------------------------------------
148−parseIndexedJSON :: FromJSON a => Int -> Value -> Parser a
169+parseIndexedJSON :: (Value -> Parser a) -> Int -> Value -> Parser a
149−parseIndexedJSON idx value = parseJSON value <?> Index idx
170+parseIndexedJSON p idx value = p value <?> Index idx
171+{-# INLINE parseIndexedJSON #-}
151−parseIndexedJSONPair :: FromJSON b => (Value -> Parser a) -> Int -> Value -> Parser (a, b)
173+parseIndexedJSONPair :: (Value -> Parser a) -> (Value -> Parser b) -> Int -> Value -> Parser (a, b)
152−parseIndexedJSONPair keyParser idx value = p value <?> Index idx
174+parseIndexedJSONPair keyParser valParser idx value = p value <?> Index idx
154 p = withArray "(k,v)" $ \ab ->
176 p = withArray "(k,v)" $ \ab ->
155 let n = V.length ab
177 let n = V.length ab
156 in if n == 2
178 in if n == 2
157− then (,) <$> parseJSONElemAtIndex' keyParser 0 ab
179+ then (,) <$> parseJSONElemAtIndex keyParser 0 ab
158− <*> parseJSONElemAtIndex 1 ab
180+ <*> parseJSONElemAtIndex valParser 1 ab
159 else fail $ "cannot unpack array of length " ++
181 else fail $ "cannot unpack array of length " ++
160 show n ++ " into a pair"
182 show n ++ " into a pair"
184+ parseJSONElemAtIndex :: (Value -> Parser a) -> Int -> Vector Value -> Parser a
185+ parseJSONElemAtIndex p' idx' ary = p' (V.unsafeIndex ary idx') <?> Index idx'
186+{-# INLINE parseIndexedJSONPair #-}
162−toJSONPair :: ToJSON b => (a -> Value) -> (a, b) -> Value
188+toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
163−toJSONPair keySerialiser (a, b) = Array $ V.create $ do
189+toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
164 mv <- VM.unsafeNew 2
190 mv <- VM.unsafeNew 2
165 VM.unsafeWrite mv 0 (keySerialiser a)
191 VM.unsafeWrite mv 0 (keySerialiser a)
166− VM.unsafeWrite mv 1 (toJSON b)
192+ VM.unsafeWrite mv 1 (valSerializer b)
167 return mv
193 return mv
194+{-# INLINE toJSONPair #-}
196+realFloatToJSON :: RealFloat a => a -> Value
198+ | isNaN d || isInfinite d = Null
199+ | otherwise = Number $ Scientific.fromFloatDigits d
200+{-# INLINE realFloatToJSON #-}
202+realFloatToEncoding :: RealFloat a => a -> Encoding
203+realFloatToEncoding d
204+ | isNaN d || isInfinite d = Encoding E.null_
205+ | otherwise = toEncoding (Scientific.fromFloatDigits d)
206+{-# INLINE realFloatToEncoding #-}
208+scientificToNumber :: Scientific -> Number
210+ | e < 0 = D $ Scientific.toRealFloat s
211+ | otherwise = I $ c * 10 ^ e
213+ e = Scientific.base10Exponent s
214+ c = Scientific.coefficient s
215+{-# INLINE scientificToNumber #-}
217+parseRealFloat :: RealFloat a => String -> Value -> Parser a
218+parseRealFloat _ (Number s) = pure $ Scientific.toRealFloat s
219+parseRealFloat _ Null = pure (0/0)
220+parseRealFloat expected v = typeMismatch expected v
221+{-# INLINE parseRealFloat #-}
223+parseIntegral :: Integral a => String -> Value -> Parser a
224+parseIntegral expected = withScientific expected $ pure . truncate
225+{-# INLINE parseIntegral #-}
227+-------------------------------------------------------------------------------
229+-------------------------------------------------------------------------------
231+instance ToJSON1 Identity where
232+ liftToJSON to _ (Identity a) = to a
233+ {-# INLINE liftToJSON #-}
235+ liftToEncoding to _ (Identity a) = to a
236+ {-# INLINE liftToEncoding #-}
169instance (ToJSON a) => ToJSON (Identity a) where
238instance (ToJSON a) => ToJSON (Identity a) where
170− toJSON (Identity a) = toJSON a
239+ toJSON = toJSON1
171 {-# INLINE toJSON #-}
240 {-# INLINE toJSON #-}
173− toEncoding (Identity a) = toEncoding a
242+ toEncoding = toEncoding1
174 {-# INLINE toEncoding #-}
243 {-# INLINE toEncoding #-}
245+instance FromJSON1 Identity where
246+ liftParseJSON p _ a = Identity <$> p a
247+ {-# INLINE liftParseJSON #-}
176instance (FromJSON a) => FromJSON (Identity a) where
249instance (FromJSON a) => FromJSON (Identity a) where
177− parseJSON a = Identity <$> parseJSON a
250+ parseJSON = parseJSON1
178 {-# INLINE parseJSON #-}
251 {-# INLINE parseJSON #-}
254+instance ToJSON2 Const where
255+ liftToJSON2 to _ _ _ (Const x) = to x
256+ {-# INLINE liftToJSON2 #-}
258+ liftToEncoding2 to _ _ _ (Const x) = to x
259+ {-# INLINE liftToEncoding2 #-}
261+instance FromJSON2 Const where
262+ liftParseJSON2 p _ _ _ = fmap Const . p
263+ {-# INLINE liftParseJSON2 #-}
265+instance ToJSON a => ToJSON1 (Const a) where
266+ liftToJSON _ _ (Const x) = toJSON x
267+ {-# INLINE liftToJSON #-}
269+ liftToEncoding _ _ (Const x) = toEncoding x
270+ {-# INLINE liftToEncoding #-}
272+instance FromJSON a => FromJSON1 (Const a) where
273+ liftParseJSON _ _ = fmap Const . parseJSON
274+ {-# INLINE liftParseJSON #-}
276+instance ToJSON a => ToJSON (Const a b) where
277+ toJSON (Const x) = toJSON x
278+ {-# INLINE toJSON #-}
280+ toEncoding (Const x) = toEncoding x
281+ {-# INLINE toEncoding #-}
283+instance FromJSON a => FromJSON (Const a b) where
284+ {-# INLINE parseJSON #-}
285+ parseJSON = fmap Const . parseJSON
288+instance ToJSON1 Maybe where
289+ liftToJSON to _ (Just a) = to a
290+ liftToJSON _ _ Nothing = Null
291+ {-# INLINE liftToJSON #-}
293+ liftToEncoding to _ (Just a) = to a
294+ liftToEncoding _ _ Nothing = Encoding E.null_
295+ {-# INLINE liftToEncoding #-}
180instance (ToJSON a) => ToJSON (Maybe a) where
297instance (ToJSON a) => ToJSON (Maybe a) where
181− toJSON (Just a) = toJSON a
298+ toJSON = toJSON1
182− toJSON Nothing = Null
183 {-# INLINE toJSON #-}
299 {-# INLINE toJSON #-}
185− toEncoding (Just a) = toEncoding a
301+ toEncoding = toEncoding1
186− toEncoding Nothing = Encoding E.null_
187 {-# INLINE toEncoding #-}
302 {-# INLINE toEncoding #-}
304+instance FromJSON1 Maybe where
305+ liftParseJSON _ _ Null = pure Nothing
306+ liftParseJSON p _ a = Just <$> p a
307+ {-# INLINE liftParseJSON #-}
189instance (FromJSON a) => FromJSON (Maybe a) where
309instance (FromJSON a) => FromJSON (Maybe a) where
190− parseJSON Null = pure Nothing
310+ parseJSON = parseJSON1
191− parseJSON a = Just <$> parseJSON a
192 {-# INLINE parseJSON #-}
311 {-# INLINE parseJSON #-}
314+instance ToJSON2 Either where
315+ liftToJSON2 toA _ _toB _ (Left a) = Object $ H.singleton left (toA a)
316+ liftToJSON2 _toA _ toB _ (Right b) = Object $ H.singleton right (toB b)
317+ {-# INLINE liftToJSON2 #-}
319+ liftToEncoding2 toA _ _toB _ (Left a) =
320+ Encoding (B.shortByteString "{\"Left\":")
322+ <> Encoding (B.char7 '}')
324+ liftToEncoding2 _toA _ toB _ (Right b) =
325+ Encoding (B.shortByteString "{\"Right\":")
327+ <> Encoding (B.char7 '}')
328+ {-# INLINE liftToEncoding2 #-}
330+instance (ToJSON a) => ToJSON1 (Either a) where
331+ liftToJSON = liftToJSON2 toJSON toJSONList
332+ {-# INLINE liftToJSON #-}
334+ liftToEncoding = liftToEncoding2 toEncoding toEncodingList
335+ {-# INLINE liftToEncoding #-}
194instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
337instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
195− toJSON (Left a) = object [left .= a]
338+ toJSON = toJSON2
196− toJSON (Right b) = object [right .= b]
197 {-# INLINE toJSON #-}
339 {-# INLINE toJSON #-}
199− toEncoding (Left a) = Encoding $
341+ toEncoding = toEncoding2
200− B.shortByteString "{\"Left\":" <> builder a <> B.char7 '}'
201− toEncoding (Right a) = Encoding $
202− B.shortByteString "{\"Right\":" <> builder a <> B.char7 '}'
203 {-# INLINE toEncoding #-}
342 {-# INLINE toEncoding #-}
205−instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
344+instance FromJSON2 Either where
206− parseJSON (Object (H.toList -> [(key, value)]))
345+ liftParseJSON2 pA _ pB _ (Object (H.toList -> [(key, value)]))
207− | key == left = Left <$> parseJSON value <?> Key left
346+ | key == left = Left <$> pA value <?> Key left
208− | key == right = Right <$> parseJSON value <?> Key right
347+ | key == right = Right <$> pB value <?> Key right
209− parseJSON _ = fail $
348+
349+ liftParseJSON2 _ _ _ _ _ = fail $
210 "expected an object with a single property " ++
350 "expected an object with a single property " ++
211 "where the property key should be either " ++
351 "where the property key should be either " ++
212 "\"Left\" or \"Right\""
352 "\"Left\" or \"Right\""
353+ {-# INLINE liftParseJSON2 #-}
355+instance (FromJSON a) => FromJSON1 (Either a) where
356+ liftParseJSON = liftParseJSON2 parseJSON parseJSONList
357+ {-# INLINE liftParseJSON #-}
359+instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
360+ parseJSON = parseJSON2
213 {-# INLINE parseJSON #-}
361 {-# INLINE parseJSON #-}
215left, right :: Text
363left, right :: Text
216left = "Left"
364left = "Left"
217right = "Right"
365right = "Right"
219instance ToJSON Bool where
368instance ToJSON Bool where
220 toJSON = Bool
369 toJSON = Bool
221 {-# INLINE toJSON #-}
370 {-# INLINE toJSON #-}
@@ -259,24 +408,19 @@ instance FromJSON () where
259 else fail "Expected an empty array"
408 else fail "Expected an empty array"
260 {-# INLINE parseJSON #-}
409 {-# INLINE parseJSON #-}
262−instance INCOHERENT_ ToJSON [Char] where
263− toJSON = String . T.pack
264− {-# INLINE toJSON #-}
266− toEncoding = Encoding . E.string
267− {-# INLINE toEncoding #-}
269−instance INCOHERENT_ FromJSON [Char] where
270− parseJSON = withText "String" $ pure . T.unpack
271− {-# INLINE parseJSON #-}
273instance ToJSON Char where
411instance ToJSON Char where
274 toJSON = String . T.singleton
412 toJSON = String . T.singleton
275 {-# INLINE toJSON #-}
413 {-# INLINE toJSON #-}
415+ toJSONList = String . T.pack
416+ {-# INLINE toJSONList #-}
277 toEncoding = Encoding . E.string . (:[])
418 toEncoding = Encoding . E.string . (:[])
278 {-# INLINE toEncoding #-}
419 {-# INLINE toEncoding #-}
421+ toEncodingList = Encoding . E.string
422+ {-# INLINE toEncodingList #-}
280instance FromJSON Char where
424instance FromJSON Char where
281 parseJSON = withText "Char" $ \t ->
425 parseJSON = withText "Char" $ \t ->
282 if T.compareLength t 1 == EQ
426 if T.compareLength t 1 == EQ
@@ -284,16 +428,8 @@ instance FromJSON Char where
284 else fail "Expected a string of length 1"
428 else fail "Expected a string of length 1"
285 {-# INLINE parseJSON #-}
429 {-# INLINE parseJSON #-}
287−instance ToJSON Scientific where
431+ parseJSONList = withText "String" $ pure . T.unpack
288− toJSON = Number
432+ {-# INLINE parseJSONList #-}
289− {-# INLINE toJSON #-}
291− toEncoding = Encoding . E.number
292− {-# INLINE toEncoding #-}
294−instance FromJSON Scientific where
295− parseJSON = withScientific "Scientific" pure
296− {-# INLINE parseJSON #-}
298instance ToJSON Double where
434instance ToJSON Double where
299 toJSON = realFloatToJSON
435 toJSON = realFloatToJSON
@@ -527,45 +663,253 @@ instance FromJSON LT.Text where
527 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
663 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
528 {-# INLINE parseJSON #-}
664 {-# INLINE parseJSON #-}
666+instance ToJSON Version where
667+ toJSON = toJSON . showVersion
668+ {-# INLINE toJSON #-}
670+ toEncoding = toEncoding . showVersion
671+ {-# INLINE toEncoding #-}
673+instance FromJSON Version where
674+ {-# INLINE parseJSON #-}
675+ parseJSON = withText "Version" $ go . readP_to_S parseVersion . unpack
677+ go [(v,[])] = return v
679+ go _ = fail $ "could not parse Version"
681+-------------------------------------------------------------------------------
682+-- semigroups NonEmpty
683+-------------------------------------------------------------------------------
685+instance ToJSON1 NonEmpty where
686+ liftToJSON to _ = listValue to . NE.toList
687+ {-# INLINE liftToJSON #-}
689+ liftToEncoding to _ = listEncoding to . NE.toList
690+ {-# INLINE liftToEncoding #-}
530instance (ToJSON a) => ToJSON (NonEmpty a) where
692instance (ToJSON a) => ToJSON (NonEmpty a) where
531− toJSON = toJSON . toList
693+ toJSON = toJSON1
532 {-# INLINE toJSON #-}
694 {-# INLINE toJSON #-}
534− toEncoding = toEncoding . toList
696+ toEncoding = toEncoding1
535 {-# INLINE toEncoding #-}
697 {-# INLINE toEncoding #-}
537−instance (FromJSON a) => FromJSON (NonEmpty a) where
699+instance FromJSON1 NonEmpty where
538− parseJSON = withArray "NonEmpty a" $
700+ liftParseJSON p _ = withArray "NonEmpty a" $
539− (>>= ne) . Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
701+ (>>= ne) . Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
541 ne [] = fail "Expected a NonEmpty but got an empty list"
703 ne [] = fail "Expected a NonEmpty but got an empty list"
542 ne (x:xs) = pure (x :| xs)
704 ne (x:xs) = pure (x :| xs)
705+ {-# INLINE liftParseJSON #-}
544−instance OVERLAPPABLE_ (ToJSON a) => ToJSON [a] where
707+instance (FromJSON a) => FromJSON (NonEmpty a) where
545− toJSON = Array . V.fromList . map toJSON
708+ parseJSON = parseJSON1
709+ {-# INLINE parseJSON #-}
711+-------------------------------------------------------------------------------
713+-------------------------------------------------------------------------------
715+instance ToJSON Scientific where
546 {-# INLINE toJSON #-}
717 {-# INLINE toJSON #-}
548− toEncoding xs = list xs
719+ toEncoding = Encoding . E.number
549 {-# INLINE toEncoding #-}
720 {-# INLINE toEncoding #-}
551−instance OVERLAPPABLE_ (FromJSON a) => FromJSON [a] where
722+instance FromJSON Scientific where
552− parseJSON = withArray "[a]" $ Tr.sequence .
723+ parseJSON = withScientific "Scientific" pure
553− zipWith parseIndexedJSON [0..] . V.toList
554 {-# INLINE parseJSON #-}
724 {-# INLINE parseJSON #-}
726+-------------------------------------------------------------------------------
728+-------------------------------------------------------------------------------
730+instance ToJSON1 Seq.Seq where
731+ liftToJSON to _ = listValue to . toList
732+ {-# INLINE liftToJSON #-}
734+ liftToEncoding to _ = listEncoding to . toList
735+ {-# INLINE liftToEncoding #-}
556instance (ToJSON a) => ToJSON (Seq.Seq a) where
737instance (ToJSON a) => ToJSON (Seq.Seq a) where
557− toJSON = toJSON . toList
738+ toJSON = toJSON1
558 {-# INLINE toJSON #-}
739 {-# INLINE toJSON #-}
560− toEncoding = foldable
741+ toEncoding = toEncoding1
561 {-# INLINE toEncoding #-}
742 {-# INLINE toEncoding #-}
563−instance (FromJSON a) => FromJSON (Seq.Seq a) where
744+instance FromJSON1 Seq.Seq where
564− parseJSON = withArray "Seq a" $
745+ liftParseJSON p _ = withArray "Seq a" $
565 fmap Seq.fromList .
746 fmap Seq.fromList .
566− Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
747+ Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
748+ {-# INLINE liftParseJSON #-}
750+instance (FromJSON a) => FromJSON (Seq.Seq a) where
751+ parseJSON = parseJSON1
752+ {-# INLINE parseJSON #-}
755+instance ToJSON1 Set.Set where
756+ liftToJSON to _ = listValue to . Set.toList
757+ {-# INLINE liftToJSON #-}
759+ liftToEncoding to _ = listEncoding to . Set.toList
760+ {-# INLINE liftToEncoding #-}
762+instance (ToJSON a) => ToJSON (Set.Set a) where
764+ {-# INLINE toJSON #-}
766+ toEncoding = toEncoding1
767+ {-# INLINE toEncoding #-}
769+instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
770+ parseJSON = fmap Set.fromList . parseJSON
771+ {-# INLINE parseJSON #-}
774+instance FromJSON IntSet.IntSet where
775+ parseJSON = fmap IntSet.fromList . parseJSON
776+ {-# INLINE parseJSON #-}
778+instance ToJSON IntSet.IntSet where
779+ toJSON = toJSON . IntSet.toList
780+ {-# INLINE toJSON #-}
782+ toEncoding = toEncoding . IntSet.toList
783+ {-# INLINE toEncoding #-}
786+instance ToJSON1 IntMap.IntMap where
787+ liftToJSON to tol = liftToJSON to' tol' . IntMap.toList
789+ to' = liftToJSON2 toJSON toJSONList to tol
790+ tol' = liftToJSONList2 toJSON toJSONList to tol
791+ {-# INLINE liftToJSON #-}
793+ liftToEncoding to tol = liftToEncoding to' tol' . IntMap.toList
795+ to' = liftToEncoding2 toEncoding toEncodingList to tol
796+ tol' = liftToEncodingList2 toEncoding toEncodingList to tol
797+ {-# INLINE liftToEncoding #-}
799+instance ToJSON a => ToJSON (IntMap.IntMap a) where
801+ {-# INLINE toJSON #-}
803+ toEncoding = toEncoding1
804+ {-# INLINE toEncoding #-}
806+instance FromJSON1 IntMap.IntMap where
807+ liftParseJSON p pl = fmap IntMap.fromList . liftParseJSON p' pl'
809+ p' = liftParseJSON2 parseJSON parseJSONList p pl
810+ pl' = liftParseJSONList2 parseJSON parseJSONList p pl
811+ {-# INLINE liftParseJSON #-}
813+instance FromJSON a => FromJSON (IntMap.IntMap a) where
814+ parseJSON = fmap IntMap.fromList . parseJSON
815+ {-# INLINE parseJSON #-}
818+instance ToJSONKey k => ToJSON1 (M.Map k) where
819+ liftToJSON g _ = case toJSONKey of
820+ ToJSONKeyText (f,_) -> Object . mapHashKeyVal f g
821+ ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . M.toList
822+ {-# INLINE liftToJSON #-}
824+ -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> M.Map k a -> Encoding
825+ liftToEncoding g _ = case toJSONKey of
826+ ToJSONKeyText (_,f) -> encodeMap f g M.minViewWithKey M.foldrWithKey
827+ ToJSONKeyValue (_,f) -> list (pairEncoding f) . M.toList
829+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
830+ pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder' g b
831+ {-# INLINE liftToEncoding #-}
833+instance (FromJSONKey k, Ord k) => FromJSON1 (M.Map k) where
834+ liftParseJSON p _ = case fromJSONKey of
835+ FromJSONKeyCoerce _-> withObject "Map k v" $
836+ fmap (H.foldrWithKey (M.insert . unsafeCoerce) M.empty) . H.traverseWithKey (\k v -> p v <?> Key k)
837+ FromJSONKeyText f -> withObject "Map k v" $
838+ fmap (H.foldrWithKey (M.insert . f) M.empty) . H.traverseWithKey (\k v -> p v <?> Key k)
839+ FromJSONKeyTextParser f -> withObject "Map k v" $
840+ H.foldrWithKey (\k v m -> M.insert <$> f k <*> (p v <?> Key k) <*> m) (pure M.empty)
841+ FromJSONKeyValue f -> withArray "Map k v" $ \arr ->
842+ M.fromList <$> (Tr.sequence .
843+ zipWith (parseIndexedJSONPair f p) [0..] . V.toList $ arr)
844+ {-# INLINE liftParseJSON #-}
846+instance (ToJSON v, ToJSONKey k) => ToJSON (M.Map k v) where
848+ {-# INLINE toJSON #-}
850+ toEncoding = toEncoding1
851+ {-# INLINE toEncoding #-}
853+instance (FromJSON v, FromJSONKey k, Ord k) => FromJSON (M.Map k v) where
854+ parseJSON = parseJSON1
567 {-# INLINE parseJSON #-}
855 {-# INLINE parseJSON #-}
858+instance ToJSON1 Tree.Tree where
859+ liftToJSON to tol = go
861+ go (Tree.Node root branches) =
862+ liftToJSON2 to tol to' tol' (root, branches)
864+ to' = liftToJSON go (listValue go)
865+ tol' = liftToJSONList go (listValue go)
866+ {-# INLINE liftToJSON #-}
868+ liftToEncoding to tol = go
870+ go (Tree.Node root branches) =
871+ liftToEncoding2 to tol to' tol' (root, branches)
873+ to' = liftToEncoding go (listEncoding go)
874+ tol' = liftToEncodingList go (listEncoding go)
875+ {-# INLINE liftToEncoding #-}
877+instance (ToJSON v) => ToJSON (Tree.Tree v) where
879+ {-# INLINE toJSON #-}
881+ toEncoding = toEncoding1
882+ {-# INLINE toEncoding #-}
884+instance FromJSON1 Tree.Tree where
885+ liftParseJSON p pl = go
887+ go v = uncurry Tree.Node <$> liftParseJSON2 p pl p' pl' v
889+ p' = liftParseJSON go (listParser go)
890+ pl'= liftParseJSONList go (listParser go)
892+instance (FromJSON v) => FromJSON (Tree.Tree v) where
893+ parseJSON = parseJSON1
894+ {-# INLINE parseJSON #-}
896+-------------------------------------------------------------------------------
898+-------------------------------------------------------------------------------
900+instance ToJSON1 Vector where
901+ liftToJSON to _ = Array . V.map to
902+ {-# INLINE liftToJSON #-}
904+ liftToEncoding to _ xs
905+ | V.null xs = E.emptyArray_
906+ | otherwise = Encoding $
907+ B.char7 '[' <> fromEncoding (to (V.unsafeHead xs)) <>
908+ V.foldr go (B.char7 ']') (V.unsafeTail xs)
910+ go v b = B.char7 ',' <> fromEncoding (to v) <> b
911+ {-# INLINE liftToEncoding #-}
569instance (ToJSON a) => ToJSON (Vector a) where
913instance (ToJSON a) => ToJSON (Vector a) where
570 toJSON = Array . V.map toJSON
914 toJSON = Array . V.map toJSON
571 {-# INLINE toJSON #-}
915 {-# INLINE toJSON #-}
@@ -582,9 +926,13 @@ encodeVector xs
582 where go v b = B.char7 ',' <> builder v <> b
926 where go v b = B.char7 ',' <> builder v <> b
583{-# INLINE encodeVector #-}
927{-# INLINE encodeVector #-}
929+instance FromJSON1 Vector where
930+ liftParseJSON p _ = withArray "Vector a" $
931+ V.mapM (uncurry $ parseIndexedJSON p) . V.indexed
932+ {-# INLINE liftParseJSON #-}
585instance (FromJSON a) => FromJSON (Vector a) where
934instance (FromJSON a) => FromJSON (Vector a) where
586− parseJSON = withArray "Vector a" $ V.mapM (uncurry parseIndexedJSON) .
935+ parseJSON = parseJSON1
588 {-# INLINE parseJSON #-}
936 {-# INLINE parseJSON #-}
590vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
938vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
@@ -592,7 +940,7 @@ vectorToJSON = Array . V.map toJSON . V.convert
592{-# INLINE vectorToJSON #-}
940{-# INLINE vectorToJSON #-}
594vectorParseJSON :: (FromJSON a, VG.Vector w a) => String -> Value -> Parser (w a)
942vectorParseJSON :: (FromJSON a, VG.Vector w a) => String -> Value -> Parser (w a)
595−vectorParseJSON s = withArray s $ fmap V.convert . V.mapM (uncurry parseIndexedJSON) . V.indexed
943+vectorParseJSON s = withArray s $ fmap V.convert . V.mapM (uncurry $ parseIndexedJSON parseJSON) . V.indexed
596{-# INLINE vectorParseJSON #-}
944{-# INLINE vectorParseJSON #-}
598instance (Storable a, ToJSON a) => ToJSON (VS.Vector a) where
946instance (Storable a, ToJSON a) => ToJSON (VS.Vector a) where
@@ -627,154 +975,74 @@ instance (VG.Vector VU.Vector a, FromJSON a) => FromJSON (VU.Vector a) where
627 parseJSON = vectorParseJSON "Data.Vector.Unboxed.Vector a"
975 parseJSON = vectorParseJSON "Data.Vector.Unboxed.Vector a"
628 {-# INLINE parseJSON #-}
976 {-# INLINE parseJSON #-}
630−instance (ToJSON a) => ToJSON (Set.Set a) where
978+-------------------------------------------------------------------------------
631− toJSON = toJSON . Set.toList
979+-- unordered-containers
632− {-# INLINE toJSON #-}
980+-------------------------------------------------------------------------------
634− toEncoding = encodeSet Set.minView Set.foldr
982+instance ToJSON1 HashSet.HashSet where
635− {-# INLINE toEncoding #-}
983+ liftToJSON to _ = listValue to . HashSet.toList
984+ {-# INLINE liftToJSON #-}
637−instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
986+ liftToEncoding to _ = listEncoding to . HashSet.toList
638− parseJSON = fmap Set.fromList . parseJSON
987+ {-# INLINE liftToEncoding #-}
639− {-# INLINE parseJSON #-}
641instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
989instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
642− toJSON = toJSON . HashSet.toList
990+ toJSON = toJSON1
643 {-# INLINE toJSON #-}
991 {-# INLINE toJSON #-}
645− toEncoding = foldable
993+ toEncoding = toEncoding1
646 {-# INLINE toEncoding #-}
994 {-# INLINE toEncoding #-}
648instance (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet.HashSet a) where
996instance (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet.HashSet a) where
649 parseJSON = fmap HashSet.fromList . parseJSON
997 parseJSON = fmap HashSet.fromList . parseJSON
650 {-# INLINE parseJSON #-}
998 {-# INLINE parseJSON #-}
652−instance ToJSON IntSet.IntSet where
653− toJSON = toJSON . IntSet.toList
654− {-# INLINE toJSON #-}
656− toEncoding = encodeSet IntSet.minView IntSet.foldr
657− {-# INLINE toEncoding #-}
659−encodeSet :: (ToJSON a) =>
661− -> ((a -> B.Builder -> B.Builder) -> B.Builder -> s -> B.Builder)
663−encodeSet minView foldr xs =
665− Nothing -> E.emptyArray_
666− Just (m,ys) -> Encoding $
667− B.char7 '[' <> builder m <> foldr go (B.char7 ']') ys
668− where go v b = B.char7 ',' <> builder v <> b
669−{-# INLINE encodeSet #-}
671−instance FromJSON IntSet.IntSet where
672− parseJSON = fmap IntSet.fromList . parseJSON
673− {-# INLINE parseJSON #-}
675−instance ToJSON a => ToJSON (IntMap.IntMap a) where
676− toJSON = toJSON . IntMap.toList
677− {-# INLINE toJSON #-}
679− toEncoding = toEncoding . IntMap.toList
680− {-# INLINE toEncoding #-}
682−instance FromJSON a => FromJSON (IntMap.IntMap a) where
1001+instance ToJSONKey k => ToJSON1 (H.HashMap k) where
683− parseJSON = fmap IntMap.fromList . parseJSON
684− {-# INLINE parseJSON #-}
686−encodeMap :: (ToJSON v)
688− -> (m -> Maybe ((k,v), m))
689− -> ((k -> v -> B.Builder -> B.Builder) -> B.Builder -> m -> B.Builder)
691−encodeMap encodeKey minViewWithKey foldrWithKey xs =
692− case minViewWithKey xs of
693− Nothing -> E.emptyObject_
694− Just ((k,v),ys) -> Encoding $
695− B.char7 '{' <> encodeKV encodeKey k v <>
696− foldrWithKey go (B.char7 '}') ys
697− where go k v b = B.char7 ',' <> encodeKV encodeKey k v <> b
698−{-# INLINE encodeMap #-}
700−encodeWithKey :: (ToJSON v)
702− -> ((k -> v -> Series -> Series) -> Series -> m -> Series)
704−encodeWithKey encodeKey foldrWithKey = brackets '{' '}' . foldrWithKey go mempty
705− where go k v c = Value (Encoding $ encodeKV encodeKey k v) <> c
706−{-# INLINE encodeWithKey #-}
708−encodeKV :: (ToJSON v) => (k -> Encoding) -> k -> v -> B.Builder
709−encodeKV encodeKey k v = fromEncoding (encodeKey k) <> B.char7 ':' <> builder v
710−{-# INLINE encodeKV #-}
712−instance (ToJSON v, ToJSONKey k) => ToJSON (M.Map k v) where
713− toJSON = case toJSONKey of
1002+ liftToJSON g _ = case toJSONKey of
714− ToJSONKeyText (f,_) -> Object . mapHashKeyVal f toJSON
1003+ ToJSONKeyText (f,_) -> Object . mapKeyVal f g
715− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f) . M.toList
1004+ ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . H.toList
716− {-# INLINE toJSON #-}
1005+ {-# INLINE liftToJSON #-}
718− toEncoding = case toJSONKey of
719− ToJSONKeyText (_,f) -> encodeMap f M.minViewWithKey M.foldrWithKey
720− ToJSONKeyValue (_,f) -> list' (pairEncoding f) . M.toList
721− where pairEncoding :: (k -> Encoding) -> (k, v) -> Encoding
722− pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder b
723− {-# INLINE toEncoding #-}
725−instance (FromJSON v, FromJSONKey k, Ord k) => FromJSON (M.Map k v) where
1007+ -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
726− parseJSON = case fromJSONKey of
1008+ liftToEncoding g _ = case toJSONKey of
727− FromJSONKeyCoerce _-> withObject "Map k v" $
1009+ ToJSONKeyText (_,f) -> encodeWithKey f g H.foldrWithKey
728− fmap (H.foldrWithKey (M.insert . unsafeCoerce) M.empty) . H.traverseWithKey (\k v -> parseJSON v <?> Key k)
1010+ ToJSONKeyValue (_,f) -> list (pairEncoding f) . H.toList
729− FromJSONKeyText f -> withObject "Map k v" $
1011+ where
730− fmap (H.foldrWithKey (M.insert . f) M.empty) . H.traverseWithKey (\k v -> parseJSON v <?> Key k)
1012+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
731− FromJSONKeyTextParser f -> withObject "Map k v" $
1013+ pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder' g b
732− H.foldrWithKey (\k v m -> M.insert <$> f k <*> (parseJSON v <?> Key k) <*> m) (pure M.empty)
1014+ {-# INLINE liftToEncoding #-}
733− FromJSONKeyValue f -> withArray "Map k v" $ \arr ->
734− M.fromList <$> (Tr.sequence .
735− zipWith (parseIndexedJSONPair f) [0..] . V.toList $ arr)
736− {-# INLINE parseJSON #-}
738instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
1016instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
739− toJSON = case toJSONKey of
1017+ toJSON = toJSON1
740− ToJSONKeyText (f,_) -> Object . mapKeyVal f toJSON
741− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f) . H.toList
742 {-# INLINE toJSON #-}
1018 {-# INLINE toJSON #-}
744− toEncoding = case toJSONKey of
1020+ toEncoding = toEncoding1
745− ToJSONKeyText (_,f) -> encodeWithKey f H.foldrWithKey
746− ToJSONKeyValue (_,f) -> list' (pairEncoding f) . H.toList
747− where pairEncoding :: (k -> Encoding) -> (k, v) -> Encoding
748− pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder b
749 {-# INLINE toEncoding #-}
1021 {-# INLINE toEncoding #-}
751−instance (FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (H.HashMap k v) where
1023+instance (FromJSONKey k, Eq k, Hashable k) => FromJSON1 (H.HashMap k) where
752− parseJSON = case fromJSONKey of
1024+ liftParseJSON p _ = case fromJSONKey of
753 FromJSONKeyCoerce _ -> withObject "HashMap ~Text v" $
1025 FromJSONKeyCoerce _ -> withObject "HashMap ~Text v" $
754− uc . H.traverseWithKey (\k v -> parseJSON v <?> Key k)
1026+ uc . H.traverseWithKey (\k v -> p v <?> Key k)
755 FromJSONKeyText f -> withObject "HashMap k v" $
1027 FromJSONKeyText f -> withObject "HashMap k v" $
756− fmap (mapKey f) . H.traverseWithKey (\k v -> parseJSON v <?> Key k)
1028+ fmap (mapKey f) . H.traverseWithKey (\k v -> p v <?> Key k)
757 FromJSONKeyTextParser f -> withObject "HashMap k v" $
1029 FromJSONKeyTextParser f -> withObject "HashMap k v" $
758− H.foldrWithKey (\k v m -> H.insert <$> f k <*> (parseJSON v <?> Key k) <*> m) (pure H.empty)
1030+ H.foldrWithKey (\k v m -> H.insert <$> f k <*> (p v <?> Key k) <*> m) (pure H.empty)
759 FromJSONKeyValue f -> withArray "Map k v" $ \arr ->
1031 FromJSONKeyValue f -> withArray "Map k v" $ \arr ->
760 H.fromList <$> (Tr.sequence .
1032 H.fromList <$> (Tr.sequence .
761− zipWith (parseIndexedJSONPair f) [0..] . V.toList $ arr)
1033+ zipWith (parseIndexedJSONPair f p) [0..] . V.toList $ arr)
763 uc :: Parser (H.HashMap Text v) -> Parser (H.HashMap k v)
1035 uc :: Parser (H.HashMap Text v) -> Parser (H.HashMap k v)
764 uc = unsafeCoerce
1036 uc = unsafeCoerce
765− {-# INLINE parseJSON #-}
767−instance (ToJSON v) => ToJSON (Tree.Tree v) where
768− toJSON (Tree.Node root branches) = toJSON (root,branches)
769− {-# INLINE toJSON #-}
771− toEncoding (Tree.Node root branches) = toEncoding (root,branches)
772− {-# INLINE toEncoding #-}
774−instance (FromJSON v) => FromJSON (Tree.Tree v) where
1038+instance (FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (H.HashMap k v) where
775− parseJSON j = uncurry Tree.Node <$> parseJSON j
1039+ parseJSON = parseJSON1
776 {-# INLINE parseJSON #-}
1040 {-# INLINE parseJSON #-}
1042+-------------------------------------------------------------------------------
1044+-------------------------------------------------------------------------------
778instance ToJSON Value where
1046instance ToJSON Value where
779 toJSON a = a
1047 toJSON a = a
780 {-# INLINE toJSON #-}
1048 {-# INLINE toJSON #-}
@@ -783,7 +1051,7 @@ instance ToJSON Value where
783 {-# INLINE toEncoding #-}
1051 {-# INLINE toEncoding #-}
785instance FromJSON Value where
1053instance FromJSON Value where
786− parseJSON a = pure a
1054+ parseJSON = pure
787 {-# INLINE parseJSON #-}
1055 {-# INLINE parseJSON #-}
789instance ToJSON DotNetTime where
1057instance ToJSON DotNetTime where
@@ -804,6 +1072,10 @@ instance FromJSON DotNetTime where
804 _ -> fail "could not parse .NET time"
1072 _ -> fail "could not parse .NET time"
805 {-# INLINE parseJSON #-}
1073 {-# INLINE parseJSON #-}
1075+-------------------------------------------------------------------------------
1077+-------------------------------------------------------------------------------
807instance ToJSON Day where
1079instance ToJSON Day where
808 toJSON = stringEncoding
1080 toJSON = stringEncoding
809 toEncoding z = Encoding (E.quote $ E.day z)
1081 toEncoding z = Encoding (E.quote $ E.day z)
@@ -864,715 +1136,81 @@ instance FromJSON NominalDiffTime where
864 parseJSON = withScientific "NominalDiffTime" $ pure . realToFrac
1136 parseJSON = withScientific "NominalDiffTime" $ pure . realToFrac
865 {-# INLINE parseJSON #-}
1137 {-# INLINE parseJSON #-}
867−parseJSONElemAtIndex :: FromJSON a => Int -> Vector Value -> Parser a
1139+-------------------------------------------------------------------------------
868−parseJSONElemAtIndex = parseJSONElemAtIndex' parseJSON
1140+-- base Monoid/Semigroup
869−
1141+-------------------------------------------------------------------------------
870−parseJSONElemAtIndex' :: (Value -> Parser a) -> Int -> Vector Value -> Parser a
871−parseJSONElemAtIndex' p idx ary = p (V.unsafeIndex ary idx) <?> Index idx
873−tuple :: B.Builder -> Encoding
874−tuple b = Encoding (B.char7 '[' <> b <> B.char7 ']')
877−(>*<) :: B.Builder -> B.Builder -> B.Builder
878−a >*< b = a <> B.char7 ',' <> b
882−instance (ToJSON a, ToJSON b) => ToJSON (a,b) where
883− toJSON (a,b) = Array $ V.create $ do
884− mv <- VM.unsafeNew 2
885− VM.unsafeWrite mv 0 (toJSON a)
886− VM.unsafeWrite mv 1 (toJSON b)
888− {-# INLINE toJSON #-}
890− toEncoding (a,b) = tuple $
891− builder a >*< builder b
892− {-# INLINE toEncoding #-}
894−instance (FromJSON a, FromJSON b) => FromJSON (a,b) where
895− parseJSON = withArray "(a,b)" $ \ab ->
898− then (,) <$> parseJSONElemAtIndex 0 ab
899− <*> parseJSONElemAtIndex 1 ab
900− else fail $ "cannot unpack array of length " ++
901− show n ++ " into a pair"
902− {-# INLINE parseJSON #-}
904−instance (ToJSON a, ToJSON b, ToJSON c) => ToJSON (a,b,c) where
905− toJSON (a,b,c) = Array $ V.create $ do
906− mv <- VM.unsafeNew 3
907− VM.unsafeWrite mv 0 (toJSON a)
908− VM.unsafeWrite mv 1 (toJSON b)
909− VM.unsafeWrite mv 2 (toJSON c)
911− {-# INLINE toJSON #-}
913− toEncoding (a,b,c) = tuple $
917− {-# INLINE toEncoding #-}
919−instance (FromJSON a, FromJSON b, FromJSON c) => FromJSON (a,b,c) where
920− parseJSON = withArray "(a,b,c)" $ \abc ->
921− let n = V.length abc
923− then (,,) <$> parseJSONElemAtIndex 0 abc
924− <*> parseJSONElemAtIndex 1 abc
925− <*> parseJSONElemAtIndex 2 abc
926− else fail $ "cannot unpack array of length " ++
927− show n ++ " into a 3-tuple"
928− {-# INLINE parseJSON #-}
930−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (a,b,c,d) where
931− toJSON (a,b,c,d) = Array $ V.create $ do
932− mv <- VM.unsafeNew 4
933− VM.unsafeWrite mv 0 (toJSON a)
934− VM.unsafeWrite mv 1 (toJSON b)
935− VM.unsafeWrite mv 2 (toJSON c)
936− VM.unsafeWrite mv 3 (toJSON d)
938− {-# INLINE toJSON #-}
940− toEncoding (a,b,c,d) = tuple $
945− {-# INLINE toEncoding #-}
947−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d) =>
948− FromJSON (a,b,c,d) where
949− parseJSON = withArray "(a,b,c,d)" $ \abcd ->
950− let n = V.length abcd
952− then (,,,) <$> parseJSONElemAtIndex 0 abcd
953− <*> parseJSONElemAtIndex 1 abcd
954− <*> parseJSONElemAtIndex 2 abcd
955− <*> parseJSONElemAtIndex 3 abcd
956− else fail $ "cannot unpack array of length " ++
957− show n ++ " into a 4-tuple"
958− {-# INLINE parseJSON #-}
960−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e) =>
961− ToJSON (a,b,c,d,e) where
962− toJSON (a,b,c,d,e) = Array $ V.create $ do
963− mv <- VM.unsafeNew 5
964− VM.unsafeWrite mv 0 (toJSON a)
965− VM.unsafeWrite mv 1 (toJSON b)
966− VM.unsafeWrite mv 2 (toJSON c)
967− VM.unsafeWrite mv 3 (toJSON d)
968− VM.unsafeWrite mv 4 (toJSON e)
970− {-# INLINE toJSON #-}
972− toEncoding (a,b,c,d,e) = tuple $
978− {-# INLINE toEncoding #-}
980−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e) =>
981− FromJSON (a,b,c,d,e) where
982− parseJSON = withArray "(a,b,c,d,e)" $ \abcde ->
983− let n = V.length abcde
985− then (,,,,) <$> parseJSONElemAtIndex 0 abcde
986− <*> parseJSONElemAtIndex 1 abcde
987− <*> parseJSONElemAtIndex 2 abcde
988− <*> parseJSONElemAtIndex 3 abcde
989− <*> parseJSONElemAtIndex 4 abcde
990− else fail $ "cannot unpack array of length " ++
991− show n ++ " into a 5-tuple"
992− {-# INLINE parseJSON #-}
994−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f) =>
995− ToJSON (a,b,c,d,e,f) where
996− toJSON (a,b,c,d,e,f) = Array $ V.create $ do
997− mv <- VM.unsafeNew 6
998− VM.unsafeWrite mv 0 (toJSON a)
999− VM.unsafeWrite mv 1 (toJSON b)
1000− VM.unsafeWrite mv 2 (toJSON c)
1001− VM.unsafeWrite mv 3 (toJSON d)
1002− VM.unsafeWrite mv 4 (toJSON e)
1003− VM.unsafeWrite mv 5 (toJSON f)
1005− {-# INLINE toJSON #-}
1007− toEncoding (a,b,c,d,e,f) = tuple $
1014− {-# INLINE toEncoding #-}
1016−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1017− FromJSON f) => FromJSON (a,b,c,d,e,f) where
1018− parseJSON = withArray "(a,b,c,d,e,f)" $ \abcdef ->
1019− let n = V.length abcdef
1021− then (,,,,,) <$> parseJSONElemAtIndex 0 abcdef
1022− <*> parseJSONElemAtIndex 1 abcdef
1023− <*> parseJSONElemAtIndex 2 abcdef
1024− <*> parseJSONElemAtIndex 3 abcdef
1025− <*> parseJSONElemAtIndex 4 abcdef
1026− <*> parseJSONElemAtIndex 5 abcdef
1027− else fail $ "cannot unpack array of length " ++
1028− show n ++ " into a 6-tuple"
1029− {-# INLINE parseJSON #-}
1031−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1032− ToJSON g) => ToJSON (a,b,c,d,e,f,g) where
1033− toJSON (a,b,c,d,e,f,g) = Array $ V.create $ do
1034− mv <- VM.unsafeNew 7
1035− VM.unsafeWrite mv 0 (toJSON a)
1036− VM.unsafeWrite mv 1 (toJSON b)
1037− VM.unsafeWrite mv 2 (toJSON c)
1038− VM.unsafeWrite mv 3 (toJSON d)
1039− VM.unsafeWrite mv 4 (toJSON e)
1040− VM.unsafeWrite mv 5 (toJSON f)
1041− VM.unsafeWrite mv 6 (toJSON g)
1043− {-# INLINE toJSON #-}
1045− toEncoding (a,b,c,d,e,f,g) = tuple $
1053− {-# INLINE toEncoding #-}
1055−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1056− FromJSON f, FromJSON g) => FromJSON (a,b,c,d,e,f,g) where
1057− parseJSON = withArray "(a,b,c,d,e,f,g)" $ \abcdefg ->
1058− let n = V.length abcdefg
1060− then (,,,,,,) <$> parseJSONElemAtIndex 0 abcdefg
1061− <*> parseJSONElemAtIndex 1 abcdefg
1062− <*> parseJSONElemAtIndex 2 abcdefg
1063− <*> parseJSONElemAtIndex 3 abcdefg
1064− <*> parseJSONElemAtIndex 4 abcdefg
1065− <*> parseJSONElemAtIndex 5 abcdefg
1066− <*> parseJSONElemAtIndex 6 abcdefg
1067− else fail $ "cannot unpack array of length " ++
1068− show n ++ " into a 7-tuple"
1069− {-# INLINE parseJSON #-}
1071−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1072− ToJSON g, ToJSON h) => ToJSON (a,b,c,d,e,f,g,h) where
1073− toJSON (a,b,c,d,e,f,g,h) = Array $ V.create $ do
1074− mv <- VM.unsafeNew 8
1075− VM.unsafeWrite mv 0 (toJSON a)
1076− VM.unsafeWrite mv 1 (toJSON b)
1077− VM.unsafeWrite mv 2 (toJSON c)
1078− VM.unsafeWrite mv 3 (toJSON d)
1079− VM.unsafeWrite mv 4 (toJSON e)
1080− VM.unsafeWrite mv 5 (toJSON f)
1081− VM.unsafeWrite mv 6 (toJSON g)
1082− VM.unsafeWrite mv 7 (toJSON h)
1084− {-# INLINE toJSON #-}
1086− toEncoding (a,b,c,d,e,f,g,h) = tuple $
1095− {-# INLINE toEncoding #-}
1097−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1098− FromJSON f, FromJSON g, FromJSON h) =>
1099− FromJSON (a,b,c,d,e,f,g,h) where
1100− parseJSON = withArray "(a,b,c,d,e,f,g,h)" $ \ary ->
1101− let n = V.length ary
1103− then fail $ "cannot unpack array of length " ++
1104− show n ++ " into an 8-tuple"
1106− <$> parseJSONElemAtIndex 0 ary
1107− <*> parseJSONElemAtIndex 1 ary
1108− <*> parseJSONElemAtIndex 2 ary
1109− <*> parseJSONElemAtIndex 3 ary
1110− <*> parseJSONElemAtIndex 4 ary
1111− <*> parseJSONElemAtIndex 5 ary
1112− <*> parseJSONElemAtIndex 6 ary
1113− <*> parseJSONElemAtIndex 7 ary
1114− {-# INLINE parseJSON #-}
1116−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1117− ToJSON g, ToJSON h, ToJSON i) => ToJSON (a,b,c,d,e,f,g,h,i) where
1118− toJSON (a,b,c,d,e,f,g,h,i) = Array $ V.create $ do
1119− mv <- VM.unsafeNew 9
1120− VM.unsafeWrite mv 0 (toJSON a)
1121− VM.unsafeWrite mv 1 (toJSON b)
1122− VM.unsafeWrite mv 2 (toJSON c)
1123− VM.unsafeWrite mv 3 (toJSON d)
1124− VM.unsafeWrite mv 4 (toJSON e)
1125− VM.unsafeWrite mv 5 (toJSON f)
1126− VM.unsafeWrite mv 6 (toJSON g)
1127− VM.unsafeWrite mv 7 (toJSON h)
1128− VM.unsafeWrite mv 8 (toJSON i)
1130− {-# INLINE toJSON #-}
1132− toEncoding (a,b,c,d,e,f,g,h,i) = tuple $
1142− {-# INLINE toEncoding #-}
1144−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1145− FromJSON f, FromJSON g, FromJSON h, FromJSON i) =>
1146− FromJSON (a,b,c,d,e,f,g,h,i) where
1147− parseJSON = withArray "(a,b,c,d,e,f,g,h,i)" $ \ary ->
1148− let n = V.length ary
1150− then fail $ "cannot unpack array of length " ++
1151− show n ++ " into a 9-tuple"
1153− <$> parseJSONElemAtIndex 0 ary
1154− <*> parseJSONElemAtIndex 1 ary
1155− <*> parseJSONElemAtIndex 2 ary
1156− <*> parseJSONElemAtIndex 3 ary
1157− <*> parseJSONElemAtIndex 4 ary
1158− <*> parseJSONElemAtIndex 5 ary
1159− <*> parseJSONElemAtIndex 6 ary
1160− <*> parseJSONElemAtIndex 7 ary
1161− <*> parseJSONElemAtIndex 8 ary
1162− {-# INLINE parseJSON #-}
1164−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1165− ToJSON g, ToJSON h, ToJSON i, ToJSON j) =>
1166− ToJSON (a,b,c,d,e,f,g,h,i,j) where
1167− toJSON (a,b,c,d,e,f,g,h,i,j) = Array $ V.create $ do
1168− mv <- VM.unsafeNew 10
1169− VM.unsafeWrite mv 0 (toJSON a)
1170− VM.unsafeWrite mv 1 (toJSON b)
1171− VM.unsafeWrite mv 2 (toJSON c)
1172− VM.unsafeWrite mv 3 (toJSON d)
1173− VM.unsafeWrite mv 4 (toJSON e)
1174− VM.unsafeWrite mv 5 (toJSON f)
1175− VM.unsafeWrite mv 6 (toJSON g)
1176− VM.unsafeWrite mv 7 (toJSON h)
1177− VM.unsafeWrite mv 8 (toJSON i)
1178− VM.unsafeWrite mv 9 (toJSON j)
1180− {-# INLINE toJSON #-}
1182− toEncoding (a,b,c,d,e,f,g,h,i,j) = tuple $
1143+instance ToJSON1 Dual where
1183− builder a >*<
1144+ liftToJSON to _ = to . getDual
1184− builder b >*<
1145+ {-# INLINE liftToJSON #-}
1193− {-# INLINE toEncoding #-}
1195−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1147+ liftToEncoding to _ = to . getDual
1196− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) =>
1148+ {-# INLINE liftToEncoding #-}
1197− FromJSON (a,b,c,d,e,f,g,h,i,j) where
1198− parseJSON = withArray "(a,b,c,d,e,f,g,h,i,j)" $ \ary ->
1199− let n = V.length ary
1201− then fail $ "cannot unpack array of length " ++
1202− show n ++ " into a 10-tuple"
1204− <$> parseJSONElemAtIndex 0 ary
1205− <*> parseJSONElemAtIndex 1 ary
1206− <*> parseJSONElemAtIndex 2 ary
1207− <*> parseJSONElemAtIndex 3 ary
1208− <*> parseJSONElemAtIndex 4 ary
1209− <*> parseJSONElemAtIndex 5 ary
1210− <*> parseJSONElemAtIndex 6 ary
1211− <*> parseJSONElemAtIndex 7 ary
1212− <*> parseJSONElemAtIndex 8 ary
1213− <*> parseJSONElemAtIndex 9 ary
1214− {-# INLINE parseJSON #-}
1216−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1217− ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k) =>
1218− ToJSON (a,b,c,d,e,f,g,h,i,j,k) where
1219− toJSON (a,b,c,d,e,f,g,h,i,j,k) = Array $ V.create $ do
1220− mv <- VM.unsafeNew 11
1221− VM.unsafeWrite mv 0 (toJSON a)
1222− VM.unsafeWrite mv 1 (toJSON b)
1223− VM.unsafeWrite mv 2 (toJSON c)
1224− VM.unsafeWrite mv 3 (toJSON d)
1225− VM.unsafeWrite mv 4 (toJSON e)
1226− VM.unsafeWrite mv 5 (toJSON f)
1227− VM.unsafeWrite mv 6 (toJSON g)
1228− VM.unsafeWrite mv 7 (toJSON h)
1229− VM.unsafeWrite mv 8 (toJSON i)
1230− VM.unsafeWrite mv 9 (toJSON j)
1231− VM.unsafeWrite mv 10 (toJSON k)
1233− {-# INLINE toJSON #-}
1235− toEncoding (a,b,c,d,e,f,g,h,i,j,k) = tuple $
1150+instance ToJSON a => ToJSON (Dual a) where
1236− builder a >*<
1151+ toJSON = toJSON1
1247− {-# INLINE toEncoding #-}
1249−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1250− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1252− FromJSON (a,b,c,d,e,f,g,h,i,j,k) where
1253− parseJSON = withArray "(a,b,c,d,e,f,g,h,i,j,k)" $ \ary ->
1254− let n = V.length ary
1256− then fail $ "cannot unpack array of length " ++
1257− show n ++ " into an 11-tuple"
1258− else (,,,,,,,,,,)
1259− <$> parseJSONElemAtIndex 0 ary
1260− <*> parseJSONElemAtIndex 1 ary
1261− <*> parseJSONElemAtIndex 2 ary
1262− <*> parseJSONElemAtIndex 3 ary
1263− <*> parseJSONElemAtIndex 4 ary
1264− <*> parseJSONElemAtIndex 5 ary
1265− <*> parseJSONElemAtIndex 6 ary
1266− <*> parseJSONElemAtIndex 7 ary
1267− <*> parseJSONElemAtIndex 8 ary
1268− <*> parseJSONElemAtIndex 9 ary
1269− <*> parseJSONElemAtIndex 10 ary
1270− {-# INLINE parseJSON #-}
1272−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1273− ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l) =>
1274− ToJSON (a,b,c,d,e,f,g,h,i,j,k,l) where
1275− toJSON (a,b,c,d,e,f,g,h,i,j,k,l) = Array $ V.create $ do
1276− mv <- VM.unsafeNew 12
1277− VM.unsafeWrite mv 0 (toJSON a)
1278− VM.unsafeWrite mv 1 (toJSON b)
1279− VM.unsafeWrite mv 2 (toJSON c)
1280− VM.unsafeWrite mv 3 (toJSON d)
1281− VM.unsafeWrite mv 4 (toJSON e)
1282− VM.unsafeWrite mv 5 (toJSON f)
1283− VM.unsafeWrite mv 6 (toJSON g)
1284− VM.unsafeWrite mv 7 (toJSON h)
1285− VM.unsafeWrite mv 8 (toJSON i)
1286− VM.unsafeWrite mv 9 (toJSON j)
1287− VM.unsafeWrite mv 10 (toJSON k)
1288− VM.unsafeWrite mv 11 (toJSON l)
1290 {-# INLINE toJSON #-}
1152 {-# INLINE toJSON #-}
1292− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l) = tuple $
1154+ toEncoding = toEncoding1
1305 {-# INLINE toEncoding #-}
1155 {-# INLINE toEncoding #-}
1307−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1157+instance FromJSON1 Dual where
1308− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1158+ liftParseJSON p _ = fmap Dual . p
1309− FromJSON k, FromJSON l) =>
1159+ {-# INLINE liftParseJSON #-}
1310− FromJSON (a,b,c,d,e,f,g,h,i,j,k,l) where
1311− parseJSON = withArray "(a,b,c,d,e,f,g,h,i,j,k,l)" $ \ary ->
1312− let n = V.length ary
1314− then fail $ "cannot unpack array of length " ++
1315− show n ++ " into a 12-tuple"
1316− else (,,,,,,,,,,,)
1317− <$> parseJSONElemAtIndex 0 ary
1318− <*> parseJSONElemAtIndex 1 ary
1319− <*> parseJSONElemAtIndex 2 ary
1320− <*> parseJSONElemAtIndex 3 ary
1321− <*> parseJSONElemAtIndex 4 ary
1322− <*> parseJSONElemAtIndex 5 ary
1323− <*> parseJSONElemAtIndex 6 ary
1324− <*> parseJSONElemAtIndex 7 ary
1325− <*> parseJSONElemAtIndex 8 ary
1326− <*> parseJSONElemAtIndex 9 ary
1327− <*> parseJSONElemAtIndex 10 ary
1328− <*> parseJSONElemAtIndex 11 ary
1329− {-# INLINE parseJSON #-}
1331−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1332− ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l,
1334− ToJSON (a,b,c,d,e,f,g,h,i,j,k,l,m) where
1335− toJSON (a,b,c,d,e,f,g,h,i,j,k,l,m) = Array $ V.create $ do
1336− mv <- VM.unsafeNew 13
1337− VM.unsafeWrite mv 0 (toJSON a)
1338− VM.unsafeWrite mv 1 (toJSON b)
1339− VM.unsafeWrite mv 2 (toJSON c)
1340− VM.unsafeWrite mv 3 (toJSON d)
1341− VM.unsafeWrite mv 4 (toJSON e)
1342− VM.unsafeWrite mv 5 (toJSON f)
1343− VM.unsafeWrite mv 6 (toJSON g)
1344− VM.unsafeWrite mv 7 (toJSON h)
1345− VM.unsafeWrite mv 8 (toJSON i)
1346− VM.unsafeWrite mv 9 (toJSON j)
1347− VM.unsafeWrite mv 10 (toJSON k)
1348− VM.unsafeWrite mv 11 (toJSON l)
1349− VM.unsafeWrite mv 12 (toJSON m)
1351− {-# INLINE toJSON #-}
1353− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m) = tuple $
1367− {-# INLINE toEncoding #-}
1369−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1370− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1371− FromJSON k, FromJSON l, FromJSON m) =>
1372− FromJSON (a,b,c,d,e,f,g,h,i,j,k,l,m) where
1161+instance FromJSON a => FromJSON (Dual a) where
1373− parseJSON = withArray "(a,b,c,d,e,f,g,h,i,j,k,l,m)" $ \ary ->
1162+ parseJSON = parseJSON1
1374− let n = V.length ary
1376− then fail $ "cannot unpack array of length " ++
1377− show n ++ " into a 13-tuple"
1378− else (,,,,,,,,,,,,)
1379− <$> parseJSONElemAtIndex 0 ary
1380− <*> parseJSONElemAtIndex 1 ary
1381− <*> parseJSONElemAtIndex 2 ary
1382− <*> parseJSONElemAtIndex 3 ary
1383− <*> parseJSONElemAtIndex 4 ary
1384− <*> parseJSONElemAtIndex 5 ary
1385− <*> parseJSONElemAtIndex 6 ary
1386− <*> parseJSONElemAtIndex 7 ary
1387− <*> parseJSONElemAtIndex 8 ary
1388− <*> parseJSONElemAtIndex 9 ary
1389− <*> parseJSONElemAtIndex 10 ary
1390− <*> parseJSONElemAtIndex 11 ary
1391− <*> parseJSONElemAtIndex 12 ary
1392 {-# INLINE parseJSON #-}
1163 {-# INLINE parseJSON #-}
1394−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1395− ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l,
1396− ToJSON m, ToJSON n) =>
1397− ToJSON (a,b,c,d,e,f,g,h,i,j,k,l,m,n) where
1398− toJSON (a,b,c,d,e,f,g,h,i,j,k,l,m,n) = Array $ V.create $ do
1399− mv <- VM.unsafeNew 14
1400− VM.unsafeWrite mv 0 (toJSON a)
1401− VM.unsafeWrite mv 1 (toJSON b)
1402− VM.unsafeWrite mv 2 (toJSON c)
1403− VM.unsafeWrite mv 3 (toJSON d)
1404− VM.unsafeWrite mv 4 (toJSON e)
1405− VM.unsafeWrite mv 5 (toJSON f)
1406− VM.unsafeWrite mv 6 (toJSON g)
1407− VM.unsafeWrite mv 7 (toJSON h)
1408− VM.unsafeWrite mv 8 (toJSON i)
1409− VM.unsafeWrite mv 9 (toJSON j)
1410− VM.unsafeWrite mv 10 (toJSON k)
1411− VM.unsafeWrite mv 11 (toJSON l)
1412− VM.unsafeWrite mv 12 (toJSON m)
1413− VM.unsafeWrite mv 13 (toJSON n)
1415− {-# INLINE toJSON #-}
1417− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m,n) = tuple $
1166+instance ToJSON1 First where
1418− builder a >*<
1167+ liftToJSON to to' = liftToJSON to to' . getFirst
1419− builder b >*<
1168+ {-# INLINE liftToJSON #-}
1432− {-# INLINE toEncoding #-}
1434−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1170+ liftToEncoding to to' = liftToEncoding to to' . getFirst
1435− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1171+ {-# INLINE liftToEncoding #-}
1436− FromJSON k, FromJSON l, FromJSON m, FromJSON n) =>
1437− FromJSON (a,b,c,d,e,f,g,h,i,j,k,l,m,n) where
1438− parseJSON = withArray "(a,b,c,d,e,f,g,h,i,j,k,l,m,n)" $ \ary ->
1439− let n = V.length ary
1441− then fail $ "cannot unpack array of length " ++
1442− show n ++ " into a 14-tuple"
1443− else (,,,,,,,,,,,,,)
1444− <$> parseJSONElemAtIndex 0 ary
1445− <*> parseJSONElemAtIndex 1 ary
1446− <*> parseJSONElemAtIndex 2 ary
1447− <*> parseJSONElemAtIndex 3 ary
1448− <*> parseJSONElemAtIndex 4 ary
1449− <*> parseJSONElemAtIndex 5 ary
1450− <*> parseJSONElemAtIndex 6 ary
1451− <*> parseJSONElemAtIndex 7 ary
1452− <*> parseJSONElemAtIndex 8 ary
1453− <*> parseJSONElemAtIndex 9 ary
1454− <*> parseJSONElemAtIndex 10 ary
1455− <*> parseJSONElemAtIndex 11 ary
1456− <*> parseJSONElemAtIndex 12 ary
1457− <*> parseJSONElemAtIndex 13 ary
1458− {-# INLINE parseJSON #-}
1460−instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e, ToJSON f,
1461− ToJSON g, ToJSON h, ToJSON i, ToJSON j, ToJSON k, ToJSON l,
1462− ToJSON m, ToJSON n, ToJSON o) =>
1463− ToJSON (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) where
1173+instance ToJSON a => ToJSON (First a) where
1464− toJSON (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) = Array $ V.create $ do
1174+ toJSON = toJSON1
1465− mv <- VM.unsafeNew 15
1466− VM.unsafeWrite mv 0 (toJSON a)
1467− VM.unsafeWrite mv 1 (toJSON b)
1468− VM.unsafeWrite mv 2 (toJSON c)
1469− VM.unsafeWrite mv 3 (toJSON d)
1470− VM.unsafeWrite mv 4 (toJSON e)
1471− VM.unsafeWrite mv 5 (toJSON f)
1472− VM.unsafeWrite mv 6 (toJSON g)
1473− VM.unsafeWrite mv 7 (toJSON h)
1474− VM.unsafeWrite mv 8 (toJSON i)
1475− VM.unsafeWrite mv 9 (toJSON j)
1476− VM.unsafeWrite mv 10 (toJSON k)
1477− VM.unsafeWrite mv 11 (toJSON l)
1478− VM.unsafeWrite mv 12 (toJSON m)
1479− VM.unsafeWrite mv 13 (toJSON n)
1480− VM.unsafeWrite mv 14 (toJSON o)
1482 {-# INLINE toJSON #-}
1175 {-# INLINE toJSON #-}
1484− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) = tuple $
1177+ toEncoding = toEncoding1
1500 {-# INLINE toEncoding #-}
1178 {-# INLINE toEncoding #-}
1502−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1180+instance FromJSON1 First where
1503− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1181+ liftParseJSON p p' = fmap First . liftParseJSON p p'
1504− FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) =>
1182+ {-# INLINE liftParseJSON #-}
1505− FromJSON (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) where
1506− parseJSON = withArray "(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o)" $ \ary ->
1507− let n = V.length ary
1509− then fail $ "cannot unpack array of length " ++
1510− show n ++ " into a 15-tuple"
1511− else (,,,,,,,,,,,,,,)
1512− <$> parseJSONElemAtIndex 0 ary
1513− <*> parseJSONElemAtIndex 1 ary
1514− <*> parseJSONElemAtIndex 2 ary
1515− <*> parseJSONElemAtIndex 3 ary
1516− <*> parseJSONElemAtIndex 4 ary
1517− <*> parseJSONElemAtIndex 5 ary
1518− <*> parseJSONElemAtIndex 6 ary
1519− <*> parseJSONElemAtIndex 7 ary
1520− <*> parseJSONElemAtIndex 8 ary
1521− <*> parseJSONElemAtIndex 9 ary
1522− <*> parseJSONElemAtIndex 10 ary
1523− <*> parseJSONElemAtIndex 11 ary
1524− <*> parseJSONElemAtIndex 12 ary
1525− <*> parseJSONElemAtIndex 13 ary
1526− <*> parseJSONElemAtIndex 14 ary
1527− {-# INLINE parseJSON #-}
1529−instance ToJSON a => ToJSON (Dual a) where
1530− toJSON = toJSON . getDual
1531− {-# INLINE toJSON #-}
1533− toEncoding = toEncoding . getDual
1534− {-# INLINE toEncoding #-}
1536−instance FromJSON a => FromJSON (Dual a) where
1184+instance FromJSON a => FromJSON (First a) where
1537− parseJSON = fmap Dual . parseJSON
1185+ parseJSON = parseJSON1
1538 {-# INLINE parseJSON #-}
1186 {-# INLINE parseJSON #-}
1540−instance ToJSON a => ToJSON (First a) where
1541− toJSON = toJSON . getFirst
1542− {-# INLINE toJSON #-}
1544− toEncoding = toEncoding . getFirst
1189+instance ToJSON1 Last where
1545− {-# INLINE toEncoding #-}
1190+ liftToJSON to to' = liftToJSON to to' . getLast
1191+ {-# INLINE liftToJSON #-}
1547−instance FromJSON a => FromJSON (First a) where
1193+ liftToEncoding to to' = liftToEncoding to to' . getLast
1548− parseJSON = fmap First . parseJSON
1194+ {-# INLINE liftToEncoding #-}
1549− {-# INLINE parseJSON #-}
1551instance ToJSON a => ToJSON (Last a) where
1196instance ToJSON a => ToJSON (Last a) where
1552− toJSON = toJSON . getLast
1197+ toJSON = toJSON1
1553 {-# INLINE toJSON #-}
1198 {-# INLINE toJSON #-}
1555− toEncoding = toEncoding . getLast
1200+ toEncoding = toEncoding1
1556 {-# INLINE toEncoding #-}
1201 {-# INLINE toEncoding #-}
1203+instance FromJSON1 Last where
1204+ liftParseJSON p p' = fmap Last . liftParseJSON p p'
1205+ {-# INLINE liftParseJSON #-}
1558instance FromJSON a => FromJSON (Last a) where
1207instance FromJSON a => FromJSON (Last a) where
1559− parseJSON = fmap Last . parseJSON
1208+ parseJSON = parseJSON1
1560 {-# INLINE parseJSON #-}
1209 {-# INLINE parseJSON #-}
1562−instance ToJSON Version where
1211+-------------------------------------------------------------------------------
1563− toJSON = toJSON . showVersion
1212+-- tagged
1564− {-# INLINE toJSON #-}
1213+-------------------------------------------------------------------------------
1566− toEncoding = toEncoding . showVersion
1567− {-# INLINE toEncoding #-}
1569−instance FromJSON Version where
1570− {-# INLINE parseJSON #-}
1571− parseJSON = withText "Version" $ go . readP_to_S parseVersion . unpack
1573− go [(v,[])] = return v
1574− go (_ : xs) = go xs
1575− go _ = fail $ "could not parse Version"
1577instance ToJSON (Proxy a) where
1215instance ToJSON (Proxy a) where
1578 toJSON _ = Null
1216 toJSON _ = Null
@@ -1586,46 +1224,42 @@ instance FromJSON (Proxy a) where
1586 parseJSON Null = pure Proxy
1224 parseJSON Null = pure Proxy
1587 parseJSON v = typeMismatch "Proxy" v
1225 parseJSON v = typeMismatch "Proxy" v
1228+instance ToJSON1 (Tagged a) where
1229+ liftToJSON to _ (Tagged x) = to x
1230+ {-# INLINE liftToJSON #-}
1232+ liftToEncoding to _ (Tagged x) = to x
1233+ {-# INLINE liftToEncoding #-}
1589instance ToJSON b => ToJSON (Tagged a b) where
1235instance ToJSON b => ToJSON (Tagged a b) where
1590− toJSON (Tagged x) = toJSON x
1236+ toJSON = toJSON1
1591 {-# INLINE toJSON #-}
1237 {-# INLINE toJSON #-}
1593− toEncoding (Tagged x) = toEncoding x
1239+ toEncoding = toEncoding1
1594 {-# INLINE toEncoding #-}
1240 {-# INLINE toEncoding #-}
1242+instance FromJSON1 (Tagged a) where
1243+ liftParseJSON p _ = fmap Tagged . p
1244+ {-# INLINE liftParseJSON #-}
1596instance FromJSON b => FromJSON (Tagged a b) where
1246instance FromJSON b => FromJSON (Tagged a b) where
1247+ parseJSON = parseJSON1
1597 {-# INLINE parseJSON #-}
1248 {-# INLINE parseJSON #-}
1598− parseJSON = fmap Tagged . parseJSON
1600−instance ToJSON a => ToJSON (Const a b) where
1250+instance FromJSONKey b => FromJSONKey (Tagged a b) where
1601− toJSON (Const x) = toJSON x
1251+ fromJSONKey = coerceFromJSONKeyFunction (fromJSONKey :: FromJSONKeyFunction b)
1602− {-# INLINE toJSON #-}
1252+ fromJSONKeyList = (fmap . fmap) Tagged fromJSONKeyList
1604− toEncoding (Const x) = toEncoding x
1605− {-# INLINE toEncoding #-}
1607−instance FromJSON a => FromJSON (Const a b) where
1254+instance ToJSONKey b => ToJSONKey (Tagged a b) where
1608− {-# INLINE parseJSON #-}
1255+ toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
1609− parseJSON = fmap Const . parseJSON
1256+ toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
1611−--------------------------------------------
1258+-------------------------------------------------------------------------------
1612-- Instances for converting to/from map keys
1259-- Instances for converting to/from map keys
1613−--------------------------------------------
1260+-------------------------------------------------------------------------------
1614−class ToJSONKey a where
1261+
1615− toJSONKey :: ToJSONKeyFunction a
1262+
1616− default toJSONKey :: ToJSON a => ToJSONKeyFunction a
1617− toJSONKey = ToJSONKeyValue (toJSON, toEncoding)
1618− toJSONKeyList :: ToJSONKeyFunction [a]
1619− default toJSONKeyList :: ToJSON a => ToJSONKeyFunction [a]
1620− toJSONKeyList = ToJSONKeyValue (toJSON, toEncoding)
1622−class FromJSONKey a where
1623− fromJSONKey :: FromJSONKeyFunction a
1624− default fromJSONKey :: FromJSON a => FromJSONKeyFunction a
1625− fromJSONKey = FromJSONKeyValue parseJSON
1626− fromJSONKeyList :: FromJSONKeyFunction [a]
1627− default fromJSONKeyList :: FromJSON a => FromJSONKeyFunction [a]
1628− fromJSONKeyList = FromJSONKeyValue parseJSON
1630instance ToJSONKey Text where
1264instance ToJSONKey Text where
1631 toJSONKey = ToJSONKeyText (id,toEncoding)
1265 toJSONKey = ToJSONKeyText (id,toEncoding)
@@ -1633,10 +1267,7 @@ instance ToJSONKey Text where
1633instance FromJSONKey Text where
1267instance FromJSONKey Text where
1634 fromJSONKey = fromJSONKeyCoerce
1268 fromJSONKey = fromJSONKeyCoerce
1636−-- | TODO: where ToJSONKey instance
1270+
1637−instance FromJSONKey b => FromJSONKey (Tagged a b) where
1638− fromJSONKey = coerceFromJSONKeyFunction (fromJSONKey :: FromJSONKeyFunction b)
1639− fromJSONKeyList = (fmap . fmap) Tagged fromJSONKeyList
1641instance ToJSONKey Bool where
1272instance ToJSONKey Bool where
1642 toJSONKey = ToJSONKeyText
1273 toJSONKey = ToJSONKeyText
@@ -1690,13 +1321,9 @@ instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
1690instance (FromJSONKey a, FromJSON a) => FromJSONKey (Identity a) where
1321instance (FromJSONKey a, FromJSON a) => FromJSONKey (Identity a) where
1691 fromJSONKey = mapFromJSONKeyFunction Identity fromJSONKey
1322 fromJSONKey = mapFromJSONKeyFunction Identity fromJSONKey
1693−contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
1324+-------------------------------------------------------------------------------
1694−contramapToJSONKeyFunction h x = case x of
1325+-- Functions
1695− ToJSONKeyText (f,g) -> ToJSONKeyText (f . h, g . h)
1326+-------------------------------------------------------------------------------
1696− ToJSONKeyValue (f,g) -> ToJSONKeyValue (f . h, g . h)
1698−mapFromJSONKeyFunction :: (a -> b) -> FromJSONKeyFunction a -> FromJSONKeyFunction b
1699−mapFromJSONKeyFunction = fmap
1701-- | @withObject expected f value@ applies @f@ to the 'Object' when @value@ is an @Object@
1328-- | @withObject expected f value@ applies @f@ to the 'Object' when @value@ is an @Object@
1702-- and fails using @'typeMismatch' expected@ otherwise.
1329-- and fails using @'typeMismatch' expected@ otherwise.
@@ -1819,34 +1446,3 @@ obj .:! key = case H.lookup key obj of
1819(.!=) :: Parser (Maybe a) -> a -> Parser a
1446(.!=) :: Parser (Maybe a) -> a -> Parser a
1820pmval .!= val = fromMaybe val <$> pmval
1447pmval .!= val = fromMaybe val <$> pmval
1821{-# INLINE (.!=) #-}
1448{-# INLINE (.!=) #-}
1823−realFloatToJSON :: RealFloat a => a -> Value
1825− | isNaN d || isInfinite d = Null
1826− | otherwise = Number $ Scientific.fromFloatDigits d
1827−{-# INLINE realFloatToJSON #-}
1829−realFloatToEncoding :: RealFloat a => a -> Encoding
1830−realFloatToEncoding d
1831− | isNaN d || isInfinite d = Encoding E.null_
1832− | otherwise = toEncoding (Scientific.fromFloatDigits d)
1833−{-# INLINE realFloatToEncoding #-}
1835−scientificToNumber :: Scientific -> Number
1836−scientificToNumber s
1837− | e < 0 = D $ Scientific.toRealFloat s
1838− | otherwise = I $ c * 10 ^ e
1840− e = Scientific.base10Exponent s
1841− c = Scientific.coefficient s
1842−{-# INLINE scientificToNumber #-}
1844−parseRealFloat :: RealFloat a => String -> Value -> Parser a
1845−parseRealFloat _ (Number s) = pure $ Scientific.toRealFloat s
1846−parseRealFloat _ Null = pure (0/0)
1847−parseRealFloat expected v = typeMismatch expected v
1848−{-# INLINE parseRealFloat #-}
1850−parseIntegral :: Integral a => String -> Value -> Parser a
1851−parseIntegral expected = withScientific expected $ pure . truncate
1852−{-# INLINE parseIntegral #-}