@@ -9,9 +9,6 @@
99{-# LANGUAGE PolyKinds #-}
1010#endif
1111
12−#define NEEDS_INCOHERENT
13−#include "overlapping-compat.h"
14−
1512{-# OPTIONS_GHC -fno-warn-orphans #-}
1613
1714-- TODO: Drop this when we remove support for Data.Attoparsec.Number
@@ -38,10 +35,23 @@ module Data.Aeson.Types.Instances
3835 -- ** Keys for maps
3936 , ToJSONKey(..)
4037 , ToJSONKeyFunction(..)
38+ , contramapToJSONKeyFunction
4139 , FromJSONKey(..)
4240 , FromJSONKeyFunction(..)
4341 , fromJSONKeyCoerce
4442 , coerceFromJSONKeyFunction
43+ , mapFromJSONKeyFunction
44+ -- ** Liftings to unary and binary type constructors
45+ , FromJSON1(..)
46+ , parseJSON1
47+ , FromJSON2(..)
48+ , parseJSON2
49+ , ToJSON1(..)
50+ , toJSON1
51+ , toEncoding1
52+ , ToJSON2(..)
53+ , toJSON2
54+ , toEncoding2
4555 -- ** Generic JSON classes
4656 , GFromJSON(..)
4757 , GToJSON(..)
@@ -71,10 +81,16 @@ module Data.Aeson.Types.Instances
7181 , tuple
7282 , (>*<)
7383 , typeMismatch
84+
85+ , listEncoding
86+ , listValue
87+ , listParser
7488 ) where
7589
90+import Data.Aeson.Types.Instances.Tuple (tuple, (>*<))
91+
7692import 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)
7894import Data.Aeson.Functions (mapHashKeyVal, mapKey, mapKeyVal)
7995import Data.Aeson.Types.Class
8096import Data.Aeson.Types.Internal
@@ -92,7 +108,7 @@ import Data.Proxy (Proxy(..))
92108import Data.Ratio (Ratio, (%), numerator, denominator)
93109import Data.Scientific (Scientific)
94110import Data.Tagged (Tagged(..))
95−import Data.Text (Text, pack, unpack)
111+import Data.Text (Text, unpack)
96112import Data.Time (Day, LocalTime, NominalDiffTime, TimeOfDay, UTCTime,
97113 ZonedTime)
98114import Data.Time.Format (FormatTime, formatTime, parseTime)
@@ -112,6 +128,7 @@ import qualified Data.HashMap.Strict as H
112128import qualified Data.HashSet as HashSet
113129import qualified Data.IntMap as IntMap
114130import qualified Data.IntSet as IntSet
131+import qualified Data.List.NonEmpty as NE
115132import qualified Data.Map as M
116133import qualified Data.Scientific as Scientific
117134import qualified Data.Sequence as Seq
@@ -125,10 +142,10 @@ import qualified Data.Text.Lazy.Builder.Int as LTBI
125142import qualified Data.Tree as Tree
126143import qualified Data.Vector as V
127144import qualified Data.Vector.Generic as VG
128−import qualified Data.Vector.Mutable as VM (unsafeNew, unsafeWrite)
129145import qualified Data.Vector.Primitive as VP
130146import qualified Data.Vector.Storable as VS
131147import qualified Data.Vector.Unboxed as VU
148+import qualified Data.Vector.Mutable as VM
132149
133150import Unsafe.Coerce (unsafeCoerce)
134151
@@ -145,77 +162,209 @@ import Data.Time.Format (defaultTimeLocale)
145162import System.Locale (defaultTimeLocale)
146163#endif
147164
148−parseIndexedJSON :: FromJSON a => Int -> Value -> Parser a
149−parseIndexedJSON idx value = parseJSON value <?> Index idx
165+-------------------------------------------------------------------------------
166+-- Helpers
167+-------------------------------------------------------------------------------
168+
169+parseIndexedJSON :: (Value -> Parser a) -> Int -> Value -> Parser a
170+parseIndexedJSON p idx value = p value <?> Index idx
171+{-# INLINE parseIndexedJSON #-}
150172
151−parseIndexedJSONPair :: FromJSON b => (Value -> Parser a) -> Int -> Value -> Parser (a, b)
152−parseIndexedJSONPair keyParser idx value = p value <?> Index idx
173+parseIndexedJSONPair :: (Value -> Parser a) -> (Value -> Parser b) -> Int -> Value -> Parser (a, b)
174+parseIndexedJSONPair keyParser valParser idx value = p value <?> Index idx
153175 where
154176 p = withArray "(k,v)" $ \ab ->
155177 let n = V.length ab
156178 in if n == 2
157− then (,) <$> parseJSONElemAtIndex' keyParser 0 ab
158− <*> parseJSONElemAtIndex 1 ab
179+ then (,) <$> parseJSONElemAtIndex keyParser 0 ab
180+ <*> parseJSONElemAtIndex valParser 1 ab
159181 else fail $ "cannot unpack array of length " ++
160182 show n ++ " into a pair"
161183
162−toJSONPair :: ToJSON b => (a -> Value) -> (a, b) -> Value
163−toJSONPair keySerialiser (a, b) = Array $ V.create $ do
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 #-}
187+
188+toJSONPair :: (a -> Value) -> (b -> Value) -> (a, b) -> Value
189+toJSONPair keySerialiser valSerializer (a, b) = Array $ V.create $ do
164190 mv <- VM.unsafeNew 2
165191 VM.unsafeWrite mv 0 (keySerialiser a)
166− VM.unsafeWrite mv 1 (toJSON b)
192+ VM.unsafeWrite mv 1 (valSerializer b)
167193 return mv
194+{-# INLINE toJSONPair #-}
195+
196+realFloatToJSON :: RealFloat a => a -> Value
197+realFloatToJSON d
198+ | isNaN d || isInfinite d = Null
199+ | otherwise = Number $ Scientific.fromFloatDigits d
200+{-# INLINE realFloatToJSON #-}
201+
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 #-}
207+
208+scientificToNumber :: Scientific -> Number
209+scientificToNumber s
210+ | e < 0 = D $ Scientific.toRealFloat s
211+ | otherwise = I $ c * 10 ^ e
212+ where
213+ e = Scientific.base10Exponent s
214+ c = Scientific.coefficient s
215+{-# INLINE scientificToNumber #-}
216+
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 #-}
222+
223+parseIntegral :: Integral a => String -> Value -> Parser a
224+parseIntegral expected = withScientific expected $ pure . truncate
225+{-# INLINE parseIntegral #-}
226+
227+-------------------------------------------------------------------------------
228+-- base
229+-------------------------------------------------------------------------------
230+
231+instance ToJSON1 Identity where
232+ liftToJSON to _ (Identity a) = to a
233+ {-# INLINE liftToJSON #-}
234+
235+ liftToEncoding to _ (Identity a) = to a
236+ {-# INLINE liftToEncoding #-}
168237
169238instance (ToJSON a) => ToJSON (Identity a) where
170− toJSON (Identity a) = toJSON a
239+ toJSON = toJSON1
171240 {-# INLINE toJSON #-}
172241
173− toEncoding (Identity a) = toEncoding a
242+ toEncoding = toEncoding1
174243 {-# INLINE toEncoding #-}
175244
245+instance FromJSON1 Identity where
246+ liftParseJSON p _ a = Identity <$> p a
247+ {-# INLINE liftParseJSON #-}
248+
176249instance (FromJSON a) => FromJSON (Identity a) where
177− parseJSON a = Identity <$> parseJSON a
250+ parseJSON = parseJSON1
178251 {-# INLINE parseJSON #-}
179252
253+
254+instance ToJSON2 Const where
255+ liftToJSON2 to _ _ _ (Const x) = to x
256+ {-# INLINE liftToJSON2 #-}
257+
258+ liftToEncoding2 to _ _ _ (Const x) = to x
259+ {-# INLINE liftToEncoding2 #-}
260+
261+instance FromJSON2 Const where
262+ liftParseJSON2 p _ _ _ = fmap Const . p
263+ {-# INLINE liftParseJSON2 #-}
264+
265+instance ToJSON a => ToJSON1 (Const a) where
266+ liftToJSON _ _ (Const x) = toJSON x
267+ {-# INLINE liftToJSON #-}
268+
269+ liftToEncoding _ _ (Const x) = toEncoding x
270+ {-# INLINE liftToEncoding #-}
271+
272+instance FromJSON a => FromJSON1 (Const a) where
273+ liftParseJSON _ _ = fmap Const . parseJSON
274+ {-# INLINE liftParseJSON #-}
275+
276+instance ToJSON a => ToJSON (Const a b) where
277+ toJSON (Const x) = toJSON x
278+ {-# INLINE toJSON #-}
279+
280+ toEncoding (Const x) = toEncoding x
281+ {-# INLINE toEncoding #-}
282+
283+instance FromJSON a => FromJSON (Const a b) where
284+ {-# INLINE parseJSON #-}
285+ parseJSON = fmap Const . parseJSON
286+
287+
288+instance ToJSON1 Maybe where
289+ liftToJSON to _ (Just a) = to a
290+ liftToJSON _ _ Nothing = Null
291+ {-# INLINE liftToJSON #-}
292+
293+ liftToEncoding to _ (Just a) = to a
294+ liftToEncoding _ _ Nothing = Encoding E.null_
295+ {-# INLINE liftToEncoding #-}
296+
180297instance (ToJSON a) => ToJSON (Maybe a) where
181− toJSON (Just a) = toJSON a
182− toJSON Nothing = Null
298+ toJSON = toJSON1
183299 {-# INLINE toJSON #-}
184300
185− toEncoding (Just a) = toEncoding a
186− toEncoding Nothing = Encoding E.null_
301+ toEncoding = toEncoding1
187302 {-# INLINE toEncoding #-}
188303
304+instance FromJSON1 Maybe where
305+ liftParseJSON _ _ Null = pure Nothing
306+ liftParseJSON p _ a = Just <$> p a
307+ {-# INLINE liftParseJSON #-}
308+
189309instance (FromJSON a) => FromJSON (Maybe a) where
190− parseJSON Null = pure Nothing
191− parseJSON a = Just <$> parseJSON a
310+ parseJSON = parseJSON1
192311 {-# INLINE parseJSON #-}
193312
313+
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 #-}
318+
319+ liftToEncoding2 toA _ _toB _ (Left a) =
320+ Encoding (B.shortByteString "{\"Left\":")
321+ <> toA a
322+ <> Encoding (B.char7 '}')
323+
324+ liftToEncoding2 _toA _ toB _ (Right b) =
325+ Encoding (B.shortByteString "{\"Right\":")
326+ <> toB b
327+ <> Encoding (B.char7 '}')
328+ {-# INLINE liftToEncoding2 #-}
329+
330+instance (ToJSON a) => ToJSON1 (Either a) where
331+ liftToJSON = liftToJSON2 toJSON toJSONList
332+ {-# INLINE liftToJSON #-}
333+
334+ liftToEncoding = liftToEncoding2 toEncoding toEncodingList
335+ {-# INLINE liftToEncoding #-}
336+
194337instance (ToJSON a, ToJSON b) => ToJSON (Either a b) where
195− toJSON (Left a) = object [left .= a]
196− toJSON (Right b) = object [right .= b]
338+ toJSON = toJSON2
197339 {-# INLINE toJSON #-}
198340
199− toEncoding (Left a) = Encoding $
200− B.shortByteString "{\"Left\":" <> builder a <> B.char7 '}'
201− toEncoding (Right a) = Encoding $
202− B.shortByteString "{\"Right\":" <> builder a <> B.char7 '}'
341+ toEncoding = toEncoding2
203342 {-# INLINE toEncoding #-}
204343
205−instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
206− parseJSON (Object (H.toList -> [(key, value)]))
207− | key == left = Left <$> parseJSON value <?> Key left
208− | key == right = Right <$> parseJSON value <?> Key right
209− parseJSON _ = fail $
344+instance FromJSON2 Either where
345+ liftParseJSON2 pA _ pB _ (Object (H.toList -> [(key, value)]))
346+ | key == left = Left <$> pA value <?> Key left
347+ | key == right = Right <$> pB value <?> Key right
348+
349+ liftParseJSON2 _ _ _ _ _ = fail $
210350 "expected an object with a single property " ++
211351 "where the property key should be either " ++
212352 "\"Left\" or \"Right\""
353+ {-# INLINE liftParseJSON2 #-}
354+
355+instance (FromJSON a) => FromJSON1 (Either a) where
356+ liftParseJSON = liftParseJSON2 parseJSON parseJSONList
357+ {-# INLINE liftParseJSON #-}
358+
359+instance (FromJSON a, FromJSON b) => FromJSON (Either a b) where
360+ parseJSON = parseJSON2
213361 {-# INLINE parseJSON #-}
214362
215363left, right :: Text
216364left = "Left"
217365right = "Right"
218366
367+
219368instance ToJSON Bool where
220369 toJSON = Bool
221370 {-# INLINE toJSON #-}
@@ -259,24 +408,19 @@ instance FromJSON () where
259408 else fail "Expected an empty array"
260409 {-# INLINE parseJSON #-}
261410
262−instance INCOHERENT_ ToJSON [Char] where
263− toJSON = String . T.pack
264− {-# INLINE toJSON #-}
265−
266− toEncoding = Encoding . E.string
267− {-# INLINE toEncoding #-}
268−
269−instance INCOHERENT_ FromJSON [Char] where
270− parseJSON = withText "String" $ pure . T.unpack
271− {-# INLINE parseJSON #-}
272−
273411instance ToJSON Char where
274412 toJSON = String . T.singleton
275413 {-# INLINE toJSON #-}
276414
415+ toJSONList = String . T.pack
416+ {-# INLINE toJSONList #-}
417+
277418 toEncoding = Encoding . E.string . (:[])
278419 {-# INLINE toEncoding #-}
279420
421+ toEncodingList = Encoding . E.string
422+ {-# INLINE toEncodingList #-}
423+
280424instance FromJSON Char where
281425 parseJSON = withText "Char" $ \t ->
282426 if T.compareLength t 1 == EQ
@@ -284,16 +428,8 @@ instance FromJSON Char where
284428 else fail "Expected a string of length 1"
285429 {-# INLINE parseJSON #-}
286430
287−instance ToJSON Scientific where
288− toJSON = Number
289− {-# INLINE toJSON #-}
290−
291− toEncoding = Encoding . E.number
292− {-# INLINE toEncoding #-}
293−
294−instance FromJSON Scientific where
295− parseJSON = withScientific "Scientific" pure
296− {-# INLINE parseJSON #-}
431+ parseJSONList = withText "String" $ pure . T.unpack
432+ {-# INLINE parseJSONList #-}
297433
298434instance ToJSON Double where
299435 toJSON = realFloatToJSON
@@ -527,45 +663,253 @@ instance FromJSON LT.Text where
527663 parseJSON = withText "Lazy Text" $ pure . LT.fromStrict
528664 {-# INLINE parseJSON #-}
529665
666+instance ToJSON Version where
667+ toJSON = toJSON . showVersion
668+ {-# INLINE toJSON #-}
669+
670+ toEncoding = toEncoding . showVersion
671+ {-# INLINE toEncoding #-}
672+
673+instance FromJSON Version where
674+ {-# INLINE parseJSON #-}
675+ parseJSON = withText "Version" $ go . readP_to_S parseVersion . unpack
676+ where
677+ go [(v,[])] = return v
678+ go (_ : xs) = go xs
679+ go _ = fail $ "could not parse Version"
680+
681+-------------------------------------------------------------------------------
682+-- semigroups NonEmpty
683+-------------------------------------------------------------------------------
684+
685+instance ToJSON1 NonEmpty where
686+ liftToJSON to _ = listValue to . NE.toList
687+ {-# INLINE liftToJSON #-}
688+
689+ liftToEncoding to _ = listEncoding to . NE.toList
690+ {-# INLINE liftToEncoding #-}
691+
530692instance (ToJSON a) => ToJSON (NonEmpty a) where
531− toJSON = toJSON . toList
693+ toJSON = toJSON1
532694 {-# INLINE toJSON #-}
533695
534− toEncoding = toEncoding . toList
696+ toEncoding = toEncoding1
535697 {-# INLINE toEncoding #-}
536698
537−instance (FromJSON a) => FromJSON (NonEmpty a) where
538− parseJSON = withArray "NonEmpty a" $
539− (>>= ne) . Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
699+instance FromJSON1 NonEmpty where
700+ liftParseJSON p _ = withArray "NonEmpty a" $
701+ (>>= ne) . Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
540702 where
541703 ne [] = fail "Expected a NonEmpty but got an empty list"
542704 ne (x:xs) = pure (x :| xs)
705+ {-# INLINE liftParseJSON #-}
543706
544−instance OVERLAPPABLE_ (ToJSON a) => ToJSON [a] where
545− toJSON = Array . V.fromList . map toJSON
707+instance (FromJSON a) => FromJSON (NonEmpty a) where
708+ parseJSON = parseJSON1
709+ {-# INLINE parseJSON #-}
710+
711+-------------------------------------------------------------------------------
712+-- scientific
713+-------------------------------------------------------------------------------
714+
715+instance ToJSON Scientific where
716+ toJSON = Number
546717 {-# INLINE toJSON #-}
547718
548− toEncoding xs = list xs
719+ toEncoding = Encoding . E.number
549720 {-# INLINE toEncoding #-}
550721
551−instance OVERLAPPABLE_ (FromJSON a) => FromJSON [a] where
552− parseJSON = withArray "[a]" $ Tr.sequence .
553− zipWith parseIndexedJSON [0..] . V.toList
722+instance FromJSON Scientific where
723+ parseJSON = withScientific "Scientific" pure
554724 {-# INLINE parseJSON #-}
555725
726+-------------------------------------------------------------------------------
727+-- containers
728+-------------------------------------------------------------------------------
729+
730+instance ToJSON1 Seq.Seq where
731+ liftToJSON to _ = listValue to . toList
732+ {-# INLINE liftToJSON #-}
733+
734+ liftToEncoding to _ = listEncoding to . toList
735+ {-# INLINE liftToEncoding #-}
736+
556737instance (ToJSON a) => ToJSON (Seq.Seq a) where
557− toJSON = toJSON . toList
738+ toJSON = toJSON1
558739 {-# INLINE toJSON #-}
559740
560− toEncoding = foldable
741+ toEncoding = toEncoding1
561742 {-# INLINE toEncoding #-}
562743
563−instance (FromJSON a) => FromJSON (Seq.Seq a) where
564− parseJSON = withArray "Seq a" $
744+instance FromJSON1 Seq.Seq where
745+ liftParseJSON p _ = withArray "Seq a" $
565746 fmap Seq.fromList .
566− Tr.sequence . zipWith parseIndexedJSON [0..] . V.toList
747+ Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
748+ {-# INLINE liftParseJSON #-}
749+
750+instance (FromJSON a) => FromJSON (Seq.Seq a) where
751+ parseJSON = parseJSON1
752+ {-# INLINE parseJSON #-}
753+
754+
755+instance ToJSON1 Set.Set where
756+ liftToJSON to _ = listValue to . Set.toList
757+ {-# INLINE liftToJSON #-}
758+
759+ liftToEncoding to _ = listEncoding to . Set.toList
760+ {-# INLINE liftToEncoding #-}
761+
762+instance (ToJSON a) => ToJSON (Set.Set a) where
763+ toJSON = toJSON1
764+ {-# INLINE toJSON #-}
765+
766+ toEncoding = toEncoding1
767+ {-# INLINE toEncoding #-}
768+
769+instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
770+ parseJSON = fmap Set.fromList . parseJSON
771+ {-# INLINE parseJSON #-}
772+
773+
774+instance FromJSON IntSet.IntSet where
775+ parseJSON = fmap IntSet.fromList . parseJSON
776+ {-# INLINE parseJSON #-}
777+
778+instance ToJSON IntSet.IntSet where
779+ toJSON = toJSON . IntSet.toList
780+ {-# INLINE toJSON #-}
781+
782+ toEncoding = toEncoding . IntSet.toList
783+ {-# INLINE toEncoding #-}
784+
785+
786+instance ToJSON1 IntMap.IntMap where
787+ liftToJSON to tol = liftToJSON to' tol' . IntMap.toList
788+ where
789+ to' = liftToJSON2 toJSON toJSONList to tol
790+ tol' = liftToJSONList2 toJSON toJSONList to tol
791+ {-# INLINE liftToJSON #-}
792+
793+ liftToEncoding to tol = liftToEncoding to' tol' . IntMap.toList
794+ where
795+ to' = liftToEncoding2 toEncoding toEncodingList to tol
796+ tol' = liftToEncodingList2 toEncoding toEncodingList to tol
797+ {-# INLINE liftToEncoding #-}
798+
799+instance ToJSON a => ToJSON (IntMap.IntMap a) where
800+ toJSON = toJSON1
801+ {-# INLINE toJSON #-}
802+
803+ toEncoding = toEncoding1
804+ {-# INLINE toEncoding #-}
805+
806+instance FromJSON1 IntMap.IntMap where
807+ liftParseJSON p pl = fmap IntMap.fromList . liftParseJSON p' pl'
808+ where
809+ p' = liftParseJSON2 parseJSON parseJSONList p pl
810+ pl' = liftParseJSONList2 parseJSON parseJSONList p pl
811+ {-# INLINE liftParseJSON #-}
812+
813+instance FromJSON a => FromJSON (IntMap.IntMap a) where
814+ parseJSON = fmap IntMap.fromList . parseJSON
815+ {-# INLINE parseJSON #-}
816+
817+
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 #-}
823+
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
828+ where
829+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
830+ pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder' g b
831+ {-# INLINE liftToEncoding #-}
832+
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 #-}
845+
846+instance (ToJSON v, ToJSONKey k) => ToJSON (M.Map k v) where
847+ toJSON = toJSON1
848+ {-# INLINE toJSON #-}
849+
850+ toEncoding = toEncoding1
851+ {-# INLINE toEncoding #-}
852+
853+instance (FromJSON v, FromJSONKey k, Ord k) => FromJSON (M.Map k v) where
854+ parseJSON = parseJSON1
567855 {-# INLINE parseJSON #-}
568856
857+
858+instance ToJSON1 Tree.Tree where
859+ liftToJSON to tol = go
860+ where
861+ go (Tree.Node root branches) =
862+ liftToJSON2 to tol to' tol' (root, branches)
863+
864+ to' = liftToJSON go (listValue go)
865+ tol' = liftToJSONList go (listValue go)
866+ {-# INLINE liftToJSON #-}
867+
868+ liftToEncoding to tol = go
869+ where
870+ go (Tree.Node root branches) =
871+ liftToEncoding2 to tol to' tol' (root, branches)
872+
873+ to' = liftToEncoding go (listEncoding go)
874+ tol' = liftToEncodingList go (listEncoding go)
875+ {-# INLINE liftToEncoding #-}
876+
877+instance (ToJSON v) => ToJSON (Tree.Tree v) where
878+ toJSON = toJSON1
879+ {-# INLINE toJSON #-}
880+
881+ toEncoding = toEncoding1
882+ {-# INLINE toEncoding #-}
883+
884+instance FromJSON1 Tree.Tree where
885+ liftParseJSON p pl = go
886+ where
887+ go v = uncurry Tree.Node <$> liftParseJSON2 p pl p' pl' v
888+
889+ p' = liftParseJSON go (listParser go)
890+ pl'= liftParseJSONList go (listParser go)
891+
892+instance (FromJSON v) => FromJSON (Tree.Tree v) where
893+ parseJSON = parseJSON1
894+ {-# INLINE parseJSON #-}
895+
896+-------------------------------------------------------------------------------
897+-- vector
898+-------------------------------------------------------------------------------
899+
900+instance ToJSON1 Vector where
901+ liftToJSON to _ = Array . V.map to
902+ {-# INLINE liftToJSON #-}
903+
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)
909+ where
910+ go v b = B.char7 ',' <> fromEncoding (to v) <> b
911+ {-# INLINE liftToEncoding #-}
912+
569913instance (ToJSON a) => ToJSON (Vector a) where
570914 toJSON = Array . V.map toJSON
571915 {-# INLINE toJSON #-}
@@ -582,9 +926,13 @@ encodeVector xs
582926 where go v b = B.char7 ',' <> builder v <> b
583927{-# INLINE encodeVector #-}
584928
929+instance FromJSON1 Vector where
930+ liftParseJSON p _ = withArray "Vector a" $
931+ V.mapM (uncurry $ parseIndexedJSON p) . V.indexed
932+ {-# INLINE liftParseJSON #-}
933+
585934instance (FromJSON a) => FromJSON (Vector a) where
586− parseJSON = withArray "Vector a" $ V.mapM (uncurry parseIndexedJSON) .
587− V.indexed
935+ parseJSON = parseJSON1
588936 {-# INLINE parseJSON #-}
589937
590938vectorToJSON :: (VG.Vector v a, ToJSON a) => v a -> Value
@@ -592,7 +940,7 @@ vectorToJSON = Array . V.map toJSON . V.convert
592940{-# INLINE vectorToJSON #-}
593941
594942vectorParseJSON :: (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
596944{-# INLINE vectorParseJSON #-}
597945
598946instance (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
627975 parseJSON = vectorParseJSON "Data.Vector.Unboxed.Vector a"
628976 {-# INLINE parseJSON #-}
629977
630−instance (ToJSON a) => ToJSON (Set.Set a) where
631− toJSON = toJSON . Set.toList
632− {-# INLINE toJSON #-}
978+-------------------------------------------------------------------------------
979+-- unordered-containers
980+-------------------------------------------------------------------------------
633981
634− toEncoding = encodeSet Set.minView Set.foldr
635− {-# INLINE toEncoding #-}
982+instance ToJSON1 HashSet.HashSet where
983+ liftToJSON to _ = listValue to . HashSet.toList
984+ {-# INLINE liftToJSON #-}
636985
637−instance (Ord a, FromJSON a) => FromJSON (Set.Set a) where
638− parseJSON = fmap Set.fromList . parseJSON
639− {-# INLINE parseJSON #-}
986+ liftToEncoding to _ = listEncoding to . HashSet.toList
987+ {-# INLINE liftToEncoding #-}
640988
641989instance (ToJSON a) => ToJSON (HashSet.HashSet a) where
642− toJSON = toJSON . HashSet.toList
990+ toJSON = toJSON1
643991 {-# INLINE toJSON #-}
644992
645− toEncoding = foldable
993+ toEncoding = toEncoding1
646994 {-# INLINE toEncoding #-}
647995
648996instance (Eq a, Hashable a, FromJSON a) => FromJSON (HashSet.HashSet a) where
649997 parseJSON = fmap HashSet.fromList . parseJSON
650998 {-# INLINE parseJSON #-}
651999
652−instance ToJSON IntSet.IntSet where
653− toJSON = toJSON . IntSet.toList
654− {-# INLINE toJSON #-}
655−
656− toEncoding = encodeSet IntSet.minView IntSet.foldr
657− {-# INLINE toEncoding #-}
658−
659−encodeSet :: (ToJSON a) =>
660− (s -> Maybe (a, s))
661− -> ((a -> B.Builder -> B.Builder) -> B.Builder -> s -> B.Builder)
662− -> s -> Encoding
663−encodeSet minView foldr xs =
664− case minView xs of
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 #-}
670−
671−instance FromJSON IntSet.IntSet where
672− parseJSON = fmap IntSet.fromList . parseJSON
673− {-# INLINE parseJSON #-}
674−
675−instance ToJSON a => ToJSON (IntMap.IntMap a) where
676− toJSON = toJSON . IntMap.toList
677− {-# INLINE toJSON #-}
678−
679− toEncoding = toEncoding . IntMap.toList
680− {-# INLINE toEncoding #-}
6811000
682−instance FromJSON a => FromJSON (IntMap.IntMap a) where
683− parseJSON = fmap IntMap.fromList . parseJSON
684− {-# INLINE parseJSON #-}
685−
686−encodeMap :: (ToJSON v)
687− => (k -> Encoding)
688− -> (m -> Maybe ((k,v), m))
689− -> ((k -> v -> B.Builder -> B.Builder) -> B.Builder -> m -> B.Builder)
690− -> m -> Encoding
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 #-}
699−
700−encodeWithKey :: (ToJSON v)
701− => (k -> Encoding)
702− -> ((k -> v -> Series -> Series) -> Series -> m -> Series)
703− -> m -> Encoding
704−encodeWithKey encodeKey foldrWithKey = brackets '{' '}' . foldrWithKey go mempty
705− where go k v c = Value (Encoding $ encodeKV encodeKey k v) <> c
706−{-# INLINE encodeWithKey #-}
707−
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 #-}
711−
712−instance (ToJSON v, ToJSONKey k) => ToJSON (M.Map k v) where
713− toJSON = case toJSONKey of
714− ToJSONKeyText (f,_) -> Object . mapHashKeyVal f toJSON
715− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f) . M.toList
716− {-# INLINE toJSON #-}
717−
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 #-}
1001+instance ToJSONKey k => ToJSON1 (H.HashMap k) where
1002+ liftToJSON g _ = case toJSONKey of
1003+ ToJSONKeyText (f,_) -> Object . mapKeyVal f g
1004+ ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f g) . H.toList
1005+ {-# INLINE liftToJSON #-}
7241006
725−instance (FromJSON v, FromJSONKey k, Ord k) => FromJSON (M.Map k v) where
726− parseJSON = case fromJSONKey of
727− FromJSONKeyCoerce _-> withObject "Map k v" $
728− fmap (H.foldrWithKey (M.insert . unsafeCoerce) M.empty) . H.traverseWithKey (\k v -> parseJSON v <?> Key k)
729− FromJSONKeyText f -> withObject "Map k v" $
730− fmap (H.foldrWithKey (M.insert . f) M.empty) . H.traverseWithKey (\k v -> parseJSON v <?> Key k)
731− FromJSONKeyTextParser f -> withObject "Map k v" $
732− H.foldrWithKey (\k v m -> M.insert <$> f k <*> (parseJSON v <?> Key k) <*> m) (pure M.empty)
733− FromJSONKeyValue f -> withArray "Map k v" $ \arr ->
734− M.fromList <$> (Tr.sequence .
735− zipWith (parseIndexedJSONPair f) [0..] . V.toList $ arr)
736− {-# INLINE parseJSON #-}
1007+ -- liftToEncoding :: forall a. (a -> Encoding) -> ([a] -> Encoding) -> H.HashMap k a -> Encoding
1008+ liftToEncoding g _ = case toJSONKey of
1009+ ToJSONKeyText (_,f) -> encodeWithKey f g H.foldrWithKey
1010+ ToJSONKeyValue (_,f) -> list (pairEncoding f) . H.toList
1011+ where
1012+ -- pairEncoding :: (k -> Encoding) -> (k, a) -> Encoding
1013+ pairEncoding f (a, b) = tuple $ fromEncoding (f a) >*< builder' g b
1014+ {-# INLINE liftToEncoding #-}
7371015
7381016instance (ToJSON v, ToJSONKey k) => ToJSON (H.HashMap k v) where
739− toJSON = case toJSONKey of
740− ToJSONKeyText (f,_) -> Object . mapKeyVal f toJSON
741− ToJSONKeyValue (f,_) -> Array . V.fromList . map (toJSONPair f) . H.toList
1017+ toJSON = toJSON1
7421018 {-# INLINE toJSON #-}
7431019
744− toEncoding = case toJSONKey of
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
1020+ toEncoding = toEncoding1
7491021 {-# INLINE toEncoding #-}
7501022
751−instance (FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (H.HashMap k v) where
752− parseJSON = case fromJSONKey of
1023+instance (FromJSONKey k, Eq k, Hashable k) => FromJSON1 (H.HashMap k) where
1024+ liftParseJSON p _ = case fromJSONKey of
7531025 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)
7551027 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)
7571029 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)
7591031 FromJSONKeyValue f -> withArray "Map k v" $ \arr ->
7601032 H.fromList <$> (Tr.sequence .
761− zipWith (parseIndexedJSONPair f) [0..] . V.toList $ arr)
1033+ zipWith (parseIndexedJSONPair f p) [0..] . V.toList $ arr)
7621034 where
7631035 uc :: Parser (H.HashMap Text v) -> Parser (H.HashMap k v)
7641036 uc = unsafeCoerce
765− {-# INLINE parseJSON #-}
766−
767−instance (ToJSON v) => ToJSON (Tree.Tree v) where
768− toJSON (Tree.Node root branches) = toJSON (root,branches)
769− {-# INLINE toJSON #-}
7701037
771− toEncoding (Tree.Node root branches) = toEncoding (root,branches)
772− {-# INLINE toEncoding #-}
773−
774−instance (FromJSON v) => FromJSON (Tree.Tree v) where
775− parseJSON j = uncurry Tree.Node <$> parseJSON j
1038+instance (FromJSON v, FromJSONKey k, Eq k, Hashable k) => FromJSON (H.HashMap k v) where
1039+ parseJSON = parseJSON1
7761040 {-# INLINE parseJSON #-}
7771041
1042+-------------------------------------------------------------------------------
1043+-- aeson
1044+-------------------------------------------------------------------------------
1045+
7781046instance ToJSON Value where
7791047 toJSON a = a
7801048 {-# INLINE toJSON #-}
@@ -783,7 +1051,7 @@ instance ToJSON Value where
7831051 {-# INLINE toEncoding #-}
7841052
7851053instance FromJSON Value where
786− parseJSON a = pure a
1054+ parseJSON = pure
7871055 {-# INLINE parseJSON #-}
7881056
7891057instance ToJSON DotNetTime where
@@ -804,6 +1072,10 @@ instance FromJSON DotNetTime where
8041072 _ -> fail "could not parse .NET time"
8051073 {-# INLINE parseJSON #-}
8061074
1075+-------------------------------------------------------------------------------
1076+-- time
1077+-------------------------------------------------------------------------------
1078+
8071079instance ToJSON Day where
8081080 toJSON = stringEncoding
8091081 toEncoding z = Encoding (E.quote $ E.day z)
@@ -864,715 +1136,81 @@ instance FromJSON NominalDiffTime where
8641136 parseJSON = withScientific "NominalDiffTime" $ pure . realToFrac
8651137 {-# INLINE parseJSON #-}
8661138
867−parseJSONElemAtIndex :: FromJSON a => Int -> Vector Value -> Parser a
868−parseJSONElemAtIndex = parseJSONElemAtIndex' parseJSON
869−
870−parseJSONElemAtIndex' :: (Value -> Parser a) -> Int -> Vector Value -> Parser a
871−parseJSONElemAtIndex' p idx ary = p (V.unsafeIndex ary idx) <?> Index idx
872−
873−tuple :: B.Builder -> Encoding
874−tuple b = Encoding (B.char7 '[' <> b <> B.char7 ']')
875−{-# INLINE tuple #-}
876−
877−(>*<) :: B.Builder -> B.Builder -> B.Builder
878−a >*< b = a <> B.char7 ',' <> b
879−{-# INLINE (>*<) #-}
880−infixr 6 >*<
881−
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)
887− return mv
888− {-# INLINE toJSON #-}
889−
890− toEncoding (a,b) = tuple $
891− builder a >*< builder b
892− {-# INLINE toEncoding #-}
893−
894−instance (FromJSON a, FromJSON b) => FromJSON (a,b) where
895− parseJSON = withArray "(a,b)" $ \ab ->
896− let n = V.length ab
897− in if n == 2
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 #-}
903−
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)
910− return mv
911− {-# INLINE toJSON #-}
912−
913− toEncoding (a,b,c) = tuple $
914− builder a >*<
915− builder b >*<
916− builder c
917− {-# INLINE toEncoding #-}
918−
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
922− in if n == 3
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 #-}
929−
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)
937− return mv
938− {-# INLINE toJSON #-}
939−
940− toEncoding (a,b,c,d) = tuple $
941− builder a >*<
942− builder b >*<
943− builder c >*<
944− builder d
945− {-# INLINE toEncoding #-}
946−
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
951− in if n == 4
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 #-}
959−
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)
969− return mv
970− {-# INLINE toJSON #-}
971−
972− toEncoding (a,b,c,d,e) = tuple $
973− builder a >*<
974− builder b >*<
975− builder c >*<
976− builder d >*<
977− builder e
978− {-# INLINE toEncoding #-}
979−
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
984− in if n == 5
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 #-}
993−
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)
1004− return mv
1005− {-# INLINE toJSON #-}
1006−
1007− toEncoding (a,b,c,d,e,f) = tuple $
1008− builder a >*<
1009− builder b >*<
1010− builder c >*<
1011− builder d >*<
1012− builder e >*<
1013− builder f
1014− {-# INLINE toEncoding #-}
1015−
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
1020− in if n == 6
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 #-}
1030−
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)
1042− return mv
1043− {-# INLINE toJSON #-}
1044−
1045− toEncoding (a,b,c,d,e,f,g) = tuple $
1046− builder a >*<
1047− builder b >*<
1048− builder c >*<
1049− builder d >*<
1050− builder e >*<
1051− builder f >*<
1052− builder g
1053− {-# INLINE toEncoding #-}
1054−
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
1059− in if n == 7
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 #-}
1070−
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)
1083− return mv
1084− {-# INLINE toJSON #-}
1085−
1086− toEncoding (a,b,c,d,e,f,g,h) = tuple $
1087− builder a >*<
1088− builder b >*<
1089− builder c >*<
1090− builder d >*<
1091− builder e >*<
1092− builder f >*<
1093− builder g >*<
1094− builder h
1095− {-# INLINE toEncoding #-}
1096−
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
1102− in if n /= 8
1103− then fail $ "cannot unpack array of length " ++
1104− show n ++ " into an 8-tuple"
1105− else (,,,,,,,)
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 #-}
1115−
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)
1129− return mv
1130− {-# INLINE toJSON #-}
1131−
1132− toEncoding (a,b,c,d,e,f,g,h,i) = tuple $
1133− builder a >*<
1134− builder b >*<
1135− builder c >*<
1136− builder d >*<
1137− builder e >*<
1138− builder f >*<
1139− builder g >*<
1140− builder h >*<
1141− builder i
1142− {-# INLINE toEncoding #-}
1143−
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
1149− in if n /= 9
1150− then fail $ "cannot unpack array of length " ++
1151− show n ++ " into a 9-tuple"
1152− else (,,,,,,,,)
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 #-}
1163−
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)
1179− return mv
1180− {-# INLINE toJSON #-}
1139+-------------------------------------------------------------------------------
1140+-- base Monoid/Semigroup
1141+-------------------------------------------------------------------------------
11811142
1182− toEncoding (a,b,c,d,e,f,g,h,i,j) = tuple $
1183− builder a >*<
1184− builder b >*<
1185− builder c >*<
1186− builder d >*<
1187− builder e >*<
1188− builder f >*<
1189− builder g >*<
1190− builder h >*<
1191− builder i >*<
1192− builder j
1193− {-# INLINE toEncoding #-}
1143+instance ToJSON1 Dual where
1144+ liftToJSON to _ = to . getDual
1145+ {-# INLINE liftToJSON #-}
11941146
1195−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1196− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j) =>
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
1200− in if n /= 10
1201− then fail $ "cannot unpack array of length " ++
1202− show n ++ " into a 10-tuple"
1203− else (,,,,,,,,,)
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 #-}
1215−
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)
1232− return mv
1233− {-# INLINE toJSON #-}
1147+ liftToEncoding to _ = to . getDual
1148+ {-# INLINE liftToEncoding #-}
12341149
1235− toEncoding (a,b,c,d,e,f,g,h,i,j,k) = tuple $
1236− builder a >*<
1237− builder b >*<
1238− builder c >*<
1239− builder d >*<
1240− builder e >*<
1241− builder f >*<
1242− builder g >*<
1243− builder h >*<
1244− builder i >*<
1245− builder j >*<
1246− builder k
1247− {-# INLINE toEncoding #-}
1248−
1249−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1250− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1251− FromJSON k) =>
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
1255− in if n /= 11
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 #-}
1271−
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)
1289− return mv
1150+instance ToJSON a => ToJSON (Dual a) where
1151+ toJSON = toJSON1
12901152 {-# INLINE toJSON #-}
12911153
1292− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l) = tuple $
1293− builder a >*<
1294− builder b >*<
1295− builder c >*<
1296− builder d >*<
1297− builder e >*<
1298− builder f >*<
1299− builder g >*<
1300− builder h >*<
1301− builder i >*<
1302− builder j >*<
1303− builder k >*<
1304− builder l
1154+ toEncoding = toEncoding1
13051155 {-# INLINE toEncoding #-}
13061156
1307−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1308− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1309− FromJSON k, FromJSON l) =>
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
1313− in if n /= 12
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 #-}
1330−
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,
1333− ToJSON m) =>
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)
1350− return mv
1351− {-# INLINE toJSON #-}
1352−
1353− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m) = tuple $
1354− builder a >*<
1355− builder b >*<
1356− builder c >*<
1357− builder d >*<
1358− builder e >*<
1359− builder f >*<
1360− builder g >*<
1361− builder h >*<
1362− builder i >*<
1363− builder j >*<
1364− builder k >*<
1365− builder l >*<
1366− builder m
1367− {-# INLINE toEncoding #-}
1157+instance FromJSON1 Dual where
1158+ liftParseJSON p _ = fmap Dual . p
1159+ {-# INLINE liftParseJSON #-}
13681160
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
1373− parseJSON = withArray "(a,b,c,d,e,f,g,h,i,j,k,l,m)" $ \ary ->
1374− let n = V.length ary
1375− in if n /= 13
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
1161+instance FromJSON a => FromJSON (Dual a) where
1162+ parseJSON = parseJSON1
13921163 {-# INLINE parseJSON #-}
13931164
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)
1414− return mv
1415− {-# INLINE toJSON #-}
14161165
1417− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m,n) = tuple $
1418− builder a >*<
1419− builder b >*<
1420− builder c >*<
1421− builder d >*<
1422− builder e >*<
1423− builder f >*<
1424− builder g >*<
1425− builder h >*<
1426− builder i >*<
1427− builder j >*<
1428− builder k >*<
1429− builder l >*<
1430− builder m >*<
1431− builder n
1432− {-# INLINE toEncoding #-}
1166+instance ToJSON1 First where
1167+ liftToJSON to to' = liftToJSON to to' . getFirst
1168+ {-# INLINE liftToJSON #-}
14331169
1434−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1435− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
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
1440− in if n /= 14
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 #-}
1170+ liftToEncoding to to' = liftToEncoding to to' . getFirst
1171+ {-# INLINE liftToEncoding #-}
14591172
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
1464− toJSON (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) = Array $ V.create $ do
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)
1481− return mv
1173+instance ToJSON a => ToJSON (First a) where
1174+ toJSON = toJSON1
14821175 {-# INLINE toJSON #-}
14831176
1484− toEncoding (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) = tuple $
1485− builder a >*<
1486− builder b >*<
1487− builder c >*<
1488− builder d >*<
1489− builder e >*<
1490− builder f >*<
1491− builder g >*<
1492− builder h >*<
1493− builder i >*<
1494− builder j >*<
1495− builder k >*<
1496− builder l >*<
1497− builder m >*<
1498− builder n >*<
1499− builder o
1177+ toEncoding = toEncoding1
15001178 {-# INLINE toEncoding #-}
15011179
1502−instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
1503− FromJSON f, FromJSON g, FromJSON h, FromJSON i, FromJSON j,
1504− FromJSON k, FromJSON l, FromJSON m, FromJSON n, FromJSON o) =>
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
1508− in if n /= 15
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 #-}
1528−
1529−instance ToJSON a => ToJSON (Dual a) where
1530− toJSON = toJSON . getDual
1531− {-# INLINE toJSON #-}
1532−
1533− toEncoding = toEncoding . getDual
1534− {-# INLINE toEncoding #-}
1180+instance FromJSON1 First where
1181+ liftParseJSON p p' = fmap First . liftParseJSON p p'
1182+ {-# INLINE liftParseJSON #-}
15351183
1536−instance FromJSON a => FromJSON (Dual a) where
1537− parseJSON = fmap Dual . parseJSON
1184+instance FromJSON a => FromJSON (First a) where
1185+ parseJSON = parseJSON1
15381186 {-# INLINE parseJSON #-}
15391187
1540−instance ToJSON a => ToJSON (First a) where
1541− toJSON = toJSON . getFirst
1542− {-# INLINE toJSON #-}
15431188
1544− toEncoding = toEncoding . getFirst
1545− {-# INLINE toEncoding #-}
1189+instance ToJSON1 Last where
1190+ liftToJSON to to' = liftToJSON to to' . getLast
1191+ {-# INLINE liftToJSON #-}
15461192
1547−instance FromJSON a => FromJSON (First a) where
1548− parseJSON = fmap First . parseJSON
1549− {-# INLINE parseJSON #-}
1193+ liftToEncoding to to' = liftToEncoding to to' . getLast
1194+ {-# INLINE liftToEncoding #-}
15501195
15511196instance ToJSON a => ToJSON (Last a) where
1552− toJSON = toJSON . getLast
1197+ toJSON = toJSON1
15531198 {-# INLINE toJSON #-}
15541199
1555− toEncoding = toEncoding . getLast
1200+ toEncoding = toEncoding1
15561201 {-# INLINE toEncoding #-}
15571202
1203+instance FromJSON1 Last where
1204+ liftParseJSON p p' = fmap Last . liftParseJSON p p'
1205+ {-# INLINE liftParseJSON #-}
1206+
15581207instance FromJSON a => FromJSON (Last a) where
1559− parseJSON = fmap Last . parseJSON
1208+ parseJSON = parseJSON1
15601209 {-# INLINE parseJSON #-}
15611210
1562−instance ToJSON Version where
1563− toJSON = toJSON . showVersion
1564− {-# INLINE toJSON #-}
1565−
1566− toEncoding = toEncoding . showVersion
1567− {-# INLINE toEncoding #-}
1568−
1569−instance FromJSON Version where
1570− {-# INLINE parseJSON #-}
1571− parseJSON = withText "Version" $ go . readP_to_S parseVersion . unpack
1572− where
1573− go [(v,[])] = return v
1574− go (_ : xs) = go xs
1575− go _ = fail $ "could not parse Version"
1211+-------------------------------------------------------------------------------
1212+-- tagged
1213+-------------------------------------------------------------------------------
15761214
15771215instance ToJSON (Proxy a) where
15781216 toJSON _ = Null
@@ -1586,46 +1224,42 @@ instance FromJSON (Proxy a) where
15861224 parseJSON Null = pure Proxy
15871225 parseJSON v = typeMismatch "Proxy" v
15881226
1227+
1228+instance ToJSON1 (Tagged a) where
1229+ liftToJSON to _ (Tagged x) = to x
1230+ {-# INLINE liftToJSON #-}
1231+
1232+ liftToEncoding to _ (Tagged x) = to x
1233+ {-# INLINE liftToEncoding #-}
1234+
15891235instance ToJSON b => ToJSON (Tagged a b) where
1590− toJSON (Tagged x) = toJSON x
1236+ toJSON = toJSON1
15911237 {-# INLINE toJSON #-}
15921238
1593− toEncoding (Tagged x) = toEncoding x
1239+ toEncoding = toEncoding1
15941240 {-# INLINE toEncoding #-}
15951241
1242+instance FromJSON1 (Tagged a) where
1243+ liftParseJSON p _ = fmap Tagged . p
1244+ {-# INLINE liftParseJSON #-}
1245+
15961246instance FromJSON b => FromJSON (Tagged a b) where
1247+ parseJSON = parseJSON1
15971248 {-# INLINE parseJSON #-}
1598− parseJSON = fmap Tagged . parseJSON
15991249
1600−instance ToJSON a => ToJSON (Const a b) where
1601− toJSON (Const x) = toJSON x
1602− {-# INLINE toJSON #-}
1603−
1604− toEncoding (Const x) = toEncoding x
1605− {-# INLINE toEncoding #-}
1250+instance FromJSONKey b => FromJSONKey (Tagged a b) where
1251+ fromJSONKey = coerceFromJSONKeyFunction (fromJSONKey :: FromJSONKeyFunction b)
1252+ fromJSONKeyList = (fmap . fmap) Tagged fromJSONKeyList
16061253
1607−instance FromJSON a => FromJSON (Const a b) where
1608− {-# INLINE parseJSON #-}
1609− parseJSON = fmap Const . parseJSON
1254+instance ToJSONKey b => ToJSONKey (Tagged a b) where
1255+ toJSONKey = contramapToJSONKeyFunction unTagged toJSONKey
1256+ toJSONKeyList = contramapToJSONKeyFunction (fmap unTagged) toJSONKeyList
16101257
1611−--------------------------------------------
1258+-------------------------------------------------------------------------------
16121259-- Instances for converting to/from map keys
1613−--------------------------------------------
1614−class ToJSONKey a where
1615− toJSONKey :: ToJSONKeyFunction a
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)
1621−
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
1260+-------------------------------------------------------------------------------
1261+
1262+
16291263
16301264instance ToJSONKey Text where
16311265 toJSONKey = ToJSONKeyText (id,toEncoding)
@@ -1633,10 +1267,7 @@ instance ToJSONKey Text where
16331267instance FromJSONKey Text where
16341268 fromJSONKey = fromJSONKeyCoerce
16351269
1636−-- | TODO: where ToJSONKey instance
1637−instance FromJSONKey b => FromJSONKey (Tagged a b) where
1638− fromJSONKey = coerceFromJSONKeyFunction (fromJSONKey :: FromJSONKeyFunction b)
1639− fromJSONKeyList = (fmap . fmap) Tagged fromJSONKeyList
1270+
16401271
16411272instance ToJSONKey Bool where
16421273 toJSONKey = ToJSONKeyText
@@ -1690,13 +1321,9 @@ instance (ToJSONKey a, ToJSON a) => ToJSONKey (Identity a) where
16901321instance (FromJSONKey a, FromJSON a) => FromJSONKey (Identity a) where
16911322 fromJSONKey = mapFromJSONKeyFunction Identity fromJSONKey
16921323
1693−contramapToJSONKeyFunction :: (b -> a) -> ToJSONKeyFunction a -> ToJSONKeyFunction b
1694−contramapToJSONKeyFunction h x = case x of
1695− ToJSONKeyText (f,g) -> ToJSONKeyText (f . h, g . h)
1696− ToJSONKeyValue (f,g) -> ToJSONKeyValue (f . h, g . h)
1697−
1698−mapFromJSONKeyFunction :: (a -> b) -> FromJSONKeyFunction a -> FromJSONKeyFunction b
1699−mapFromJSONKeyFunction = fmap
1324+-------------------------------------------------------------------------------
1325+-- Functions
1326+-------------------------------------------------------------------------------
17001327
17011328-- | @withObject expected f value@ applies @f@ to the 'Object' when @value@ is an @Object@
17021329-- and fails using @'typeMismatch' expected@ otherwise.
@@ -1819,34 +1446,3 @@ obj .:! key = case H.lookup key obj of
18191446(.!=) :: Parser (Maybe a) -> a -> Parser a
18201447pmval .!= val = fromMaybe val <$> pmval
18211448{-# INLINE (.!=) #-}
1822−
1823−realFloatToJSON :: RealFloat a => a -> Value
1824−realFloatToJSON d
1825− | isNaN d || isInfinite d = Null
1826− | otherwise = Number $ Scientific.fromFloatDigits d
1827−{-# INLINE realFloatToJSON #-}
1828−
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 #-}
1834−
1835−scientificToNumber :: Scientific -> Number
1836−scientificToNumber s
1837− | e < 0 = D $ Scientific.toRealFloat s
1838− | otherwise = I $ c * 10 ^ e
1839− where
1840− e = Scientific.base10Exponent s
1841− c = Scientific.coefficient s
1842−{-# INLINE scientificToNumber #-}
1843−
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 #-}
1849−
1850−parseIntegral :: Integral a => String -> Value -> Parser a
1851−parseIntegral expected = withScientific expected $ pure . truncate
1852−{-# INLINE parseIntegral #-}