on master help sign in
Replace Number with Scientific

Scientific numbers are floating-point numbers with arbitrary precision that are fast to parse and can be efficiently converted to/from Fractional numbers like Doubles.

commit715931e2b1a54b1639813a63c46fddb2f1002d43 authorBas van Dijk <v.dijk.bas@gmail.com> dateWed 23 Oct 2013, 01:35 (13 years ago) parente4319c9
6 files changed+56−39
Data/Aeson/Encode.hs+8−8 Data/Aeson/Parser/Internal.hs+2−2 Data/Aeson/Types.hs+1 Data/Aeson/Types/Class.hs+37−20 Data/Aeson/Types/Internal.hs+6−8 aeson.cabal+2−1
Data/Aeson/Encode.hs+8−8
@@ -24,11 +24,10 @@ module Data.Aeson.Encode
2424 ) where
2525
2626import Data.Aeson.Types (ToJSON(..), Value(..))
27−import Data.Attoparsec.Number (Number(..))
2827import Data.Monoid (mappend)
28+import Data.Scientific (Scientific, coefficient, base10Exponent, scientificBuilder)
2929import Data.Text.Lazy.Builder
3030import Data.Text.Lazy.Builder.Int (decimal)
31−import Data.Text.Lazy.Builder.RealFloat (realFloat)
3231import Data.Text.Lazy.Encoding (encodeUtf8)
3332import Numeric (showHex)
3433import qualified Data.ByteString.Lazy as L
@@ -43,7 +42,7 @@ fromValue :: Value -> Builder
4342fromValue Null = {-# SCC "fromValue/Null" #-} "null"
4443fromValue (Bool b) = {-# SCC "fromValue/Bool" #-}
4544 if b then "true" else "false"
46−fromValue (Number n) = {-# SCC "fromValue/Number" #-} fromNumber n
45+fromValue (Number s) = {-# SCC "fromValue/Number" #-} fromScientific s
4746fromValue (String s) = {-# SCC "fromValue/String" #-} string s
4847fromValue (Array v)
4948 | V.null v = {-# SCC "fromValue/Array" #-} "[]"
@@ -87,11 +86,12 @@ string s = {-# SCC "string" #-} singleton '"' <> quote s <> singleton '"'
8786 | otherwise = singleton c
8887 where h = showHex (fromEnum c) ""
8988
90−fromNumber :: Number -> Builder
91−fromNumber (I i) = decimal i
92−fromNumber (D d)
93− | isNaN d || isInfinite d = "null"
94− | otherwise = realFloat d
89+fromScientific :: Scientific -> Builder
90+fromScientific s
91+ | e < 0 = scientificBuilder s
92+ | otherwise = decimal (coefficient s * 10 ^ e)
93+ where
94+ e = base10Exponent s
9595
9696-- | Efficiently serialize a JSON value as a lazy 'L.ByteString'.
9797encode :: ToJSON a => a -> L.ByteString
Data/Aeson/Parser/Internal.hs+2−2
@@ -136,7 +136,7 @@ arrayValues val = do
136136-- implementations in other languages conform to that same restriction
137137-- to preserve interoperability and security.
138138value :: Parser Value
139−value = most <|> (Number <$> number)
139+value = most <|> (Number <$> rational)
140140 where
141141 most = do
142142 c <- satisfy (`B8.elem` "{[\"ftn")
@@ -166,7 +166,7 @@ value' = most <|> num
166166 'n' -> string "ull" *> pure Null
167167 _ -> error "attoparsec panic! the impossible happened!"
168168 num = do
169− !n <- number
169+ !n <- rational
170170 return (Number n)
171171
172172doubleQuote, backslash :: Word8
Data/Aeson/Types.hs+1
@@ -47,6 +47,7 @@ module Data.Aeson.Types
4747 , withText
4848 , withArray
4949 , withNumber
50+ , withScientific
5051 , withBool
5152
5253 -- * Constructors and accessors
Data/Aeson/Types/Class.hs+37−20
@@ -39,6 +39,7 @@ module Data.Aeson.Types.Class
3939 , withText
4040 , withArray
4141 , withNumber
42+ , withScientific
4243 , withBool
4344
4445 -- * Functions
@@ -53,7 +54,8 @@ module Data.Aeson.Types.Class
5354import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
5455import Data.Aeson.Functions
5556import Data.Aeson.Types.Internal
56−import Data.Attoparsec.Char8 (Number(..))
57+import Data.Scientific (Scientific, coefficient, base10Exponent)
58+import Data.Attoparsec.Number (Number(..))
5759import Data.Fixed
5860import Data.Hashable (Hashable(..))
5961import Data.Int (Int8, Int16, Int32, Int64)
@@ -311,23 +313,21 @@ instance FromJSON Char where
311313 {-# INLINE parseJSON #-}
312314
313315instance ToJSON Double where
314− toJSON = Number . D
316+ toJSON = Number . realToFrac
315317 {-# INLINE toJSON #-}
316318
317319instance FromJSON Double where
318− parseJSON (Number n) = case n of
319− D d -> pure d
320− I i -> pure (fromIntegral i)
320+ parseJSON (Number s) = pure $ realToFrac s
321321 parseJSON Null = pure (0/0)
322322 parseJSON v = typeMismatch "Double" v
323323 {-# INLINE parseJSON #-}
324324
325325instance ToJSON Number where
326− toJSON = Number
326+ toJSON = Number . numberToScientific
327327 {-# INLINE toJSON #-}
328328
329329instance FromJSON Number where
330− parseJSON (Number n) = pure n
330+ parseJSON (Number s) = pure $ scientificToNumber s
331331 parseJSON Null = pure (D (0/0))
332332 parseJSON v = typeMismatch "Number" v
333333 {-# INLINE parseJSON #-}
@@ -337,9 +337,7 @@ instance ToJSON Float where
337337 {-# INLINE toJSON #-}
338338
339339instance FromJSON Float where
340− parseJSON (Number n) = pure $ case n of
341− D d -> realToFrac d
342− I i -> fromIntegral i
340+ parseJSON (Number s) = pure $ realToFrac s
343341 parseJSON Null = pure (0/0)
344342 parseJSON v = typeMismatch "Float" v
345343 {-# INLINE parseJSON #-}
@@ -360,9 +358,7 @@ instance HasResolution a => ToJSON (Fixed a) where
360358 {-# INLINE toJSON #-}
361359
362360instance HasResolution a => FromJSON (Fixed a) where
363− parseJSON (Number n) = pure $ case n of
364− D d -> realToFrac d
365− I i -> fromIntegral i
361+ parseJSON (Number s) = pure $ realToFrac s
366362 parseJSON v = typeMismatch "Fixed" v
367363 {-# INLINE parseJSON #-}
368364
@@ -375,7 +371,7 @@ instance FromJSON Int where
375371 {-# INLINE parseJSON #-}
376372
377373parseIntegral :: Integral a => Value -> Parser a
378−parseIntegral = withNumber "Integral" $ pure . floor
374+parseIntegral = withScientific "Integral" $ pure . floor
379375{-# INLINE parseIntegral #-}
380376
381377instance ToJSON Integer where
@@ -857,28 +853,35 @@ withObject _ f (Object obj) = f obj
857853withObject expected _ v = typeMismatch expected v
858854{-# INLINE withObject #-}
859855
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@
861857-- and fails using @'typeMismatch' expected@ otherwise.
862858withText :: String -> (Text -> Parser a) -> Value -> Parser a
863859withText _ f (String txt) = f txt
864860withText expected _ v = typeMismatch expected v
865861{-# INLINE withText #-}
866862
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@
868864-- and fails using @'typeMismatch' expected@ otherwise.
869865withArray :: String -> (Array -> Parser a) -> Value -> Parser a
870866withArray _ f (Array arr) = f arr
871867withArray expected _ v = typeMismatch expected v
872868{-# INLINE withArray #-}
873869
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'.
875871-- and fails using @'typeMismatch' expected@ otherwise.
876872withNumber :: String -> (Number -> Parser a) -> Value -> Parser a
877−withNumber _ f (Number num) = f num
878−withNumber expected _ v = typeMismatch expected v
873+withNumber expected f = withScientific expected (f . scientificToNumber)
879874{-# INLINE withNumber #-}
875+{-# DEPRECATED withNumber "Use withScientific instead" #-}
876+
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 #-}
880883
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@
882885-- and fails using @'typeMismatch' expected@ otherwise.
883886withBool :: String -> (Bool -> Parser a) -> Value -> Parser a
884887withBool _ f (Bool arr) = f arr
@@ -954,3 +957,17 @@ typeMismatch expected actual =
954957 Number _ -> "Number"
955958 Bool _ -> "Boolean"
956959 Null -> "Null"
960+
961+scientificToNumber :: Scientific -> Number
962+scientificToNumber s
963+ | e < 0 = D $ fromInteger c / 10 ^ negate e
964+ | otherwise = I $ c * 10 ^ e
965+ where
966+ e = base10Exponent s
967+ c = coefficient s
968+{-# INLINE scientificToNumber #-}
969+
970+numberToScientific :: Number -> Scientific
971+numberToScientific (I i) = fromInteger i
972+numberToScientific (D d) = realToFrac d
973+{-# INLINE numberToScientific #-}
Data/Aeson/Types/Internal.hs+6−8
@@ -40,7 +40,7 @@ module Data.Aeson.Types.Internal
4040import Control.Applicative
4141import Control.Monad
4242import Control.DeepSeq (NFData(..))
43−import Data.Attoparsec.Char8 (Number(..))
43+import Data.Scientific (Scientific)
4444import Data.Hashable (Hashable(..))
4545import Data.HashMap.Strict (HashMap)
4646import Data.Monoid (Monoid(..))
@@ -166,7 +166,7 @@ type Array = Vector Value
166166data Value = Object !Object
167167 | Array !Array
168168 | String !Text
169− | Number !Number
169+ | Number !Scientific
170170 | Bool !Bool
171171 | Null
172172 deriving (Eq, Show, Typeable)
@@ -175,7 +175,7 @@ instance NFData Value where
175175 rnf (Object o) = rnf o
176176 rnf (Array a) = V.foldl' (\x y -> rnf y `seq` x) () a
177177 rnf (String s) = rnf s
178− rnf (Number n) = case n of I i -> rnf i; D d -> rnf d
178+ rnf (Number n) = rnf n
179179 rnf (Bool b) = rnf b
180180 rnf Null = ()
181181
@@ -189,11 +189,9 @@ instance Hashable Value where
189189 hashWithSalt s (Array a) = V.foldl' hashWithSalt
190190 (s `hashWithSalt` (1::Int)) a
191191 hashWithSalt s (String str) = s `hashWithSalt` (2::Int) `hashWithSalt` str
192− hashWithSalt s (Number n) = 3 `hashWithSalt`
193− case n of I i -> hashWithSalt s i
194− D d -> hashWithSalt s d
195− hashWithSalt s (Bool b) = s `hashWithSalt` (4::Int) `hashWithSalt` b
196− hashWithSalt s Null = s `hashWithSalt` (5::Int)
192+ hashWithSalt s (Number n) = s `hashWithSalt` (3::Int) `hashWithSalt` n
193+ hashWithSalt s (Bool b) = s `hashWithSalt` (4::Int) `hashWithSalt` b
194+ hashWithSalt s Null = s `hashWithSalt` (5::Int)
197195
198196-- | The empty array.
199197emptyArray :: Value
aeson.cabal+2−1
@@ -130,7 +130,8 @@ library
130130 text >= 0.11.1.0,
131131 time,
132132 unordered-containers >= 0.1.3.0,
133− vector >= 0.7.1
133+ vector >= 0.7.1,
134+ scientific >= 0.0 && < 0.1
134135
135136 if flag(blaze-builder)
136137 build-depends: blaze-builder >= 0.2.1.4