Search code examples
c++jsonrapidjson

Loss of floating point values :: reading JSON with rapidjson


In the project, the program reads JSON Document (using rapidjson) and then takes a part of that JSON document and tries to create new JSON Value. Following code is used for that

# jsonstring is the JSON string
rapidjson::Document firstJSON = readJsonString(jsonstring);
# val is the Value which is a small part of firstJSON document
rapidjson::Value &val = firstJSON["A"]["B"];

jsonstring is a follows

{
    "A":
    {
       "B":
       {
           "e0":0.03974359855055809,
           "e1":0.17799679934978486
       }
    }
    "R": "stringval"
}

so value of Val should be

"B":
{
  "e0":0.03974359855055809,
  "e1":0.17799679934978486
}

but it is giving me this output: Val

"B":
{
  "e0":0.0,
  "e1":0.1
}

So clearly when trying to read a part of json from Document and reading it in rapidjson::Value, it is just taking single decimal value.

Can anyone please tell me how to read from rapidjson::Document and make rapidjson::Value with Double precision of values ?

Things have tried.

  1. Tried to set the precision, but I didn't get a way to set precision for reading from rapidjson::Document to rapidjson::Value

Thank you so much !!


Solution

  • Converted OP's comment to an answer:

    The precision was set at 1, that's the reason you are getting this output. So set the precision to 18 and this issue should be solved.