mirror of
https://github.com/nlohmann/json.git
synced 2026-10-04 21:50:33 +00:00
Add API pages for basic_json_document::save() and load(), including the image_check enumeration and its three levels. Add examples that show caching a parsed document as an image, loading it without parsing, the difference between a borrowed and an owned image, and a full check rejecting a damaged image that a bounds check still reads safely. Add an "Images" section to the json_view feature page, register the new pages in mkdocs.yml and docSet.sql, group basic_json_document's member list by parsing/access/images/edits, and document parse_error.116 and type_error.320 on the exceptions page, extending out_of_range.416 for images. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
31 lines
1.1 KiB
C++
31 lines
1.1 KiB
C++
#include <cstdint>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <nlohmann/json_view.hpp>
|
|
|
|
using json_document = nlohmann::json_document;
|
|
|
|
int main()
|
|
{
|
|
std::cout << std::boolalpha;
|
|
|
|
// a configuration a service parses once and then caches as an image, so
|
|
// that later requests can load() it instead of parsing the text again
|
|
const std::string text = R"({"name": "cache", "host": "db1", "port": 6379, "replicas": ["db2", "db3"]})";
|
|
const json_document config = json_document::parse(text);
|
|
|
|
// save() turns the parsed document into a byte buffer: a 64-byte header,
|
|
// the node index, the source text, and the decoded strings
|
|
const std::vector<std::uint8_t> image = config.save();
|
|
std::cout << image.size() << '\n';
|
|
|
|
// the same document always saves to the same bytes
|
|
std::cout << (image == json_document::parse(text).save()) << '\n';
|
|
|
|
// loading the image back needs no parsing, yet dumps exactly what
|
|
// parsing the text produced
|
|
const json_document reloaded = json_document::load(image);
|
|
std::cout << (reloaded.root().dump() == config.root().dump()) << '\n';
|
|
}
|