Merge branch 'develop' into json-view/5869-7-check-utf8

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-27 07:29:14 +01:00
commit 47d84516eb
10 files changed
+131 -22

No files matched your search

+1 -1
View File
@@ -1311,7 +1311,7 @@ TEST_CASE("BSON declared document size bounds the content")
// records the events that a SAX parser receives
struct recorder : nlohmann::json_sax<json>
{
std::string events;
std::string events{};
bool null() override
{
+4 -4
View File
@@ -1122,17 +1122,17 @@ TEST_CASE("flatten of structured values")
// the same for other keys that are not valid array indices
CHECK_THROWS_WITH_AS(json({{"/a/0", 1}, {"/a/1a", 2}}).unflatten(),
"[json.exception.out_of_range.404] unresolved reference token '1a'", json::out_of_range&);
"[json.exception.out_of_range.404] unresolved reference token '1a'", json::out_of_range&);
CHECK_THROWS_WITH_AS(json({{"/a/0", 1}, {"/a/-", 2}}).unflatten(),
"[json.exception.out_of_range.404] unresolved reference token '-'", json::out_of_range&);
"[json.exception.out_of_range.404] unresolved reference token '-'", json::out_of_range&);
}
SECTION("unflatten with the empty token among array indices")
{
CHECK_THROWS_WITH_AS(json({{"/a/0", 1}, {"/a/", 2}}).unflatten(),
"[json.exception.out_of_range.404] unresolved reference token ''", json::out_of_range&);
"[json.exception.out_of_range.404] unresolved reference token ''", json::out_of_range&);
CHECK_THROWS_WITH_AS(json({{"/a/0", 1}, {"/a/1", 2}, {"/a/", 3}}).unflatten(),
"[json.exception.out_of_range.404] unresolved reference token ''", json::out_of_range&);
"[json.exception.out_of_range.404] unresolved reference token ''", json::out_of_range&);
// without an index "0", the empty token is an ordinary object key
CHECK(json({{"/a/", 2}}).unflatten() == json({{"a", {{"", 2}}}}));
+55
View File
@@ -666,6 +666,61 @@ TEST_CASE("json_view")
CHECK(small.root().materialize() == json::parse("[1,[2,3],{\"a\":\"b\\n\"}]"));
}
SECTION("block sizes")
{
// documents whose node estimate lies around the size that is allocated
// with the header, and whose decoded strings need the first growth of
// the buffer, in all sizes around the points where the policies change
for (const int count :
{
1, 2, 10, 50, 100, 255, 256, 257, 500, 1000, 1023, 1024, 1025, 2000, 5000
})
{
// strings with escapes (decoded text, minified)
std::string escaped = "[";
for (int i = 0; i < count; ++i)
{
escaped += (i != 0 ? ",\"a\\n" : "\"a\\n") + std::to_string(i) + "\\u00e4\"";
}
escaped += ']';
json_document e = json_document::parse(escaped);
CHECK(e.root().materialize() == json::parse(escaped));
e.shrink_to_fit();
CHECK(e.root().materialize() == json::parse(escaped));
// dense numbers (more nodes per byte than the estimate expects)
std::string dense = "[";
for (int i = 0; i < count; ++i)
{
dense += (i != 0 ? ",1" : "1");
}
dense += ']';
json_document n = json_document::parse(dense);
CHECK(n.node_count() == static_cast<std::size_t>(count) + 1);
CHECK(n.root().materialize() == json::parse(dense));
n.shrink_to_fit();
CHECK(n.root().size() == static_cast<std::size_t>(count));
// the same documents, pretty-printed
const std::string pretty = json::parse(escaped).dump(2);
json_document p = json_document::parse(pretty);
CHECK(p.root().materialize() == json::parse(escaped));
}
// a large document with a single escape does not need a buffer of the
// size of its input
std::string large = R"(["a\n")";
for (int i = 0; i < 100000; ++i)
{
large += ",1";
}
large += ']';
const json_document l = json_document::parse(large);
CHECK(l.root().size() == 100001);
CHECK(l.root()[0].get_string() == "a\n");
CHECK(l.root()[100000].get<int>() == 1);
}
SECTION("source offsets")
{
const std::string text = R"( {"key": "value", "escaped": "a\nb", "n": 42})";
+16 -5
View File
@@ -41,6 +41,13 @@ using nlohmann::ordered_json_document;
#include <optional>
#endif
// NLOHMANN_JSON_SERIALIZE_ENUM uses a static std::pair; same() compares floating-point values on purpose
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wfloat-equal")
DOCTEST_GCC_SUPPRESS_WARNING_PUSH
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")
// The from_json() functions of the NLOHMANN_DEFINE_TYPE_* and
// NLOHMANN_JSON_SERIALIZE_ENUM macros read views directly; other from_json()
// functions get the value of materialize().
@@ -61,10 +68,11 @@ struct point
struct shape
{
std::string name;
std::vector<point> points;
point center;
std::map<std::string, point> named;
// initialized for GCC -Weffc++
std::string name{};
std::vector<point> points{};
point center{};
std::map<std::string, point> named{};
NLOHMANN_DEFINE_TYPE_INTRUSIVE(shape, name, points, center, named)
};
@@ -79,7 +87,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(with_defaults, a, b, c)
struct with_names
{
int value = 0;
std::string text;
std::string text{}; // initialized for GCC -Weffc++
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_NAMES(with_names, "Value", value, "Text", text)
@@ -447,3 +455,6 @@ TEST_CASE("json_view from_json")
}
#endif
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP
DOCTEST_GCC_SUPPRESS_WARNING_POP
+5
View File
@@ -141,6 +141,7 @@ TEST_CASE("pointer access")
// check if pointers are returned correctly
const test_type* p1 = value.get_ptr<test_type*>();
CHECK(p1 == value.get_ptr<test_type*>());
// @infer-ignore NULLPTR_DEREFERENCE
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
@@ -170,6 +171,7 @@ TEST_CASE("pointer access")
// check if pointers are returned correctly
test_type* p1 = value.get_ptr<test_type*>();
CHECK(p1 == value.get_ptr<test_type*>());
// @infer-ignore NULLPTR_DEREFERENCE
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
@@ -199,6 +201,7 @@ TEST_CASE("pointer access")
// check if pointers are returned correctly
const test_type* p1 = value.get_ptr<test_type*>();
CHECK(p1 == value.get_ptr<test_type*>());
// @infer-ignore NULLPTR_DEREFERENCE
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
@@ -232,6 +235,7 @@ TEST_CASE("pointer access")
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p2 == value.get_ptr<const test_type*>());
// @infer-ignore NULLPTR_DEREFERENCE
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
@@ -257,6 +261,7 @@ TEST_CASE("pointer access")
// check if pointers are returned correctly
const test_type* p1 = value.get_ptr<test_type*>();
CHECK(p1 == value.get_ptr<test_type*>());
// @infer-ignore NULLPTR_DEREFERENCE
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();