Getting raw string (or bytes) for a double value in rapidjson parsing?

3k views Asked by At

Is there a way to get underlying bytes for a double value while parsing json using rapidjson.

Look at below code:

string temp_json2 = "{\"byte_size\":1000.3}";
rapidjson::Document doc;
doc.Parse<0>(temp_json2.c_str());

rapidjson::Value::ConstMemberIterator iter = doc.MemberBegin();

const rapidjson::Value& key = iter->name;
std::cout << key.GetString() << " = "; 

const rapidjson::Value& val = iter->value;
std::cout << val.GetDouble();

I want to get something like

val.GetRawString(); instead of val.GetDouble();

Reason I need this is that I don't want any precision to be lost during conversion. Note that I don't have a choice to modifying json to put quotes around double value.

2

There are 2 answers

1
Milo Yip On

Currently no.

I am working on a "full-precision" parsing option (for quite long time) which can be precisely parse string into double. The double-to-string conversion is already exact using grisu2 algorithm. But if a string cannot be represented by double precisely, it will still lose some precision.

To support your requirement, it may need to add an parsing option, and changing the SAX and DOM interface. If you would like this feature to be implement, please report here for further discussion.

0
Saleem gagguturu On

Looks like it is possible:

{
    "hash": "00000000206d413bdd4d020a7df959176440e7b52f120f3416db11cb26aaaa8f", 
    "bigint": 13671375398414879143589706241811147679151753447299444772946167816777, 
    "time": "1551597576", 
    "special": false
}
rapidjson::Document document;
document.Parse<rapidjson::kParseNumbersAsStringsFlag>( JSONmessage );

std::cout << document["hash"].GetString() << std::endl;
std::cout << document["bigint"].GetString() << std::endl;

Source: https://github.com/Tencent/rapidjson/issues/1458