Commits

Bryan O'Sullivan committed 9804740

Speed up the encoding of numbers.

  • Participants
  • Parent commits ad4cb3c

Comments (0)

Files changed (2)

File Data/Aeson/Encode.hs

 import Blaze.ByteString.Builder
 import Blaze.ByteString.Builder.Char.Utf8
 import Data.Aeson.Types (ToJSON(..), Value(..))
+import Data.Attoparsec.Number (Number(..))
 import Data.Monoid (mappend)
 import Numeric (showHex)
-import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString.Lazy.Char8 as L
 import qualified Data.Map as M
 import qualified Data.Text as T
 import qualified Data.Vector as V
+import qualified Text.Show.ByteString as S
 
 -- | Encode a JSON value to a 'Builder'.
 fromValue :: Value -> Builder
 fromValue Null = fromByteString "null"
 fromValue (Bool b) = fromByteString $ if b then "true" else "false"
-fromValue (Number n) = fromByteString (B.pack (show n))
+fromValue (Number (I n)) = fromLazyByteString (S.show n)
+fromValue (Number (D n)) = fromLazyByteString (S.show n)
 fromValue (String s) = string s
 fromValue (Array v)
     | V.null v = fromByteString "[]"
   where
     quote q = case T.uncons t of
                 Just (c,t') -> fromText h `mappend` escape c `mappend` quote t'
-                Nothing -> fromText h
+                Nothing     -> fromText h
         where (h,t) = T.break isEscape q
     isEscape c = c == '\"' || c == '\\' || c < '\x20'
     escape '\"' = fromByteString "\\\""
     base == 4.*,
     blaze-builder >= 0.2.1.4,
     bytestring,
+    bytestring-show,
     containers,
     deepseq,
     hashable,