How to solve the double problem in JsonCpp
In this article, the editor introduces in detail "how to solve the double problem in JsonCpp". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to solve the double problem in JsonCpp" can help you solve your doubts.
Values in the json file
Program code
New_item ["Voltage"] = 8.622 ["Current"] = 8.63456 ["Energy"] = 8.234234
Program running result
In the json_write.cpp of Jsoncpp
Std::string valueToString (double value, bool useSpecialFloats, unsigned int precision) {/ / Allocate a buffer that is more than large enough to store the 16 digits of / / precision requested below. Char buffer [32]; int len =-1; char formatString [6]; sprintf (formatString, "%.% dg", precision); / / Print into the buffer. We need not request the alternative representation / / that always has a decimal point because JSON doesn't distingish the / / concepts of reals and integers. If (isfinite (value)) {len = snprintf (buffer, sizeof (buffer), formatString, value);} else {/ / IEEE standard states that NaN values will not compare to themselves if (value! = value) {len = snprintf (buffer, sizeof (buffer), useSpecialFloats? "NaN": "null");} else if (value
< 0) { len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "-Infinity" : "-1e+9999"); } else { len = snprintf(buffer, sizeof(buffer), useSpecialFloats ? "Infinity" : "1e+9999"); } // For those, we do not need to call fixNumLoc, but it is fast. } assert(len >= 0); fixNumericLocale (buffer, buffer + len); return buffer;}
Here the result of sprintf (formatString, "%.% dg", precision); is ".17g". Is a valid number that outputs 17 digits. Insufficient to make up for 17.
The problem of decimal accuracy and insertion and output order of ps:JsonCpp
To put it bluntly, these two problems cannot be solved, as follows:
Officially, the number of decimal places is not supported. The default width of decimal places is 17 places, such as "value": 7.0999999999999996
Officially, output is not supported in insertion order, but in alphabetical order of key. No matter what order you insert, the following output is in this order:
"avg_abcdd": 1.16326400000014, "avg_pxczzczxczxd": 7.0999999999999996, "avg_shczxcdize": 802000.0, "deviccxz": "shebei25", "sh423423fd": 1420, "vcxzcasdasdadczco": 231
Personal emergency idea
For the problem of digital accuracy, you can consider converting it into the precision you need in C++, and then putting it into json as a string. As for the subsequent parsing, you can read the string and then convert it to a number.
Order question, two ideas:
1) do not use key, use the form of append, that is, put each item in a container
Json::Value res; std::string = entry_str; entry_str.append ("zhangsan,123"); entry_str.append ("abc,2596"); Res ["entry"] = entry_str
2) then order by name, comply with the rules, 2333333
After reading this, the article "how to solve the double problem in JsonCpp" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, you are welcome to follow the industry information channel.