@@ -5,16 +5,19 @@
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE GADTs #-}
6{-# LANGUAGE GADTs #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8+{-# LANGUAGE NamedFieldPuns #-}
8{-# LANGUAGE NoImplicitPrelude #-}
9{-# LANGUAGE NoImplicitPrelude #-}
9{-# LANGUAGE OverloadedStrings #-}
10{-# LANGUAGE OverloadedStrings #-}
10−{-# LANGUAGE PolyKinds #-}
11{-# LANGUAGE RecordWildCards #-}
11{-# LANGUAGE RecordWildCards #-}
12{-# LANGUAGE ScopedTypeVariables #-}
12{-# LANGUAGE ScopedTypeVariables #-}
13{-# LANGUAGE TypeOperators #-}
13{-# LANGUAGE TypeOperators #-}
14−{-# LANGUAGE TupleSections #-}
15{-# LANGUAGE UndecidableInstances #-}
14{-# LANGUAGE UndecidableInstances #-}
16{-# LANGUAGE ViewPatterns #-}
15{-# LANGUAGE ViewPatterns #-}
17+#if __GLASGOW_HASKELL__ >= 706
18+{-# LANGUAGE PolyKinds #-}
18#include "incoherent-compat.h"
21#include "incoherent-compat.h"
19#include "overlapping-compat.h"
22#include "overlapping-compat.h"
@@ -42,9 +45,6 @@ module Data.Aeson.Types.FromJSON
42 , coerceFromJSONKeyFunction
45 , coerceFromJSONKeyFunction
43 , mapFromJSONKeyFunction
46 , mapFromJSONKeyFunction
48 -- * List functions
48 -- * List functions
49 , listParser
49 , listParser
@@ -118,9 +118,9 @@ import GHC.Generics
118import Numeric.Natural (Natural)
118import Numeric.Natural (Natural)
119import Text.ParserCombinators.ReadP (readP_to_S)
119import Text.ParserCombinators.ReadP (readP_to_S)
120import Unsafe.Coerce (unsafeCoerce)
120import Unsafe.Coerce (unsafeCoerce)
121+import qualified Data.Aeson.Compat as Compat
121import qualified Data.Aeson.Parser.Time as Time
122import qualified Data.Aeson.Parser.Time as Time
122import qualified Data.Attoparsec.ByteString.Char8 as A (endOfInput, parseOnly, scientific)
123import qualified Data.Attoparsec.ByteString.Char8 as A (endOfInput, parseOnly, scientific)
123−import qualified Data.ByteString.Lazy as L
124import qualified Data.DList as DList
124import qualified Data.DList as DList
125import qualified Data.HashMap.Strict as H
125import qualified Data.HashMap.Strict as H
126import qualified Data.HashSet as HashSet
126import qualified Data.HashSet as HashSet
@@ -155,8 +155,18 @@ import qualified Data.Primitive.UnliftedArray as PM
155import qualified Data.Primitive.PrimArray as PM
155import qualified Data.Primitive.PrimArray as PM
158+#ifndef HAS_COERCIBLE
159+#define HAS_COERCIBLE (__GLASGOW_HASKELL__ >= 707)
159import Data.Coerce (Coercible, coerce)
163import Data.Coerce (Coercible, coerce)
164+coerce' :: Coercible a b => a -> b
168+coerce' = unsafeCoerce
161parseIndexedJSON :: (Value -> Parser a) -> Int -> Value -> Parser a
171parseIndexedJSON :: (Value -> Parser a) -> Int -> Value -> Parser a
162parseIndexedJSON p idx value = p value <?> Index idx
172parseIndexedJSON p idx value = p value <?> Index idx
@@ -396,16 +406,6 @@ class FromJSON a where
396-- > newtype SomeId = SomeId { getSomeId :: Text }
406-- > newtype SomeId = SomeId { getSomeId :: Text }
397-- > deriving (Eq,Ord,Hashable,FromJSONKey)
407-- > deriving (Eq,Ord,Hashable,FromJSONKey)
399−-- If you have a sum of nullary constructors, you may use the generic
403−-- data Color = Red | Green | Blue
406−-- instance 'FromJSONKey' Color where
407−-- 'fromJSONKey' = 'genericFromJSONKey' 'defaultJSONKeyOptions'
409class FromJSONKey a where
409class FromJSONKey a where
410 -- | Strategy for parsing the key of a map-like container.
410 -- | Strategy for parsing the key of a map-like container.
411 fromJSONKey :: FromJSONKeyFunction a
411 fromJSONKey :: FromJSONKeyFunction a
@@ -428,7 +428,11 @@ class FromJSONKey a where
428-- At the moment this type is intentionally not exported. 'FromJSONKeyFunction'
428-- At the moment this type is intentionally not exported. 'FromJSONKeyFunction'
429-- can be inspected, but cannot be constructed.
429-- can be inspected, but cannot be constructed.
430data CoerceText a where
430data CoerceText a where
431 CoerceText :: Coercible Text a => CoerceText a
432 CoerceText :: Coercible Text a => CoerceText a
434+ CoerceText :: CoerceText a
433-- | This type is related to 'ToJSONKeyFunction'. If 'FromJSONKeyValue' is used in the
437-- | This type is related to 'ToJSONKeyFunction'. If 'FromJSONKeyValue' is used in the
434-- 'FromJSONKey' instance, then 'ToJSONKeyValue' should be used in the 'ToJSONKey'
438-- 'FromJSONKey' instance, then 'ToJSONKeyValue' should be used in the 'ToJSONKey'
@@ -448,7 +452,7 @@ data FromJSONKeyFunction a
449-- | Only law abiding up to interpretation
453-- | Only law abiding up to interpretation
450instance Functor FromJSONKeyFunction where
454instance Functor FromJSONKeyFunction where
451− fmap h (FromJSONKeyCoerce CoerceText) = FromJSONKeyText (h . coerce)
455+ fmap h (FromJSONKeyCoerce CoerceText) = FromJSONKeyText (h . coerce')
452 fmap h (FromJSONKeyText f) = FromJSONKeyText (h . f)
456 fmap h (FromJSONKeyText f) = FromJSONKeyText (h . f)
453 fmap h (FromJSONKeyTextParser f) = FromJSONKeyTextParser (fmap h . f)
457 fmap h (FromJSONKeyTextParser f) = FromJSONKeyTextParser (fmap h . f)
454 fmap h (FromJSONKeyValue f) = FromJSONKeyValue (fmap h . f)
458 fmap h (FromJSONKeyValue f) = FromJSONKeyValue (fmap h . f)
@@ -462,7 +466,9 @@ instance Functor FromJSONKeyFunction where
463-- On pre GHC 7.8 this is unconstrainted function.
467-- On pre GHC 7.8 this is unconstrainted function.
464fromJSONKeyCoerce ::
468fromJSONKeyCoerce ::
465 Coercible Text a =>
470 Coercible Text a =>
466 FromJSONKeyFunction a
472 FromJSONKeyFunction a
467fromJSONKeyCoerce = FromJSONKeyCoerce CoerceText
473fromJSONKeyCoerce = FromJSONKeyCoerce CoerceText
@@ -470,52 +476,34 @@ fromJSONKeyCoerce = FromJSONKeyCoerce CoerceText
471-- See note on 'fromJSONKeyCoerce'.
477-- See note on 'fromJSONKeyCoerce'.
472coerceFromJSONKeyFunction ::
478coerceFromJSONKeyFunction ::
473 Coercible a b =>
480 Coercible a b =>
474 FromJSONKeyFunction a -> FromJSONKeyFunction b
482 FromJSONKeyFunction a -> FromJSONKeyFunction b
475coerceFromJSONKeyFunction = coerce
484coerceFromJSONKeyFunction = coerce
486+coerceFromJSONKeyFunction (FromJSONKeyCoerce CoerceText) = FromJSONKeyCoerce CoerceText
487+coerceFromJSONKeyFunction (FromJSONKeyText f) = FromJSONKeyText (coerce' . f)
488+coerceFromJSONKeyFunction (FromJSONKeyTextParser f) = FromJSONKeyTextParser (fmap coerce' . f)
489+coerceFromJSONKeyFunction (FromJSONKeyValue f) = FromJSONKeyValue (fmap coerce' . f)
478 "FromJSONKeyCoerce: fmap id" forall (x :: FromJSONKeyFunction a).
493 "FromJSONKeyCoerce: fmap id" forall (x :: FromJSONKeyFunction a).
479 fmap id x = x
494 fmap id x = x
482 "FromJSONKeyCoerce: fmap coerce" forall x .
498 "FromJSONKeyCoerce: fmap coerce" forall x .
483 fmap coerce x = coerceFromJSONKeyFunction x
499 fmap coerce x = coerceFromJSONKeyFunction x
486-- | Same as 'fmap'. Provided for the consistency with 'ToJSONKeyFunction'.
503-- | Same as 'fmap'. Provided for the consistency with 'ToJSONKeyFunction'.
487mapFromJSONKeyFunction :: (a -> b) -> FromJSONKeyFunction a -> FromJSONKeyFunction b
504mapFromJSONKeyFunction :: (a -> b) -> FromJSONKeyFunction a -> FromJSONKeyFunction b
488mapFromJSONKeyFunction = fmap
505mapFromJSONKeyFunction = fmap
490−-- | 'fromJSONKey' for 'Generic' types.
491−-- These types must be sums of nullary constructors, whose names will be used
492−-- as keys for JSON objects.
494−-- See also 'genericToJSONKey'.
499−-- data Color = Red | Green | Blue
500−-- deriving 'Generic'
502−-- instance 'FromJSONKey' Color where
503−-- 'fromJSONKey' = 'genericFromJSONKey' 'defaultJSONKeyOptions'
505−genericFromJSONKey :: forall a. (Generic a, GFromJSONKey (Rep a))
507− -> FromJSONKeyFunction a
508−genericFromJSONKey opts = FromJSONKeyTextParser $ \t ->
509− case parseSumFromString (keyModifier opts) t of
511− "invalid key " ++ show t ++ ", expected one of " ++ show cnames
512− Just k -> pure (to k)
514− cnames = unTagged2 (constructorTags (keyModifier opts) :: Tagged2 (Rep a) [String])
516−class (ConstructorNames f, SumFromString f) => GFromJSONKey f where
517−instance (ConstructorNames f, SumFromString f) => GFromJSONKey f where
519-------------------------------------------------------------------------------
507-------------------------------------------------------------------------------
520-- Functions needed for documentation
508-- Functions needed for documentation
521-------------------------------------------------------------------------------
509-------------------------------------------------------------------------------
@@ -768,13 +756,13 @@ withBoundedScientific' f v = withBoundedScientific_ id f v
768-- | A variant of 'withBoundedScientific_' parameterized by a function to apply
756-- | A variant of 'withBoundedScientific_' parameterized by a function to apply
769-- to the 'Parser' in case of failure.
757-- to the 'Parser' in case of failure.
770withBoundedScientific_ :: (Parser a -> Parser a) -> (Scientific -> Parser a) -> Value -> Parser a
758withBoundedScientific_ :: (Parser a -> Parser a) -> (Scientific -> Parser a) -> Value -> Parser a
771−withBoundedScientific_ whenFail f (Number scientific) =
759+withBoundedScientific_ whenFail f v@(Number scientific) =
772− if exp10 > 1024
760+ if exponent > 1024
773 then whenFail (fail msg)
761 then whenFail (fail msg)
774 else f scientific
762 else f scientific
776− exp10 = base10Exponent scientific
764+ exponent = base10Exponent scientific
777− msg = "found a number with exponent " ++ show exp10 ++ ", but it must not be greater than 1024"
765+ msg = "found a number with exponent " ++ show exponent ++ ", but it must not be greater than 1024"
778withBoundedScientific_ whenFail _ v =
766withBoundedScientific_ whenFail _ v =
779 whenFail (typeMismatch "Number" v)
767 whenFail (typeMismatch "Number" v)
780{-# INLINE withBoundedScientific_ #-}
768{-# INLINE withBoundedScientific_ #-}
@@ -794,7 +782,7 @@ withBool name _ v = prependContext name (typeMismatch "Boolean" v)
794-- | Decode a nested JSON-encoded string.
782-- | Decode a nested JSON-encoded string.
795withEmbeddedJSON :: String -> (Value -> Parser a) -> Value -> Parser a
783withEmbeddedJSON :: String -> (Value -> Parser a) -> Value -> Parser a
796withEmbeddedJSON _ innerParser (String txt) =
784withEmbeddedJSON _ innerParser (String txt) =
797− either fail innerParser $ eitherDecode (L.fromStrict $ T.encodeUtf8 txt)
785+ either fail innerParser $ eitherDecode (Compat.fromStrict $ T.encodeUtf8 txt)
799 eitherDecode = eitherFormatError . eitherDecodeWith jsonEOF ifromJSON
787 eitherDecode = eitherFormatError . eitherDecodeWith jsonEOF ifromJSON
800 eitherFormatError = either (Left . uncurry formatError) Right
788 eitherFormatError = either (Left . uncurry formatError) Right
@@ -863,7 +851,7 @@ parseFieldMaybe' = (.:!)
863-- E.g. @'explicitParseField' 'parseJSON1' :: ('FromJSON1' f, 'FromJSON' a) -> 'Object' -> 'Text' -> 'Parser' (f a)@
851-- E.g. @'explicitParseField' 'parseJSON1' :: ('FromJSON1' f, 'FromJSON' a) -> 'Object' -> 'Text' -> 'Parser' (f a)@
864explicitParseField :: (Value -> Parser a) -> Object -> Text -> Parser a
852explicitParseField :: (Value -> Parser a) -> Object -> Text -> Parser a
865explicitParseField p obj key = case H.lookup key obj of
853explicitParseField p obj key = case H.lookup key obj of
866− Nothing -> fail $ "key " ++ show key ++ " not found"
854+ Nothing -> fail $ "key " ++ show key ++ " not present"
867 Just v -> p v <?> Key key
855 Just v -> p v <?> Key key
868{-# INLINE explicitParseField #-}
856{-# INLINE explicitParseField #-}
@@ -922,16 +910,6 @@ type ConName = String
922contextType :: TypeName -> Parser a -> Parser a
910contextType :: TypeName -> Parser a -> Parser a
923contextType = prependContext
911contextType = prependContext
925−-- | Add the tagKey that will be looked up while building an ADT
926−-- | Produce the error equivalent to
927−-- | Left "Error in $: parsing T failed, expected an object with keys "tag" and
928−-- | "contents", where "tag" i-- |s associated to one of ["Foo", "Bar"],
929−-- | The parser returned error was: could not find key "tag"
930−contextTag :: Text -> [String] -> Parser a -> Parser a
931−contextTag tagKey cnames = prependFailure
932− ("expected Object with key \"" ++ unpack tagKey ++ "\"" ++
933− " containing one of " ++ show cnames ++ ", ")
935-- | Add the name of the constructor being parsed to a parser's error messages.
913-- | Add the name of the constructor being parsed to a parser's error messages.
936contextCons :: ConName -> TypeName -> Parser a -> Parser a
914contextCons :: ConName -> TypeName -> Parser a -> Parser a
937contextCons cname tname = prependContext (showCons cname tname)
915contextCons cname tname = prependContext (showCons cname tname)
@@ -1042,46 +1020,41 @@ parseAllNullarySum :: (SumFromString f, ConstructorNames f)
1042parseAllNullarySum tname opts =
1020parseAllNullarySum tname opts =
1043 withText tname $ \tag ->
1021 withText tname $ \tag ->
1044 maybe (badTag tag) return $
1022 maybe (badTag tag) return $
1045− parseSumFromString modifier tag
1023+ parseSumFromString opts tag
1047− badTag tag = failWithCTags tname modifier $ \cnames ->
1025+ badTag tag = failWithCTags tname opts $ \cnames ->
1048 "expected one of the tags " ++ show cnames ++
1026 "expected one of the tags " ++ show cnames ++
1049 ", but found tag " ++ show tag
1027 ", but found tag " ++ show tag
1050− modifier = constructorTagModifier opts
1052-- | Fail with an informative error message about a mismatched tag.
1029-- | Fail with an informative error message about a mismatched tag.
1053-- The error message is parameterized by the list of expected tags,
1030-- The error message is parameterized by the list of expected tags,
1054-- to be inferred from the result type of the parser.
1031-- to be inferred from the result type of the parser.
1055failWithCTags
1032failWithCTags
1056− :: forall f a t. ConstructorNames f
1033+ :: forall f a. ConstructorNames f
1057− => TypeName -> (String -> t) -> ([t] -> String) -> Parser (f a)
1034+ => TypeName -> Options -> ([String] -> String) -> Parser (f a)
1058−failWithCTags tname modifier f =
1035+failWithCTags tname opts f =
1059 contextType tname . fail $ f cnames
1036 contextType tname . fail $ f cnames
1061− cnames = unTagged2 (constructorTags modifier :: Tagged2 f [t])
1038+ cnames = unTagged2 (constructorTags opts :: Tagged2 f [String])
1063class SumFromString f where
1040class SumFromString f where
1064− parseSumFromString :: (String -> String) -> Text -> Maybe (f a)
1041+ parseSumFromString :: Options -> Text -> Maybe (f a)
1066instance (SumFromString a, SumFromString b) => SumFromString (a :+: b) where
1043instance (SumFromString a, SumFromString b) => SumFromString (a :+: b) where
1067 parseSumFromString opts key = (L1 <$> parseSumFromString opts key) <|>
1044 parseSumFromString opts key = (L1 <$> parseSumFromString opts key) <|>
1068 (R1 <$> parseSumFromString opts key)
1045 (R1 <$> parseSumFromString opts key)
1070instance (Constructor c) => SumFromString (C1 c U1) where
1047instance (Constructor c) => SumFromString (C1 c U1) where
1071− parseSumFromString modifier key
1048+ parseSumFromString opts key | key == name = Just $ M1 U1
1072− | key == name = Just $ M1 U1
1073− | otherwise = Nothing
1049+ | otherwise = Nothing
1051+ name = pack $ constructorTagModifier opts $
1075− name = pack $ modifier $ conName (undefined :: M1 _i c _f _p)
1052+ conName (undefined :: M1 _i c _f _p)
1077−-- For genericFromJSONKey
1078−instance SumFromString a => SumFromString (D1 d a) where
1079− parseSumFromString modifier key = M1 <$> parseSumFromString modifier key
1081-- | List of all constructor tags.
1054-- | List of all constructor tags.
1082−constructorTags :: ConstructorNames a => (String -> t) -> Tagged2 a [t]
1055+constructorTags :: ConstructorNames a => Options -> Tagged2 a [String]
1083−constructorTags modifier =
1056+constructorTags opts =
1084− fmap DList.toList (constructorNames' modifier)
1057+ fmap DList.toList (constructorNames' (constructorTagModifier opts))
1086-- | List of all constructor names of an ADT, after a given conversion
1059-- | List of all constructor names of an ADT, after a given conversion
1087-- function. (Better inlining.)
1060-- function. (Better inlining.)
@@ -1102,16 +1075,9 @@ instance Constructor c => ConstructorNames (C1 c a) where
1103 cname = conName (undefined :: M1 _i c _f _p)
1076 cname = conName (undefined :: M1 _i c _f _p)
1105−-- For genericFromJSONKey
1106−instance ConstructorNames a => ConstructorNames (D1 d a) where
1107− constructorNames' = retag . constructorNames'
1109− retag :: Tagged2 a u -> Tagged2 (D1 d a) u
1110− retag (Tagged2 x) = Tagged2 x
1112--------------------------------------------------------------------------------
1078--------------------------------------------------------------------------------
1113−parseNonAllNullarySum :: forall f c arity.
1080+parseNonAllNullarySum :: ( FromPair arity f
1115 , FromTaggedObject arity f
1081 , FromTaggedObject arity f
1116 , FromUntaggedValue arity f
1082 , FromUntaggedValue arity f
1117 , ConstructorNames f
1083 , ConstructorNames f
@@ -1121,7 +1087,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
1121 case sumEncoding opts of
1087 case sumEncoding opts of
1122 TaggedObject{..} ->
1088 TaggedObject{..} ->
1123 withObject tname $ \obj -> do
1089 withObject tname $ \obj -> do
1124− tag <- contextType tname . contextTag tagKey cnames_ $ obj .: tagKey
1090+ tag <- contextType tname $ obj .: tagKey
1125 fromMaybe (badTag tag <?> Key tagKey) $
1091 fromMaybe (badTag tag <?> Key tagKey) $
1126 parseFromTaggedObject (tag :* contentsFieldName :* p) obj
1092 parseFromTaggedObject (tag :* contentsFieldName :* p) obj
@@ -1129,7 +1095,6 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
1129 badTag tag = failWith_ $ \cnames ->
1095 badTag tag = failWith_ $ \cnames ->
1130 "expected tag field to be one of " ++ show cnames ++
1096 "expected tag field to be one of " ++ show cnames ++
1131 ", but found tag " ++ show tag
1097 ", but found tag " ++ show tag
1132− cnames_ = unTagged2 (constructorTags (constructorTagModifier opts) :: Tagged2 f [String])
1134 ObjectWithSingleField ->
1099 ObjectWithSingleField ->
1135 withObject tname $ \obj -> case H.toList obj of
1100 withObject tname $ \obj -> case H.toList obj of
@@ -1161,7 +1126,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
1162 UntaggedValue -> parseUntaggedValue p
1127 UntaggedValue -> parseUntaggedValue p
1164− failWith_ = failWithCTags tname (constructorTagModifier opts)
1129+ failWith_ = failWithCTags tname opts
1166--------------------------------------------------------------------------------
1131--------------------------------------------------------------------------------
@@ -1201,9 +1166,9 @@ class FromTaggedObject' arity f isRecord where
1201 :: String :* ConName :* TypeName :* Options :* FromArgs arity a
1166 :: String :* ConName :* TypeName :* Options :* FromArgs arity a
1202 -> Object -> Tagged isRecord (Parser (f a))
1167 -> Object -> Tagged isRecord (Parser (f a))
1204−instance (RecordFromJSON arity f, FieldNames f) => FromTaggedObject' arity f True where
1169+instance (RecordFromJSON arity f) => FromTaggedObject' arity f True where
1205 -- Records are unpacked in the tagged object
1170 -- Records are unpacked in the tagged object
1206− parseFromTaggedObject' (_ :* p) = Tagged . recordParseJSON (True :* p)
1171+ parseFromTaggedObject' (_ :* p) = Tagged . recordParseJSON p
1208instance (ConsFromJSON arity f) => FromTaggedObject' arity f False where
1173instance (ConsFromJSON arity f) => FromTaggedObject' arity f False where
1209 -- Nonnullary nonrecords are encoded in the contents field
1174 -- Nonnullary nonrecords are encoded in the contents field
@@ -1244,11 +1209,11 @@ instance OVERLAPPING_
1244 ) => ConsFromJSON' arity (S1 s a) True where
1209 ) => ConsFromJSON' arity (S1 s a) True where
1245 consParseJSON' p@(cname :* tname :* opts :* fargs)
1210 consParseJSON' p@(cname :* tname :* opts :* fargs)
1246 | unwrapUnaryRecords opts = Tagged . fmap M1 . gParseJSON opts fargs
1211 | unwrapUnaryRecords opts = Tagged . fmap M1 . gParseJSON opts fargs
1247− | otherwise = Tagged . withObject (showCons cname tname) (recordParseJSON (False :* p))
1212+ | otherwise = Tagged . withObject (showCons cname tname) (recordParseJSON p)
1249instance RecordFromJSON arity f => ConsFromJSON' arity f True where
1214instance RecordFromJSON arity f => ConsFromJSON' arity f True where
1250 consParseJSON' p@(cname :* tname :* _) =
1215 consParseJSON' p@(cname :* tname :* _) =
1251− Tagged . withObject (showCons cname tname) (recordParseJSON (False :* p))
1216+ Tagged . withObject (showCons cname tname) (recordParseJSON p)
1253instance OVERLAPPING_
1218instance OVERLAPPING_
1254 ConsFromJSON' arity U1 False where
1219 ConsFromJSON' arity U1 False where
@@ -1274,55 +1239,21 @@ instance (ProductFromJSON arity f, ProductSize f
1275--------------------------------------------------------------------------------
1240--------------------------------------------------------------------------------
1277−class FieldNames f where
1278− fieldNames :: f a -> [Text] -> [Text]
1280−instance (FieldNames a, FieldNames b) => FieldNames (a :*: b) where
1282− fieldNames (undefined :: a x) .
1283− fieldNames (undefined :: b y)
1285−instance (Selector s) => FieldNames (S1 s f) where
1286− fieldNames _ = (pack (selName (undefined :: M1 _i s _f _p)) :)
1288class RecordFromJSON arity f where
1242class RecordFromJSON arity f where
1289 recordParseJSON
1243 recordParseJSON
1290− :: Bool :* ConName :* TypeName :* Options :* FromArgs arity a
1291− -> Object -> Parser (f a)
1293−instance ( FieldNames f
1294− , RecordFromJSON' arity f
1295− ) => RecordFromJSON arity f where
1296− recordParseJSON (fromTaggedSum :* p@(cname :* tname :* opts :* _)) =
1297− \obj -> checkUnknown obj >> recordParseJSON' p obj
1299− knownFields :: H.HashMap Text ()
1300− knownFields = H.fromList $ map (,()) $
1301− fieldNames (undefined :: f a)
1302− [pack (tagFieldName (sumEncoding opts)) | fromTaggedSum]
1304− if not (rejectUnknownFields opts)
1305− then \_ -> return ()
1306− else \obj -> case H.keys (H.difference obj knownFields) of
1308− unknownFields -> contextCons cname tname $
1309− fail ("unknown fields: " ++ show unknownFields)
1311−class RecordFromJSON' arity f where
1313 :: ConName :* TypeName :* Options :* FromArgs arity a
1244 :: ConName :* TypeName :* Options :* FromArgs arity a
1314 -> Object -> Parser (f a)
1245 -> Object -> Parser (f a)
1316−instance ( RecordFromJSON' arity a
1247+instance ( RecordFromJSON arity a
1317− , RecordFromJSON' arity b
1248+ , RecordFromJSON arity b
1318− ) => RecordFromJSON' arity (a :*: b) where
1249+ ) => RecordFromJSON arity (a :*: b) where
1319− recordParseJSON' p obj =
1250+ recordParseJSON p obj =
1320− (:*:) <$> recordParseJSON' p obj
1251+ (:*:) <$> recordParseJSON p obj
1321− <*> recordParseJSON' p obj
1252+ <*> recordParseJSON p obj
1323instance OVERLAPPABLE_ (Selector s, GFromJSON arity a) =>
1254instance OVERLAPPABLE_ (Selector s, GFromJSON arity a) =>
1324− RecordFromJSON' arity (S1 s a) where
1255+ RecordFromJSON arity (S1 s a) where
1325− recordParseJSON' (cname :* tname :* opts :* fargs) obj = do
1256+ recordParseJSON (cname :* tname :* opts :* fargs) obj = do
1326 fv <- contextCons cname tname (obj .: label)
1257 fv <- contextCons cname tname (obj .: label)
1327 M1 <$> gParseJSON opts fargs fv <?> Key label
1258 M1 <$> gParseJSON opts fargs fv <?> Key label
@@ -1330,16 +1261,16 @@ instance OVERLAPPABLE_ (Selector s, GFromJSON arity a) =>
1330 sname = selName (undefined :: M1 _i s _f _p)
1261 sname = selName (undefined :: M1 _i s _f _p)
1332instance INCOHERENT_ (Selector s, FromJSON a) =>
1263instance INCOHERENT_ (Selector s, FromJSON a) =>
1333− RecordFromJSON' arity (S1 s (K1 i (Maybe a))) where
1264+ RecordFromJSON arity (S1 s (K1 i (Maybe a))) where
1334− recordParseJSON' (_ :* _ :* opts :* _) obj = M1 . K1 <$> obj .:? pack label
1265+ recordParseJSON (_ :* _ :* opts :* _) obj = M1 . K1 <$> obj .:? pack label
1336 label = fieldLabelModifier opts sname
1267 label = fieldLabelModifier opts sname
1337 sname = selName (undefined :: M1 _i s _f _p)
1268 sname = selName (undefined :: M1 _i s _f _p)
1339-- Parse an Option like a Maybe.
1270-- Parse an Option like a Maybe.
1340instance INCOHERENT_ (Selector s, FromJSON a) =>
1271instance INCOHERENT_ (Selector s, FromJSON a) =>
1341− RecordFromJSON' arity (S1 s (K1 i (Semigroup.Option a))) where
1272+ RecordFromJSON arity (S1 s (K1 i (Semigroup.Option a))) where
1342− recordParseJSON' p obj = wrap <$> recordParseJSON' p obj
1273+ recordParseJSON p obj = wrap <$> recordParseJSON p obj
1344 wrap :: S1 s (K1 i (Maybe a)) p -> S1 s (K1 i (Semigroup.Option a)) p
1275 wrap :: S1 s (K1 i (Maybe a)) p -> S1 s (K1 i (Semigroup.Option a)) p
1345 wrap (M1 (K1 a)) = M1 (K1 (Semigroup.Option a))
1276 wrap (M1 (K1 a)) = M1 (K1 (Semigroup.Option a))
@@ -1576,22 +1507,12 @@ instance FromJSONKey Float where
1576 _ -> Scientific.toRealFloat <$> parseScientificText t
1507 _ -> Scientific.toRealFloat <$> parseScientificText t
1578instance (FromJSON a, Integral a) => FromJSON (Ratio a) where
1509instance (FromJSON a, Integral a) => FromJSON (Ratio a) where
1579− parseJSON (Number x)
1581− , exp10 >= -1024 = return $! realToFrac x
1582− | otherwise = prependContext "Ratio" $ fail msg
1584− exp10 = base10Exponent x
1585− msg = "found a number with exponent " ++ show exp10
1586− ++ ", but it must not be greater than 1024 or less than -1024"
1587− parseJSON o = objParser o
1589− objParser = withObject "Rational" $ \obj -> do
1510+ parseJSON = withObject "Rational" $ \obj -> do
1590− numerator <- obj .: "numerator"
1511+ numerator <- obj .: "numerator"
1591− denominator <- obj .: "denominator"
1512+ denominator <- obj .: "denominator"
1592− if denominator == 0
1513+ if denominator == 0
1593− then fail "Ratio denominator was 0"
1514+ then fail "Ratio denominator was 0"
1594− else pure $ numerator % denominator
1515+ else pure $ numerator % denominator
1595 {-# INLINE parseJSON #-}
1516 {-# INLINE parseJSON #-}
1597-- | This instance includes a bounds check to prevent maliciously
1518-- | This instance includes a bounds check to prevent maliciously
@@ -2006,6 +1927,7 @@ instance FromJSON DotNetTime where
2006-- primitive
1927-- primitive
2007-------------------------------------------------------------------------------
1928-------------------------------------------------------------------------------
1930+#if MIN_VERSION_base(4,7,0)
2009instance FromJSON a => FromJSON (PM.Array a) where
1931instance FromJSON a => FromJSON (PM.Array a) where
2010 -- note: we could do better than this if vector exposed the data
1932 -- note: we could do better than this if vector exposed the data
2011 -- constructor in Data.Vector.
1933 -- constructor in Data.Vector.
@@ -2023,6 +1945,7 @@ instance (PM.PrimUnlifted a,FromJSON a) => FromJSON (PM.UnliftedArray a) where
2023 parseJSON = fmap Exts.fromList . parseJSON
1945 parseJSON = fmap Exts.fromList . parseJSON
2027-------------------------------------------------------------------------------
1950-------------------------------------------------------------------------------