@@ -39,6 +39,7 @@ module Data.Aeson.Types.Class
39 , withText
39 , withText
40 , withArray
40 , withArray
41 , withNumber
41 , withNumber
42 , withBool
43 , withBool
44 -- * Functions
45 -- * Functions
@@ -53,7 +54,8 @@ module Data.Aeson.Types.Class
53import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
54import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
54import Data.Aeson.Functions
55import Data.Aeson.Functions
55import Data.Aeson.Types.Internal
56import Data.Aeson.Types.Internal
57+import Data.Scientific (Scientific, coefficient, base10Exponent)
56−import Data.Attoparsec.Char8 (Number(..))
58+import Data.Attoparsec.Number (Number(..))
57import Data.Fixed
59import Data.Fixed
58import Data.Hashable (Hashable(..))
60import Data.Hashable (Hashable(..))
59import Data.Int (Int8, Int16, Int32, Int64)
61import Data.Int (Int8, Int16, Int32, Int64)
@@ -311,23 +313,21 @@ instance FromJSON Char where
311 {-# INLINE parseJSON #-}
313 {-# INLINE parseJSON #-}
313instance ToJSON Double where
315instance ToJSON Double where
314− toJSON = Number . D
316+ toJSON = Number . realToFrac
315 {-# INLINE toJSON #-}
317 {-# INLINE toJSON #-}
317instance FromJSON Double where
319instance FromJSON Double where
318− parseJSON (Number n) = case n of
320+ parseJSON (Number s) = pure $ realToFrac s
320− I i -> pure (fromIntegral i)
321 parseJSON Null = pure (0/0)
321 parseJSON Null = pure (0/0)
322 parseJSON v = typeMismatch "Double" v
322 parseJSON v = typeMismatch "Double" v
323 {-# INLINE parseJSON #-}
323 {-# INLINE parseJSON #-}
325instance ToJSON Number where
325instance ToJSON Number where
326− toJSON = Number
326+ toJSON = Number . numberToScientific
327 {-# INLINE toJSON #-}
327 {-# INLINE toJSON #-}
329instance FromJSON Number where
329instance FromJSON Number where
330− parseJSON (Number n) = pure n
330+ parseJSON (Number s) = pure $ scientificToNumber s
331 parseJSON Null = pure (D (0/0))
331 parseJSON Null = pure (D (0/0))
332 parseJSON v = typeMismatch "Number" v
332 parseJSON v = typeMismatch "Number" v
333 {-# INLINE parseJSON #-}
333 {-# INLINE parseJSON #-}
@@ -337,9 +337,7 @@ instance ToJSON Float where
337 {-# INLINE toJSON #-}
337 {-# INLINE toJSON #-}
339instance FromJSON Float where
339instance FromJSON Float where
340− parseJSON (Number n) = pure $ case n of
340+ parseJSON (Number s) = pure $ realToFrac s
342− I i -> fromIntegral i
343 parseJSON Null = pure (0/0)
341 parseJSON Null = pure (0/0)
344 parseJSON v = typeMismatch "Float" v
342 parseJSON v = typeMismatch "Float" v
345 {-# INLINE parseJSON #-}
343 {-# INLINE parseJSON #-}
@@ -360,9 +358,7 @@ instance HasResolution a => ToJSON (Fixed a) where
360 {-# INLINE toJSON #-}
358 {-# INLINE toJSON #-}
362instance HasResolution a => FromJSON (Fixed a) where
360instance HasResolution a => FromJSON (Fixed a) where
363− parseJSON (Number n) = pure $ case n of
361+ parseJSON (Number s) = pure $ realToFrac s
365− I i -> fromIntegral i
366 parseJSON v = typeMismatch "Fixed" v
362 parseJSON v = typeMismatch "Fixed" v
367 {-# INLINE parseJSON #-}
363 {-# INLINE parseJSON #-}
@@ -375,7 +371,7 @@ instance FromJSON Int where
375 {-# INLINE parseJSON #-}
371 {-# INLINE parseJSON #-}
377parseIntegral :: Integral a => Value -> Parser a
373parseIntegral :: Integral a => Value -> Parser a
378−parseIntegral = withNumber "Integral" $ pure . floor
374+parseIntegral = withScientific "Integral" $ pure . floor
379{-# INLINE parseIntegral #-}
375{-# INLINE parseIntegral #-}
381instance ToJSON Integer where
377instance ToJSON Integer where
@@ -857,28 +853,35 @@ withObject _ f (Object obj) = f obj
857withObject expected _ v = typeMismatch expected v
853withObject expected _ v = typeMismatch expected v
858{-# INLINE withObject #-}
854{-# INLINE withObject #-}
860−-- | @withObject expected f value@ applies @f@ to the 'Text' when @value@ is a @String@
856+-- | @withText expected f value@ applies @f@ to the 'Text' when @value@ is a @String@
861-- and fails using @'typeMismatch' expected@ otherwise.
857-- and fails using @'typeMismatch' expected@ otherwise.
862withText :: String -> (Text -> Parser a) -> Value -> Parser a
858withText :: String -> (Text -> Parser a) -> Value -> Parser a
863withText _ f (String txt) = f txt
859withText _ f (String txt) = f txt
864withText expected _ v = typeMismatch expected v
860withText expected _ v = typeMismatch expected v
865{-# INLINE withText #-}
861{-# INLINE withText #-}
867−-- | @withObject expected f value@ applies @f@ to the 'Array' when @value@ is an @Array@
863+-- | @withArray expected f value@ applies @f@ to the 'Array' when @value@ is an @Array@
868-- and fails using @'typeMismatch' expected@ otherwise.
864-- and fails using @'typeMismatch' expected@ otherwise.
869withArray :: String -> (Array -> Parser a) -> Value -> Parser a
865withArray :: String -> (Array -> Parser a) -> Value -> Parser a
870withArray _ f (Array arr) = f arr
866withArray _ f (Array arr) = f arr
871withArray expected _ v = typeMismatch expected v
867withArray expected _ v = typeMismatch expected v
872{-# INLINE withArray #-}
868{-# INLINE withArray #-}
874−-- | @withObject expected f value@ applies @f@ to the 'Number' when @value@ is a @Number@
870+-- | @withNumber expected f value@ applies @f@ to the 'Number' when @value@ is a 'Number'.
875-- and fails using @'typeMismatch' expected@ otherwise.
871-- and fails using @'typeMismatch' expected@ otherwise.
876withNumber :: String -> (Number -> Parser a) -> Value -> Parser a
872withNumber :: String -> (Number -> Parser a) -> Value -> Parser a
877−withNumber _ f (Number num) = f num
873+withNumber expected f = withScientific expected (f . scientificToNumber)
878−withNumber expected _ v = typeMismatch expected v
879{-# INLINE withNumber #-}
874{-# INLINE withNumber #-}
875+{-# DEPRECATED withNumber "Use withScientific instead" #-}
877+-- | @withScientific expected f value@ applies @f@ to the 'Scientific' number when @value@ is a 'Number'.
878+-- and fails using @'typeMismatch' expected@ otherwise.
879+withScientific :: String -> (Scientific -> Parser a) -> Value -> Parser a
880+withScientific _ f (Number scientific) = f scientific
881+withScientific expected _ v = typeMismatch expected v
882+{-# INLINE withScientific #-}
881−-- | @withObject expected f value@ applies @f@ to the 'Bool' when @value@ is a @Bool@
884+-- | @withBool expected f value@ applies @f@ to the 'Bool' when @value@ is a @Bool@
882-- and fails using @'typeMismatch' expected@ otherwise.
885-- and fails using @'typeMismatch' expected@ otherwise.
883withBool :: String -> (Bool -> Parser a) -> Value -> Parser a
886withBool :: String -> (Bool -> Parser a) -> Value -> Parser a
884withBool _ f (Bool arr) = f arr
887withBool _ f (Bool arr) = f arr
@@ -954,3 +957,17 @@ typeMismatch expected actual =
954 Number _ -> "Number"
957 Number _ -> "Number"
955 Bool _ -> "Boolean"
958 Bool _ -> "Boolean"
956 Null -> "Null"
959 Null -> "Null"
961+scientificToNumber :: Scientific -> Number
963+ | e < 0 = D $ fromInteger c / 10 ^ negate e
964+ | otherwise = I $ c * 10 ^ e
966+ e = base10Exponent s
968+{-# INLINE scientificToNumber #-}
970+numberToScientific :: Number -> Scientific
971+numberToScientific (I i) = fromInteger i
972+numberToScientific (D d) = realToFrac d
973+{-# INLINE numberToScientific #-}