- API pages for basic_json_document and basic_json_view, one per member, and for the four aliases, each with an example - features/json_view.md: the problem the view solves, ownership and lifetime, what matches basic_json::parse() and what differs, and when to choose json, ordered_json, SAX, or the view - the examples show why one would use the view, not only how: borrowed vs. owned input, reading a few fields and materializing one subtree, reusing a document across many messages - registered in the mkdocs navigation, llms.txt, the docset, the exceptions page (out_of_range.416), architecture.md, the integration page, and the README; the yyjson credit is added to the README and license.md Signed-off-by: Niels Lohmann <mail@nlohmann.me>
34 lines
1.4 KiB
C++
34 lines
1.4 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <nlohmann/json_view.hpp>
|
|
|
|
using json_document = nlohmann::json_document;
|
|
|
|
int main()
|
|
{
|
|
std::cout << std::boolalpha;
|
|
|
|
// source_offset() points into source(): useful to report *where* in the
|
|
// original text a value came from (error messages, syntax highlighting,
|
|
// forwarding a sub-range verbatim, ...) without materializing it
|
|
json_document doc = json_document::parse(R"( 123)");
|
|
auto v = doc.root();
|
|
std::cout << v.source_offset() << ' '
|
|
<< std::string(doc.source().data() + v.source_offset(), 3) << '\n';
|
|
|
|
// a string without escapes also stays in the source text
|
|
json_document plain = json_document::parse(R"("ab")");
|
|
std::cout << plain.source()[plain.root().source_offset()] << '\n';
|
|
|
|
// a string with escapes is decoded once into the document's own buffer, so
|
|
// there is no single byte range in source() to point at: source_offset()
|
|
// returns the "not applicable" sentinel
|
|
json_document escaped = json_document::parse(R"("a\nb")");
|
|
std::cout << (escaped.root().source_offset() == static_cast<std::size_t>(-1)) << '\n';
|
|
|
|
// a discarded view -- default-constructed, or the root of a failed parse
|
|
// with allow_exceptions == false -- has no offset either
|
|
json_document failed = json_document::parse("not json", false);
|
|
std::cout << (failed.root().source_offset() == static_cast<std::size_t>(-1)) << '\n';
|
|
}
|