on master help sign in
Make it possible to JSON encode floats
445ea07Evan Czaplicki
src/Json/Encode.hs+12−1
@@ -12,6 +12,7 @@ module Json.Encode
12 , string
12 , string
13 , bool
13 , bool
14 , int
14 , int
15+ , number
15 , null
16 , null
16 , dict
17 , dict
17 , list
18 , list
@@ -25,6 +26,7 @@ import Control.Arrow ((***))
25import qualified Data.ByteString.Char8 as BSC
26import qualified Data.ByteString.Char8 as BSC
26import qualified Data.ByteString.Builder as B
27import qualified Data.ByteString.Builder as B
27import qualified Data.Map as Map
28import qualified Data.Map as Map
29+import qualified Data.Scientific as Sci
28import Data.Monoid ((<>))
30import Data.Monoid ((<>))
29import qualified Data.Text as Text
31import qualified Data.Text as Text
30import qualified Numeric
32import qualified Numeric
@@ -41,6 +43,7 @@ data Value
41 | String B.Builder
43 | String B.Builder
42 | Boolean Bool
44 | Boolean Bool
43 | Integer Int
45 | Integer Int
46+ | Number Sci.Scientific
44 | Null
47 | Null
45
48
46
49
@@ -74,6 +77,11 @@ int =
74 Integer
77 Integer
75
78
76
79
80+number :: Sci.Scientific -> Value
81+number =
82+ Number
83+
84+
77null :: Value
85null :: Value
78null =
86null =
79 Null
87 Null
@@ -150,6 +158,9 @@ encodeUgly value =
150 Integer n ->
158 Integer n ->
151 B.intDec n
159 B.intDec n
152
160
161+ Number scientific ->
162+ B.string7 (Sci.formatScientific Sci.Generic Nothing scientific)
163+
153 Null ->
164 Null ->
154 "null"
165 "null"
155
166
@@ -242,7 +253,7 @@ encodeSequence :: B.Builder -> B.Builder -> (BSC.ByteString -> a -> B.Builder) -
242encodeSequence open close encodeEntry indent first rest =
253encodeSequence open close encodeEntry indent first rest =
243 let
254 let
244 newIndent =
255 newIndent =
245− indent <> "\t"
256+ indent <> " "
246
257
247 newIndentBuilder =
258 newIndentBuilder =
248 B.byteString newIndent
259 B.byteString newIndent