JSON for Modern C++ 3.10.4

◆ get_to() [3/3]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<typename ValueType , detail::enable_if_t< !detail::is_basic_json< ValueType >::value &&detail::has_from_json< basic_json_t, ValueType >::value, int > = 0>
ValueType & nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::get_to ( ValueType &  v) const
inlinenoexcept

Explicit type conversion between the JSON value and a compatible value. The value is filled into the input parameter by calling the json_serializer<ValueType> from_json() method.

The function is equivalent to executing

ValueType v;
JSONSerializer<ValueType>::from_json(*this, v);

This overloads is chosen if:

Template Parameters
ValueTypethe input parameter type.
Returns
the input parameter, allowing chaining calls.
Exceptions
whatjson_serializer<ValueType> from_json() method throws
Example
The example below shows several conversions from JSON values to other types. There a few things to note: (1) Floating-point numbers can be converted to integers, (2) A JSON array can be converted to a standard std::vector<short>, (3) A JSON object can be converted to C++ associative containers such as std::unordered_map<std::string, json>.
1#include <iostream>
2#include <unordered_map>
3#include <nlohmann/json.hpp>
4
5using json = nlohmann::json;
6
7int main()
8{
9 // create a JSON value with different types
10 json json_types =
11 {
12 {"boolean", true},
13 {
14 "number", {
15 {"integer", 42},
16 {"floating-point", 17.23}
17 }
18 },
19 {"string", "Hello, world!"},
20 {"array", {1, 2, 3, 4, 5}},
21 {"null", nullptr}
22 };
23
24 bool v1;
25 int v2;
26 short v3;
27 float v4;
28 int v5;
29 std::string v6;
30 std::vector<short> v7;
31 std::unordered_map<std::string, json> v8;
32
33
34 // use explicit conversions
35 json_types["boolean"].get_to(v1);
36 json_types["number"]["integer"].get_to(v2);
37 json_types["number"]["integer"].get_to(v3);
38 json_types["number"]["floating-point"].get_to(v4);
39 json_types["number"]["floating-point"].get_to(v5);
40 json_types["string"].get_to(v6);
41 json_types["array"].get_to(v7);
42 json_types.get_to(v8);
43
44 // print the conversion results
45 std::cout << v1 << '\n';
46 std::cout << v2 << ' ' << v3 << '\n';
47 std::cout << v4 << ' ' << v5 << '\n';
48 std::cout << v6 << '\n';
49
50 for (auto i : v7)
51 {
52 std::cout << i << ' ';
53 }
54 std::cout << "\n\n";
55
56 for (auto i : v8)
57 {
58 std::cout << i.first << ": " << i.second << '\n';
59 }
60}
basic_json<> json
default JSON class
Definition: json.hpp:3472

Output (play with this example online):
1
42 42
17.23 17
Hello, world!
1 2 3 4 5 

string: "Hello, world!"
number: {"floating-point":17.23,"integer":42}
null: null
boolean: true
array: [1,2,3,4,5]
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/get_to.cpp -o get_to 
Since
version 3.3.0

Definition at line 20793 of file json.hpp.