mirror of
https://github.com/nlohmann/json.git
synced 2024-12-16 23:17:49 +08:00
504012a3db
As <iostream> is not included in json.hpp any more, all code examples need to include <iostream> now.
28 lines
564 B
C++
28 lines
564 B
C++
#include <iostream>
|
|
#include "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';
|
|
}
|
|
}
|