Merge branch 'json-view/18-view-object-index' into json-view/19-edit-set

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-29 23:35:40 +02:00
14 changed files with 102 additions and 37 deletions

View File

@@ -15,7 +15,7 @@ int main()
const auto values = measurements.root();
const int sum = std::accumulate(values.cbegin(), values.cend(), 0,
[](int total, const json_view & v)
[](int total, const json_view & v)
{
return total + v.materialize().get<int>();
});

View File

@@ -15,7 +15,7 @@ int main()
const auto records = batch.root();
const bool all_objects = std::all_of(records.cbegin(), records.cend(),
[](const json_view & v)
[](const json_view & v)
{
return v.is_object();
});

View File

@@ -19,7 +19,7 @@ int main()
// the source text; a json value's object_t is std::map, so
// materialize().dump() of the very same view sorts the keys instead
const json_document config = json_document::parse(
R"({"name": "cache", "host": "db1", "port": 6379, "timeout": 30})");
R"({"name": "cache", "host": "db1", "port": 6379, "timeout": 30})");
std::cout << config.root().dump(2) << "\n\n";
std::cout << config.root().materialize().dump(2) << '\n';
}

View File

@@ -10,9 +10,9 @@ int main()
// directly as views, without ever building a nlohmann::json value for
// either one
const json_document previous = json_document::parse(
R"({"name": "cache", "port": 6379, "timeout": 30})");
R"({"name": "cache", "port": 6379, "timeout": 30})");
const json_document current = json_document::parse(
R"({"port": 6379.0, "timeout": 30, "name": "cache"})");
R"({"port": 6379.0, "timeout": 30, "name": "cache"})");
// same members, reordered, and 6379 written as a float -- operator==
// treats them the same way BasicJsonType::operator== would
@@ -20,7 +20,7 @@ int main()
// an actually changed value is detected the same way
const json_document changed = json_document::parse(
R"({"name": "cache", "port": 6380, "timeout": 30})");
R"({"name": "cache", "port": 6380, "timeout": 30})");
std::cout << (previous.root() == changed.root()) << '\n';
// comparing a view directly against an expected json value -- handy in a

View File

@@ -11,7 +11,7 @@ int main()
// unwanted shape -- without ever materializing it into a json value just
// to compare
const json_document received = json_document::parse(
R"({"status": "ok", "code": 200})");
R"({"status": "ok", "code": 200})");
const json unwanted = {{"status", "error"}, {"code", 500}};
std::cout << std::boolalpha << (received.root() != unwanted) << '\n';

View File

@@ -13,7 +13,10 @@ int main()
json_document bad = json_document::parse("[1, 2, 3]");
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
for (const json_view v : { good.root(), bad.root(), failed.root() })
for (const json_view v :
{
good.root(), bad.root(), failed.root()
})
{
if (v.is_object())
{