dev65
dev65

Reputation: 1605

why rapidjson fails to get this integer value

I'm surprised that the following code doesn't work as intended , I remember I had similar json which contained a lot of numbers but I don't know when it stopped working !!!

std::string_view jsonStr = "{ \"num\": 1, \"str\": 's' }";
rapidjson::Document doc;
doc.Parse(jsonStr.data());
std::cout << "num : " << doc["num"].GetUint() << std::endl;

the output :

0

if I remove the str I get 1 !

Am I making something wrong ?

Upvotes: 0

Views: 379

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

That is not valid JSON.

Strings are surrounded by double quotes, not single quotes.

Refer to https://json.org for the schema.

Then check for error codes in your JSON parsing library: somewhere it will be telling you this, and you're ignoring it.

GetUint() on a non-existent property of a broken document will be returning zero either by default or as part of some "undefined behaviour" in the library; read its documentation to find out which.

Upvotes: 1

Related Questions