Skip to content

Commit

Permalink
Merge pull request #162 from basvandijk/master
Browse files Browse the repository at this point in the history
Encode RealFloat numbers using Scientific.fromFloatDigits
  • Loading branch information
bos committed Nov 19, 2013
2 parents 645328e + bf7313d commit b0b4c24
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Data/Aeson/Types/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
import Data.Aeson.Functions
import Data.Aeson.Types.Class
import Data.Aeson.Types.Internal
import Data.Scientific (Scientific, coefficient, base10Exponent)
import Data.Scientific (Scientific)
import qualified Data.Scientific as Scientific (coefficient, base10Exponent, fromFloatDigits)
import Data.Attoparsec.Number (Number(..))
import Data.Fixed
import Data.Hashable (Hashable(..))
Expand Down Expand Up @@ -166,7 +167,7 @@ instance ToJSON Double where
realFloatToJSON :: RealFloat a => a -> Value
realFloatToJSON d
| isNaN d || isInfinite d = Null
| otherwise = Number $ realToFrac d
| otherwise = Number $ Scientific.fromFloatDigits d
{-# INLINE realFloatToJSON #-}

instance FromJSON Double where
Expand All @@ -177,7 +178,7 @@ instance FromJSON Double where

instance ToJSON Number where
toJSON (D d) = toJSON d
toJSON (I i) = Number $ fromInteger i
toJSON (I i) = toJSON i
{-# INLINE toJSON #-}

instance FromJSON Number where
Expand Down Expand Up @@ -229,7 +230,7 @@ parseIntegral = withScientific "Integral" $ pure . floor
{-# INLINE parseIntegral #-}

instance ToJSON Integer where
toJSON = Number . fromIntegral
toJSON = Number . fromInteger
{-# INLINE toJSON #-}

instance FromJSON Integer where
Expand Down Expand Up @@ -788,6 +789,6 @@ scientificToNumber s
| e < 0 = D $ fromInteger c / 10 ^ negate e
| otherwise = I $ c * 10 ^ e
where
e = base10Exponent s
c = coefficient s
e = Scientific.base10Exponent s
c = Scientific.coefficient s
{-# INLINE scientificToNumber #-}

0 comments on commit b0b4c24

Please sign in to comment.