Document element access, lookup, and iteration of json_view

- API pages for operator[], at, front, back, find, contains, count,
  begin, end, cbegin, cend, items, and type_name of basic_json_view,
  linked both ways with the basic_json pages
- the feature page and size() describe document order and duplicate
  keys
- the examples show when the view helps: reading a few fields of a large
  text, probing optional members, and members in source order

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-30 21:01:33 +02:00
parent 7dae258350
commit 08dcdaf9b8
58 changed files with 1388 additions and 12 deletions
@@ -0,0 +1,24 @@
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
int main()
{
// the build log of a running job; front() reads just the earliest event
// without materializing the (possibly long) rest of the log
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
std::cout << log.root().front().materialize().dump() << '\n';
// an empty log -- front() throws instead of the undefined behavior
// basic_json::front() has for an empty array
json_document empty_log = json_document::parse("[]");
try
{
static_cast<void>(empty_log.root().front());
}
catch (const nlohmann::json::invalid_iterator& e)
{
std::cout << e.what() << '\n';
}
}