Skip to content

basic_json::operator[key] and basic_json::value(key) return different values #4626

@benjcarson

Description

@benjcarson

Description

When parsing the following json, operator[key] and value(key) return different results despite 'key' being a valid field.

{
	"array": [
		{
			"id": 3024667086235176575
				},
		{
			"id": 5894322762133244432
				},
		{
			"id": 7912588194672682964
				},
		{
			"id": 16577926123393930376
				}
		]
}

In this case j["array"][0]["id"] != j["array"][0].value("id",0).

Reproduction steps

See minimal example below.

Expected vs. actual results

If a key exists in a json object, I would expect j[key] to return the same value as j.value(key, default).

Minimal code example

Here is a minimal program that reproduces the bug:


#include "json.hpp"
#include <string>

int main(int argc, char* argv[])
{
	std::string s = R"({
                          "array": [
                            {
                              "id": 3024667086235176575
                            },
                            {
                              "id": 5894322762133244432
                            },
                            {
                              "id": 7912588194672682964
                            },
                            {
                              "id": 16577926123393930376
                            }
                          ]
                        })";

	json j = json::parse(s);

	json& arr = j["array"];
	json& arr0 = arr[0];
	uint64_t idTest0 = arr0["id"];
	uint64_t idTest1 = arr0.value("id", 0);

	// Expected idTest0 == idTest1 == 3024667086235176575
	// Got idTest0 == 3024667086235176575
	//     idTest1 == 18446744071677218431

	return 0;
}

Error messages

Compiler and operating system

msvc (VS2022), windows 11

Library version

json-3.11.3

Validation

Metadata

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions