Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode RealFloat numbers using Scientific.fromFloatDigits #162

Merged
merged 3 commits into from
Nov 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -158,7 +159,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 @@ -169,7 +170,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 @@ -221,7 +222,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 @@ -780,6 +781,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 #-}
2 changes: 1 addition & 1 deletion aeson.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ library
time,
unordered-containers >= 0.1.3.0,
vector >= 0.7.1,
scientific >= 0.0
scientific >= 0.1

if flag(blaze-builder)
build-depends: blaze-builder >= 0.2.1.4
Expand Down