I am having a hard time with JSON.jl. I want to serialize large Float64 instances without scientific notation and the only way I was able to get at least the rounding working results in a string, instead of a float value ;) I am reposting it from Discourse since I feel like it's a too specific question and might be better discussed here.
using Printf
using JSON
import JSON.Serializations: CommonSerialization, StandardSerialization
import JSON.Writer: StructuralContext, show_json
struct RoundedFloatSerialization <: CommonSerialization end
show_json(io::StructuralContext,
::RoundedFloatSerialization,
f::AbstractFloat) =
show_json(io, StandardSerialization(), @sprintf("%.3f", f))
Here is an example which shows that 1e4 is serialized to 10000.0 by default, whereas 1e9 to 1.0e9. The hook into the show_json works, but it yields a String for obvious reasons, see below an example.
I am having a hard time with JSON.jl. I want to serialize large
Float64
instances without scientific notation and the only way I was able to get at least the rounding working results in a string, instead of a float value ;) I am reposting it from Discourse since I feel like it's a too specific question and might be better discussed here.Here is an example which shows that
1e4
is serialized to10000.0
by default, whereas1e9
to1.0e9
. The hook into theshow_json
works, but it yields aString
for obvious reasons, see below an example.Where should I intercept the scientific notation for large floats without being a pirate? :pirate_flag: