Compare commits

..
Author SHA1 Message Date
Niels Lohmann b73b7c72db 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>
2026-11-16 13:07:47 +01:00
9 changed files with 112 additions and 185 deletions

No files matched your search

@@ -100,8 +100,7 @@ points apply to every edit:
[`root()`](root.md). New values (and the element sequences of an edited array/object) go to storage owned by the
document, allocated on demand.
- **A view keeps referring to the same value.** After [`set`](set.md) replaces the value a view refers to, that view
sees the new value; a view of a value that a later edit replaces or drops keeps showing what it last held. Such a
view can still be edited: the edit changes only that detached value, never anything reachable from `root()`. An edit
sees the new value; a view of a value that a later edit replaces or drops keeps showing what it last held. An edit
of an array or object, however, **invalidates the iterators taken over it** (its members may now live in a
different sequence), and a string obtained with [`get_string()`](../basic_json_view/get_string.md) stays valid even
as further edits happen (earlier buffers of edited text are kept alive, not overwritten).
@@ -164,9 +164,7 @@ general.
The same switch happens, for the same reason, when overload 1. replaces a multi-node array/object value with a
scalar: the *parent's* element sequence is what has to switch to links, not `target` itself, because the parent
originally stepped over `target`'s whole subtree by its node count, which no longer applies once `target` is a
one-node scalar. This also holds when `target` is no longer part of the document (an edit erased or replaced it, or
a value that contains it): if the container that `target` was in still uses its parsed layout, it switches to links
(otherwise nothing steps over `target` any more), and only the dropped values change.
one-node scalar.
## Examples
+41 -2
View File
@@ -636,7 +636,7 @@ class serializer
put_literal("\"subtype\": ");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
}
else
{
@@ -662,7 +662,7 @@ class serializer
put_literal("],\"subtype\":");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
put_char('}');
}
else
@@ -1883,6 +1883,40 @@ class serializer
write_buffer_pos = pos;
}
/*!
@brief dump the subtype of a binary value
The subtype is a std::uint64_t, whatever the number types are. @ref
dump_integer converts its argument to the magnitude type of
number_unsigned_t, so it writes the subtype only if that type has at least
64 bits; otherwise (it would truncate the subtype) the digits are written
one by one.
@param[in] subtype the subtype to dump
*/
void dump_subtype(std::uint64_t subtype)
{
dump_subtype(subtype, std::integral_constant<bool, magnitude_has_at_least_64_bits> {});
}
void dump_subtype(std::uint64_t subtype, std::true_type /*magnitude has at least 64 bits*/)
{
dump_integer(subtype);
}
void dump_subtype(std::uint64_t subtype, std::false_type /*magnitude has less than 64 bits*/)
{
std::array<char, 20> digits{{}}; // 2^64 - 1 has 20 digits
std::size_t start = digits.size();
do
{
digits[--start] = static_cast<char>('0' + (subtype % 10));
subtype /= 10;
}
while (subtype != 0);
put_string(digits, start, digits.size());
}
/*!
@brief dump an integer
@@ -2136,6 +2170,11 @@ class serializer
static constexpr bool magnitude_has_at_most_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits <= 64;
/// whether every std::uint64_t fits number_magnitude_t (then dump_integer
/// can write a binary subtype)
static constexpr bool magnitude_has_at_least_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits >= 64;
/// whether @a x fits 64 bits: always if number_magnitude_t has at most
/// 64 bits (selected by type, as the comparison would be reported as
/// always true), else compared
+2 -15
View File
@@ -384,22 +384,9 @@ class editor
// for the container is linear in the size of the document, so
// links (which are marked in the slot) avoid it.
node* const p = parent_known ? parent : find_parent(m_doc, slot);
if (p != nullptr)
if (p != nullptr && ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0))
{
if ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0)
{
block_of(m_doc, p, 0);
}
}
else
{
// the slot is no longer part of the document, but the
// (dropped) container it is in may still step over it
node* const q = find_layout_parent(m_doc, slot);
if (q != nullptr)
{
block_of(m_doc, q, 0);
}
block_of(m_doc, p, 0);
}
}
*slot = e.scalar;
+1 -38
View File
@@ -194,9 +194,7 @@ inline node* block_of(document_data& d, node* n, std::size_t extra)
h->len = n->len;
h->next = static_cast<std::uint32_t>(used);
node* o = h + 1;
// (the bound on o keeps the copy inside the block even if the elements
// disagreed with the element count)
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n), *const full = h + used; c != e && o != full;)
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n); c != e;)
{
if (object)
{
@@ -278,41 +276,6 @@ inline node* find_parent(const document_data& d, const node* target)
}
}
/// The container whose contiguous layout (in the parsed index, or in a new
/// value) steps over `target`, also when an edit dropped the container or a
/// value that encloses it; nullptr if there is none, or if the container no
/// longer walks that layout (its elements moved). An edit never gives a node a
/// larger extent than it was parsed or encoded with (a scalar is one node, an
/// assigned array/object keeps the extent of the slot), so the containers whose
/// extent covers `target` are its original ancestors: the nearest one before it
/// is its original parent. The root of a new value that is the element sequence
/// of another container is walked by that container (which find_parent() finds
/// while it still uses the sequence), not by itself.
inline node* find_layout_parent(const document_data& d, const node* target)
{
const std::less<const node*> lt{};
const node* lo = d.nodes;
if (lt(target, lo) || !lt(target, d.nodes + d.node_count))
{
auto it = d.edits->regions.upper_bound(target);
if (it == d.edits->regions.begin())
{
return nullptr; // LCOV_EXCL_LINE (an array/object with elements is in the index or a new value)
}
--it;
lo = it->second != nullptr ? it->first + 1 : it->first;
}
for (const node* c = target; c != lo;)
{
--c;
if (is_container(*c) && lt(target, c + c->next))
{
return (c->flags & node_flags::moved) == 0 ? const_cast<node*>(c) : nullptr; // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to the document
}
}
return nullptr;
}
} // namespace view
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END
+41 -2
View File
@@ -38122,7 +38122,7 @@ class serializer
put_literal("\"subtype\": ");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
}
else
{
@@ -38148,7 +38148,7 @@ class serializer
put_literal("],\"subtype\":");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
put_char('}');
}
else
@@ -39369,6 +39369,40 @@ class serializer
write_buffer_pos = pos;
}
/*!
@brief dump the subtype of a binary value
The subtype is a std::uint64_t, whatever the number types are. @ref
dump_integer converts its argument to the magnitude type of
number_unsigned_t, so it writes the subtype only if that type has at least
64 bits; otherwise (it would truncate the subtype) the digits are written
one by one.
@param[in] subtype the subtype to dump
*/
void dump_subtype(std::uint64_t subtype)
{
dump_subtype(subtype, std::integral_constant<bool, magnitude_has_at_least_64_bits> {});
}
void dump_subtype(std::uint64_t subtype, std::true_type /*magnitude has at least 64 bits*/)
{
dump_integer(subtype);
}
void dump_subtype(std::uint64_t subtype, std::false_type /*magnitude has less than 64 bits*/)
{
std::array<char, 20> digits{{}}; // 2^64 - 1 has 20 digits
std::size_t start = digits.size();
do
{
digits[--start] = static_cast<char>('0' + (subtype % 10));
subtype /= 10;
}
while (subtype != 0);
put_string(digits, start, digits.size());
}
/*!
@brief dump an integer
@@ -39622,6 +39656,11 @@ class serializer
static constexpr bool magnitude_has_at_most_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits <= 64;
/// whether every std::uint64_t fits number_magnitude_t (then dump_integer
/// can write a binary subtype)
static constexpr bool magnitude_has_at_least_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits >= 64;
/// whether @a x fits 64 bits: always if number_magnitude_t has at most
/// 64 bits (selected by type, as the comparison would be reported as
/// always true), else compared
+3 -53
View File
@@ -5564,9 +5564,7 @@ inline node* block_of(document_data& d, node* n, std::size_t extra)
h->len = n->len;
h->next = static_cast<std::uint32_t>(used);
node* o = h + 1;
// (the bound on o keeps the copy inside the block even if the elements
// disagreed with the element count)
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n), *const full = h + used; c != e && o != full;)
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n); c != e;)
{
if (object)
{
@@ -5648,41 +5646,6 @@ inline node* find_parent(const document_data& d, const node* target)
}
}
/// The container whose contiguous layout (in the parsed index, or in a new
/// value) steps over `target`, also when an edit dropped the container or a
/// value that encloses it; nullptr if there is none, or if the container no
/// longer walks that layout (its elements moved). An edit never gives a node a
/// larger extent than it was parsed or encoded with (a scalar is one node, an
/// assigned array/object keeps the extent of the slot), so the containers whose
/// extent covers `target` are its original ancestors: the nearest one before it
/// is its original parent. The root of a new value that is the element sequence
/// of another container is walked by that container (which find_parent() finds
/// while it still uses the sequence), not by itself.
inline node* find_layout_parent(const document_data& d, const node* target)
{
const std::less<const node*> lt{};
const node* lo = d.nodes;
if (lt(target, lo) || !lt(target, d.nodes + d.node_count))
{
auto it = d.edits->regions.upper_bound(target);
if (it == d.edits->regions.begin())
{
return nullptr; // LCOV_EXCL_LINE (an array/object with elements is in the index or a new value)
}
--it;
lo = it->second != nullptr ? it->first + 1 : it->first;
}
for (const node* c = target; c != lo;)
{
--c;
if (is_container(*c) && lt(target, c + c->next))
{
return (c->flags & node_flags::moved) == 0 ? const_cast<node*>(c) : nullptr; // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to the document
}
}
return nullptr;
}
} // namespace view
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END
@@ -6559,22 +6522,9 @@ class editor
// for the container is linear in the size of the document, so
// links (which are marked in the slot) avoid it.
node* const p = parent_known ? parent : find_parent(m_doc, slot);
if (p != nullptr)
if (p != nullptr && ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0))
{
if ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0)
{
block_of(m_doc, p, 0);
}
}
else
{
// the slot is no longer part of the document, but the
// (dropped) container it is in may still step over it
node* const q = find_layout_parent(m_doc, slot);
if (q != nullptr)
{
block_of(m_doc, q, 0);
}
block_of(m_doc, p, 0);
}
}
*slot = e.scalar;
+22
View File
@@ -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}");
}
-70
View File
@@ -589,76 +589,6 @@ TEST_CASE("json_view edits: a container that was added is replaced by a scalar")
CHECK(d.root().materialize() == json::parse(R"({"a":[1,6,{"k":"s"}]})"));
}
TEST_CASE("json_view edits: a scalar replaces an array or object that is no longer part of the document")
{
// the dropped container that the array/object is in still steps over its
// parsed extent: that container switches to links first, as a live one does
SECTION("members of an erased object")
{
json_editable_document d = json_editable_document::parse(R"({"p": {"a": [1, [2]], "b": 3, "e": []}, "q": 0})");
const json_editable_view p = d.root()["p"];
const json_editable_view a = p["a"];
d.erase(d.root(), "p");
d.set(a[1], 5);
CHECK(p.dump() == R"({"a":[1,5],"b":3,"e":[]})");
d.set(a, 5);
CHECK(p.size() == 3);
CHECK(p.dump() == R"({"a":5,"b":3,"e":[]})");
d.push_back(p["e"], 6);
CHECK(p.dump() == R"({"a":5,"b":3,"e":[6]})");
CHECK(d.root().dump() == R"({"q":0})");
}
SECTION("an array whose elements moved")
{
json_editable_document d = json_editable_document::parse(R"({"p": {"a": [1, 2], "b": 3}, "q": 0})");
const json_editable_view p = d.root()["p"];
const json_editable_view a = p["a"];
d.erase(d.root(), "p");
d.push_back(a, 4); // (the extent of a stays)
d.set(a, 5);
CHECK(p.dump() == R"({"a":5,"b":3})");
CHECK(d.root().dump() == R"({"q":0})");
}
SECTION("an element of an erased array, which grows afterwards")
{
json_editable_document d = json_editable_document::parse(R"({"p": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], "q": 0})");
const json_editable_view p = d.root()["p"];
d.erase(d.root(), "p");
d.set(p[0], 5);
d.push_back(p, 1);
CHECK(p.dump() == "[5,1]");
CHECK(d.root().dump() == R"({"q":0})");
}
SECTION("values in an erased new value")
{
json_editable_document d = json_editable_document::parse("[0]");
const json_editable_view x = d.push_back(d.root(), json::parse("[[[1, 2]], 3]"));
const json_editable_view y = x[0];
d.erase(d.root(), 1);
d.set(y[0], 5);
CHECK(x.dump() == "[[5],3]");
d.set(y, 5);
CHECK(x.dump() == "[5,3]");
CHECK(d.root().dump() == "[0]");
}
SECTION("values in the elements that an array had before it was replaced")
{
json_editable_document d = json_editable_document::parse("[0]");
d.set(d.root()[0], json::parse("[[[1, 2]]]"));
const json_editable_view y = d.root()[0][0];
d.set(d.root()[0], json::array({3}));
d.set(y[0], 5);
CHECK(y.dump() == "[5]");
d.set(y, 5);
CHECK(y.dump() == "5");
CHECK(d.root().dump() == "[[3]]");
}
}
#if !defined(JSON_NOEXCEPTION)
TEST_CASE("json_view edits: the size of a text arena")
{