haskell - In aeson, how do you encode from a Value without resulting in scientific notation? -
i have parsed large amount of json, manipulated values , i'd write out. aeson decodes numbers scientific, when encodes it, default, scientific shows numbers in scientific notation in many cases, , aeson not offer means can see change that.
> decode "[\"asdf\", 1, 1.0, 1000000000.1, 0.01]" :: maybe value (array [string "asdf",number 1.0,number 1.0,number 1.0000000001e9,number 1.0e-2]) encode (array [string "asdf",number 1.0,number 1.0,number 1.0000000001e9,number 1.0e-2]) "[\"asdf\",1,1,1.0000000001e9,1.0e-2]" > encode (array [string "asdf", number 1, number 1.0, number 1000000000.1, number 0.01]) "[\"asdf\",1,1,1.0000000001e9,1.0e-2]"
how can write out value numbers in more acceptable format other languages can consume? let's pretend i'm not concerned precision loss or integer overflows. scientific package has means format numbers in manner, aeson happened not use it.
>formatscientific fixed nothing (0.01) "0.01" >formatscientific fixed nothing (1000000000.1) "1000000000.1"
the pretty printer aeson (only since version 0.8, quite recent) has configuration option you're asking:
prelude> :m +data.aeson data.aeson.encode.pretty prelude data.aeson data.aeson.encode.pretty> encodepretty' (defconfig{confnumformat=decimal}) (array [string "asdf",number 1.0,number 1.0,number 1.0000000001e9,number 1.0e-2]) "[\n \"asdf\",\n 1.0,\n 1.0,\n 1000000000.1,\n 0.01\n]"
unfortunately, pretty-printer outputs actual integral numbers 1.0
instead of 1
, consider more severe problem printing decimals in scientific notation (which should ok everywhere). happens default setting:
prelude data.aeson data.aeson.encode.pretty> encodepretty (array [string "asdf",number 1.0,number 1.0,number 1.0000000001e9,number 1.0e-2]) "[\n \"asdf\",\n 1.0,\n 1.0,\n 1.0000000001e9,\n 1.0e-2\n]"
version 0.7.2 still printed integers without fractional part, normal aeson encode
does:
aeson-pretty-0.7.2:data.aeson.encode.pretty> encodepretty (array [string "asdf",number 1.0,number 1.0,number 1.0000000001e9,number 1.0e-2]) "[\n \"asdf\",\n 1,\n 1,\n 1.0000000001e9,\n 1.0e-2\n]"
i think i'll file bug report.
note there's custom
numberformat
option, gives complete control how numbers should appear.
Comments
Post a Comment