Throw type_error.313 when an unflatten key is a prefix of an earlier key

With an object type that keeps the insertion order (ordered_json), a key
such as "/a" can come after "/a/b". The final assignment then replaced the
object built for "/a/b" without an error, so {"/a/b":2,"/a":1} gave
{"a":1}. json never saw this because std::map visits the shorter key first.

Throw type_error.313 if the target of the final assignment is no longer
null, as json does for the same members. A null value keeps the container,
so {"/a/b":2,"/a":null} gives {"a":{"b":2}} for both object types.

Fixes #6105.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-13 09:59:20 +01:00
1 parent fa992d9b6f
commit ad5c9305d5
4 files changed
+59 -1

No files matched your search

+13
View File
@@ -28742,6 +28742,19 @@ class json_pointer
pos = end;
}
if (JSON_HEDLEY_UNLIKELY(!current->is_null()))
{
// the key is a prefix of an earlier key (only possible if the
// object type does not sort its keys), so the value is the
// array or object built for that key: a null leaves it as it
// is (as when the shorter key comes first), any other value
// conflicts with it
if (element.second.is_null())
{
continue;
}
JSON_THROW(detail::type_error::create(detail::exception_id::unflatten_invalid_value, "invalid value to unflatten", &result));
}
*current = element.second;
}