mirror of
https://github.com/nlohmann/json.git
synced 2026-08-29 20:47:14 +00:00
Fix unflatten and binary dumping for non-default configurations
unflatten() decided between array and object by looking at the first reference
token it happened to see for a node: it started an array only when that token
was 0. With a sorted object type the token 0 always arrives first, so the
result was correct by accident; with an object type whose iteration order is
unspecified, {"/c/2":3,"/c/1":2,"/c/0":1} unflattened to an object with the
keys "0", "1", and "2" instead of an array.
Collect the pointer prefixes that have a reference token 0 among their children
before building the result, and let get_and_create() consult that set. The
outcome is now independent of the iteration order and matches, for every input,
what a sorted object type produced before: a value is restored as an array if
and only if one of its keys is 0. Iterating the flattened object in a different
order would have been simpler, but it would have changed the key order of the
result for insertion-ordered object types.
The serializer, std::hash, and the UBJSON writer converted the elements of a
binary value to an integer implicitly, which does not compile for a BinaryType
whose value type is std::byte, and which made dump() write the bytes of a
signed value type as negative numbers. Convert to std::uint8_t explicitly in
all three places, so every byte type dumps as 0..255. The default
std::vector<std::uint8_t> configuration is unaffected.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018hxZxz8svM54c6ATEvXp5E
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
co-authored by
Claude Opus 5
parent
06feaa8d04
commit
7a37a27a67
@@ -1154,6 +1154,39 @@ TEST_CASE("regression tests 2")
|
||||
CHECK(!default_json.is_binary());
|
||||
}
|
||||
|
||||
SECTION("dumping a binary value with a custom BinaryType")
|
||||
{
|
||||
// the elements of a binary value are dumped as the numbers 0..255,
|
||||
// whatever the value type of the configured BinaryType is
|
||||
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
|
||||
CHECK(json_4804::binary(bytes).dump() == R"({"bytes":[0,1,255],"subtype":null})");
|
||||
CHECK(json_4804::binary(bytes, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
|
||||
CHECK(json_4804::binary({}).dump() == R"({"bytes":[],"subtype":null})");
|
||||
|
||||
// a signed byte type must not dump negative numbers
|
||||
using json_char_binary = nlohmann::basic_json <
|
||||
std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t,
|
||||
double, std::allocator, nlohmann::adl_serializer, std::vector<char>, void >;
|
||||
const std::vector<char> chars{char(0), char(1), char(0xFF)};
|
||||
CHECK(json_char_binary::binary(chars).dump() == R"({"bytes":[0,1,255],"subtype":null})");
|
||||
|
||||
// the default binary type is unchanged
|
||||
CHECK(json::binary({0, 1, 255}, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
|
||||
}
|
||||
|
||||
SECTION("hashing and UBJSON with a custom BinaryType")
|
||||
{
|
||||
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
|
||||
const auto j = json_4804::binary(bytes);
|
||||
|
||||
CHECK(std::hash<json_4804> {}(j) == std::hash<json_4804> {}(j));
|
||||
CHECK(json_4804::from_cbor(json_4804::to_cbor(j)) == j);
|
||||
CHECK(json_4804::from_msgpack(json_4804::to_msgpack(j)) == j);
|
||||
|
||||
// UBJSON has no binary type, so binary values are written as arrays
|
||||
CHECK(json_4804::from_ubjson(json_4804::to_ubjson(j)) == json_4804({0, 1, 255}));
|
||||
}
|
||||
|
||||
SECTION("discussion #4209 - custom BinaryType extraction from parsed array")
|
||||
{
|
||||
// Test that extracting a custom BinaryType from a parsed JSON array still works
|
||||
|
||||
Reference in New Issue
Block a user