Write a binary subtype independently of number_unsigned_t in dump()
The subtype of a binary value is a std::uint64_t, but dump() wrote it with dump_integer, which converts its argument to the magnitude type of number_unsigned_t. With a number_unsigned_t narrower than 64 bits, the subtype was truncated (subtype 300 with std::uint8_t numbers was printed as 44). A dump_subtype helper now writes the subtype for both the compact and the pretty-printed output: through dump_integer if the magnitude type of number_unsigned_t has at least 64 bits (the default json, so its output and speed are unchanged), and with a digit loop otherwise. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
3 files changed
+104
-4
No files matched your search
@@ -483,3 +483,25 @@ TEST_CASE("custom number types: 32-bit integers in the binary formats")
|
||||
CHECK(json32::to_msgpack(values) == nlohmann::json::to_msgpack(values64));
|
||||
CHECK(json32::to_ubjson(values) == nlohmann::json::to_ubjson(values64));
|
||||
}
|
||||
|
||||
TEST_CASE("custom number types: binary subtypes wider than the integer types")
|
||||
{
|
||||
// the subtype of a binary value is a std::uint64_t, whatever the number types are
|
||||
using json8 = nlohmann::json::with_integers_t<std::int8_t, std::uint8_t>;
|
||||
using json16 = nlohmann::json::with_integers_t<std::int16_t, std::uint16_t>;
|
||||
using json32 = nlohmann::json::with_integers_t<std::int32_t, std::uint32_t>;
|
||||
const std::uint64_t max_uint64 = (std::numeric_limits<std::uint64_t>::max)();
|
||||
|
||||
CHECK(json8::binary({1}, 300).dump() == "{\"bytes\":[1],\"subtype\":300}");
|
||||
CHECK(json8::binary({200}, 255).dump() == "{\"bytes\":[200],\"subtype\":255}");
|
||||
CHECK(json16::binary({1}, 65543).dump() == "{\"bytes\":[1],\"subtype\":65543}");
|
||||
CHECK(json32::binary({1}, 4294967301u).dump() == "{\"bytes\":[1],\"subtype\":4294967301}");
|
||||
CHECK(json32::binary({1}, max_uint64).dump() == "{\"bytes\":[1],\"subtype\":18446744073709551615}");
|
||||
CHECK(json32::binary({1}, 0).dump() == "{\"bytes\":[1],\"subtype\":0}");
|
||||
CHECK(json32::binary({1}, 4294967301u).dump(1) == "{\n \"bytes\": [1],\n \"subtype\": 4294967301\n}");
|
||||
CHECK(json8::binary({1}).dump() == "{\"bytes\":[1],\"subtype\":null}");
|
||||
|
||||
// unchanged for the default number types
|
||||
CHECK(nlohmann::json::binary({1}, 4294967301u).dump() == "{\"bytes\":[1],\"subtype\":4294967301}");
|
||||
CHECK(nlohmann::json::binary({1}, max_uint64).dump(1) == "{\n \"bytes\": [1],\n \"subtype\": 18446744073709551615\n}");
|
||||
}
|
||||
Reference in new issue
Block a user