mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 21:20:30 +00:00
- 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.0 KiB
C++
34 lines
1.0 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <nlohmann/json.hpp>
|
|
#include <nlohmann/json_view.hpp>
|
|
|
|
using json_document = nlohmann::json_document;
|
|
|
|
int main()
|
|
{
|
|
std::cout << std::boolalpha;
|
|
|
|
// an lvalue std::string is BORROWED: the document only stores a pointer
|
|
// into `text`, so `text` must outlive `borrowed`
|
|
std::string text = R"({"count": 3})";
|
|
json_document borrowed = json_document::parse(text);
|
|
std::cout << borrowed.owns_source() << '\n'; // false
|
|
|
|
// an rvalue std::string is MOVED into the document -- no copy of the text
|
|
json_document owned = json_document::parse(std::string(R"({"count": 3})"));
|
|
std::cout << owned.owns_source() << '\n'; // true
|
|
|
|
// errors are identical to basic_json::parse: same exception id, message,
|
|
// and position, because the library parser runs on the same bytes on a
|
|
// failing input
|
|
try
|
|
{
|
|
static_cast<void>(json_document::parse(R"({"count": )"));
|
|
}
|
|
catch (const nlohmann::json::parse_error& e)
|
|
{
|
|
std::cout << e.id << '\n';
|
|
}
|
|
}
|