Files
json/docs/mkdocs/docs/examples/basic_json_view__operator[].cpp
T
Niels Lohmann ac1764b33a Test discarded views with is_discarded() in the operator[] docs and examples
The explicit conversion to bool of basic_json_view is gone.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-11 11:30:36 +02:00

45 lines
1.4 KiB
C++

#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
int main()
{
// two user records from a large API response; only the fields that are
// actually read are ever touched, and no nlohmann::json tree is built
// for the batch
json_document batch = json_document::parse(R"(
[
{"name": "Alice", "email": "alice@example.com", "tags": ["admin", "ops"]},
{"name": "Bob", "tags": []}
]
)");
const auto users = batch.root();
for (std::size_t i = 0; i < users.size(); ++i)
{
const auto user = users[i];
std::cout << user["name"].materialize().dump();
// operator[] on a missing object key gives a discarded view -- test
// it with is_discarded(). The const overload of json::operator[]
// would instead be undefined behavior (guarded by an assertion) for
// a missing key
const auto email = user["email"];
if (!email.is_discarded())
{
std::cout << " <" << email.materialize().dump() << ">";
}
// the same holds for an array index past the end: a discarded view,
// not undefined behavior
const auto first_tag = user["tags"][0];
if (!first_tag.is_discarded())
{
std::cout << " #" << first_tag.materialize().dump();
}
std::cout << '\n';
}
}