jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

Unexpected behavior with foreach for Json::arrayType

Open archilanfedorov opened this issue 5 months ago • 1 comments

Describe the bug bad foreach cycle for arrayValue

To Reproduce

	Json::Value j;
	j["version"] = "1.0.0";
	j["name"] = "arch";
	j["arr"] = Json::arrayValue;
	j["arr"][5] = "Hello, World!";
	for (auto it : j["arr"]) {
		std::cout << it << std::endl;
	}

in output only "Hello, World"

Expected behavior in output: 5 null's and Hello, World!

Desktop (please complete the following information):

  • OS: Win32
  • jsoncpp Version: 1.9.5

archilanfedorov avatar Jul 09 '25 05:07 archilanfedorov

What you are asking for would also be unexpected, for other people. In JavaScript, "arrays" are just a map who's keys all happen to be integers. This is mimicking that and is thus arguably correct. Arguably, asking for keys that aren't populated should actually return undefined rather than null. That said JSON is not actual JavaScript so that argument isn't conclusive. At the same time, I don't think the JSON standard say anything at all about anything past syntax.

What should happen with iteration if other keys were added?:

  • -5
  • "5"
  • "foo"

bcsgh avatar Sep 02 '25 01:09 bcsgh