-
|
Hello, I'm trying find a way to trim excess zero when dumping to json, but I couldn't find any discussion about it. nlohmann::json j = {
{"time", 1.0},
{"duration", 2.0f},
};
// Current: {"duration":2.0,"time":1.0}
// Desired: {"duration":2,"time":1}
j.dump(-1);My main motivation for this is just reducing file size. |
Beta Was this translation helpful? Give feedback.
Answered by
gregmarr
Jan 5, 2026
Replies: 1 comment 6 replies
-
|
This is not possible, because we want to keep sufficient precision to roundtrip the numbers. If you want to save bandwidth, consider using a binary format like CBOR. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think if you're seeing the upper range of that, then you probably have other differences. I don't think it's possible to get a 66% reduction by eliminating the trailing zeros for anything larger than a file with just a single integer between 0 and 9 and no trailing newline. Then you would get the 3 bytes with the trailing zero and 1 byte without it, for a 66.6% reduction. Anything beyond that, and all the extra brackets, commas, colons, quotes, and names are going to reduce that percentage.
The difference between
{"duration":2,"time":1}and{"duration":2.0,"time":1.0}is 23 bytes vs 27 bytes. The reduced file is 14.8% smaller…