Jsoncpp writing float values incorrectly Jsoncpp writing float values incorrectly json json

Jsoncpp writing float values incorrectly


It is actually an issue of floating point number parsing/printing implementation. Although floating point numbers can only represent some decimal numbers exactly (0.25 is one of ~2^64), it is necessary to parse a string representation to the nearest binary representation. When printing floating point, it is also necessary to print the (preferably the shortest) string representation which can be restored to the binary representation.

I admit that I had not investigate JsonCPP to see if there is a solution for this. But as I am the author of RapidJSON, I tried to see how RapidJSON performs for this:

const char json[] =     "{"    "\"MinXValue\": 0.1,"    "\"MaxXValue\": 0.15,"    "\"MinYValue\": 0.25,"    "\"MaxYValue\": 1.1,"    "\"MinObjectSizeValue\": 1"    "}";using namespace rapidjson;Document d;d.Parse(json);StringBuffer sb;PrettyWriter<StringBuffer> writer(sb);d.Accept(writer);std::cout << sb.GetString();

And the result:

{    "MinXValue": 0.1,    "MaxXValue": 0.15,    "MinYValue": 0.25,    "MaxYValue": 1.1,    "MinObjectSizeValue": 1}

RapidJSON implemented both parsing and printing algorithms internally. Normal precision parsing will have maximum 3 ULP errors, but with full precision parsing flag (kParseFullPrecisionFlag) it can always parse to nearest representation. The printing part implemented Grisu2 algorithm. It does always generate an exact result, and more than 99% of time to be shortest (optimal).

Actually, using strtod() and sprintf(..., "%.17g", ...) can solve this problem too. But they are much slower in current C/C++ standard library. For example, I have done a benchmark for printing double. So in RapidJSON we implemented its own optimized header-only solutions.


One solution is to a make small change to the jsoncpp source file.

Replace the 17 with a 15 on the following line such that it reads (line 4135 in my copy):

std::string valueToString(double value) { return valueToString(value, false, 15); }

Basically it's reducing the max number of printed digits from 17 to 15, but if you're ok with that it seems to fix all the undesirable printing artefacts you mention. I think one could also argue you shouldn't be using json's to pass around >15 significant digits anyways (near the limit of double precision), but that's another story...

E.g. what used to print for me as:

"yo" : 1726.6969999999999,

now prints as:

"yo" : 1726.697,


This feature has alreay been supported, for those who are still looking into this problem: https://github.com/open-source-parsers/jsoncpp/commit/772f634548f0cec058a0f16a2e641d9f7b78343d

std::ofstream ofs("example.json");Json::Value root;// ... Build your json object....Json::StreamWriterBuilder wbuilder;wbuilder["indentation"] = "";wbuilder.settings_["precision"] = 6;std::unique_ptr<Json::StreamWriter> writer(wbuilder.newStreamWriter());// Write to file.writer->write(root, &ofs);ofs.close();