mirror of
https://github.com/nlohmann/json.git
synced 2026-10-09 07:57:13 +00:00
Add element access, iteration, values, and JSON pointers to json_view
Give basic_json_view the read-only access functions of basic_json: operator[] and at() with keys and indices, front()/back(), find(), contains(), count(), begin()/end() and cbegin()/cend(), items() with structured bindings from C++17 on, and type_name(). Exceptions have the ids and messages of the const functions of basic_json. Where basic_json has undefined behavior the view answers safely: operator[] with a missing key or an out-of-range index returns a discarded view, and front()/back() of an empty container throw invalid_iterator.214. Objects are iterated in document order, and all members are visited; duplicate-key lookups find the first member (as yyjson and simdjson do), while parse(), materialize(), and the map conversions keep the last value, as parse() does. Keys of up to 16 bytes are compared with two overlapping loads. Add value conversions: get<T>()/get_to() for arithmetic types, bool, nullptr_t, strings (std::basic_string copied, string_view_t without a copy), BasicJsonType, views, std::vector, and maps with string keys; get_string() for the string without a copy; number_token() for the number exactly as written in the source; value() with keys and JSON pointers; and operator[]/at()/ contains() with JSON pointers. Everything else, including types with from_json(), goes through materialize() of that subtree. get<T>() of arithmetic types is inlined down to the conversion, so reading an integer needs no call. detail::json_pointer_access exposes a pointer's reference tokens to code outside basic_json. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
98 files changed
+5436
-62
No files matched your search
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// required fields of a service configuration -- at() reports a missing
|
||||
// or wrong-typed field with the very same exception basic_json::at()
|
||||
// would throw for the equivalent nlohmann::json value, so error
|
||||
// handling written against basic_json::at() keeps working unchanged
|
||||
json_document config = json_document::parse(R"({"name": "cache", "port": "6379"})");
|
||||
const json_view service = config.root();
|
||||
|
||||
std::cout << service.at("port").materialize().dump() << '\n';
|
||||
|
||||
try
|
||||
{
|
||||
// "port" is a string, not an array
|
||||
static_cast<void>(service.at("port").at(0));
|
||||
}
|
||||
catch (const nlohmann::json::type_error& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
static_cast<void>(service.at("timeout"));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
"6379"
|
||||
[json.exception.type_error.304] cannot use at() with string
|
||||
[json.exception.out_of_range.403] key 'timeout' not found
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"region": "eu", "servers": ["eu-1", "eu-2"]})");
|
||||
const auto root = doc.root();
|
||||
|
||||
std::cout << root.at(json_pointer("/servers/1")).materialize().dump() << '\n';
|
||||
|
||||
// at() throws for every resolution failure -- with the very same
|
||||
// message json::at(ptr) would throw for the same pointer and the same
|
||||
// document
|
||||
try
|
||||
{
|
||||
static_cast<void>(root.at(json_pointer("/servers/5")));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
static_cast<void>(root.at(json_pointer("/missing")));
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
"eu-2"
|
||||
[json.exception.out_of_range.401] array index 5 is out of range
|
||||
[json.exception.out_of_range.403] key 'missing' not found
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// the same build log; back() reads only the final status. It is linear
|
||||
// in the number of events (unlike front(), which is constant), but
|
||||
// still far less work than materializing the whole array
|
||||
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
|
||||
std::cout << log.root().back().materialize().dump() << '\n';
|
||||
|
||||
// an empty log -- back() throws instead of the undefined behavior
|
||||
// basic_json::back() has for an empty array
|
||||
json_document empty_log = json_document::parse("[]");
|
||||
try
|
||||
{
|
||||
static_cast<void>(empty_log.root().back());
|
||||
}
|
||||
catch (const nlohmann::json::invalid_iterator& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
"done"
|
||||
[json.exception.invalid_iterator.214] cannot get value
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a log record: the fields matter in the order they were written, e.g.
|
||||
// to reproduce the record as it was logged. A nlohmann::json object
|
||||
// sorts its keys, so materializing and iterating it would instead print
|
||||
// them alphabetically ("level", "message", "time")
|
||||
json_document record = json_document::parse(R"({"time": "10:00:01", "level": "info", "message": "started"})");
|
||||
|
||||
for (auto it = record.root().begin(); it != record.root().end(); ++it)
|
||||
{
|
||||
std::cout << it.key() << '=' << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
time="10:00:01"
|
||||
level="info"
|
||||
message="started"
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// sum many measurements with std::accumulate; cbegin()/cend() (identical
|
||||
// to begin()/end() here -- the view is always read-only) let the view be
|
||||
// used with standard algorithms without ever materializing the whole
|
||||
// array into a nlohmann::json value
|
||||
json_document measurements = json_document::parse("[3, 1, 4, 1, 5, 9, 2, 6]");
|
||||
const auto values = measurements.root();
|
||||
|
||||
const int sum = std::accumulate(values.cbegin(), values.cend(), 0,
|
||||
[](int total, const json_view & v)
|
||||
{
|
||||
return total + v.materialize().get<int>();
|
||||
});
|
||||
|
||||
std::cout << sum << '\n';
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
31
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// check that every element of a (possibly large) batch is an object,
|
||||
// before materializing any of them -- cbegin()/cend() (identical to
|
||||
// begin()/end() here) work as the range for std::all_of like they would
|
||||
// for any standard container
|
||||
json_document batch = json_document::parse(R"([{"id": 1}, {"id": 2}, {"id": 3}])");
|
||||
const auto records = batch.root();
|
||||
|
||||
const bool all_objects = std::all_of(records.cbegin(), records.cend(),
|
||||
[](const json_view & v)
|
||||
{
|
||||
return v.is_object();
|
||||
});
|
||||
|
||||
std::cout << std::boolalpha << all_objects << '\n';
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
true
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// count how many of many incoming records carry an optional "retry_of"
|
||||
// field -- contains() only walks the flat index, so scanning a large
|
||||
// batch like this never builds a single nlohmann::json value
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1},
|
||||
{"id": 2, "retry_of": 1},
|
||||
{"id": 3},
|
||||
{"id": 4, "retry_of": 3}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto records = batch.root();
|
||||
std::size_t retries = 0;
|
||||
for (std::size_t i = 0; i < records.size(); ++i)
|
||||
{
|
||||
if (records[i].contains("retry_of"))
|
||||
{
|
||||
++retries;
|
||||
}
|
||||
}
|
||||
std::cout << retries << " of " << records.size() << " records are retries\n";
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
2 of 4 records are retries
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
// "retry_of" is only present on some records, nested a level down
|
||||
// inside "meta"
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1, "meta": {}},
|
||||
{"id": 2, "meta": {"retry_of": 1}}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto records = batch.root();
|
||||
const json_pointer retry_of("/meta/retry_of");
|
||||
for (std::size_t i = 0; i < records.size(); ++i)
|
||||
{
|
||||
const auto record = records[i];
|
||||
if (record.contains(retry_of))
|
||||
{
|
||||
std::cout << "record " << i << " is a retry of " << record[retry_of].get<int>() << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "record " << i << " is original\n";
|
||||
}
|
||||
}
|
||||
|
||||
// contains() with a JSON pointer never throws -- not even for a
|
||||
// pointer that indexes into a primitive ("/0/id/x") or uses a
|
||||
// malformed array index ("/01"), either of which would need a
|
||||
// try/catch with json::contains(ptr)
|
||||
std::cout << std::boolalpha << records.contains(json_pointer("/0/id/x")) << '\n';
|
||||
std::cout << std::boolalpha << records.contains(json_pointer("/01")) << '\n';
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
record 0 is original
|
||||
record 1 is a retry of 1
|
||||
false
|
||||
false
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// validate that every transaction of a batch carries a mandatory
|
||||
// "amount" field before materializing any of them into a nlohmann::json
|
||||
// value -- count() returns 0 or 1 for an object
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"id": 1, "amount": 9.99},
|
||||
{"id": 2}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto transactions = batch.root();
|
||||
for (std::size_t i = 0; i < transactions.size(); ++i)
|
||||
{
|
||||
const auto transaction = transactions[i];
|
||||
if (transaction.count("amount") == 0)
|
||||
{
|
||||
std::cout << "transaction " << i << " is missing \"amount\"\n";
|
||||
continue;
|
||||
}
|
||||
std::cout << "transaction " << i << ": " << transaction["amount"].materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
transaction 0: 9.99
|
||||
transaction 1 is missing "amount"
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// scan a (possibly large) array of readings for the first one over a
|
||||
// threshold; the loop stops at end() as soon as one is found, and only
|
||||
// the matching reading is ever materialized
|
||||
json_document readings = json_document::parse("[12, 18, 25, 31, 9]");
|
||||
const auto values = readings.root();
|
||||
|
||||
auto it = values.begin();
|
||||
for (; it != values.end(); ++it)
|
||||
{
|
||||
if (it->materialize().get<int>() > 20)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (it != values.end())
|
||||
{
|
||||
std::cout << "first reading over 20: " << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
first reading over 20: 25
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a batch of incoming events; only some carry a "user_id" -- find()
|
||||
// locates it without throwing for the events that turn out to not be
|
||||
// objects, and without materializing an event that does not match
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"type": "click", "user_id": 42},
|
||||
{"type": "ping"},
|
||||
{"type": "click", "user_id": 7}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto events = batch.root();
|
||||
for (std::size_t i = 0; i < events.size(); ++i)
|
||||
{
|
||||
const auto event = events[i];
|
||||
const auto it = event.find("user_id");
|
||||
if (it != event.end())
|
||||
{
|
||||
std::cout << "user " << it->materialize().dump() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
user 42
|
||||
user 7
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// the build log of a running job; front() reads just the earliest event
|
||||
// without materializing the (possibly long) rest of the log
|
||||
json_document log = json_document::parse(R"(["queued", "started", "compiling", "linking", "done"])");
|
||||
std::cout << log.root().front().materialize().dump() << '\n';
|
||||
|
||||
// an empty log -- front() throws instead of the undefined behavior
|
||||
// basic_json::front() has for an empty array
|
||||
json_document empty_log = json_document::parse("[]");
|
||||
try
|
||||
{
|
||||
static_cast<void>(empty_log.root().front());
|
||||
}
|
||||
catch (const nlohmann::json::invalid_iterator& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
"queued"
|
||||
[json.exception.invalid_iterator.214] cannot get value
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
// address has no direct conversion in get<T>(), so get<address>() falls back
|
||||
// to materialize().get<address>() -- a real nlohmann::json value is built
|
||||
// for just this one member, and its own from_json() runs on that
|
||||
struct address
|
||||
{
|
||||
std::string city;
|
||||
int zip = 0;
|
||||
};
|
||||
|
||||
void from_json(const nlohmann::json& j, address& a)
|
||||
{
|
||||
j.at("city").get_to(a.city);
|
||||
j.at("zip").get_to(a.zip);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"(
|
||||
{
|
||||
"name": "Alice",
|
||||
"active": true,
|
||||
"orders": [1, 2, 3],
|
||||
"address": {"city": "Berlin", "zip": 10115}
|
||||
}
|
||||
)");
|
||||
const json_view customer = doc.root();
|
||||
|
||||
// read typed fields straight into C++ variables -- none of these build
|
||||
// a nlohmann::json value
|
||||
const std::string name = customer["name"].get<std::string>();
|
||||
const bool active = customer["active"].get<bool>();
|
||||
std::cout << name << (active ? " (active)" : " (inactive)") << '\n';
|
||||
|
||||
// std::vector<json_view> keeps views of the array elements instead of
|
||||
// copies of their values
|
||||
bool first = true;
|
||||
for (const json_view order : customer["orders"].get<std::vector<json_view>>())
|
||||
{
|
||||
std::cout << (first ? "" : " ") << order.get<int>();
|
||||
first = false;
|
||||
}
|
||||
std::cout << '\n';
|
||||
|
||||
// everything else goes through materialize()
|
||||
const address a = customer["address"].get<address>();
|
||||
std::cout << a.city << ' ' << a.zip << '\n';
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Alice (active)
|
||||
1 2 3
|
||||
Berlin 10115
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a "large response" stand-in: only the "id" field is ever read out of it
|
||||
const std::string text =
|
||||
R"({"id": "8f14e45f-ceea-467e-bb92-963f5e3c7a08", "note": "created via API\n", "payload": "..."})";
|
||||
const json_document doc = json_document::parse(text);
|
||||
const json_view response = doc.root();
|
||||
|
||||
const json_view::string_view_t id = response["id"].get_string();
|
||||
std::cout << id << '\n';
|
||||
|
||||
// no std::string was allocated for "id": its bytes still live inside
|
||||
// the original buffer, so id's data lies inside [text.data(),
|
||||
// text.data() + text.size())
|
||||
const bool id_in_source = id.data() >= text.data() && id.data() + id.size() <= text.data() + text.size();
|
||||
std::cout << std::boolalpha << id_in_source << '\n';
|
||||
|
||||
// "note" contains an escape sequence ('\n'), so it was decoded once
|
||||
// into the document's own buffer -- get_string() still avoids a copy
|
||||
// into a new std::string, but the bytes no longer live inside "text"
|
||||
const json_view::string_view_t note = response["note"].get_string();
|
||||
const bool note_in_source = note.data() >= text.data() && note.data() + note.size() <= text.data() + text.size();
|
||||
std::cout << std::boolalpha << note_in_source << '\n';
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
8f14e45f-ceea-467e-bb92-963f5e3c7a08
|
||||
true
|
||||
false
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"host": "db.example.com", "port": 5432, "ssl": true})");
|
||||
const json_view config = doc.root();
|
||||
|
||||
// get_to() writes directly into existing variables -- handy for filling
|
||||
// in the members of a struct one field at a time, without an
|
||||
// intermediate value from get<T>() for each one
|
||||
std::string host;
|
||||
int port = 0;
|
||||
bool ssl = false;
|
||||
config["host"].get_to(host);
|
||||
config["port"].get_to(port);
|
||||
config["ssl"].get_to(ssl);
|
||||
std::cout << host << ':' << port << (ssl ? " (tls)" : "") << '\n';
|
||||
|
||||
// the return value is a reference to the argument, so a call can be
|
||||
// used directly in a larger expression
|
||||
std::string other_host;
|
||||
std::cout << config["host"].get_to(other_host).size() << '\n';
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
db.example.com:5432 (tls)
|
||||
14
|
||||
@@ -0,0 +1,22 @@
|
||||
#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[] only ever sees the first
|
||||
// one, and materialize() -- like basic_json::parse() -- keeps the last
|
||||
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 << "first \"retries\" seen by operator[]: " << settings["retries"].materialize().dump() << '\n';
|
||||
std::cout << "last \"retries\" kept by materialize(): " << settings.materialize()["retries"].dump() << '\n';
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
retries=1
|
||||
timeout=30
|
||||
retries=5
|
||||
first "retries" seen by operator[]: 1
|
||||
last "retries" kept by materialize(): 5
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a price and an order id from an incoming order -- both need to be
|
||||
// reproduced exactly, e.g. for an invoice or an audit log
|
||||
json_document doc = json_document::parse(R"(
|
||||
{"price": 19.90, "order_id": 1234567890123456789012345, "quantity": 3}
|
||||
)");
|
||||
const json_view order = doc.root();
|
||||
|
||||
// number_token() returns the number exactly as written in the source
|
||||
std::cout << order["price"].number_token() << '\n';
|
||||
std::cout << order["order_id"].number_token() << '\n';
|
||||
|
||||
// get<double>() converts it instead -- the exact source text is gone:
|
||||
// "19.90" becomes the double closest to 19.9, printed without the
|
||||
// trailing zero, and the 25-digit order id -- far beyond any 64-bit
|
||||
// integer -- can only be approximated as a double
|
||||
std::cout << order["price"].get<double>() << '\n';
|
||||
std::cout << order.materialize()["order_id"].dump() << '\n';
|
||||
|
||||
// an ordinary quantity has nothing to lose either way
|
||||
std::cout << order["quantity"].number_token() << " == " << order["quantity"].get<int>() << '\n';
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
19.90
|
||||
1234567890123456789012345
|
||||
19.9
|
||||
1.2345678901234568e+24
|
||||
3 == 3
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
|
||||
int main()
|
||||
{
|
||||
// two user records from a large API response; only the fields that are
|
||||
// actually read are ever touched, and no nlohmann::json tree is built
|
||||
// for the batch
|
||||
json_document batch = json_document::parse(R"(
|
||||
[
|
||||
{"name": "Alice", "email": "alice@example.com", "tags": ["admin", "ops"]},
|
||||
{"name": "Bob", "tags": []}
|
||||
]
|
||||
)");
|
||||
|
||||
const auto users = batch.root();
|
||||
for (std::size_t i = 0; i < users.size(); ++i)
|
||||
{
|
||||
const auto user = users[i];
|
||||
std::cout << user["name"].materialize().dump();
|
||||
|
||||
// operator[] on a missing object key gives a discarded view -- test
|
||||
// it with a plain "if". The const overload of json::operator[]
|
||||
// would instead be undefined behavior (guarded by an assertion) for
|
||||
// a missing key
|
||||
if (const auto email = user["email"])
|
||||
{
|
||||
std::cout << " <" << email.materialize().dump() << ">";
|
||||
}
|
||||
|
||||
// the same holds for an array index past the end: a discarded view,
|
||||
// not undefined behavior
|
||||
if (const auto first_tag = user["tags"][0])
|
||||
{
|
||||
std::cout << " #" << first_tag.materialize().dump();
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
"Alice" <"alice@example.com"> #"admin"
|
||||
"Bob"
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
// a larger document; operator[] with a JSON pointer reaches straight to
|
||||
// one deeply nested field, without ever building a tree for the rest
|
||||
json_document doc = json_document::parse(R"(
|
||||
{
|
||||
"region": {
|
||||
"servers": [
|
||||
{"name": "eu-1", "metrics": {"cpu": 0.42}},
|
||||
{"name": "eu-2", "metrics": {"cpu": 0.71}}
|
||||
]
|
||||
}
|
||||
}
|
||||
)");
|
||||
|
||||
const auto root = doc.root();
|
||||
std::cout << root[json_pointer("/region/servers/1/metrics/cpu")].materialize().dump() << '\n';
|
||||
|
||||
// a missing key or an out-of-range index along the path gives a
|
||||
// discarded view, exactly where const json::operator[] would be
|
||||
// undefined behavior for the same pointer
|
||||
if (const auto missing = root[json_pointer("/region/servers/5/metrics/cpu")])
|
||||
{
|
||||
std::cout << missing.materialize().dump() << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "no such server\n";
|
||||
}
|
||||
|
||||
// indexing into a primitive still throws, as basic_json::operator[]
|
||||
// does for the same pointer
|
||||
try
|
||||
{
|
||||
static_cast<void>(root[json_pointer("/region/servers/0/name/x")]);
|
||||
}
|
||||
catch (const nlohmann::json::out_of_range& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
0.71
|
||||
no such server
|
||||
[json.exception.out_of_range.404] unresolved reference token 'x'
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
// report why some parsed messages were rejected, using only
|
||||
// type_name() -- no nlohmann::json value is built for the ones that
|
||||
// are wrong
|
||||
json_document good = json_document::parse(R"({"id": 1})");
|
||||
json_document bad = json_document::parse("[1, 2, 3]");
|
||||
json_document failed = json_document::parse("not json", /* allow_exceptions */ false);
|
||||
|
||||
for (const json_view v :
|
||||
{
|
||||
good.root(), bad.root(), failed.root()
|
||||
})
|
||||
{
|
||||
if (v.is_object())
|
||||
{
|
||||
std::cout << "ok\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "expected an object, got " << v.type_name() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
ok
|
||||
expected an object, got array
|
||||
expected an object, got discarded
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
#include <string>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"server": {"host": "localhost"}})");
|
||||
const json_view server = doc.root()["server"];
|
||||
|
||||
// "port" is missing -- value() returns the default instead of
|
||||
// throwing, so optional configuration fields never need their own
|
||||
// try/catch
|
||||
std::cout << server.value("host", std::string("0.0.0.0")) << '\n';
|
||||
std::cout << server.value("port", 8080) << '\n';
|
||||
|
||||
// a present but wrong-typed default still throws -- value() only
|
||||
// replaces "not found", not "wrong type", exactly as basic_json::value
|
||||
try
|
||||
{
|
||||
static_cast<void>(server.value("host", 0));
|
||||
}
|
||||
catch (const nlohmann::json::type_error& e)
|
||||
{
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
localhost
|
||||
8080
|
||||
[json.exception.type_error.302] type must be number, but is string
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json_view.hpp>
|
||||
|
||||
using json_document = nlohmann::json_document;
|
||||
using json_view = nlohmann::json_view;
|
||||
using json_pointer = nlohmann::json::json_pointer;
|
||||
|
||||
int main()
|
||||
{
|
||||
json_document doc = json_document::parse(R"({"server": {"host": "localhost", "limits": {"connections": 100}}})");
|
||||
const json_view config = doc.root();
|
||||
|
||||
// a nested, optional setting read with a default -- no exception, even
|
||||
// though "timeout" is missing several levels down
|
||||
std::cout << config.value(json_pointer("/server/limits/connections"), 10) << '\n';
|
||||
std::cout << config.value(json_pointer("/server/limits/timeout"), 30) << '\n';
|
||||
|
||||
// an out-of-range array index also falls back to the default
|
||||
json_document list_doc = json_document::parse(R"({"servers": ["a", "b"]})");
|
||||
std::cout << list_doc.root().value(json_pointer("/servers/5"), std::string("none")) << '\n';
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
100
|
||||
30
|
||||
none
|
||||
Reference in new issue
Block a user