'How to set the float precision for the Json::FastWriter using C++ and jsoncpp
I am accessing some json objects via a rest interface, by converting the Json::Value to a string and respond this string to the request.
std::string json_to_string(Json::Value& json_object) {
Json::FastWriter fastWriter;
return fastWriter.write(json_object);
}
Numeric values parsed from a float variable are looking like this
"float_value":0.089996337890625
How can I set a precision for the Json::FastWriter?
For saving the json object to the filesystem, I a found a simple solution, but I was not able to transfer this to the Json::FastWriter (which is necessary for the rest interface).
int save_json_file(std::string path, Json::Value json_object) {
Json::Value root;
Json::StreamWriterBuilder builder;
builder["precision"] = 3;
const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
std::ofstream ofs;
ofs.open(path);
writer->write(json_object, &ofs);
return EXIT_SUCCESS;
}
Alternative suggestions are welcome!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
