mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 05:00:30 +00:00
Add API reference pages for basic_json_document::insert and basic_json_document::erase, matching the style of set.md and push_back.md: signatures, parameters, return values, exception safety, exceptions with their exact ids and messages, complexity, notes on duplicate keys and view/iterator validity, and an example. Add example programs basic_json_document__insert.cpp and basic_json_document__erase.cpp with their expected output, each comparing an edit on an editable document with the same edit on a plain json value to show what is preserved: member order, the spelling of untouched numbers, and, for insert, that a view taken before the insert keeps referring to the same element after its index shifts. Register both new pages in mkdocs.yml, docSet.sql, and the member list of basic_json_document/index.md, add cross-references to them from set.md and push_back.md, and mention insert/erase in the "Editing a document" section of features/json_view.md. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
39 lines
1.5 KiB
C++
39 lines
1.5 KiB
C++
#include <iostream>
|
|
#include <nlohmann/json_view.hpp>
|
|
|
|
using json = nlohmann::json;
|
|
using json_editable_document = nlohmann::json_editable_document;
|
|
using json_editable_view = nlohmann::json_editable_view;
|
|
|
|
int main()
|
|
{
|
|
// a deprecated field is dropped from a configuration file, and a
|
|
// decommissioned replica is removed from the list -- "price" keeps its
|
|
// trailing zero, and the fields around the removed ones keep their order
|
|
const std::string text = R"({
|
|
"name": "cache",
|
|
"legacy_host": "db0",
|
|
"host": "db1",
|
|
"price": 19.90,
|
|
"replicas": ["db2", "db3", "db4"]
|
|
})";
|
|
|
|
json_editable_document doc = json_editable_document::parse(text);
|
|
|
|
doc.erase(doc.root(), "legacy_host"); // (1) an object member
|
|
doc.erase(doc.root()["replicas"], 1); // (2) an array element ("db3")
|
|
const std::size_t removed = doc.erase(json::json_pointer("/replicas/0")); // (3) via a JSON pointer
|
|
|
|
std::cout << removed << '\n';
|
|
std::cout << doc.root().dump(2, ' ', false, json_editable_view::number_format::source) << "\n\n";
|
|
|
|
// the same edits on a plain json value: object_t is a std::map, so
|
|
// parsing already sorted the keys, and dump() rewrites every number to
|
|
// its shortest form, even "price", which was never touched
|
|
json plain = json::parse(text);
|
|
plain.erase("legacy_host");
|
|
plain["replicas"].erase(1);
|
|
plain["replicas"].erase(0);
|
|
std::cout << plain.dump(2) << '\n';
|
|
}
|