on master help sign in
Merge pull request #162 from basvandijk/master

Encode RealFloat numbers using Scientific.fromFloatDigits

commit20b843265478b4a88d4d8da8a61f6578db23b5bb authorBryan O'Sullivan <bos@serpentine.com> dateMon 18 Nov 2013, 21:03 (13 years ago) parente2a91d98841e67
1 file changed+7−6
Data/Aeson/Types/Instances.hs+7−6
Data/Aeson/Types/Instances.hs+7−6
@@ -55,7 +55,8 @@ import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
5555import Data.Aeson.Functions
5656import Data.Aeson.Types.Class
5757import Data.Aeson.Types.Internal
58−import Data.Scientific (Scientific, coefficient, base10Exponent)
58+import Data.Scientific (Scientific)
59+import qualified Data.Scientific as Scientific (coefficient, base10Exponent, fromFloatDigits)
5960import Data.Attoparsec.Number (Number(..))
6061import Data.Fixed
6162import Data.Hashable (Hashable(..))
@@ -166,7 +167,7 @@ instance ToJSON Double where
166167realFloatToJSON :: RealFloat a => a -> Value
167168realFloatToJSON d
168169 | isNaN d || isInfinite d = Null
169− | otherwise = Number $ realToFrac d
170+ | otherwise = Number $ Scientific.fromFloatDigits d
170171{-# INLINE realFloatToJSON #-}
171172
172173instance FromJSON Double where
@@ -177,7 +178,7 @@ instance FromJSON Double where
177178
178179instance ToJSON Number where
179180 toJSON (D d) = toJSON d
180− toJSON (I i) = Number $ fromInteger i
181+ toJSON (I i) = toJSON i
181182 {-# INLINE toJSON #-}
182183
183184instance FromJSON Number where
@@ -229,7 +230,7 @@ parseIntegral = withScientific "Integral" $ pure . floor
229230{-# INLINE parseIntegral #-}
230231
231232instance ToJSON Integer where
232− toJSON = Number . fromIntegral
233+ toJSON = Number . fromInteger
233234 {-# INLINE toJSON #-}
234235
235236instance FromJSON Integer where
@@ -788,6 +789,6 @@ scientificToNumber s
788789 | e < 0 = D $ fromInteger c / 10 ^ negate e
789790 | otherwise = I $ c * 10 ^ e
790791 where
791− e = base10Exponent s
792− c = coefficient s
792+ e = Scientific.base10Exponent s
793+ c = Scientific.coefficient s
793794{-# INLINE scientificToNumber #-}