mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 07:25:20 +00:00
- API pages for get, get_to, get_string, number_token, and value of basic_json_view; JSON pointer overloads of operator[], at, and contains; links both ways with the basic_json pages - the feature page describes which conversions copy nothing - the examples show when the view helps: strings without copies, numbers exactly as written, and paths into a large text Signed-off-by: Niels Lohmann <mail@nlohmann.me>
29 lines
962 B
C++
29 lines
962 B
C++
#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';
|
|
}
|