mirror of
https://github.com/nlohmann/json.git
synced 2026-10-10 08:27:13 +00:00
* Lookups (operator[], at, find, contains, count, value, JSON pointers) return the last member of a duplicate key, as materialize() and parse() keep it. * operator[] on a discarded view returns a discarded view instead of throwing, so v["a"]["b"] is safe for a missing "a". * operator[] and at() take any integer type (not only int and size_t), fixing ambiguous calls with unsigned, long, std::int64_t, ... * Fix the operator[] documentation, which claimed a discarded view for a type mismatch where type_error.305 is thrown. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
23 lines
903 B
C++
23 lines
903 B
C++
#include <iostream>
|
|
#include <nlohmann/json_view.hpp>
|
|
|
|
using json_document = nlohmann::json_document;
|
|
|
|
int main()
|
|
{
|
|
// a settings object whose source text records every update to a key as
|
|
// a duplicate member. items() visits all of them, in document order, so
|
|
// the update history is visible; operator[] and materialize() -- like
|
|
// basic_json::parse() -- see the last one
|
|
json_document updates = json_document::parse(R"({"retries": 1, "timeout": 30, "retries": 5})");
|
|
const auto settings = updates.root();
|
|
|
|
for (const auto& item : settings.items())
|
|
{
|
|
std::cout << item.key() << '=' << item.value().materialize().dump() << '\n';
|
|
}
|
|
|
|
std::cout << "last \"retries\" seen by operator[]: " << settings["retries"].materialize().dump() << '\n';
|
|
std::cout << "last \"retries\" kept by materialize(): " << settings.materialize()["retries"].dump() << '\n';
|
|
}
|