on master help sign in
Use Scientific.toBoundedInteger in truncateBoundedIntegral
52f0bdaOleg Grenrus
Data/Aeson/Types/FromJSON.hs+7−11
@@ -189,18 +189,14 @@ parseIntegral :: Integral a => String -> Value -> Parser a
189parseIntegral expected = withScientific expected $ truncateIntegral expected
189parseIntegral expected = withScientific expected $ truncateIntegral expected
190{-# INLINE parseIntegral #-}
190{-# INLINE parseIntegral #-}
191
191
192−truncateBoundedIntegral :: Integral a => String -> Scientific -> Parser a
192+truncateBoundedIntegral :: (Bounded a, Integral a) => String -> Scientific -> Parser a
193−truncateBoundedIntegral expected s
193+truncateBoundedIntegral expected s = maybe
194− | Scientific.isInteger s =
195− let x = truncate s
196− in if fromIntegral x == s
197− then pure x
198− else fail $ "Truncating to " ++ expected ++ " cause over or underflow: " ++ show s
194+ (fail $ expected ++ " is either floating or will cause over or underflow: " ++ show s)
199− | otherwise =
195+ pure
200− fail $ "Floating number specified for " ++ expected ++ ": " ++ show s
196+ (Scientific.toBoundedInteger s)
201{-# INLINE truncateBoundedIntegral #-}
197{-# INLINE truncateBoundedIntegral #-}
202
198
203−parseBoundedIntegral :: Integral a => String -> Value -> Parser a
199+parseBoundedIntegral :: (Bounded a, Integral a) => String -> Value -> Parser a
204parseBoundedIntegral expected = withScientific expected $ truncateBoundedIntegral expected
200parseBoundedIntegral expected = withScientific expected $ truncateBoundedIntegral expected
205{-# INLINE parseBoundedIntegral #-}
201{-# INLINE parseBoundedIntegral #-}
206
202
@@ -215,7 +211,7 @@ parseIntegralText expected t =
215 parseScientificText t >>= truncateIntegral expected
211 parseScientificText t >>= truncateIntegral expected
216{-# INLINE parseIntegralText #-}
212{-# INLINE parseIntegralText #-}
217
213
218−parseBoundedIntegralText :: Integral a => String -> Text -> Parser a
214+parseBoundedIntegralText :: (Bounded a, Integral a) => String -> Text -> Parser a
219parseBoundedIntegralText expected t =
215parseBoundedIntegralText expected t =
220 parseScientificText t >>= truncateBoundedIntegral expected
216 parseScientificText t >>= truncateBoundedIntegral expected
221
217