@@ -5,16 +5,19 @@
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE GADTs #-}
77{-# LANGUAGE MultiParamTypeClasses #-}
8+{-# LANGUAGE NamedFieldPuns #-}
89{-# LANGUAGE NoImplicitPrelude #-}
910{-# LANGUAGE OverloadedStrings #-}
10−{-# LANGUAGE PolyKinds #-}
1111{-# LANGUAGE RecordWildCards #-}
1212{-# LANGUAGE ScopedTypeVariables #-}
1313{-# LANGUAGE TypeOperators #-}
14−{-# LANGUAGE TupleSections #-}
1514{-# LANGUAGE UndecidableInstances #-}
1615{-# LANGUAGE ViewPatterns #-}
1716
17+#if __GLASGOW_HASKELL__ >= 706
18+{-# LANGUAGE PolyKinds #-}
19+#endif
20+
1821#include "incoherent-compat.h"
1922#include "overlapping-compat.h"
2023
@@ -42,9 +45,6 @@ module Data.Aeson.Types.FromJSON
4245 , coerceFromJSONKeyFunction
4346 , mapFromJSONKeyFunction
4447
45− , GFromJSONKey()
46− , genericFromJSONKey
47−
4848 -- * List functions
4949 , listParser
5050
@@ -87,7 +87,7 @@ import Data.Aeson.Parser.Internal (eitherDecodeWith, jsonEOF)
8787import Data.Aeson.Types.Generic
8888import Data.Aeson.Types.Internal
8989import Data.Bits (unsafeShiftR)
90−import Data.Fixed (Fixed, HasResolution (resolution), Nano)
90+import Data.Fixed (Fixed, HasResolution)
9191import Data.Functor.Compose (Compose(..))
9292import Data.Functor.Identity (Identity(..))
9393import Data.Functor.Product (Product(..))
@@ -103,10 +103,8 @@ import Data.Scientific (Scientific, base10Exponent)
103103import Data.Tagged (Tagged(..))
104104import Data.Text (Text, pack, unpack)
105105import Data.Time (Day, DiffTime, LocalTime, NominalDiffTime, TimeOfDay, UTCTime, ZonedTime)
106−import Data.Time.Calendar.Compat (CalendarDiffDays (..), DayOfWeek (..))
107−import Data.Time.LocalTime.Compat (CalendarDiffTime (..))
108−import Data.Time.Clock.System.Compat (SystemTime (..))
109−import Data.Time.Format.Compat (parseTimeM, defaultTimeLocale)
106+import Data.Time.Format (parseTime)
107+import Data.Time.Locale.Compat (defaultTimeLocale)
110108import Data.Traversable as Tr (sequence)
111109import Data.Vector (Vector)
112110import Data.Version (Version, parseVersion)
@@ -118,9 +116,9 @@ import GHC.Generics
118116import Numeric.Natural (Natural)
119117import Text.ParserCombinators.ReadP (readP_to_S)
120118import Unsafe.Coerce (unsafeCoerce)
119+import qualified Data.Aeson.Compat as Compat
121120import qualified Data.Aeson.Parser.Time as Time
122121import qualified Data.Attoparsec.ByteString.Char8 as A (endOfInput, parseOnly, scientific)
123−import qualified Data.ByteString.Lazy as L
124122import qualified Data.DList as DList
125123import qualified Data.HashMap.Strict as H
126124import qualified Data.HashSet as HashSet
@@ -149,14 +147,22 @@ import qualified Data.Primitive.SmallArray as PM
149147import qualified Data.Primitive.Types as PM
150148
151149#if MIN_VERSION_primitive(0,6,4)
152−#if !MIN_VERSION_primitive(0,7,0)
153150import qualified Data.Primitive.UnliftedArray as PM
154−#endif
155151import qualified Data.Primitive.PrimArray as PM
156152#endif
157153
154+#ifndef HAS_COERCIBLE
155+#define HAS_COERCIBLE (__GLASGOW_HASKELL__ >= 707)
156+#endif
158157
158+#if HAS_COERCIBLE
159159import Data.Coerce (Coercible, coerce)
160+coerce' :: Coercible a b => a -> b
161+coerce' = coerce
162+#else
163+coerce' :: a -> b
164+coerce' = unsafeCoerce
165+#endif
160166
161167parseIndexedJSON :: (Value -> Parser a) -> Int -> Value -> Parser a
162168parseIndexedJSON p idx value = p value <?> Index idx
@@ -396,16 +402,6 @@ class FromJSON a where
396402-- > newtype SomeId = SomeId { getSomeId :: Text }
397403-- > deriving (Eq,Ord,Hashable,FromJSONKey)
398404--
399−-- If you have a sum of nullary constructors, you may use the generic
400−-- implementation:
401−--
402−-- @
403−-- data Color = Red | Green | Blue
404−-- deriving Generic
405−--
406−-- instance 'FromJSONKey' Color where
407−-- 'fromJSONKey' = 'genericFromJSONKey' 'defaultJSONKeyOptions'
408−-- @
409405class FromJSONKey a where
410406 -- | Strategy for parsing the key of a map-like container.
411407 fromJSONKey :: FromJSONKeyFunction a
@@ -428,7 +424,11 @@ class FromJSONKey a where
428424-- At the moment this type is intentionally not exported. 'FromJSONKeyFunction'
429425-- can be inspected, but cannot be constructed.
430426data CoerceText a where
427+#if HAS_COERCIBLE
431428 CoerceText :: Coercible Text a => CoerceText a
429+#else
430+ CoerceText :: CoerceText a
431+#endif
432432
433433-- | This type is related to 'ToJSONKeyFunction'. If 'FromJSONKeyValue' is used in the
434434-- 'FromJSONKey' instance, then 'ToJSONKeyValue' should be used in the 'ToJSONKey'
@@ -448,7 +448,7 @@ data FromJSONKeyFunction a
448448
449449-- | Only law abiding up to interpretation
450450instance Functor FromJSONKeyFunction where
451− fmap h (FromJSONKeyCoerce CoerceText) = FromJSONKeyText (h . coerce)
451+ fmap h (FromJSONKeyCoerce CoerceText) = FromJSONKeyText (h . coerce')
452452 fmap h (FromJSONKeyText f) = FromJSONKeyText (h . f)
453453 fmap h (FromJSONKeyTextParser f) = FromJSONKeyTextParser (fmap h . f)
454454 fmap h (FromJSONKeyValue f) = FromJSONKeyValue (fmap h . f)
@@ -462,7 +462,9 @@ instance Functor FromJSONKeyFunction where
462462--
463463-- On pre GHC 7.8 this is unconstrainted function.
464464fromJSONKeyCoerce ::
465+#if HAS_COERCIBLE
465466 Coercible Text a =>
467+#endif
466468 FromJSONKeyFunction a
467469fromJSONKeyCoerce = FromJSONKeyCoerce CoerceText
468470
@@ -470,52 +472,34 @@ fromJSONKeyCoerce = FromJSONKeyCoerce CoerceText
470472--
471473-- See note on 'fromJSONKeyCoerce'.
472474coerceFromJSONKeyFunction ::
475+#if HAS_COERCIBLE
473476 Coercible a b =>
477+#endif
474478 FromJSONKeyFunction a -> FromJSONKeyFunction b
479+#if HAS_COERCIBLE
475480coerceFromJSONKeyFunction = coerce
481+#else
482+coerceFromJSONKeyFunction (FromJSONKeyCoerce CoerceText) = FromJSONKeyCoerce CoerceText
483+coerceFromJSONKeyFunction (FromJSONKeyText f) = FromJSONKeyText (coerce' . f)
484+coerceFromJSONKeyFunction (FromJSONKeyTextParser f) = FromJSONKeyTextParser (fmap coerce' . f)
485+coerceFromJSONKeyFunction (FromJSONKeyValue f) = FromJSONKeyValue (fmap coerce' . f)
486+#endif
476487
477488{-# RULES
478489 "FromJSONKeyCoerce: fmap id" forall (x :: FromJSONKeyFunction a).
479490 fmap id x = x
480491 #-}
492+#if HAS_COERCIBLE
481493{-# RULES
482494 "FromJSONKeyCoerce: fmap coerce" forall x .
483495 fmap coerce x = coerceFromJSONKeyFunction x
484496 #-}
497+#endif
485498
486499-- | Same as 'fmap'. Provided for the consistency with 'ToJSONKeyFunction'.
487500mapFromJSONKeyFunction :: (a -> b) -> FromJSONKeyFunction a -> FromJSONKeyFunction b
488501mapFromJSONKeyFunction = fmap
489502
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.
493−--
494−-- See also 'genericToJSONKey'.
495−--
496−-- === __Example__
497−--
498−-- @
499−-- data Color = Red | Green | Blue
500−-- deriving 'Generic'
501−--
502−-- instance 'FromJSONKey' Color where
503−-- 'fromJSONKey' = 'genericFromJSONKey' 'defaultJSONKeyOptions'
504−-- @
505−genericFromJSONKey :: forall a. (Generic a, GFromJSONKey (Rep a))
506− => JSONKeyOptions
507− -> FromJSONKeyFunction a
508−genericFromJSONKey opts = FromJSONKeyTextParser $ \t ->
509− case parseSumFromString (keyModifier opts) t of
510− Nothing -> fail $
511− "invalid key " ++ show t ++ ", expected one of " ++ show cnames
512− Just k -> pure (to k)
513− where
514− cnames = unTagged2 (constructorTags (keyModifier opts) :: Tagged2 (Rep a) [String])
515−
516−class (ConstructorNames f, SumFromString f) => GFromJSONKey f where
517−instance (ConstructorNames f, SumFromString f) => GFromJSONKey f where
518−
519503-------------------------------------------------------------------------------
520504-- Functions needed for documentation
521505-------------------------------------------------------------------------------
@@ -768,13 +752,13 @@ withBoundedScientific' f v = withBoundedScientific_ id f v
768752-- | A variant of 'withBoundedScientific_' parameterized by a function to apply
769753-- to the 'Parser' in case of failure.
770754withBoundedScientific_ :: (Parser a -> Parser a) -> (Scientific -> Parser a) -> Value -> Parser a
771−withBoundedScientific_ whenFail f (Number scientific) =
772− if exp10 > 1024
755+withBoundedScientific_ whenFail f v@(Number scientific) =
756+ if exponent > 1024
773757 then whenFail (fail msg)
774758 else f scientific
775759 where
776− exp10 = base10Exponent scientific
777− msg = "found a number with exponent " ++ show exp10 ++ ", but it must not be greater than 1024"
760+ exponent = base10Exponent scientific
761+ msg = "found a number with exponent " ++ show exponent ++ ", but it must not be greater than 1024"
778762withBoundedScientific_ whenFail _ v =
779763 whenFail (typeMismatch "Number" v)
780764{-# INLINE withBoundedScientific_ #-}
@@ -794,7 +778,7 @@ withBool name _ v = prependContext name (typeMismatch "Boolean" v)
794778-- | Decode a nested JSON-encoded string.
795779withEmbeddedJSON :: String -> (Value -> Parser a) -> Value -> Parser a
796780withEmbeddedJSON _ innerParser (String txt) =
797− either fail innerParser $ eitherDecode (L.fromStrict $ T.encodeUtf8 txt)
781+ either fail innerParser $ eitherDecode (Compat.fromStrict $ T.encodeUtf8 txt)
798782 where
799783 eitherDecode = eitherFormatError . eitherDecodeWith jsonEOF ifromJSON
800784 eitherFormatError = either (Left . uncurry formatError) Right
@@ -863,7 +847,7 @@ parseFieldMaybe' = (.:!)
863847-- E.g. @'explicitParseField' 'parseJSON1' :: ('FromJSON1' f, 'FromJSON' a) -> 'Object' -> 'Text' -> 'Parser' (f a)@
864848explicitParseField :: (Value -> Parser a) -> Object -> Text -> Parser a
865849explicitParseField p obj key = case H.lookup key obj of
866− Nothing -> fail $ "key " ++ show key ++ " not found"
850+ Nothing -> fail $ "key " ++ show key ++ " not present"
867851 Just v -> p v <?> Key key
868852{-# INLINE explicitParseField #-}
869853
@@ -922,16 +906,6 @@ type ConName = String
922906contextType :: TypeName -> Parser a -> Parser a
923907contextType = prependContext
924908
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 ++ ", ")
934−
935909-- | Add the name of the constructor being parsed to a parser's error messages.
936910contextCons :: ConName -> TypeName -> Parser a -> Parser a
937911contextCons cname tname = prependContext (showCons cname tname)
@@ -1042,46 +1016,41 @@ parseAllNullarySum :: (SumFromString f, ConstructorNames f)
10421016parseAllNullarySum tname opts =
10431017 withText tname $ \tag ->
10441018 maybe (badTag tag) return $
1045− parseSumFromString modifier tag
1019+ parseSumFromString opts tag
10461020 where
1047− badTag tag = failWithCTags tname modifier $ \cnames ->
1021+ badTag tag = failWithCTags tname opts $ \cnames ->
10481022 "expected one of the tags " ++ show cnames ++
10491023 ", but found tag " ++ show tag
1050− modifier = constructorTagModifier opts
10511024
10521025-- | Fail with an informative error message about a mismatched tag.
10531026-- The error message is parameterized by the list of expected tags,
10541027-- to be inferred from the result type of the parser.
10551028failWithCTags
1056− :: forall f a t. ConstructorNames f
1057− => TypeName -> (String -> t) -> ([t] -> String) -> Parser (f a)
1058−failWithCTags tname modifier f =
1029+ :: forall f a. ConstructorNames f
1030+ => TypeName -> Options -> ([String] -> String) -> Parser (f a)
1031+failWithCTags tname opts f =
10591032 contextType tname . fail $ f cnames
10601033 where
1061− cnames = unTagged2 (constructorTags modifier :: Tagged2 f [t])
1034+ cnames = unTagged2 (constructorTags opts :: Tagged2 f [String])
10621035
10631036class SumFromString f where
1064− parseSumFromString :: (String -> String) -> Text -> Maybe (f a)
1037+ parseSumFromString :: Options -> Text -> Maybe (f a)
10651038
10661039instance (SumFromString a, SumFromString b) => SumFromString (a :+: b) where
10671040 parseSumFromString opts key = (L1 <$> parseSumFromString opts key) <|>
10681041 (R1 <$> parseSumFromString opts key)
10691042
10701043instance (Constructor c) => SumFromString (C1 c U1) where
1071− parseSumFromString modifier key
1072− | key == name = Just $ M1 U1
1073− | otherwise = Nothing
1074− where
1075− name = pack $ modifier $ conName (undefined :: M1 _i c _f _p)
1076−
1077−-- For genericFromJSONKey
1078−instance SumFromString a => SumFromString (D1 d a) where
1079− parseSumFromString modifier key = M1 <$> parseSumFromString modifier key
1044+ parseSumFromString opts key | key == name = Just $ M1 U1
1045+ | otherwise = Nothing
1046+ where
1047+ name = pack $ constructorTagModifier opts $
1048+ conName (undefined :: M1 _i c _f _p)
10801049
10811050-- | List of all constructor tags.
1082−constructorTags :: ConstructorNames a => (String -> t) -> Tagged2 a [t]
1083−constructorTags modifier =
1084− fmap DList.toList (constructorNames' modifier)
1051+constructorTags :: ConstructorNames a => Options -> Tagged2 a [String]
1052+constructorTags opts =
1053+ fmap DList.toList (constructorNames' (constructorTagModifier opts))
10851054
10861055-- | List of all constructor names of an ADT, after a given conversion
10871056-- function. (Better inlining.)
@@ -1102,16 +1071,9 @@ instance Constructor c => ConstructorNames (C1 c a) where
11021071 where
11031072 cname = conName (undefined :: M1 _i c _f _p)
11041073
1105−-- For genericFromJSONKey
1106−instance ConstructorNames a => ConstructorNames (D1 d a) where
1107− constructorNames' = retag . constructorNames'
1108− where
1109− retag :: Tagged2 a u -> Tagged2 (D1 d a) u
1110− retag (Tagged2 x) = Tagged2 x
1111−
11121074--------------------------------------------------------------------------------
1113−parseNonAllNullarySum :: forall f c arity.
1114− ( FromPair arity f
1075+
1076+parseNonAllNullarySum :: ( FromPair arity f
11151077 , FromTaggedObject arity f
11161078 , FromUntaggedValue arity f
11171079 , ConstructorNames f
@@ -1121,7 +1083,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
11211083 case sumEncoding opts of
11221084 TaggedObject{..} ->
11231085 withObject tname $ \obj -> do
1124− tag <- contextType tname . contextTag tagKey cnames_ $ obj .: tagKey
1086+ tag <- contextType tname $ obj .: tagKey
11251087 fromMaybe (badTag tag <?> Key tagKey) $
11261088 parseFromTaggedObject (tag :* contentsFieldName :* p) obj
11271089 where
@@ -1129,7 +1091,6 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
11291091 badTag tag = failWith_ $ \cnames ->
11301092 "expected tag field to be one of " ++ show cnames ++
11311093 ", but found tag " ++ show tag
1132− cnames_ = unTagged2 (constructorTags (constructorTagModifier opts) :: Tagged2 f [String])
11331094
11341095 ObjectWithSingleField ->
11351096 withObject tname $ \obj -> case H.toList obj of
@@ -1161,7 +1122,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
11611122
11621123 UntaggedValue -> parseUntaggedValue p
11631124 where
1164− failWith_ = failWithCTags tname (constructorTagModifier opts)
1125+ failWith_ = failWithCTags tname opts
11651126
11661127--------------------------------------------------------------------------------
11671128
@@ -1201,9 +1162,9 @@ class FromTaggedObject' arity f isRecord where
12011162 :: String :* ConName :* TypeName :* Options :* FromArgs arity a
12021163 -> Object -> Tagged isRecord (Parser (f a))
12031164
1204−instance (RecordFromJSON arity f, FieldNames f) => FromTaggedObject' arity f True where
1165+instance (RecordFromJSON arity f) => FromTaggedObject' arity f True where
12051166 -- Records are unpacked in the tagged object
1206− parseFromTaggedObject' (_ :* p) = Tagged . recordParseJSON (True :* p)
1167+ parseFromTaggedObject' (_ :* p) = Tagged . recordParseJSON p
12071168
12081169instance (ConsFromJSON arity f) => FromTaggedObject' arity f False where
12091170 -- Nonnullary nonrecords are encoded in the contents field
@@ -1244,11 +1205,11 @@ instance OVERLAPPING_
12441205 ) => ConsFromJSON' arity (S1 s a) True where
12451206 consParseJSON' p@(cname :* tname :* opts :* fargs)
12461207 | unwrapUnaryRecords opts = Tagged . fmap M1 . gParseJSON opts fargs
1247− | otherwise = Tagged . withObject (showCons cname tname) (recordParseJSON (False :* p))
1208+ | otherwise = Tagged . withObject (showCons cname tname) (recordParseJSON p)
12481209
12491210instance RecordFromJSON arity f => ConsFromJSON' arity f True where
12501211 consParseJSON' p@(cname :* tname :* _) =
1251− Tagged . withObject (showCons cname tname) (recordParseJSON (False :* p))
1212+ Tagged . withObject (showCons cname tname) (recordParseJSON p)
12521213
12531214instance OVERLAPPING_
12541215 ConsFromJSON' arity U1 False where
@@ -1274,55 +1235,21 @@ instance (ProductFromJSON arity f, ProductSize f
12741235
12751236--------------------------------------------------------------------------------
12761237
1277−class FieldNames f where
1278− fieldNames :: f a -> [Text] -> [Text]
1279−
1280−instance (FieldNames a, FieldNames b) => FieldNames (a :*: b) where
1281− fieldNames _ =
1282− fieldNames (undefined :: a x) .
1283− fieldNames (undefined :: b y)
1284−
1285−instance (Selector s) => FieldNames (S1 s f) where
1286− fieldNames _ = (pack (selName (undefined :: M1 _i s _f _p)) :)
1287−
12881238class RecordFromJSON arity f where
12891239 recordParseJSON
1290− :: Bool :* ConName :* TypeName :* Options :* FromArgs arity a
1291− -> Object -> Parser (f a)
1292−
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
1298− where
1299− knownFields :: H.HashMap Text ()
1300− knownFields = H.fromList $ map (,()) $
1301− fieldNames (undefined :: f a)
1302− [pack (tagFieldName (sumEncoding opts)) | fromTaggedSum]
1303− checkUnknown =
1304− if not (rejectUnknownFields opts)
1305− then \_ -> return ()
1306− else \obj -> case H.keys (H.difference obj knownFields) of
1307− [] -> return ()
1308− unknownFields -> contextCons cname tname $
1309− fail ("unknown fields: " ++ show unknownFields)
1310−
1311−class RecordFromJSON' arity f where
1312− recordParseJSON'
13131240 :: ConName :* TypeName :* Options :* FromArgs arity a
13141241 -> Object -> Parser (f a)
13151242
1316−instance ( RecordFromJSON' arity a
1317− , RecordFromJSON' arity b
1318− ) => RecordFromJSON' arity (a :*: b) where
1319− recordParseJSON' p obj =
1320− (:*:) <$> recordParseJSON' p obj
1321− <*> recordParseJSON' p obj
1243+instance ( RecordFromJSON arity a
1244+ , RecordFromJSON arity b
1245+ ) => RecordFromJSON arity (a :*: b) where
1246+ recordParseJSON p obj =
1247+ (:*:) <$> recordParseJSON p obj
1248+ <*> recordParseJSON p obj
13221249
13231250instance OVERLAPPABLE_ (Selector s, GFromJSON arity a) =>
1324− RecordFromJSON' arity (S1 s a) where
1325− recordParseJSON' (cname :* tname :* opts :* fargs) obj = do
1251+ RecordFromJSON arity (S1 s a) where
1252+ recordParseJSON (cname :* tname :* opts :* fargs) obj = do
13261253 fv <- contextCons cname tname (obj .: label)
13271254 M1 <$> gParseJSON opts fargs fv <?> Key label
13281255 where
@@ -1330,16 +1257,16 @@ instance OVERLAPPABLE_ (Selector s, GFromJSON arity a) =>
13301257 sname = selName (undefined :: M1 _i s _f _p)
13311258
13321259instance INCOHERENT_ (Selector s, FromJSON a) =>
1333− RecordFromJSON' arity (S1 s (K1 i (Maybe a))) where
1334− recordParseJSON' (_ :* _ :* opts :* _) obj = M1 . K1 <$> obj .:? pack label
1260+ RecordFromJSON arity (S1 s (K1 i (Maybe a))) where
1261+ recordParseJSON (_ :* _ :* opts :* _) obj = M1 . K1 <$> obj .:? pack label
13351262 where
13361263 label = fieldLabelModifier opts sname
13371264 sname = selName (undefined :: M1 _i s _f _p)
13381265
13391266-- Parse an Option like a Maybe.
13401267instance INCOHERENT_ (Selector s, FromJSON a) =>
1341− RecordFromJSON' arity (S1 s (K1 i (Semigroup.Option a))) where
1342− recordParseJSON' p obj = wrap <$> recordParseJSON' p obj
1268+ RecordFromJSON arity (S1 s (K1 i (Semigroup.Option a))) where
1269+ recordParseJSON p obj = wrap <$> recordParseJSON p obj
13431270 where
13441271 wrap :: S1 s (K1 i (Maybe a)) p -> S1 s (K1 i (Semigroup.Option a)) p
13451272 wrap (M1 (K1 a)) = M1 (K1 (Semigroup.Option a))
@@ -1576,22 +1503,12 @@ instance FromJSONKey Float where
15761503 _ -> Scientific.toRealFloat <$> parseScientificText t
15771504
15781505instance (FromJSON a, Integral a) => FromJSON (Ratio a) where
1579− parseJSON (Number x)
1580− | exp10 <= 1024
1581− , exp10 >= -1024 = return $! realToFrac x
1582− | otherwise = prependContext "Ratio" $ fail msg
1583− where
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
1588− where
1589− objParser = withObject "Rational" $ \obj -> do
1590− numerator <- obj .: "numerator"
1591− denominator <- obj .: "denominator"
1592− if denominator == 0
1593− then fail "Ratio denominator was 0"
1594− else pure $ numerator % denominator
1506+ parseJSON = withObject "Rational" $ \obj -> do
1507+ numerator <- obj .: "numerator"
1508+ denominator <- obj .: "denominator"
1509+ if denominator == 0
1510+ then fail "Ratio denominator was 0"
1511+ else pure $ numerator % denominator
15951512 {-# INLINE parseJSON #-}
15961513
15971514-- | This instance includes a bounds check to prevent maliciously
@@ -1742,7 +1659,7 @@ instance FromJSON1 NonEmpty where
17421659 liftParseJSON p _ = withArray "NonEmpty" $
17431660 (>>= ne) . Tr.sequence . zipWith (parseIndexedJSON p) [0..] . V.toList
17441661 where
1745− ne [] = fail "parsing NonEmpty failed, unexpected empty list"
1662+ ne [] = fail "parsing NonEmpty failed, unpexpected empty list"
17461663 ne (x:xs) = pure (x :| xs)
17471664 {-# INLINE liftParseJSON #-}
17481665
@@ -1997,7 +1914,7 @@ instance FromJSON DotNetTime where
19971914 parseJSON = withText "DotNetTime" $ \t ->
19981915 let (s,m) = T.splitAt (T.length t - 5) t
19991916 t' = T.concat [s,".",m]
2000− in case parseTimeM True defaultTimeLocale "/Date(%s%Q)/" (unpack t') of
1917+ in case parseTime defaultTimeLocale "/Date(%s%Q)/" (unpack t') of
20011918 Just d -> pure (DotNetTime d)
20021919 _ -> fail "could not parse .NET time"
20031920 {-# INLINE parseJSON #-}
@@ -2006,6 +1923,7 @@ instance FromJSON DotNetTime where
20061923-- primitive
20071924-------------------------------------------------------------------------------
20081925
1926+#if MIN_VERSION_base(4,7,0)
20091927instance FromJSON a => FromJSON (PM.Array a) where
20101928 -- note: we could do better than this if vector exposed the data
20111929 -- constructor in Data.Vector.
@@ -2018,7 +1936,6 @@ instance FromJSON a => FromJSON (PM.SmallArray a) where
20181936instance (PM.Prim a,FromJSON a) => FromJSON (PM.PrimArray a) where
20191937 parseJSON = fmap Exts.fromList . parseJSON
20201938
2021−#if !MIN_VERSION_primitive(0,7,0)
20221939instance (PM.PrimUnlifted a,FromJSON a) => FromJSON (PM.UnliftedArray a) where
20231940 parseJSON = fmap Exts.fromList . parseJSON
20241941#endif
@@ -2091,40 +2008,6 @@ instance FromJSON DiffTime where
20912008 parseJSON = withBoundedScientific "DiffTime" $ pure . realToFrac
20922009 {-# INLINE parseJSON #-}
20932010
2094−instance FromJSON SystemTime where
2095− parseJSON v = prependContext "SystemTime" $ do
2096− n <- parseJSON v
2097− let n' = floor (n * fromInteger (resolution n) :: Nano)
2098− let (secs, nano) = n' `divMod` resolution n
2099− return (MkSystemTime (fromInteger secs) (fromInteger nano))
2100−
2101−instance FromJSON CalendarDiffTime where
2102− parseJSON = withObject "CalendarDiffTime" $ \obj -> CalendarDiffTime
2103− <$> obj .: "months"
2104− <*> obj .: "time"
2105−
2106−instance FromJSON CalendarDiffDays where
2107− parseJSON = withObject "CalendarDiffDays" $ \obj -> CalendarDiffDays
2108− <$> obj .: "months"
2109− <*> obj .: "days"
2110−
2111−instance FromJSON DayOfWeek where
2112− parseJSON = withText "DaysOfWeek" parseDayOfWeek
2113−
2114−parseDayOfWeek :: T.Text -> Parser DayOfWeek
2115−parseDayOfWeek t = case T.toLower t of
2116− "monday" -> return Monday
2117− "tuesday" -> return Tuesday
2118− "wednesday" -> return Wednesday
2119− "thursday" -> return Thursday
2120− "friday" -> return Friday
2121− "saturday" -> return Saturday
2122− "sunday" -> return Sunday
2123− _ -> fail "Invalid week day"
2124−
2125−instance FromJSONKey DayOfWeek where
2126− fromJSONKey = FromJSONKeyTextParser parseDayOfWeek
2127−
21282011-------------------------------------------------------------------------------
21292012-- base Monoid/Semigroup
21302013-------------------------------------------------------------------------------