@@ -18,19 +18,21 @@ module Data.Aeson.Encode
18import Blaze.ByteString.Builder
18import Blaze.ByteString.Builder
19import Blaze.ByteString.Builder.Char.Utf8
19import Blaze.ByteString.Builder.Char.Utf8
20import Data.Aeson.Types (ToJSON(..), Value(..))
20import Data.Aeson.Types (ToJSON(..), Value(..))
21+import Data.Attoparsec.Number (Number(..))
21import Data.Monoid (mappend)
22import Data.Monoid (mappend)
22import Numeric (showHex)
23import Numeric (showHex)
23−import qualified Data.ByteString.Char8 as B
24import qualified Data.ByteString.Lazy.Char8 as L
24import qualified Data.ByteString.Lazy.Char8 as L
25import qualified Data.Map as M
25import qualified Data.Map as M
26import qualified Data.Text as T
26import qualified Data.Text as T
27import qualified Data.Vector as V
27import qualified Data.Vector as V
28+import qualified Text.Show.ByteString as S
29-- | Encode a JSON value to a 'Builder'.
30-- | Encode a JSON value to a 'Builder'.
30fromValue :: Value -> Builder
31fromValue :: Value -> Builder
31fromValue Null = fromByteString "null"
32fromValue Null = fromByteString "null"
32fromValue (Bool b) = fromByteString $ if b then "true" else "false"
33fromValue (Bool b) = fromByteString $ if b then "true" else "false"
33−fromValue (Number n) = fromByteString (B.pack (show n))
34+fromValue (Number (I n)) = fromLazyByteString (S.show n)
35+fromValue (Number (D n)) = fromLazyByteString (S.show n)
34fromValue (String s) = string s
36fromValue (String s) = string s
35fromValue (Array v)
37fromValue (Array v)
36 | V.null v = fromByteString "[]"
38 | V.null v = fromByteString "[]"
@@ -52,7 +54,7 @@ string s = fromChar '"' `mappend` quote s `mappend` fromChar '"'
53 quote q = case T.uncons t of
55 quote q = case T.uncons t of
54 Just (c,t') -> fromText h `mappend` escape c `mappend` quote t'
56 Just (c,t') -> fromText h `mappend` escape c `mappend` quote t'
55− Nothing -> fromText h
57+ Nothing -> fromText h
56 where (h,t) = T.break isEscape q
58 where (h,t) = T.break isEscape q
57 isEscape c = c == '\"' || c == '\\' || c < '\x20'
59 isEscape c = c == '\"' || c == '\\' || c < '\x20'
58 escape '\"' = fromByteString "\\\""
60 escape '\"' = fromByteString "\\\""