4

I'm using RapidJSON (https://github.com/miloyip/rapidjson) to create quite big arrays (~ 5 MB) and a lot of the space is waste due to too accurate floating point numbers. E.g.

StringBuffer s;
Writer<StringBuffer> writer(s);
writer.StartObject();
writer.String("value");
writer.Double(1.0/3.0);
writer.EndObject();

This results in a json "{'value': 0.33333333333}" which is very annoying when I only need a few significant numbers.

I found a solution Set floating point precision using rapidjson in this post, but it's already a few years old and outdated with the latest rapidjson build. Has anyone got a solution for this?

Community
  • 1
  • 1
Ollie
  • 127
  • 1
  • 3
  • 8

2 Answers2

3

Currently Writer always generate accurate string representation of floating point number, in the sense that parsing the output can restore the original value.

You may try the workaround on this. If you would like to request for providing this in RapidJSON, please comment there and we can further discuss.

Milo Yip
  • 4,902
  • 2
  • 25
  • 27
3

Use rapidjson::Writer::SetMaxDecimalPlaces http://rapidjson.org/classrapidjson_1_1_writer.html#aa7b6967dc237519e2a6d8b3939fb9634

writer.SetMaxDecimalPlaces(3);
writer.Double(1.0/3.0);
Dan
  • 71
  • 4