Files
json/docs/mkdocs/docs/examples/basic_json_view__back.cpp
Niels Lohmann 5a9f59a688 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>
2026-09-29 14:02:50 +02:00

26 lines
855 B
C++

#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
int main()
{
// the same build log; back() reads only the final status. It is linear
// in the number of events (unlike front(), which is constant), but
// still far less work than materializing the whole array
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
std::cout << log.root().back().materialize().dump() << '\n';
// an empty log -- back() throws instead of the undefined behavior
// basic_json::back() has for an empty array
json_document empty_log = json_document::parse("[]");
try
{
static_cast<void>(empty_log.root().back());
}
catch (const nlohmann::json::invalid_iterator& e)
{
std::cout << e.what() << '\n';
}
}