You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/vendor/nlohmann/doc/examples/get_ref.cpp

28 lines
573 B
C++

#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create a JSON number
json value = 17;
// explicitly getting references
auto r1 = value.get_ref<const json::number_integer_t&>();
auto r2 = value.get_ref<json::number_integer_t&>();
// print the values
std::cout << r1 << ' ' << r2 << '\n';
// incompatible type throws exception
try
{
auto r3 = value.get_ref<json::number_float_t&>();
}
catch (json::type_error& ex)
{
std::cout << ex.what() << '\n';
}
}