From a1284028b26d182a89393107825071f6ed7629c7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:50:36 +0100 Subject: [PATCH 01/14] json_view: fix a scalar set() through a view of an erased value (#6132) Signed-off-by: Niels Lohmann --- .../docs/api/basic_json_document/index.md | 3 +- .../docs/api/basic_json_document/set.md | 4 +- include/nlohmann/detail/view/edit.hpp | 17 ++++- include/nlohmann/detail/view/edit_storage.hpp | 39 ++++++++++- single_include/nlohmann/json_view.hpp | 56 ++++++++++++++- tests/src/unit-json_view_edit.cpp | 70 +++++++++++++++++++ 6 files changed, 181 insertions(+), 8 deletions(-) diff --git a/docs/mkdocs/docs/api/basic_json_document/index.md b/docs/mkdocs/docs/api/basic_json_document/index.md index 3d7f41aad..c31857bb3 100644 --- a/docs/mkdocs/docs/api/basic_json_document/index.md +++ b/docs/mkdocs/docs/api/basic_json_document/index.md @@ -100,7 +100,8 @@ 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. An edit + 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 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). diff --git a/docs/mkdocs/docs/api/basic_json_document/set.md b/docs/mkdocs/docs/api/basic_json_document/set.md index 5c68da311..a983a4055 100644 --- a/docs/mkdocs/docs/api/basic_json_document/set.md +++ b/docs/mkdocs/docs/api/basic_json_document/set.md @@ -164,7 +164,9 @@ 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. +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. ## Examples diff --git a/include/nlohmann/detail/view/edit.hpp b/include/nlohmann/detail/view/edit.hpp index 8ddf3f849..af213e9fd 100644 --- a/include/nlohmann/detail/view/edit.hpp +++ b/include/nlohmann/detail/view/edit.hpp @@ -384,9 +384,22 @@ 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 && ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0)) + if (p != nullptr) { - block_of(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); + } } } *slot = e.scalar; diff --git a/include/nlohmann/detail/view/edit_storage.hpp b/include/nlohmann/detail/view/edit_storage.hpp index 34b804c46..d9c0339a7 100644 --- a/include/nlohmann/detail/view/edit_storage.hpp +++ b/include/nlohmann/detail/view/edit_storage.hpp @@ -194,7 +194,9 @@ inline node* block_of(document_data& d, node* n, std::size_t extra) h->len = n->len; h->next = static_cast(used); node* o = h + 1; - for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n); c != e;) + // (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;) { if (object) { @@ -276,6 +278,41 @@ 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 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(c) : nullptr; // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to the document + } + } + return nullptr; +} + } // namespace view } // namespace detail NLOHMANN_JSON_NAMESPACE_END diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 233a55487..b23199021 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -5564,7 +5564,9 @@ inline node* block_of(document_data& d, node* n, std::size_t extra) h->len = n->len; h->next = static_cast(used); node* o = h + 1; - for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n); c != e;) + // (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;) { if (object) { @@ -5646,6 +5648,41 @@ 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 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(c) : nullptr; // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to the document + } + } + return nullptr; +} + } // namespace view } // namespace detail NLOHMANN_JSON_NAMESPACE_END @@ -6522,9 +6559,22 @@ 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 && ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0)) + if (p != nullptr) { - block_of(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); + } } } *slot = e.scalar; diff --git a/tests/src/unit-json_view_edit.cpp b/tests/src/unit-json_view_edit.cpp index db6d3054b..94b4df706 100644 --- a/tests/src/unit-json_view_edit.cpp +++ b/tests/src/unit-json_view_edit.cpp @@ -589,6 +589,76 @@ 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") { From 03ce377876aa421d176ed1913744092558874add Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:51:42 +0100 Subject: [PATCH 02/14] Pass the complete text of a UBJSON/BJData high-precision number to SAX number_float (#6134) Signed-off-by: Niels Lohmann --- .../nlohmann/detail/input/binary_reader.hpp | 9 +++++---- single_include/nlohmann/json.hpp | 9 +++++---- tests/src/unit-bjdata.cpp | 19 +++++++++++++++++++ tests/src/unit-ubjson.cpp | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index bbd2daceb..c7b1b92f8 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -4096,6 +4096,10 @@ class binary_reader auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); const auto result_number = number_lexer.scan(); const auto number_string = number_lexer.get_token_string(); + // the number as the lexer read it, for SAX number_float(): unlike + // get_token_string(), which is meant for error messages and shortens + // long tokens, it is complete; the next scan() replaces it + const string_t number_text = number_lexer.get_string(); const auto result_remainder = number_lexer.scan(); using token_type = typename detail::lexer_base::token_type; @@ -4122,10 +4126,7 @@ class binary_reader number_string, out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr)); } - // number_string is a std::string, while the SAX interface takes a - // string_t; convert explicitly, as the two are only implicitly - // convertible for some string types - return sax->number_float(parsed_float, string_t(number_string.data(), number_string.size())); + return sax->number_float(parsed_float, number_text); } case token_type::uninitialized: case token_type::literal_true: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 473bfa894..d6320c897 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22774,6 +22774,10 @@ class binary_reader auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); const auto result_number = number_lexer.scan(); const auto number_string = number_lexer.get_token_string(); + // the number as the lexer read it, for SAX number_float(): unlike + // get_token_string(), which is meant for error messages and shortens + // long tokens, it is complete; the next scan() replaces it + const string_t number_text = number_lexer.get_string(); const auto result_remainder = number_lexer.scan(); using token_type = typename detail::lexer_base::token_type; @@ -22800,10 +22804,7 @@ class binary_reader number_string, out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr)); } - // number_string is a std::string, while the SAX interface takes a - // string_t; convert explicitly, as the two are only implicitly - // convertible for some string types - return sax->number_float(parsed_float, string_t(number_string.data(), number_string.size())); + return sax->number_float(parsed_float, number_text); } case token_type::uninitialized: case token_type::literal_true: diff --git a/tests/src/unit-bjdata.cpp b/tests/src/unit-bjdata.cpp index 304db5676..6a0dc0f8f 100644 --- a/tests/src/unit-bjdata.cpp +++ b/tests/src/unit-bjdata.cpp @@ -1278,6 +1278,25 @@ TEST_CASE("BJData") CHECK(j.dump() == "3.141592653589793"); } + SECTION("long floating-point number") + { + // the SAX parser receives the number text as it is in the + // input, also when it is longer than the part of a token + // that error messages show + const std::vector lengths = {8192, 8193, 10000}; + for (const auto length : lengths) + { + CAPTURE(length) + const std::string number = "1." + std::string(length - 2, '3'); + std::vector vec = {'H', 'I', static_cast(length & 0xFF), static_cast(length >> 8)}; + vec.insert(vec.end(), number.begin(), number.end()); + SaxEventLogger logger; + CHECK(json::sax_parse(vec, &logger, json::input_format_t::bjdata)); + CHECK(logger.events == std::vector({"number_float(" + number + ")"})); + CHECK(json::from_bjdata(vec).is_number_float()); + } + } + SECTION("errors") { // error while parsing length diff --git a/tests/src/unit-ubjson.cpp b/tests/src/unit-ubjson.cpp index 315f1dea1..58e51b5b3 100644 --- a/tests/src/unit-ubjson.cpp +++ b/tests/src/unit-ubjson.cpp @@ -793,6 +793,25 @@ TEST_CASE("UBJSON") CHECK(j.dump() == "3.141592653589793"); } + SECTION("long floating-point number") + { + // the SAX parser receives the number text as it is in the + // input, also when it is longer than the part of a token + // that error messages show + const std::vector lengths = {8192, 8193, 10000}; + for (const auto length : lengths) + { + CAPTURE(length) + const std::string number = "1." + std::string(length - 2, '3'); + std::vector vec = {'H', 'I', static_cast(length >> 8), static_cast(length & 0xFF)}; + vec.insert(vec.end(), number.begin(), number.end()); + SaxEventLogger logger; + CHECK(json::sax_parse(vec, &logger, json::input_format_t::ubjson)); + CHECK(logger.events == std::vector({"number_float(" + number + ")"})); + CHECK(json::from_ubjson(vec).is_number_float()); + } + } + SECTION("errors") { // error while parsing length From 1f7250f77fd189ddc576f63222b68592e970f241 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:51:52 +0100 Subject: [PATCH 03/14] Write a binary subtype independently of number_unsigned_t in dump() (#6135) Signed-off-by: Niels Lohmann --- include/nlohmann/detail/output/serializer.hpp | 47 +++++++++++++++++-- single_include/nlohmann/json.hpp | 47 +++++++++++++++++-- tests/src/unit-custom-number-types.cpp | 22 +++++++++ 3 files changed, 108 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index eeba0a9de..22fcfa6ac 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -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 @@ -1571,7 +1571,7 @@ class serializer { return n_digits + 3; } - x = x / 10000u; + x = static_cast(x / 10000u); n_digits += 4; } } @@ -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 {}); + } + + 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 digits{{}}; // 2^64 - 1 has 20 digits + std::size_t start = digits.size(); + do + { + digits[--start] = static_cast('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::is_specialized && std::numeric_limits::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::is_specialized + && std::numeric_limits::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 @@ -2161,7 +2200,7 @@ class serializer number_magnitude_t remove_sign(number_integer_t x) noexcept { JSON_ASSERT(x < 0); - return static_cast(-(x + 1)) + 1; + return static_cast(static_cast(-(x + 1)) + 1); } private: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index d6320c897..ae91542de 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -38123,7 +38123,7 @@ class serializer put_literal("\"subtype\": "); if (binary.has_subtype()) { - dump_integer(binary.subtype()); + dump_subtype(binary.subtype()); } else { @@ -38149,7 +38149,7 @@ class serializer put_literal("],\"subtype\":"); if (binary.has_subtype()) { - dump_integer(binary.subtype()); + dump_subtype(binary.subtype()); put_char('}'); } else @@ -39058,7 +39058,7 @@ class serializer { return n_digits + 3; } - x = x / 10000u; + x = static_cast(x / 10000u); n_digits += 4; } } @@ -39370,6 +39370,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 {}); + } + + 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 digits{{}}; // 2^64 - 1 has 20 digits + std::size_t start = digits.size(); + do + { + digits[--start] = static_cast('0' + (subtype % 10)); + subtype /= 10; + } + while (subtype != 0); + put_string(digits, start, digits.size()); + } + /*! @brief dump an integer @@ -39623,6 +39657,11 @@ class serializer static constexpr bool magnitude_has_at_most_64_bits = std::numeric_limits::is_specialized && std::numeric_limits::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::is_specialized + && std::numeric_limits::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 @@ -39648,7 +39687,7 @@ class serializer number_magnitude_t remove_sign(number_integer_t x) noexcept { JSON_ASSERT(x < 0); - return static_cast(-(x + 1)) + 1; + return static_cast(static_cast(-(x + 1)) + 1); } private: diff --git a/tests/src/unit-custom-number-types.cpp b/tests/src/unit-custom-number-types.cpp index 58353d67e..09d716dc2 100644 --- a/tests/src/unit-custom-number-types.cpp +++ b/tests/src/unit-custom-number-types.cpp @@ -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; + using json16 = nlohmann::json::with_integers_t; + using json32 = nlohmann::json::with_integers_t; + const std::uint64_t max_uint64 = (std::numeric_limits::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}"); +} From 6ac411e14c0d18dc9116204f85eb7aba0dd27a96 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:52:02 +0100 Subject: [PATCH 04/14] Report the same error position for bulk number runs as for byte-wise input (#6137) Signed-off-by: Niels Lohmann --- .../nlohmann/detail/input/binary_reader.hpp | 115 ++++++++++------ single_include/nlohmann/json.hpp | 115 ++++++++++------ tests/src/unit-binary_formats.cpp | 123 ++++++++++++++++++ 3 files changed, 267 insertions(+), 86 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index c7b1b92f8..f2facaabd 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -1927,6 +1927,21 @@ class binary_reader return result; } + /*! + @brief move past a value of @a size bytes, starting with @a head, that a + bulk run reads, as get() and get_number() would + + Called before the value is passed on, so that an error reported while + passing it on (see @ref emit_float) has the position and the last byte + that reading the value byte by byte gives. + */ + void bulk_advance(const std::size_t size, const unsigned char head) + { + ia.bulk_skip(size); + chars_read += size; + current = head; + } + /*! @brief read the integers, floats, booleans, and nulls that follow in a definite-length array, without the per-element bookkeeping of @@ -1952,71 +1967,70 @@ class binary_reader { const auto* const data = reinterpret_cast(ia.bulk_data()); const unsigned char head = data[0]; - std::size_t size = 0; bool accepted = false; switch (head) { case 0x18: - size = 2; + bulk_advance(2, head); accepted = emit_unsigned(data[1]); break; case 0x19: - size = 3; + bulk_advance(3, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0x1A: - size = 5; + bulk_advance(5, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0x1B: - size = 9; + bulk_advance(9, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0x38: - size = 2; + bulk_advance(2, head); accepted = emit_cbor_negative_integer(data[1]); break; case 0x39: - size = 3; + bulk_advance(3, head); accepted = emit_cbor_negative_integer(load_big_endian(data + 1)); break; case 0x3A: - size = 5; + bulk_advance(5, head); accepted = emit_cbor_negative_integer(load_big_endian(data + 1)); break; case 0x3B: - size = 9; + bulk_advance(9, head); accepted = emit_cbor_negative_integer(load_big_endian(data + 1)); break; case 0xF4: - size = 1; + bulk_advance(1, head); accepted = sax->boolean(false); break; case 0xF5: - size = 1; + bulk_advance(1, head); accepted = sax->boolean(true); break; case 0xF6: - size = 1; + bulk_advance(1, head); accepted = sax->null(); break; case 0xFA: - size = 5; + bulk_advance(5, head); accepted = emit_float(load_big_endian(data + 1)); break; case 0xFB: - size = 9; + bulk_advance(9, head); accepted = emit_float(load_big_endian(data + 1)); break; default: if (head <= 0x17) { - size = 1; + bulk_advance(1, head); accepted = sax->number_unsigned(static_cast(head)); } else if (head >= 0x20 && head <= 0x37) { - size = 1; + bulk_advance(1, head); accepted = sax->number_integer(static_cast(0x20 - 1 - head)); } else @@ -2026,10 +2040,6 @@ class binary_reader } break; } - // as if read by get() and get_number(): current is the head byte - ia.bulk_skip(size); - chars_read += size; - current = head; --remaining; if (JSON_HEDLEY_UNLIKELY(!accepted)) { @@ -2867,73 +2877,72 @@ class binary_reader { const auto* const data = reinterpret_cast(ia.bulk_data()); const unsigned char head = data[0]; - std::size_t size = 0; bool accepted = false; switch (head) { case 0xC0: // nil - size = 1; + bulk_advance(1, head); accepted = sax->null(); break; case 0xC2: // false - size = 1; + bulk_advance(1, head); accepted = sax->boolean(false); break; case 0xC3: // true - size = 1; + bulk_advance(1, head); accepted = sax->boolean(true); break; case 0xCA: // float 32 - size = 5; + bulk_advance(5, head); accepted = emit_float(load_big_endian(data + 1)); break; case 0xCB: // float 64 - size = 9; + bulk_advance(9, head); accepted = emit_float(load_big_endian(data + 1)); break; case 0xCC: // uint 8 - size = 2; + bulk_advance(2, head); accepted = emit_unsigned(data[1]); break; case 0xCD: // uint 16 - size = 3; + bulk_advance(3, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0xCE: // uint 32 - size = 5; + bulk_advance(5, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0xCF: // uint 64 - size = 9; + bulk_advance(9, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0xD0: // int 8 - size = 2; + bulk_advance(2, head); accepted = emit_signed(static_cast(data[1])); break; case 0xD1: // int 16 - size = 3; + bulk_advance(3, head); accepted = emit_signed(load_big_endian(data + 1)); break; case 0xD2: // int 32 - size = 5; + bulk_advance(5, head); accepted = emit_signed(load_big_endian(data + 1)); break; case 0xD3: // int 64 - size = 9; + bulk_advance(9, head); accepted = emit_signed(load_big_endian(data + 1)); break; default: if (head <= 0x7F) { // positive fixint - size = 1; + bulk_advance(1, head); accepted = sax->number_unsigned(static_cast(head)); } else if (head >= 0xE0) { // negative fixint - size = 1; + bulk_advance(1, head); accepted = sax->number_integer(static_cast(head)); } else @@ -2943,10 +2952,6 @@ class binary_reader } break; } - // as if read by get() and get_number(): current is the last byte - ia.bulk_skip(size); - chars_read += size; - current = data[size - 1]; --remaining; if (JSON_HEDLEY_UNLIKELY(!accepted)) { @@ -3850,6 +3855,7 @@ class binary_reader const auto* const data = reinterpret_cast(ia.bulk_data()); // UBJSON stores numbers big-endian, BJData little-endian const bool swap = is_little_endian != (input_format == input_format_t::bjdata); + const std::size_t start = chars_read; std::size_t read = 0; bool accepted = true; while (read < count && accepted) @@ -3861,16 +3867,39 @@ class binary_reader byte_swap(number); } ++read; + // where passing the value on can report an error (see + // emit_float), the position is that after the value, as if it + // was read by get_number() + set_chars_read(start + (read * size), std::integral_constant()> {}); accepted = emit_ubjson_number(number); } - // as if read by get_number(): current is the last byte read + // get_number() does not change current ia.bulk_skip(read * size); - chars_read += read * size; - current = data[(read * size) - 1]; + chars_read = start + (read * size); remaining -= read; return accepted; } + /*! + @brief whether a value of type @a NumberType can be finite and still + overflow number_float_t, which @ref emit_float reports as an error + + False for the default number types, so that the loop in @ref + parse_ubjson_typed_run need not track the position per value. + */ + template + static constexpr bool may_overflow_number_float() noexcept + { + return static_cast((std::numeric_limits::max)()) < static_cast((std::numeric_limits::max)()); + } + + void set_chars_read(const std::size_t position, std::true_type /*track*/) noexcept + { + chars_read = position; + } + + void set_chars_read(const std::size_t /*position*/, std::false_type /*track*/) const noexcept {} + template::value, int> = 0> bool emit_ubjson_number(const NumberType number) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ae91542de..e06c98452 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20605,6 +20605,21 @@ class binary_reader return result; } + /*! + @brief move past a value of @a size bytes, starting with @a head, that a + bulk run reads, as get() and get_number() would + + Called before the value is passed on, so that an error reported while + passing it on (see @ref emit_float) has the position and the last byte + that reading the value byte by byte gives. + */ + void bulk_advance(const std::size_t size, const unsigned char head) + { + ia.bulk_skip(size); + chars_read += size; + current = head; + } + /*! @brief read the integers, floats, booleans, and nulls that follow in a definite-length array, without the per-element bookkeeping of @@ -20630,71 +20645,70 @@ class binary_reader { const auto* const data = reinterpret_cast(ia.bulk_data()); const unsigned char head = data[0]; - std::size_t size = 0; bool accepted = false; switch (head) { case 0x18: - size = 2; + bulk_advance(2, head); accepted = emit_unsigned(data[1]); break; case 0x19: - size = 3; + bulk_advance(3, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0x1A: - size = 5; + bulk_advance(5, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0x1B: - size = 9; + bulk_advance(9, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0x38: - size = 2; + bulk_advance(2, head); accepted = emit_cbor_negative_integer(data[1]); break; case 0x39: - size = 3; + bulk_advance(3, head); accepted = emit_cbor_negative_integer(load_big_endian(data + 1)); break; case 0x3A: - size = 5; + bulk_advance(5, head); accepted = emit_cbor_negative_integer(load_big_endian(data + 1)); break; case 0x3B: - size = 9; + bulk_advance(9, head); accepted = emit_cbor_negative_integer(load_big_endian(data + 1)); break; case 0xF4: - size = 1; + bulk_advance(1, head); accepted = sax->boolean(false); break; case 0xF5: - size = 1; + bulk_advance(1, head); accepted = sax->boolean(true); break; case 0xF6: - size = 1; + bulk_advance(1, head); accepted = sax->null(); break; case 0xFA: - size = 5; + bulk_advance(5, head); accepted = emit_float(load_big_endian(data + 1)); break; case 0xFB: - size = 9; + bulk_advance(9, head); accepted = emit_float(load_big_endian(data + 1)); break; default: if (head <= 0x17) { - size = 1; + bulk_advance(1, head); accepted = sax->number_unsigned(static_cast(head)); } else if (head >= 0x20 && head <= 0x37) { - size = 1; + bulk_advance(1, head); accepted = sax->number_integer(static_cast(0x20 - 1 - head)); } else @@ -20704,10 +20718,6 @@ class binary_reader } break; } - // as if read by get() and get_number(): current is the head byte - ia.bulk_skip(size); - chars_read += size; - current = head; --remaining; if (JSON_HEDLEY_UNLIKELY(!accepted)) { @@ -21545,73 +21555,72 @@ class binary_reader { const auto* const data = reinterpret_cast(ia.bulk_data()); const unsigned char head = data[0]; - std::size_t size = 0; bool accepted = false; switch (head) { case 0xC0: // nil - size = 1; + bulk_advance(1, head); accepted = sax->null(); break; case 0xC2: // false - size = 1; + bulk_advance(1, head); accepted = sax->boolean(false); break; case 0xC3: // true - size = 1; + bulk_advance(1, head); accepted = sax->boolean(true); break; case 0xCA: // float 32 - size = 5; + bulk_advance(5, head); accepted = emit_float(load_big_endian(data + 1)); break; case 0xCB: // float 64 - size = 9; + bulk_advance(9, head); accepted = emit_float(load_big_endian(data + 1)); break; case 0xCC: // uint 8 - size = 2; + bulk_advance(2, head); accepted = emit_unsigned(data[1]); break; case 0xCD: // uint 16 - size = 3; + bulk_advance(3, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0xCE: // uint 32 - size = 5; + bulk_advance(5, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0xCF: // uint 64 - size = 9; + bulk_advance(9, head); accepted = emit_unsigned(load_big_endian(data + 1)); break; case 0xD0: // int 8 - size = 2; + bulk_advance(2, head); accepted = emit_signed(static_cast(data[1])); break; case 0xD1: // int 16 - size = 3; + bulk_advance(3, head); accepted = emit_signed(load_big_endian(data + 1)); break; case 0xD2: // int 32 - size = 5; + bulk_advance(5, head); accepted = emit_signed(load_big_endian(data + 1)); break; case 0xD3: // int 64 - size = 9; + bulk_advance(9, head); accepted = emit_signed(load_big_endian(data + 1)); break; default: if (head <= 0x7F) { // positive fixint - size = 1; + bulk_advance(1, head); accepted = sax->number_unsigned(static_cast(head)); } else if (head >= 0xE0) { // negative fixint - size = 1; + bulk_advance(1, head); accepted = sax->number_integer(static_cast(head)); } else @@ -21621,10 +21630,6 @@ class binary_reader } break; } - // as if read by get() and get_number(): current is the last byte - ia.bulk_skip(size); - chars_read += size; - current = data[size - 1]; --remaining; if (JSON_HEDLEY_UNLIKELY(!accepted)) { @@ -22528,6 +22533,7 @@ class binary_reader const auto* const data = reinterpret_cast(ia.bulk_data()); // UBJSON stores numbers big-endian, BJData little-endian const bool swap = is_little_endian != (input_format == input_format_t::bjdata); + const std::size_t start = chars_read; std::size_t read = 0; bool accepted = true; while (read < count && accepted) @@ -22539,16 +22545,39 @@ class binary_reader byte_swap(number); } ++read; + // where passing the value on can report an error (see + // emit_float), the position is that after the value, as if it + // was read by get_number() + set_chars_read(start + (read * size), std::integral_constant()> {}); accepted = emit_ubjson_number(number); } - // as if read by get_number(): current is the last byte read + // get_number() does not change current ia.bulk_skip(read * size); - chars_read += read * size; - current = data[(read * size) - 1]; + chars_read = start + (read * size); remaining -= read; return accepted; } + /*! + @brief whether a value of type @a NumberType can be finite and still + overflow number_float_t, which @ref emit_float reports as an error + + False for the default number types, so that the loop in @ref + parse_ubjson_typed_run need not track the position per value. + */ + template + static constexpr bool may_overflow_number_float() noexcept + { + return static_cast((std::numeric_limits::max)()) < static_cast((std::numeric_limits::max)()); + } + + void set_chars_read(const std::size_t position, std::true_type /*track*/) noexcept + { + chars_read = position; + } + + void set_chars_read(const std::size_t /*position*/, std::false_type /*track*/) const noexcept {} + template::value, int> = 0> bool emit_ubjson_number(const NumberType number) { diff --git a/tests/src/unit-binary_formats.cpp b/tests/src/unit-binary_formats.cpp index 333252190..f9778d9e7 100644 --- a/tests/src/unit-binary_formats.cpp +++ b/tests/src/unit-binary_formats.cpp @@ -14,6 +14,7 @@ using nlohmann::json; #include #include #include +#include #include #include #include @@ -367,3 +368,125 @@ TEST_CASE("Binary formats with narrow number types") CHECK(format.decode(format.encode(json(1e300)), false).is_discarded()); } } + +namespace +{ +// records the SAX events of narrow_json, including the arguments of parse_error +struct narrow_event_recorder +{ + using number_integer_t = narrow_json::number_integer_t; + using number_unsigned_t = narrow_json::number_unsigned_t; + using number_float_t = narrow_json::number_float_t; + using string_t = narrow_json::string_t; + using binary_t = narrow_json::binary_t; + + bool null() + { + events.emplace_back("null"); + return true; + } + bool boolean(bool val) + { + events.emplace_back(val ? "true" : "false"); + return true; + } + bool number_integer(number_integer_t val) + { + events.push_back(std::to_string(val)); + return true; + } + bool number_unsigned(number_unsigned_t val) + { + events.push_back(std::to_string(val)); + return true; + } + bool number_float(number_float_t val, const string_t& /*unused*/) + { + events.push_back(std::to_string(val)); + return true; + } + bool string(string_t& val) + { + events.push_back(val); + return true; + } + bool binary(binary_t& /*unused*/) + { + events.emplace_back("binary"); + return true; + } + bool start_object(std::size_t /*unused*/) + { + events.emplace_back("{"); + return true; + } + bool key(string_t& val) + { + events.push_back(val); + return true; + } + bool end_object() + { + events.emplace_back("}"); + return true; + } + bool start_array(std::size_t /*unused*/) + { + events.emplace_back("["); + return true; + } + bool end_array() + { + events.emplace_back("]"); + return true; + } + bool parse_error(std::size_t position, const std::string& last_token, const nlohmann::detail::exception& ex) + { + events.push_back("parse_error(" + std::to_string(position) + ", " + last_token + ", " + ex.what() + ")"); + return false; + } + + std::vector events {}; // NOLINT(readability-redundant-member-init) +}; +} // namespace + +TEST_CASE("Binary formats with narrow number types from contiguous and non-contiguous input") +{ + // contiguous input reads runs of numbers in one loop; a number that + // overflows number_float_t must be reported with the same position and + // last byte as when the input is read byte by byte + const json values = {1, 70000, -70000, 1.5, 2.5, 1e300, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5}; + const json doubles = {1.5, 2.5, 3.5, 1e300, 4.5, 5.5, 6.5, 7.5, 8.5}; + + struct binary_input + { + const char* name; + json::input_format_t format; + bytes data; + }; + const std::vector inputs = + { + {"CBOR", json::input_format_t::cbor, json::to_cbor(values)}, + {"CBOR doubles", json::input_format_t::cbor, json::to_cbor(doubles)}, + {"MessagePack", json::input_format_t::msgpack, json::to_msgpack(values)}, + {"MessagePack doubles", json::input_format_t::msgpack, json::to_msgpack(doubles)}, + {"UBJSON", json::input_format_t::ubjson, json::to_ubjson(values, true)}, + {"UBJSON typed doubles", json::input_format_t::ubjson, json::to_ubjson(doubles, true, true)}, + {"BJData", json::input_format_t::bjdata, json::to_bjdata(values, true)}, + {"BJData typed doubles", json::input_format_t::bjdata, json::to_bjdata(doubles, true, true)}, + }; + + for (const auto& input : inputs) + { + const std::string name = input.name; + CAPTURE(name) + const std::list bytewise(input.data.begin(), input.data.end()); + narrow_event_recorder from_contiguous; + narrow_event_recorder from_bytewise; + CHECK_FALSE(narrow_json::sax_parse(input.data, &from_contiguous, input.format)); + CHECK_FALSE(narrow_json::sax_parse(bytewise.begin(), bytewise.end(), &from_bytewise, input.format)); + CHECK(from_contiguous.events == from_bytewise.events); + REQUIRE(!from_contiguous.events.empty()); + CHECK(from_contiguous.events.back().find("out_of_range.406") != std::string::npos); + } +} From f91e900875d38ef719aa8122e77c2d1adad7f412 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:52:12 +0100 Subject: [PATCH 05/14] Fix stale comments in include/ (#6138) Signed-off-by: Niels Lohmann --- include/nlohmann/detail/conversions/to_chars.hpp | 7 ++++--- include/nlohmann/detail/input/number_parse.hpp | 2 +- include/nlohmann/detail/output/binary_writer.hpp | 2 +- include/nlohmann/detail/output/serializer.hpp | 5 +++-- include/nlohmann/json_view.hpp | 2 +- single_include/nlohmann/json.hpp | 16 +++++++++------- single_include/nlohmann/json_view.hpp | 2 +- 7 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_chars.hpp b/include/nlohmann/detail/conversions/to_chars.hpp index 6cfb9b8e7..67a708f62 100644 --- a/include/nlohmann/detail/conversions/to_chars.hpp +++ b/include/nlohmann/detail/conversions/to_chars.hpp @@ -48,10 +48,11 @@ namespace detail { /*! -@brief implements the Grisu2 algorithm for binary to decimal floating-point -conversion. +@brief implements binary to decimal floating-point conversion: the shortest +digits of Zmij (see zmij.hpp) for IEEE 754 binary64 numbers, and the Grisu2 +algorithm for all other formats. -This implementation is a slightly modified version of the reference +The Grisu2 implementation is a slightly modified version of the reference implementation which may be obtained from http://florian.loitsch.com/publications (bench.tar.gz). diff --git a/include/nlohmann/detail/input/number_parse.hpp b/include/nlohmann/detail/input/number_parse.hpp index 71215d3df..41d8b1ae5 100644 --- a/include/nlohmann/detail/input/number_parse.hpp +++ b/include/nlohmann/detail/input/number_parse.hpp @@ -257,7 +257,7 @@ struct ieee_binary_format<53> // binary64 These formats (float, double, and long double where it is binary64, e.g. with MSVC or on Apple arm64) are converted by parse_float_native(). The -predicate is the one the serializer uses to choose Grisu2. +predicate is the one the serializer uses to choose to_chars() over snprintf. */ template struct has_native_float_format diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index cf114b82f..c23dc0738 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -915,7 +915,7 @@ class binary_writer JSON_THROW(out_of_range::create(exception_id::subtype_out_of_range, concat("subtype ", std::to_string(subtype), " is too large for ", what, " (max 255)"), &j)); } - /// @throw other_error.407 always + /// @throw other_error.502 always JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_size_marker_required(const BasicJsonType& j) { diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 22fcfa6ac..e0562c8bd 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -2067,8 +2067,9 @@ class serializer } // If number_float_t is an IEEE-754 single or double precision number, - // use the Grisu2 algorithm to produce short numbers which are - // guaranteed to round-trip, using strtof and strtod, resp. + // use to_chars() to produce short numbers which are guaranteed to + // round-trip, using strtof and strtod, resp. (the shortest digits of + // Zmij for binary64, the Grisu2 algorithm for binary32). // // NB: The test below works if == . static constexpr bool is_ieee_single_or_double diff --git a/include/nlohmann/json_view.hpp b/include/nlohmann/json_view.hpp index b922d82dd..e54867d54 100644 --- a/include/nlohmann/json_view.hpp +++ b/include/nlohmann/json_view.hpp @@ -849,7 +849,7 @@ class basic_json_view return !navigation::editable() || m_doc->edits == nullptr; } - /// the member of this object with @a key (the last one if the key occurs more than once), or nullptr + /// the member of this object with @a key (the first one if the key occurs more than once), or nullptr NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const { return find_key(key, std::integral_constant {}); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e06c98452..f77c68073 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -11642,7 +11642,7 @@ struct ieee_binary_format<53> // binary64 These formats (float, double, and long double where it is binary64, e.g. with MSVC or on Apple arm64) are converted by parse_float_native(). The -predicate is the one the serializer uses to choose Grisu2. +predicate is the one the serializer uses to choose to_chars() over snprintf. */ template struct has_native_float_format @@ -31177,7 +31177,7 @@ class binary_writer JSON_THROW(out_of_range::create(exception_id::subtype_out_of_range, concat("subtype ", std::to_string(subtype), " is too large for ", what, " (max 255)"), &j)); } - /// @throw other_error.407 always + /// @throw other_error.502 always JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_size_marker_required(const BasicJsonType& j) { @@ -36036,10 +36036,11 @@ namespace detail { /*! -@brief implements the Grisu2 algorithm for binary to decimal floating-point -conversion. +@brief implements binary to decimal floating-point conversion: the shortest +digits of Zmij (see zmij.hpp) for IEEE 754 binary64 numbers, and the Grisu2 +algorithm for all other formats. -This implementation is a slightly modified version of the reference +The Grisu2 implementation is a slightly modified version of the reference implementation which may be obtained from http://florian.loitsch.com/publications (bench.tar.gz). @@ -39583,8 +39584,9 @@ class serializer } // If number_float_t is an IEEE-754 single or double precision number, - // use the Grisu2 algorithm to produce short numbers which are - // guaranteed to round-trip, using strtof and strtod, resp. + // use to_chars() to produce short numbers which are guaranteed to + // round-trip, using strtof and strtod, resp. (the shortest digits of + // Zmij for binary64, the Grisu2 algorithm for binary32). // // NB: The test below works if == . static constexpr bool is_ieee_single_or_double diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index b23199021..d56c63272 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -11066,7 +11066,7 @@ class basic_json_view return !navigation::editable() || m_doc->edits == nullptr; } - /// the member of this object with @a key (the last one if the key occurs more than once), or nullptr + /// the member of this object with @a key (the first one if the key occurs more than once), or nullptr NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const { return find_key(key, std::integral_constant {}); From ac0d25f51c354c582974bb6f8e0b155475a839a9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:52:24 +0100 Subject: [PATCH 06/14] Build the tests with C++17/C++20-only sections in those standards (#6139) Signed-off-by: Niels Lohmann --- tests/CMakeLists.txt | 3 +-- tests/src/unit-allocator.cpp | 8 ++++++++ tests/src/unit-class_parser.cpp | 12 ++++++++++-- tests/src/unit-custom-number-types.cpp | 8 ++++++++ tests/src/unit-json_view_builder.cpp | 10 +++++++++- tests/src/unit-recursion_depth_limit.cpp | 8 ++++++++ tests/src/unit-to_json-rvalue.cpp | 13 ++++++++++++- tests/src/unit-ubjson_valueless.cpp | 8 ++++++++ 8 files changed, 64 insertions(+), 6 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 52d46f618..3b3727085 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -445,8 +445,7 @@ json_test_add_test_for(src/unit-stream-bulk-scan.cpp # with other values of JSON_UBJSON_VALUELESS_ELEMENTS_PER_BYTE: 1 (the smallest), # 1000000 (so large that the budget cannot be reached), and the largest value of # a 64-bit integer (which saturates the limit already at the first byte, and is -# clamped to the largest std::size_t where that has 32 bits). The file does not -# mention JSON_HAS_CPP_, so only C++11 is built unless the standards are forced +# clamped to the largest std::size_t where that has 32 bits) json_test_set_test_options(test-ubjson_valueless_rate_1 COMPILE_DEFINITIONS JSON_UBJSON_VALUELESS_ELEMENTS_PER_BYTE=1 ) diff --git a/tests/src/unit-allocator.cpp b/tests/src/unit-allocator.cpp index 73693347f..7bebe8a81 100644 --- a/tests/src/unit-allocator.cpp +++ b/tests/src/unit-allocator.cpp @@ -6,6 +6,13 @@ // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT +// cmake/test.cmake selects the C++ standard versions with which to build a +// unit test based on the presence of JSON_HAS_CPP_ macros. +// When using macros that are only defined for particular versions of the standard +// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding +// version macro in a comment close by, like this: +// JSON_HAS_CPP_ (do not remove; see note at top of file) + #include "doctest_compatibility.h" #define JSON_TESTS_PRIVATE @@ -15,6 +22,7 @@ using nlohmann::json; #include #include #include +// JSON_HAS_CPP_20 (do not remove; see note at top of file) #if JSON_HAS_RANGES #include #endif diff --git a/tests/src/unit-class_parser.cpp b/tests/src/unit-class_parser.cpp index b015a87d0..c1a86ca5f 100644 --- a/tests/src/unit-class_parser.cpp +++ b/tests/src/unit-class_parser.cpp @@ -6,6 +6,13 @@ // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT +// cmake/test.cmake selects the C++ standard versions with which to build a +// unit test based on the presence of JSON_HAS_CPP_ macros. +// When using macros that are only defined for particular versions of the standard +// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding +// version macro in a comment close by, like this: +// JSON_HAS_CPP_ (do not remove; see note at top of file) + #include "doctest_compatibility.h" // capture whether JSON_STRICT_NUL_HANDLING was enabled on the command line @@ -517,6 +524,7 @@ TEST_CASE("parser class") CHECK(json::accept(u"[1]")); CHECK(json::parse(U"[1]") == json({1})); CHECK(json::accept(U"[1]")); + // JSON_HAS_CPP_20 (do not remove; see note at top of file) #if defined(__cpp_char8_t) CHECK(json::parse(u8"[1]") == json({1})); CHECK(json::accept(u8"[1]")); @@ -2502,7 +2510,7 @@ TEST_CASE("text of float tokens") std::vector result; std::copy_if(events.begin(), events.end(), std::back_inserter(result), [](const std::string & e) { - return e.compare(0, 13, "number_float(") == 0; + return e.compare(0, 13, "number_float(") == 0; // NOLINT(modernize-use-starts-ends-with): C++11 }); return result; }; @@ -3103,7 +3111,7 @@ TEST_CASE("object keys are delivered decoded and validated") std::vector keys; for (const auto& e : l.events) { - if (e.compare(0, 4, "key(") == 0) + if (e.compare(0, 4, "key(") == 0) // NOLINT(modernize-use-starts-ends-with): C++11 { keys.push_back(e.substr(4, e.size() - 5)); } diff --git a/tests/src/unit-custom-number-types.cpp b/tests/src/unit-custom-number-types.cpp index 09d716dc2..cf4055422 100644 --- a/tests/src/unit-custom-number-types.cpp +++ b/tests/src/unit-custom-number-types.cpp @@ -6,6 +6,13 @@ // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT +// cmake/test.cmake selects the C++ standard versions with which to build a +// unit test based on the presence of JSON_HAS_CPP_ macros. +// When using macros that are only defined for particular versions of the standard +// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding +// version macro in a comment close by, like this: +// JSON_HAS_CPP_ (do not remove; see note at top of file) + #include "doctest_compatibility.h" #include @@ -17,6 +24,7 @@ #include #include +// JSON_HAS_CPP_20 (do not remove; see note at top of file) #if JSON_HAS_THREE_WAY_COMPARISON #include #endif diff --git a/tests/src/unit-json_view_builder.cpp b/tests/src/unit-json_view_builder.cpp index fed2b7a19..60b7661af 100644 --- a/tests/src/unit-json_view_builder.cpp +++ b/tests/src/unit-json_view_builder.cpp @@ -6,6 +6,13 @@ // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT +// cmake/test.cmake selects the C++ standard versions with which to build a +// unit test based on the presence of JSON_HAS_CPP_ macros. +// When using macros that are only defined for particular versions of the standard +// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding +// version macro in a comment close by, like this: +// JSON_HAS_CPP_ (do not remove; see note at top of file) + #include "doctest_compatibility.h" #include @@ -272,6 +279,7 @@ struct generator TEST_CASE("json_view string_ref") { // std::string_view in C++17, a stand-in with the same members before + // JSON_HAS_CPP_17 (do not remove; see note at top of file) using nlohmann::detail::view::string_ref; const std::string text = "abc"; const string_ref r(text); @@ -945,7 +953,7 @@ TEST_CASE("json_view document_data::holds() (the debug check of read())") SECTION("current edit buffer") { - d->edits.reset(new document_data::edit_state()); // NOLINT(cppcoreguidelines-owning-memory) + d->edits.reset(new document_data::edit_state()); // NOLINT(cppcoreguidelines-owning-memory,modernize-make-unique) CHECK(!d->holds(other.data(), other.size())); d->edits->texts.emplace_back(new char[16]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-owning-memory) d->edits->text_cap = 16; diff --git a/tests/src/unit-recursion_depth_limit.cpp b/tests/src/unit-recursion_depth_limit.cpp index 2869a7241..0757d42f1 100644 --- a/tests/src/unit-recursion_depth_limit.cpp +++ b/tests/src/unit-recursion_depth_limit.cpp @@ -6,6 +6,13 @@ // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT +// cmake/test.cmake selects the C++ standard versions with which to build a +// unit test based on the presence of JSON_HAS_CPP_ macros. +// When using macros that are only defined for particular versions of the standard +// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding +// version macro in a comment close by, like this: +// JSON_HAS_CPP_ (do not remove; see note at top of file) + #include "doctest_compatibility.h" #include @@ -348,6 +355,7 @@ static_assert(nlohmann::detail::recursion_depth_limit_is_char_or_bool::value, "wchar_t is refused"); static_assert(nlohmann::detail::recursion_depth_limit_is_char_or_bool::value, "char16_t is refused"); static_assert(nlohmann::detail::recursion_depth_limit_is_char_or_bool::value, "char32_t is refused"); +// JSON_HAS_CPP_20 (do not remove; see note at top of file) #if defined(__cpp_char8_t) static_assert(nlohmann::detail::recursion_depth_limit_is_char_or_bool::value, "char8_t is refused"); #endif diff --git a/tests/src/unit-to_json-rvalue.cpp b/tests/src/unit-to_json-rvalue.cpp index 4aabda5a1..98993e014 100644 --- a/tests/src/unit-to_json-rvalue.cpp +++ b/tests/src/unit-to_json-rvalue.cpp @@ -6,6 +6,13 @@ // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT +// cmake/test.cmake selects the C++ standard versions with which to build a +// unit test based on the presence of JSON_HAS_CPP_ macros. +// When using macros that are only defined for particular versions of the standard +// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding +// version macro in a comment close by, like this: +// JSON_HAS_CPP_ (do not remove; see note at top of file) + #include "doctest_compatibility.h" #include @@ -23,8 +30,11 @@ using nlohmann::json; #include #include +// JSON_HAS_CPP_17 (do not remove; see note at top of file) #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) - #if __has_include() + // libstdc++ 8 needs -lstdc++fs for std::filesystem (GCC bug 90050), which clang on such a + // libstdc++ does not get from the test options + #if __has_include() && (!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 9) #include #define TO_JSON_RVALUE_HAS_FS 1 #endif @@ -244,6 +254,7 @@ TEST_CASE("to_json from rvalue containers") SECTION("std::filesystem::path rvalue is still a string") { CHECK(json(std::filesystem::path("a/b")) == json("a/b")); + // JSON_HAS_CPP_20 (do not remove; see note at top of file) #ifdef __cpp_lib_char8_t CHECK(json(std::u8string(u8"a/b")) == json("a/b")); #endif diff --git a/tests/src/unit-ubjson_valueless.cpp b/tests/src/unit-ubjson_valueless.cpp index 5a6afdab9..2ceb3f99b 100644 --- a/tests/src/unit-ubjson_valueless.cpp +++ b/tests/src/unit-ubjson_valueless.cpp @@ -6,6 +6,13 @@ // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT +// cmake/test.cmake selects the C++ standard versions with which to build a +// unit test based on the presence of JSON_HAS_CPP_ macros. +// When using macros that are only defined for particular versions of the standard +// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding +// version macro in a comment close by, like this: +// JSON_HAS_CPP_ (do not remove; see note at top of file) + // The budget for the elements of the UBJSON containers of a valueless type // ('Z', 'T', 'F') of a whole input: 1048576 plus // JSON_UBJSON_VALUELESS_ELEMENTS_PER_BYTE for every byte read (see @@ -92,6 +99,7 @@ static_assert(nlohmann::detail::valueless_elements_rate_is_char_or_bool::value, "wchar_t is refused"); static_assert(nlohmann::detail::valueless_elements_rate_is_char_or_bool::value, "char16_t is refused"); static_assert(nlohmann::detail::valueless_elements_rate_is_char_or_bool::value, "char32_t is refused"); +// JSON_HAS_CPP_20 (do not remove; see note at top of file) #if defined(__cpp_char8_t) static_assert(nlohmann::detail::valueless_elements_rate_is_char_or_bool::value, "char8_t is refused"); #endif From f6eb5e4fb14e43f9a07c31866b092dac53f1175c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:52:34 +0100 Subject: [PATCH 07/14] Fix documentation errors and clean up repository leftovers (#6140) Signed-off-by: Niels Lohmann --- .github/labeler.yml | 2 +- .github/workflows/check_amalgamation.yml | 4 +- .gitignore | 5 +- FILES.md | 1 + Makefile | 2 +- cmake/ci.cmake | 77 ------------------- docs/mkdocs/docs/api/basic_json/std_swap.md | 16 +++- docs/mkdocs/docs/api/basic_json/swap.md | 12 ++- docs/mkdocs/docs/api/basic_json/to_ubjson.md | 3 +- .../docs/api/basic_json_document/erase.md | 3 +- .../docs/api/basic_json_view/contains.md | 7 +- docs/mkdocs/docs/api/basic_json_view/items.md | 7 +- .../docs/api/basic_json_view/operator_eq.md | 10 ++- .../docs/api/basic_json_view/operator_ne.md | 6 ++ .../docs/api/basic_json_view/source_offset.md | 4 + .../mkdocs/docs/api/json_editable_document.md | 5 +- docs/mkdocs/docs/api/json_editable_view.md | 3 +- .../macros/nlohmann_define_type_intrusive.md | 2 +- .../macros/nlohmann_define_type_with_names.md | 2 +- docs/mkdocs/docs/features/serialization.md | 2 +- docs/mkdocs/docs/features/type_recipes.md | 4 +- docs/mkdocs/docs/home/exceptions.md | 10 ++- .../thirdparty/hedley/hedley_undef.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- tests/src/unit-unicode.cpp | 49 +++++++----- 25 files changed, 106 insertions(+), 134 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index f9e81e2bf..54857c868 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -66,7 +66,7 @@ labels: - "tests/src/fuzzer-(parse_json_view|json_view_image)\\.cpp" - "tests/benchmarks/json_view/.*" - "tools/amalgamate/config_json_view\\.json" - - "docs/mkdocs/docs/features/json_view\\.md" + - "docs/mkdocs/docs/features/json_view/.*" - "docs/mkdocs/docs/api/basic_json_(document|view)/.*" - "docs/mkdocs/docs/api/(ordered_)?json_(editable_)?(document|view)\\.md" - "docs/mkdocs/docs/examples/(basic_json_(document|view)__|(ordered_)?json_(editable_)?(document|view)).*" diff --git a/.github/workflows/check_amalgamation.yml b/.github/workflows/check_amalgamation.yml index d4eef35a7..448d0d85b 100644 --- a/.github/workflows/check_amalgamation.yml +++ b/.github/workflows/check_amalgamation.yml @@ -115,9 +115,7 @@ jobs: python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json.json -s . python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json_fwd.json -s . - # the configuration of json_view.hpp comes with the pull request until - # it is on develop; the tool itself is still develop's - python3 $TOOL_DIR/amalgamate.py -c $MAIN_DIR/tools/amalgamate/config_json_view.json -s . + python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json_view.json -s . # the header list of the Bazel "json" target must match the files in include/ cmake -P cmake/scripts/gen_bazel_build_file.cmake diff --git a/.gitignore b/.gitignore index 6bacb8947..455dd1c0f 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,7 @@ # fuzzers /tests/corpus_* -/tests/parse_*_fuzzer +/tests/*_fuzzer # documentation /docs/docset/JSON_for_Modern_C++.docset/ @@ -47,11 +47,10 @@ nlohmann_json.spdx # Bazel-related MODULE.bazel.lock -# backups left by `make check-amalgamation` or the ci_test_amalgamation target if they are interrupted +# backups left by `make check-amalgamation` if it is interrupted /BUILD.bazel~ /nlohmann_json.natvis~ /single_include/nlohmann/*.hpp~ -/venv_astyle/ # release outputs of `make release` /include.zip diff --git a/FILES.md b/FILES.md index 142997607..086873372 100644 --- a/FILES.md +++ b/FILES.md @@ -28,6 +28,7 @@ The [GitHub Actions](https://docs.github.com/en/actions) workflows that build, t - `publish_documentation.yml` — build and publish the documentation on pushes to the `develop` branch that touch the documentation sources (or the files it embeds). - `check_docs_links.yml` — check the links of the documentation weekly, as external links can break without any change in this repository. - `cancel_closed_pr_runs.yml` — cancel unfinished workflow runs of a pull request once it is closed or merged. +- `json_view_benchmarks.yml` — compare the performance of `json_view` with other JSON libraries on GitHub-hosted x86-64 and AArch64 Linux runners, on demand or when a pull request gets the label "benchmark". Further documentation: diff --git a/Makefile b/Makefile index e5d6879f4..b0eb81631 100644 --- a/Makefile +++ b/Makefile @@ -215,7 +215,7 @@ release: include.zip json.tar.xz # clean up clean: - rm -fr *.dSYM tests/*.dSYM tests/parse_*_fuzzer + rm -fr *.dSYM tests/*.dSYM tests/*_fuzzer rm -fr cmake-build-benchmarks cmake-build-pvs-studio release_files $(MAKE) clean -Cdocs diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 7cc5adebe..1b86c55de 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -406,61 +406,6 @@ add_custom_target(ci_test_clang_sanitizer COMMENT "Compile and test with sanitizers" ) -############################################################################### -# Check if header is amalgamated and sources are properly indented. -############################################################################### - -# Same file set as .github/workflows/check_amalgamation.yml, so this CMake target (which can be run locally) is held -# to the same standard as the workflow (which runs for pull requests and for pushes to develop/master/release/*). -file(GLOB_RECURSE INDENT_FILES - ${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.hpp - ${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.cpp - ${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.cu - ${PROJECT_SOURCE_DIR}/include/*.hpp - ${PROJECT_SOURCE_DIR}/include/*.cpp - ${PROJECT_SOURCE_DIR}/include/*.cu - ${PROJECT_SOURCE_DIR}/tests/*.hpp - ${PROJECT_SOURCE_DIR}/tests/*.cpp - ${PROJECT_SOURCE_DIR}/tests/*.cu -) -list(FILTER INDENT_FILES EXCLUDE REGEX "/tests/thirdparty/|/tests/abi/include/nlohmann/") - -set(include_dir ${PROJECT_SOURCE_DIR}/single_include/nlohmann) -set(tool_dir ${PROJECT_SOURCE_DIR}/tools/amalgamate) -add_custom_target(ci_test_amalgamation - COMMAND rm -fr ${include_dir}/json.hpp~ ${include_dir}/json_fwd.hpp~ ${include_dir}/json_literals.hpp~ ${include_dir}/json_view.hpp~ - COMMAND cp ${include_dir}/json.hpp ${include_dir}/json.hpp~ - COMMAND cp ${include_dir}/json_fwd.hpp ${include_dir}/json_fwd.hpp~ - COMMAND cp ${include_dir}/json_literals.hpp ${include_dir}/json_literals.hpp~ - COMMAND cp ${include_dir}/json_view.hpp ${include_dir}/json_view.hpp~ - COMMAND cp ${PROJECT_SOURCE_DIR}/BUILD.bazel ${PROJECT_SOURCE_DIR}/BUILD.bazel~ - - COMMAND ${Python3_EXECUTABLE} -mvenv venv_astyle - COMMAND venv_astyle/bin/pip3 --quiet install -r ${CMAKE_SOURCE_DIR}/tools/astyle/requirements.txt - COMMAND venv_astyle/bin/astyle --version - - COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json.json -s . - COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json_fwd.json -s . - COMMAND cp ${PROJECT_SOURCE_DIR}/include/nlohmann/json_literals.hpp ${include_dir}/json_literals.hpp - COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json_view.json -s . - COMMAND venv_astyle/bin/astyle --project=tools/astyle/.astylerc --suffix=none ${include_dir}/json.hpp ${include_dir}/json_fwd.hpp ${include_dir}/json_literals.hpp ${include_dir}/json_view.hpp - COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/scripts/gen_bazel_build_file.cmake - - COMMAND diff ${include_dir}/json.hpp~ ${include_dir}/json.hpp - COMMAND diff ${include_dir}/json_fwd.hpp~ ${include_dir}/json_fwd.hpp - COMMAND diff ${include_dir}/json_literals.hpp~ ${include_dir}/json_literals.hpp - COMMAND diff ${include_dir}/json_view.hpp~ ${include_dir}/json_view.hpp - COMMAND diff ${PROJECT_SOURCE_DIR}/BUILD.bazel~ ${PROJECT_SOURCE_DIR}/BUILD.bazel - - COMMAND venv_astyle/bin/astyle --project=tools/astyle/.astylerc --suffix=orig ${INDENT_FILES} - COMMAND for FILE in `find . -name '*.orig'`\; do false \; done - - COMMAND rm -f ${include_dir}/json.hpp~ ${include_dir}/json_fwd.hpp~ ${include_dir}/json_literals.hpp~ ${include_dir}/json_view.hpp~ ${PROJECT_SOURCE_DIR}/BUILD.bazel~ - - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - COMMENT "Check amalgamation, formatting, and BUILD.bazel" -) - ############################################################################### # Build and test using the amalgamated header ############################################################################### @@ -692,19 +637,6 @@ else() ) endif() -############################################################################### -# Benchmarks -############################################################################### - -add_custom_target(ci_benchmarks - COMMAND ${CMAKE_COMMAND} - -DCMAKE_BUILD_TYPE=Release -GNinja - -S${PROJECT_SOURCE_DIR}/tests/benchmarks -B${PROJECT_BINARY_DIR}/build_benchmarks - COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_benchmarks --target json_benchmarks - COMMAND cd ${PROJECT_BINARY_DIR}/build_benchmarks && ./json_benchmarks - COMMENT "Run benchmarks" -) - ############################################################################### # CMake flags ############################################################################### @@ -965,12 +897,3 @@ add_custom_target(ci_test_documentation_mermaid WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/docs/mkdocs COMMENT "Check the Mermaid diagrams of the documentation" ) - -############################################################################### -# Clean up all generated files. -############################################################################### - -add_custom_target(ci_clean - COMMAND rm -fr ${PROJECT_BINARY_DIR}/build_* ${PROJECT_BINARY_DIR}/cmake-3.5.0 ${PROJECT_BINARY_DIR}/cmake-3.31.6 ${PROJECT_BINARY_DIR}/cmake-4.0.0 ${JSON_CMAKE_FLAG_BUILD_DIRS} ${single_binaries} - COMMENT "Clean generated directories" -) diff --git a/docs/mkdocs/docs/api/basic_json/std_swap.md b/docs/mkdocs/docs/api/basic_json/std_swap.md index 995d08af6..445c00c5f 100644 --- a/docs/mkdocs/docs/api/basic_json/std_swap.md +++ b/docs/mkdocs/docs/api/basic_json/std_swap.md @@ -2,12 +2,21 @@ ```cpp namespace std { - void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2); + void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2) noexcept( + is_nothrow_move_constructible::value && + is_nothrow_move_assignable::value + ); } ``` Exchanges the values of two JSON objects. +This specialization is only provided before C++20, which no longer allows specializing function templates in +namespace `std`. With C++20 (and in generic code with any standard), use the idiom `#!cpp using std::swap; swap(j1, j2);`: it finds +the [`swap`](swap.md) friend function (overload 2) by argument-dependent lookup. A qualified `#!cpp std::swap(j1, j2)` +still compiles with C++20, but uses the generic `std::swap`, which swaps by three moves instead of exchanging the +contents. + ## Parameters `j1` (in, out) @@ -18,7 +27,9 @@ Exchanges the values of two JSON objects. ## Exception safety -No-throw guarantee: this function never throws exceptions. +No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the +[`json_base_class_t`](json_base_class_t.md) can throw. With the default base class, this function never throws +exceptions. ## Complexity @@ -57,3 +68,4 @@ void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2) - Added in version 1.0.0. - Extended for arbitrary basic_json types in version 3.10.5. +- Not provided with C++20 since version 3.9.0. diff --git a/docs/mkdocs/docs/api/basic_json/swap.md b/docs/mkdocs/docs/api/basic_json/swap.md index 8da743d1f..22ea8d6e4 100644 --- a/docs/mkdocs/docs/api/basic_json/swap.md +++ b/docs/mkdocs/docs/api/basic_json/swap.md @@ -75,8 +75,12 @@ void swap(typename binary_t::container_type& other); ## Exception safety -1. No-throw guarantee: this function never throws exceptions. -2. No-throw guarantee: this function never throws exceptions. +1. No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the + [`json_base_class_t`](json_base_class_t.md) can throw (moving the type tag and the stored value never throws). + With the default base class, this function never throws exceptions. +2. No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the + [`json_base_class_t`](json_base_class_t.md) can throw (moving the type tag and the stored value never throws). + With the default base class, this function never throws exceptions. 3. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. 4. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. 5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. @@ -85,8 +89,8 @@ void swap(typename binary_t::container_type& other); ## Exceptions -1. No-throw guarantee: this function never throws exceptions. -2. No-throw guarantee: this function never throws exceptions. +1. Throws only what moving the [`json_base_class_t`](json_base_class_t.md) throws; nothing with the default base class. +2. Throws only what moving the [`json_base_class_t`](json_base_class_t.md) throws; nothing with the default base class. 3. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than arrays; example: `"cannot use swap(array_t&) with boolean"` 4. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than diff --git a/docs/mkdocs/docs/api/basic_json/to_ubjson.md b/docs/mkdocs/docs/api/basic_json/to_ubjson.md index 6c2a84be0..73a117a07 100644 --- a/docs/mkdocs/docs/api/basic_json/to_ubjson.md +++ b/docs/mkdocs/docs/api/basic_json/to_ubjson.md @@ -57,7 +57,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va ## Exceptions - Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size` - is false, and `j` contains a non-empty array, object, or binary value. + is false, and `j` contains a non-empty array, object, or binary value. Scalars and empty arrays, objects, or binary + values do not throw. - Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is not valid UTF-8 and `error_handler` is `strict` (the default only if [`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) diff --git a/docs/mkdocs/docs/api/basic_json_document/erase.md b/docs/mkdocs/docs/api/basic_json_document/erase.md index 02a202a1d..671abf742 100644 --- a/docs/mkdocs/docs/api/basic_json_document/erase.md +++ b/docs/mkdocs/docs/api/basic_json_document/erase.md @@ -66,7 +66,8 @@ has `erase`; calling it on a read-only `basic_json_document` fails to compile (` [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) (a leading `#!cpp '0'`), [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) (not a number), [`out_of_range.410`](../../home/exceptions.md#jsonexceptionout_of_range410) (too large for `size_type`), or - [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token); otherwise (an + [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token, or the token + `#!cpp "-"`, which never names an existing element); otherwise (an object, or a primitive value the pointer's parent resolves to) throws what 1. throws. Overloads 1. and 2. also throw [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) diff --git a/docs/mkdocs/docs/api/basic_json_view/contains.md b/docs/mkdocs/docs/api/basic_json_view/contains.md index 0abb37be7..186c7d843 100644 --- a/docs/mkdocs/docs/api/basic_json_view/contains.md +++ b/docs/mkdocs/docs/api/basic_json_view/contains.md @@ -62,10 +62,9 @@ view. !!! info "Overload 2 never throws" - Unlike [`BasicJsonType::contains(const json_pointer&)`](../basic_json/contains.md), which can throw for certain - malformed pointers (for instance an empty array-index reference token), overload 2 never throws: a missing key, - an out-of-range or malformed array index, a `#!cpp "-"` index, or a reference token used on a primitive all - simply make it return `#!cpp false`. + Like [`BasicJsonType::contains(const json_pointer&)`](../basic_json/contains.md), overload 2 never throws: a + missing key, an out-of-range or malformed array index, a `#!cpp "-"` index, or a reference token used on a + primitive all simply make it return `#!cpp false`. ## Examples diff --git a/docs/mkdocs/docs/api/basic_json_view/items.md b/docs/mkdocs/docs/api/basic_json_view/items.md index 9f1efffef..341b24738 100644 --- a/docs/mkdocs/docs/api/basic_json_view/items.md +++ b/docs/mkdocs/docs/api/basic_json_view/items.md @@ -27,9 +27,10 @@ for (const auto [key, value] : v.items()) } ``` -Note the `#!cpp const auto` (by value), not `#!cpp const auto&`: unlike `BasicJsonType::items()`, whose elements are -references into an existing object, a view's `item` is produced on the fly for each step of the iteration, so there -is nothing for a reference to bind to. +Taking `item` by value (`#!cpp const auto`) is recommended: unlike `BasicJsonType::items()`, whose elements are +references into an existing object, a view's `item` is produced on the fly for each step of the iteration and is +cheap to copy. A `#!cpp const auto&` (as in the first snippet) also works, as it binds to that temporary, but a +non-const `#!cpp auto&` does not compile. ## Return value diff --git a/docs/mkdocs/docs/api/basic_json_view/operator_eq.md b/docs/mkdocs/docs/api/basic_json_view/operator_eq.md index 2da60db9e..94c6fc508 100644 --- a/docs/mkdocs/docs/api/basic_json_view/operator_eq.md +++ b/docs/mkdocs/docs/api/basic_json_view/operator_eq.md @@ -7,6 +7,10 @@ bool operator==(const basic_json_view& lhs, const basic_json_view& rhs); // (2) bool operator==(const basic_json_view& lhs, const BasicJsonType& rhs); bool operator==(const BasicJsonType& lhs, const basic_json_view& rhs); + +// (3) +template +bool operator==(const basic_json_view& lhs, const basic_json_view& rhs); ``` 1. Compares two views for equality: whether the values [`BasicJsonType::parse()`](../basic_json/parse.md) would @@ -14,8 +18,12 @@ bool operator==(const BasicJsonType& lhs, const basic_json_view& rhs); 2. Compares a view and a `BasicJsonType` value for equality, in either order: whether the value `parse()` would produce for the view and the other operand are equal, according to `BasicJsonType`'s [`operator==`](../basic_json/operator_eq.md). +3. Like overload 1, for two views of the same `BasicJsonType` but of different [view modes](../view_mode.md), in + either order: a view of a read-only document (such as a [`json_view`](../json_view.md)) and a view of an editable + document (such as a [`json_editable_view`](../json_editable_view.md)). This overload only takes part in overload + resolution if `M` is not the mode of `lhs`. -Neither overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers +No overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers compare by value across their types (`#!cpp 1 == 1.0`), and an object compares by its members, with duplicate keys resolved exactly as `parse()` resolves them — the last value, at the position of the first occurrence of the key. This is not what [`operator[]`](operator[].md) and the other lookups return for a duplicate key: they find the *first* diff --git a/docs/mkdocs/docs/api/basic_json_view/operator_ne.md b/docs/mkdocs/docs/api/basic_json_view/operator_ne.md index d4bb32f34..b9661e01d 100644 --- a/docs/mkdocs/docs/api/basic_json_view/operator_ne.md +++ b/docs/mkdocs/docs/api/basic_json_view/operator_ne.md @@ -7,11 +7,17 @@ bool operator!=(const basic_json_view& lhs, const basic_json_view& rhs); // (2) bool operator!=(const basic_json_view& lhs, const BasicJsonType& rhs); bool operator!=(const BasicJsonType& lhs, const basic_json_view& rhs); + +// (3) +template +bool operator!=(const basic_json_view& lhs, const basic_json_view& rhs); ``` 1. Compares two views for inequality. Returns `#!cpp !(lhs == rhs)`, see [operator==](operator_eq.md). 2. Compares a view and a `BasicJsonType` value for inequality, in either order. Returns `#!cpp !(lhs == rhs)` (or, for the reversed order, `#!cpp !(rhs == lhs)`), see [operator==](operator_eq.md). +3. Compares a read-only and an editable view of the same `BasicJsonType` for inequality, in either order. Returns + `#!cpp !(lhs == rhs)`, see [operator==](operator_eq.md). Since `operator!=` is defined as the negation of [`operator==`](operator_eq.md), it follows the same rules for special cases: for instance, since a [discarded](is_discarded.md) view is never equal to anything by default (see diff --git a/docs/mkdocs/docs/api/basic_json_view/source_offset.md b/docs/mkdocs/docs/api/basic_json_view/source_offset.md index dbb3601b0..cee899d0e 100644 --- a/docs/mkdocs/docs/api/basic_json_view/source_offset.md +++ b/docs/mkdocs/docs/api/basic_json_view/source_offset.md @@ -15,6 +15,10 @@ materializing anything. - `#!cpp static_cast(-1)` for a [discarded](is_discarded.md) view, and for a string that contains escapes — such a string was decoded once into the document's own buffer, so there is no single byte range in `source()` left to point at. +- `#!cpp static_cast(-1)` in an [editable document](../json_editable_document.md) for a value written + by an edit ([`set`](../basic_json_document/set.md), [`push_back`](../basic_json_document/push_back.md), + [`insert`](../basic_json_document/insert.md)), and for an array or object whose elements an edit added or removed. + The values an edit did not touch keep their offsets. ## Exception safety diff --git a/docs/mkdocs/docs/api/json_editable_document.md b/docs/mkdocs/docs/api/json_editable_document.md index f40406093..056a0d649 100644 --- a/docs/mkdocs/docs/api/json_editable_document.md +++ b/docs/mkdocs/docs/api/json_editable_document.md @@ -8,8 +8,9 @@ using json_editable_document = basic_json_document; This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the default [`json`](json.md) specialization: in addition to everything [`json_document`](json_document.md) offers, -[`set`](basic_json_document/set.md) and [`push_back`](basic_json_document/push_back.md) change values after -parsing, without ever rewriting the source text — see [Edits](basic_json_document/index.md#edits) and +[`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md), +[`insert`](basic_json_document/insert.md), and [`erase`](basic_json_document/erase.md) change values after parsing, +without ever rewriting the source text — see [Edits](basic_json_document/index.md#edits) and [Editing a document](../features/json_view/editing.md). ## Examples diff --git a/docs/mkdocs/docs/api/json_editable_view.md b/docs/mkdocs/docs/api/json_editable_view.md index 3cc31fbdb..e4b53b14c 100644 --- a/docs/mkdocs/docs/api/json_editable_view.md +++ b/docs/mkdocs/docs/api/json_editable_view.md @@ -9,7 +9,8 @@ using json_editable_view = basic_json_view; This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a [`json_editable_document`](json_editable_document.md). It offers the same read-only interface as [`json_view`](json_view.md); what is different is what it can be a view *of* — a value that -[`set`](basic_json_document/set.md) and [`push_back`](basic_json_document/push_back.md) can change, with every view +[`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md), +[`insert`](basic_json_document/insert.md), and [`erase`](basic_json_document/erase.md) can change, with every view still referring to it seeing the change, see [Edits](basic_json_document/index.md#edits). ## Examples diff --git a/docs/mkdocs/docs/api/macros/nlohmann_define_type_intrusive.md b/docs/mkdocs/docs/api/macros/nlohmann_define_type_intrusive.md index 99ceffd2e..27a81929c 100644 --- a/docs/mkdocs/docs/api/macros/nlohmann_define_type_intrusive.md +++ b/docs/mkdocs/docs/api/macros/nlohmann_define_type_intrusive.md @@ -171,7 +171,7 @@ See the examples below for the concrete generated code. The macro is equivalent to: - ```cpp hl_lines="22 22 23 24 25 26 27 28" + ```cpp hl_lines="22 23 24 25 26 27 28" --8<-- "examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp" ``` diff --git a/docs/mkdocs/docs/api/macros/nlohmann_define_type_with_names.md b/docs/mkdocs/docs/api/macros/nlohmann_define_type_with_names.md index 580305d6a..59a2958e3 100644 --- a/docs/mkdocs/docs/api/macros/nlohmann_define_type_with_names.md +++ b/docs/mkdocs/docs/api/macros/nlohmann_define_type_with_names.md @@ -69,7 +69,7 @@ For further information please refer to the corresponding macros without `WITH_N The macro is equivalent to: - ```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28" + ```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28 29 30" --8<-- "examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp" ``` diff --git a/docs/mkdocs/docs/features/serialization.md b/docs/mkdocs/docs/features/serialization.md index fd482cb73..679b7c112 100644 --- a/docs/mkdocs/docs/features/serialization.md +++ b/docs/mkdocs/docs/features/serialization.md @@ -98,7 +98,7 @@ serialization fails by default. The fourth argument of `dump` selects an ## Using `std::format`, `std::print`, and `fmt` -Since version 3.12.0, JSON values can be formatted directly with C++20's +Since version 3.13.0, JSON values can be formatted directly with C++20's [`std::format`](https://en.cppreference.com/w/cpp/utility/format/format) whenever the standard library provides the `` header (controlled by [`JSON_HAS_STD_FORMAT`](../api/macros/json_has_std_format.md)). This is enabled by the [`std::formatter`](../api/basic_json/std_formatter.md) specialization, which also makes JSON values work with diff --git a/docs/mkdocs/docs/features/type_recipes.md b/docs/mkdocs/docs/features/type_recipes.md index d46b858e6..6ced22e06 100644 --- a/docs/mkdocs/docs/features/type_recipes.md +++ b/docs/mkdocs/docs/features/type_recipes.md @@ -1,7 +1,7 @@ # Recipes for Common Types -The library converts the standard containers, strings, numbers, tuples, `std::optional`, and `std::filesystem::path` -(the last two since version 3.12.0, C++17) out of the box, and your own types once you write conversion functions or use +The library converts the standard containers, strings, numbers, tuples, `std::optional` (since version 3.12.0), and +`std::filesystem::path` (since version 3.10.5), the last two with C++17, out of the box, and your own types once you write conversion functions or use the macros (see [Arbitrary type conversions](arbitrary_types.md)). It deliberately does **not** convert smart pointers, `std::variant`, `std::chrono` types, or class hierarchies, because there is more than one reasonable JSON representation for each of them. This page shows a representation for each that you can copy and adapt. diff --git a/docs/mkdocs/docs/home/exceptions.md b/docs/mkdocs/docs/home/exceptions.md index f290120b2..e75ecc355 100644 --- a/docs/mkdocs/docs/home/exceptions.md +++ b/docs/mkdocs/docs/home/exceptions.md @@ -987,7 +987,9 @@ contains other characters (e.g., `1a`), or `-` where it cannot be used. The JSON Patch operations 'remove' and 'move' cannot be applied to the root element of the JSON value. The exception is also thrown when the parent or a reference token of an empty JSON Pointer is requested, for instance by -[`back`](../api/json_pointer/back.md) or [`pop_back`](../api/json_pointer/pop_back.md). +[`front`](../api/json_pointer/front.md), [`back`](../api/json_pointer/back.md), +[`pop_front`](../api/json_pointer/pop_front.md), or [`pop_back`](../api/json_pointer/pop_back.md), and when +[`basic_json_document::erase`](../api/basic_json_document/erase.md) is called with an empty JSON Pointer. !!! failure "Example message" @@ -1263,9 +1265,9 @@ A JSON Patch operation 'test' failed. The unsuccessful operation is also printed ### json.exception.other_error.502 [`to_ubjson`](../api/basic_json/to_ubjson.md) and [`to_bjdata`](../api/basic_json/to_bjdata.md) were called with -`use_type = true` but `use_size = false`, and the value contains a non-empty array or object (or, with BJData Draft 3, -any binary value). UBJSON requires a size marker (`#`) after a type marker (`$`); scalars and empty arrays or objects -do not throw. +`use_type = true` but `use_size = false`, and the value contains a non-empty array, object, or binary value (with +BJData Draft 3, any binary value, including an empty one). UBJSON requires a size marker (`#`) after a type marker +(`$`); scalars and empty arrays or objects do not throw. !!! failure "Example message" diff --git a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp index e4d9838cb..baddd0d86 100644 --- a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp +++ b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp @@ -50,6 +50,7 @@ #undef JSON_HEDLEY_EMSCRIPTEN_VERSION #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK #undef JSON_HEDLEY_END_C_DECLS +#undef JSON_HEDLEY_FALL_THROUGH #undef JSON_HEDLEY_FLAGS #undef JSON_HEDLEY_FLAGS_CAST #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE @@ -158,4 +159,3 @@ #undef JSON_HEDLEY_WARNING #undef JSON_HEDLEY_WARN_UNUSED_RESULT #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG -#undef JSON_HEDLEY_FALL_THROUGH diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f77c68073..f040c90ab 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -48828,6 +48828,7 @@ struct formatter // NOLINT(cert-dcl58-c #undef JSON_HEDLEY_EMSCRIPTEN_VERSION #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK #undef JSON_HEDLEY_END_C_DECLS +#undef JSON_HEDLEY_FALL_THROUGH #undef JSON_HEDLEY_FLAGS #undef JSON_HEDLEY_FLAGS_CAST #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE @@ -48936,7 +48937,6 @@ struct formatter // NOLINT(cert-dcl58-c #undef JSON_HEDLEY_WARNING #undef JSON_HEDLEY_WARN_UNUSED_RESULT #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG -#undef JSON_HEDLEY_FALL_THROUGH // IWYU pragma: keep diff --git a/tests/src/unit-unicode.cpp b/tests/src/unit-unicode.cpp index 72c8f812f..aefba71a6 100644 --- a/tests/src/unit-unicode.cpp +++ b/tests/src/unit-unicode.cpp @@ -15,6 +15,7 @@ using nlohmann::json; #include #include #include +#include #include "make_test_data_available.hpp" #include "test_utils.hpp" @@ -265,39 +266,50 @@ TEST_CASE("Unicode") CHECK_THROWS_WITH_AS(_ = json::parse("\"\\uD800\\uE000\""), "[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF; last read: '\"\\uD800\\uE000'", json::parse_error&); } - } -#if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if) - SECTION("incorrect sequences") - { SECTION("high surrogate without low surrogate") { // D800..DBFF are high surrogates and must be followed by low // surrogates DC00..DFFF; here, nothing follows + json _; for (std::size_t cp = 0xD800u; cp <= 0xDBFFu; ++cp) { - std::string json_text = "\"" + codepoint_to_unicode(cp) + "\""; + const std::string json_text = "\"" + codepoint_to_unicode(cp) + "\""; CAPTURE(json_text) - CHECK_THROWS_AS(json::parse(json_text), json::parse_error&); + CHECK_THROWS_AS(_ = json::parse(json_text), json::parse_error&); } } SECTION("high surrogate with wrong low surrogate") { // D800..DBFF are high surrogates and must be followed by low - // surrogates DC00..DFFF; here a different sequence follows + // surrogates DC00..DFFF; here a different code unit follows. + // All pairs would be 1024 * 64512 parses, so every high + // surrogate is paired with the boundaries of the ranges around + // DC00..DFFF, and one high surrogate with every code unit + // (with accept(), which does not throw and is much faster) + const auto escaped_pair = [&](std::size_t cp1, std::size_t cp2) + { + return "\"" + codepoint_to_unicode(cp1) + codepoint_to_unicode(cp2) + "\""; + }; + const std::vector boundaries = {0x0000u, 0x0041u, 0xD7FFu, 0xD800u, 0xDBFFu, 0xE000u, 0xFFFFu}; + json _; for (std::size_t cp1 = 0xD800u; cp1 <= 0xDBFFu; ++cp1) { - for (std::size_t cp2 = 0x0000u; cp2 <= 0xFFFFu; ++cp2) + for (const std::size_t cp2 : boundaries) { - if (0xDC00u <= cp2 && cp2 <= 0xDFFFu) - { - continue; - } - - std::string json_text = "\"" + codepoint_to_unicode(cp1) + codepoint_to_unicode(cp2) + "\""; + const std::string json_text = escaped_pair(cp1, cp2); CAPTURE(json_text) - CHECK_THROWS_AS(json::parse(json_text), json::parse_error&); + CHECK_THROWS_AS(_ = json::parse(json_text), json::parse_error&); + } + } + for (std::size_t cp2 = 0x0000u; cp2 <= 0xFFFFu; ++cp2) + { + if (cp2 < 0xDC00u || cp2 > 0xDFFFu) + { + const std::string json_text = escaped_pair(0xDA00u, cp2); + CAPTURE(json_text) + CHECK_FALSE(json::accept(json_text)); } } } @@ -306,16 +318,15 @@ TEST_CASE("Unicode") { // low surrogates DC00..DFFF must follow high surrogates; here, // they occur alone + json _; for (std::size_t cp = 0xDC00u; cp <= 0xDFFFu; ++cp) { - std::string json_text = "\"" + codepoint_to_unicode(cp) + "\""; + const std::string json_text = "\"" + codepoint_to_unicode(cp) + "\""; CAPTURE(json_text) - CHECK_THROWS_AS(json::parse(json_text), json::parse_error&); + CHECK_THROWS_AS(_ = json::parse(json_text), json::parse_error&); } } - } -#endif } SECTION("read all unicode characters") From 77611d412f965d1cfb12766f290fdce38e91d17e Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:52:44 +0100 Subject: [PATCH 08/14] Document the Nix package in the package manager list (#6145) Signed-off-by: Niels Lohmann --- README.md | 1 + .../docs/images/package_managers/nix.svg | 187 ++++++++++++++++++ .../docs/integration/nix/CMakeLists.txt | 7 + docs/mkdocs/docs/integration/nix/example.cpp | 10 + .../docs/integration/package_managers.md | 53 ++++- 5 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 docs/mkdocs/docs/images/package_managers/nix.svg create mode 100644 docs/mkdocs/docs/integration/nix/CMakeLists.txt create mode 100644 docs/mkdocs/docs/integration/nix/example.cpp diff --git a/README.md b/README.md index bfcd841eb..f42d4e5b5 100644 --- a/README.md +++ b/README.md @@ -1383,6 +1383,7 @@ Use your favorite [**package manager**](https://json.nlohmann.me/integration/pac - [**build2**](https://json.nlohmann.me/integration/package_managers/#build2) `nlohmann-json` -  [**cpm.cmake**](https://json.nlohmann.me/integration/package_managers/#cpmcmake) `gh:nlohmann/json` -  [**xmake**](https://json.nlohmann.me/integration/package_managers/#xmake) `nlohmann_json` +-  [**Nix**](https://json.nlohmann.me/integration/package_managers/#nix) `nlohmann_json` The library is part of many package managers. See the [**documentation**](https://json.nlohmann.me/integration/package_managers/) for detailed descriptions and examples. diff --git a/docs/mkdocs/docs/images/package_managers/nix.svg b/docs/mkdocs/docs/images/package_managers/nix.svg new file mode 100644 index 000000000..2a065bc9d --- /dev/null +++ b/docs/mkdocs/docs/images/package_managers/nix.svg @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/docs/mkdocs/docs/integration/nix/CMakeLists.txt b/docs/mkdocs/docs/integration/nix/CMakeLists.txt new file mode 100644 index 000000000..12f4ae1f2 --- /dev/null +++ b/docs/mkdocs/docs/integration/nix/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.15) +project(json_example) + +find_package(nlohmann_json CONFIG REQUIRED) + +add_executable(json_example example.cpp) +target_link_libraries(json_example PRIVATE nlohmann_json::nlohmann_json) diff --git a/docs/mkdocs/docs/integration/nix/example.cpp b/docs/mkdocs/docs/integration/nix/example.cpp new file mode 100644 index 000000000..c51a502aa --- /dev/null +++ b/docs/mkdocs/docs/integration/nix/example.cpp @@ -0,0 +1,10 @@ +#include +#include +#include + +using nlohmann::json; + +int main() +{ + std::cout << std::setw(4) << json::meta() << std::endl; +} diff --git a/docs/mkdocs/docs/integration/package_managers.md b/docs/mkdocs/docs/integration/package_managers.md index 84fceae7a..b5bcef278 100644 --- a/docs/mkdocs/docs/integration/package_managers.md +++ b/docs/mkdocs/docs/integration/package_managers.md @@ -14,7 +14,8 @@ ![Conda](../images/package_managers/conda.svg){: style="height:1em"} [**Conda**](#conda) `nlohmann_json`   ![MacPorts](../images/package_managers/macports.svg){: style="height:1em"} [**MacPorts**](#macports) `nlohmann-json`
![cpm.cmake](../images/package_managers/CPM.png){: style="height:1em"} [**CPM.cmake**](#cpmcmake) `gh:nlohmann/json`   -![xmake](../images/package_managers/xmake.svg){: style="height:1em"} [**xmake**](#xmake) `nlohmann_json` +![xmake](../images/package_managers/xmake.svg){: style="height:1em"} [**xmake**](#xmake) `nlohmann_json`   +![Nix](../images/package_managers/nix.svg){: style="height:1em"} [**Nix**](#nix) `nlohmann_json`

## Running example @@ -906,6 +907,56 @@ CPMAddPackage("gh:nlohmann/json@3.12.0") * * * +## Nix + +!!! abstract "Summary" + + package: [**`nlohmann_json`**](https://search.nixos.org/packages?channel=unstable&show=nlohmann_json&query=nlohmann_json) + + - :octicons-tag-24: Available versions: current version of each Nixpkgs channel (may lag behind the latest release) + - :octicons-rocket-24: The package is maintained by the Nixpkgs community and updated after releases. + - :octicons-file-24: File issues at the [Nixpkgs issue tracker](https://github.com/NixOS/nixpkgs/issues) + - :octicons-question-24: [Nix website](https://nixos.org) + +If you are using [Nix](https://nixos.org) or [NixOS](https://nixos.org/manual/nixos/stable/), the library is available +as the [nlohmann_json](https://search.nixos.org/packages?channel=unstable&show=nlohmann_json&query=nlohmann_json) +package in [Nixpkgs](https://github.com/NixOS/nixpkgs). To try it in a temporary shell, execute + +```shell +nix-shell -p nlohmann_json +``` + +On NixOS, the package can be installed system-wide by adding it to `configuration.nix`: + +```nix +environment.systemPackages = [ pkgs.nlohmann_json ]; +``` + +??? example "Example: CMake" + + 1. Create the following files: + + ```cpp title="example.cpp" + --8<-- "integration/nix/example.cpp" + ``` + + ```cmake title="CMakeLists.txt" + --8<-- "integration/nix/CMakeLists.txt" + ``` + + 2. Enter a shell that provides the package, CMake, and a C++ compiler: + + ```sh + nix-shell -p nlohmann_json cmake + ``` + + 3. Compile the code: + + ```sh + cmake -S . -B build + cmake --build build + ``` + ## Other package managers The library is also contained in many other package repositories; [Repology](https://repology.org/project/nlohmann-json/versions) tracks the packaged From 2951790d40ebf91ca08d9351d829b5ddc819dc04 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 18 Nov 2026 08:52:54 +0100 Subject: [PATCH 09/14] Fix three dead links in the list of customers (#6147) Signed-off-by: Niels Lohmann --- docs/mkdocs/docs/home/customers.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/mkdocs/docs/home/customers.md b/docs/mkdocs/docs/home/customers.md index 336cc55c8..f4e9b4606 100644 --- a/docs/mkdocs/docs/home/customers.md +++ b/docs/mkdocs/docs/home/customers.md @@ -102,7 +102,7 @@ the result of an internet search. If you know further customers of the library, - [**Apple iOS and macOS**](https://www.apple.com/macos), a family of operating systems developed by Apple, including iOS for mobile devices and macOS for desktop computers - [**Chromium**](https://chromium.googlesource.com/chromium/src/+/main/third_party/nlohmann_json/), the open-source browser project that Google Chrome, Microsoft Edge, and many other browsers are built on, where the library is used as data container for on-device model execution -- [**Google Fuchsia**](https://fuchsia.googlesource.com/third_party/json/), an open-source operating system developed by Google, designed to be secure, updatable, and adaptable across various devices +- [**Google Fuchsia**](https://fuchsia.googlesource.com/fuchsia/+/refs/heads/main/scripts/prebuilts/swiftshader/build_config), an open-source operating system developed by Google, designed to be secure, updatable, and adaptable across various devices, where the library is used indirectly through bundled dependencies such as SwiftShader's debugger support and cppdap - [**LG webOS**](https://github.com/webosose/com.webos.service.camera), a Linux-based operating system used in LG smart TVs, signage, and embedded devices - [**Microsoft Azure Linux**](https://github.com/microsoft/azurelinux), a Linux distribution developed by Microsoft for Azure infrastructure and edge workloads - [**OpenHarmony**](https://github.com/openharmony/third_party_json), an open-source operating system for smart devices and the foundation of HarmonyOS @@ -174,7 +174,7 @@ the result of an internet search. If you know further customers of the library, - [**NVIDIA Instant NGP**](https://github.com/NVlabs/instant-ngp/blob/master/src/nerf_loader.cu), an implementation of instant neural graphics primitives for rapid scene reconstruction - [**NVIDIA TensorRT**](https://github.com/NVIDIA/TensorRT), an SDK for high-performance deep learning inference, including its TensorRT-LLM extension for large language models - [**NVIDIA TensorRT-LLM**](https://github.com/NVIDIA/TensorRT-LLM/blob/main/cpp/tensorrt_llm/common/safetensors.cpp), a toolkit for optimizing and serving large language model inference on GPUs -- [**ONNX Runtime**](https://github.com/microsoft/onnxruntime), a cross-platform inference and training accelerator for machine learning models +- [**ONNX Runtime**](https://onnxruntime.ai), a cross-platform inference and training accelerator for machine learning models - [**OpenVINO**](https://github.com/openvinotoolkit/openvino), Intel's toolkit for optimizing and deploying deep learning inference across CPUs, GPUs, and NPUs - [**PaddleOCR**](https://github.com/PaddlePaddle/PaddleOCR/blob/main/deploy/cpp_infer/src/modules/text_detection/result.cc), an optical character recognition toolkit that turns documents and images into structured data - [**PaddlePaddle**](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/ap/src/axpr/anf_expr.cc), a deep learning framework for distributed training and inference @@ -216,7 +216,7 @@ the result of an internet search. If you know further customers of the library, - [**Check Point**](https://www.checkpoint.com/about-us/copyright-and-trademarks/), a cybersecurity company specializing in threat prevention and network security solutions, offering a range of products designed to protect enterprises from cyber threats and ensure data integrity - [**EasyEffects**](https://github.com/wwmm/easyeffects/blob/master/src/presets_manager.hpp), an audio effects processor for PipeWire offering limiting, compression and equalization - [**espanso**](https://github.com/espanso/espanso/blob/dev/espanso-ui/src/win32/native.cpp), a cross-platform text expander -- [**Karabiner-Elements**](https://github.com/pqrs-org/Karabiner-Elements/blob/main/src/share/app_icon.hpp), a keyboard customizer for macOS +- [**Karabiner-Elements**](https://github.com/pqrs-org/Karabiner-Elements/blob/main/src/share/json_utility.hpp), a keyboard customizer for macOS - [**MacType**](https://github.com/snowie2000/mactype/blob/directwrite/settings.h), a font rendering engine for Windows - [**magicplan**](https://help.magicplan.app/acknowledgments), a mobile application for creating floor plans and interior designs using augmented reality - [**Microsoft Office for Mac**](https://officecdnmac.microsoft.com/pr/legal/mac/OfficeforMacAttributions.html), a suite of productivity applications developed by Microsoft for macOS, including tools for word processing, spreadsheets, and presentations From 0ac14610d6e5f194129a50ec0dc0030a07d5681a Mon Sep 17 00:00:00 2001 From: Shubham Shakti <130799986+0xmyst3ry@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:26:46 +0530 Subject: [PATCH 10/14] docs: correct erase() empty-range and initializer-list move documentation (#6122) Signed-off-by: Niels Lohmann --- docs/mkdocs/docs/api/basic_json/basic_json.md | 4 +++- docs/mkdocs/docs/api/basic_json/erase.md | 12 +++++++----- docs/mkdocs/docs/api/basic_json/object.md | 4 +++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/mkdocs/docs/api/basic_json/basic_json.md b/docs/mkdocs/docs/api/basic_json/basic_json.md index c029f519c..48c89dd20 100644 --- a/docs/mkdocs/docs/api/basic_json/basic_json.md +++ b/docs/mkdocs/docs/api/basic_json/basic_json.md @@ -265,7 +265,9 @@ basic_json(basic_json&& other) noexcept; 4. Depends on the called constructor. For types directly supported by the library (i.e., all types for which no `to_json()` function was provided), a strong guarantee holds: if an exception is thrown, there are no changes to any JSON value. -5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. +5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. Values that are moved into + `init` (e.g., `a` in `#!cpp json::object({std::move(a), 5})`) are moved when the initializer list is created, + before the constructor runs, and stay moved-from if it throws. 6. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. 7. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. 8. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. diff --git a/docs/mkdocs/docs/api/basic_json/erase.md b/docs/mkdocs/docs/api/basic_json/erase.md index 68eccc5a3..728ba4ace 100644 --- a/docs/mkdocs/docs/api/basic_json/erase.md +++ b/docs/mkdocs/docs/api/basic_json/erase.md @@ -26,10 +26,12 @@ void erase(const size_type idx); If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`. -2. Remove an element range specified by `[first; last)` from a JSON value. The iterator `first` does not need to be - dereferenceable if `first == last`: erasing an empty range is a no-op. +2. Remove an element range specified by `[first; last)` from a JSON value. For arrays and objects, the iterator `first` + does not need to be dereferenceable if `first == last`: erasing an empty range is a no-op. - If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`. + If called on a primitive type other than `#!json null`, the range must be `[begin(); end())`, and the resulting JSON + value will be `#!json null`. Any other range, including the empty ranges `[begin(); begin())` and + `[end(); end())`, throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204). 3. Removes an element from a JSON object by key. @@ -93,8 +95,8 @@ Strong exception safety: if an exception occurs, the original value stays intact - Throws [`invalid_iterator.203`](../../home/exceptions.md#jsonexceptioninvalid_iterator203) if called on iterators which does not belong to the current JSON value; example: `"iterators do not fit current value"` - Throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) if called on a - primitive type with invalid iterators (i.e., if `first != begin()` and `last != end()`); example: `"iterators out - of range"` + primitive type with invalid iterators (i.e., if `first != begin()` or `last != end()`, which includes the empty + ranges `[begin(); begin())` and `[end(); end())`); example: `"iterators out of range"` 3. The function can throw the following exceptions: - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than JSON object; example: `"cannot use erase() with null"` diff --git a/docs/mkdocs/docs/api/basic_json/object.md b/docs/mkdocs/docs/api/basic_json/object.md index 53d969e56..2378910d8 100644 --- a/docs/mkdocs/docs/api/basic_json/object.md +++ b/docs/mkdocs/docs/api/basic_json/object.md @@ -18,7 +18,9 @@ JSON object value ## Exception safety -Strong guarantee: if an exception is thrown, there are no changes in the JSON value. +Strong guarantee: if an exception is thrown, there are no changes in the JSON value. Values that are moved into `init` +(e.g., `a` in `#!cpp json::object({std::move(a), 5})`) are moved when the initializer list is created, before this +function runs, and stay moved-from if it throws. ## Exceptions From 8c3335d41f9b457ea69da16fcef57ec8d9b4cb17 Mon Sep 17 00:00:00 2001 From: Aditya Singh Bakawat Date: Wed, 18 Nov 2026 13:26:51 +0530 Subject: [PATCH 11/14] docs: correct assertion conditions and remove stale diagnostic locations (#6131) Signed-off-by: Niels Lohmann --- docs/mkdocs/docs/api/macros/json_assert.md | 8 +++---- docs/mkdocs/docs/features/assertions.md | 28 +++++++--------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/docs/mkdocs/docs/api/macros/json_assert.md b/docs/mkdocs/docs/api/macros/json_assert.md index 158fe7a68..d29d801db 100644 --- a/docs/mkdocs/docs/api/macros/json_assert.md +++ b/docs/mkdocs/docs/api/macros/json_assert.md @@ -47,11 +47,9 @@ Therefore, assertions can be switched off by defining `NDEBUG`. } ``` - Output: - - ``` - Assertion failed: (m_value.object->find(key) != m_value.object->end()), function operator[], file json.hpp, line 2144. - ``` + The assertion `#!cpp it != m_data.m_value.object->end()` fails in `#!cpp operator[]`. + The exact diagnostic format, source filename, and line number depend on the platform, + library version, and whether the single-header or multi-header version is used. ??? example "Example: user-defined behavior" diff --git a/docs/mkdocs/docs/features/assertions.md b/docs/mkdocs/docs/features/assertions.md index 2f2ad3ff7..c42f222c3 100644 --- a/docs/mkdocs/docs/features/assertions.md +++ b/docs/mkdocs/docs/features/assertions.md @@ -18,6 +18,10 @@ before including the `json.hpp` header. ## Function with runtime assertions +The examples below identify the failing assertion and the function containing it. +The exact diagnostic format, source filename, and line number depend on the platform, +library version, and whether the single-header or multi-header version is used. + ### Unchecked access to a const value Function [`operator[]`](../api/basic_json/operator%5B%5D.md) implements unchecked access for arrays and objects. Whereas @@ -46,11 +50,7 @@ See also the documentation on [element access](element_access/index.md). } ``` - Output: - - ``` - Assertion failed: (it != m_data.m_value.object->end()), function operator[], file json.hpp, line 28795. - ``` + The assertion `#!cpp it != m_data.m_value.object->end()` fails in `#!cpp operator[]`. ??? example "Example 2: Invalid array index in a JSON pointer" @@ -69,11 +69,7 @@ See also the documentation on [element access](element_access/index.md). } ``` - Output: - - ``` - Assertion failed: (idx < m_data.m_value.array->size()), function operator[], file json.hpp, line 28758. - ``` + The assertion `#!cpp idx < ptr->m_data.m_value.array->size()` fails in `#!cpp get_unchecked`. ### Constructing from an uninitialized iterator range @@ -96,11 +92,7 @@ uninitialized iterator is undefined behavior and yields a runtime assertion. } ``` - Output: - - ``` - Assertion failed: (m_object != nullptr), function operator++, file iter_impl.hpp, line 368. - ``` + The assertion `#!cpp first.m_object != nullptr` fails in `#!cpp basic_json`. ### Operations on uninitialized iterators @@ -123,11 +115,7 @@ behavior and yields a runtime assertion. } ``` - Output: - - ``` - Assertion failed: (m_object != nullptr), function operator++, file iter_impl.hpp, line 368. - ``` + The assertion `#!cpp m_object != nullptr` fails in `#!cpp operator++`. ## Changes From ccb253baf1b789c8b10f28f07ecfa521d67a9b45 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:57:02 +0100 Subject: [PATCH 12/14] build(deps): bump mkdocs-material from 9.8.0 to 9.8.1 in /docs/mkdocs (#6141) Signed-off-by: Niels Lohmann --- docs/mkdocs/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/mkdocs/requirements.txt b/docs/mkdocs/requirements.txt index 4014cf3b7..52c2e5843 100644 --- a/docs/mkdocs/requirements.txt +++ b/docs/mkdocs/requirements.txt @@ -2,7 +2,7 @@ wheel==0.48.0 mkdocs==1.6.1 # documentation framework mkdocs-git-revision-date-localized-plugin==1.6.3 # plugin "git-revision-date-localized" -mkdocs-material==9.8.0 # theme for mkdocs +mkdocs-material==9.8.1 # theme for mkdocs mkdocs-material-extensions==1.3.1 # extensions mkdocs-minify-plugin==0.8.0 # plugin "minify" mkdocs-redirects==1.2.3 # plugin "redirects" From b927100ca153b27dcfeb4dea09afa52b8b8482ef Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:57:19 +0100 Subject: [PATCH 13/14] build(deps): bump step-security/harden-runner from 2.22.3 to 2.22.4 (#6143) Signed-off-by: Niels Lohmann --- .github/workflows/cancel_closed_pr_runs.yml | 2 +- .github/workflows/check_amalgamation.yml | 4 ++-- .github/workflows/check_docs_links.yml | 2 +- .github/workflows/cifuzz.yml | 2 +- .github/workflows/codeql-analysis.yml | 2 +- .github/workflows/comment_check_amalgamation.yml | 2 +- .github/workflows/dependency-review.yml | 2 +- .github/workflows/flawfinder.yml | 2 +- .github/workflows/json_view_benchmarks.yml | 2 +- .github/workflows/labeler.yml | 2 +- .github/workflows/publish_documentation.yml | 2 +- .github/workflows/scorecards.yml | 2 +- .github/workflows/semgrep.yml | 2 +- .github/workflows/stale.yml | 2 +- .github/workflows/ubuntu.yml | 10 +++++----- 15 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/workflows/cancel_closed_pr_runs.yml b/.github/workflows/cancel_closed_pr_runs.yml index fd30c5047..7f0023f90 100644 --- a/.github/workflows/cancel_closed_pr_runs.yml +++ b/.github/workflows/cancel_closed_pr_runs.yml @@ -28,7 +28,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/check_amalgamation.yml b/.github/workflows/check_amalgamation.yml index 448d0d85b..956524897 100644 --- a/.github/workflows/check_amalgamation.yml +++ b/.github/workflows/check_amalgamation.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit @@ -46,7 +46,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/check_docs_links.yml b/.github/workflows/check_docs_links.yml index 0ec9778e6..3cfc80274 100644 --- a/.github/workflows/check_docs_links.yml +++ b/.github/workflows/check_docs_links.yml @@ -20,7 +20,7 @@ jobs: timeout-minutes: 30 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/cifuzz.yml b/.github/workflows/cifuzz.yml index 02a5a8173..2bac97387 100644 --- a/.github/workflows/cifuzz.yml +++ b/.github/workflows/cifuzz.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-22.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index aa982f139..5ebe01f10 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -27,7 +27,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/comment_check_amalgamation.yml b/.github/workflows/comment_check_amalgamation.yml index 558d5fba4..f8a76d6e4 100644 --- a/.github/workflows/comment_check_amalgamation.yml +++ b/.github/workflows/comment_check_amalgamation.yml @@ -20,7 +20,7 @@ jobs: pull-requests: write steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index 8034aa1c8..373208049 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -21,7 +21,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/flawfinder.yml b/.github/workflows/flawfinder.yml index 608523b7e..6978cc0cf 100644 --- a/.github/workflows/flawfinder.yml +++ b/.github/workflows/flawfinder.yml @@ -31,7 +31,7 @@ jobs: security-events: write steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/json_view_benchmarks.yml b/.github/workflows/json_view_benchmarks.yml index 49876a07b..d84cc43ac 100644 --- a/.github/workflows/json_view_benchmarks.yml +++ b/.github/workflows/json_view_benchmarks.yml @@ -54,7 +54,7 @@ jobs: timeout-minutes: 60 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index 004039586..5a38bd293 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -23,7 +23,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/publish_documentation.yml b/.github/workflows/publish_documentation.yml index ba8f435bf..ffa22f33a 100644 --- a/.github/workflows/publish_documentation.yml +++ b/.github/workflows/publish_documentation.yml @@ -37,7 +37,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/scorecards.yml b/.github/workflows/scorecards.yml index b2e08c488..2ccaabacb 100644 --- a/.github/workflows/scorecards.yml +++ b/.github/workflows/scorecards.yml @@ -40,7 +40,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/semgrep.yml b/.github/workflows/semgrep.yml index ca73edb55..034b77562 100644 --- a/.github/workflows/semgrep.yml +++ b/.github/workflows/semgrep.yml @@ -36,7 +36,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index b624f9f40..3d0573ace 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -16,7 +16,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index d8a467f29..9d3f6d823 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -80,7 +80,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit @@ -112,7 +112,7 @@ jobs: target: [ci_test_single_header, ci_cppcheck, ci_cpplint, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata, ci_reuse_compliance, ci_test_valgrind] steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit @@ -178,7 +178,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit @@ -436,7 +436,7 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit @@ -459,7 +459,7 @@ jobs: target: [ci_test_examples, ci_test_build_documentation, ci_test_documentation_mermaid] steps: - name: Harden Runner - uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit From d732d616badc771ab1e85876b3a9eb8191fdf4c0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:57:29 +0100 Subject: [PATCH 14/14] build(deps): bump semgrep from 1.184.0 to 1.185.0 in /cmake/requirements (#6146) Signed-off-by: Niels Lohmann --- cmake/requirements/requirements-semgrep.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/requirements/requirements-semgrep.txt b/cmake/requirements/requirements-semgrep.txt index 028580449..f9754d99e 100644 --- a/cmake/requirements/requirements-semgrep.txt +++ b/cmake/requirements/requirements-semgrep.txt @@ -1 +1 @@ -semgrep==1.184.0 +semgrep==1.185.0