Actually, this is the problem of implementing parsing floating point numbers. Although floating point numbers can accurately represent only some decimal numbers (0.25 is one of ~ 2 ^ 64), you need to parse the string representation to the nearest binary representation. When printing with floating point, you must also print a (preferably shortest) string representation, which can be restored in binary representation.
I admit that I did not research JsonCPP to see if there is a solution for this. But since 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. Conventional parsing will have a maximum of 3 ULP errors, but with the full precision parsing flag ( kParseFullPrecisionFlag ), it can always parse to the nearest view. In terms of printing, the Grisu2 algorithm is implemented. It always generates an accurate result and more than 99% of the time will be the shortest (optimal).
Actually, using strtod() and sprintf(..., "%.17g", ...) can also solve this problem. But in the current C / C ++ standard library, they are much slower. For example, I made a benchmark to print double . Thus, in RapidJSON we implemented our own optimized header-only solutions.
source share