JSON for Modern C++  2.1.1

◆ basic_json() [3/9]

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>
template<typename CompatibleType , typename U = detail::uncvref_t<CompatibleType>, detail::enable_if_t< not std::is_base_of< std::istream, U >::value and not std::is_same< U, basic_json_t >::value and not detail::is_basic_json_nested_type< basic_json_t, U >::value and detail::has_to_json< basic_json, U >::value, int > = 0>
nlohmann::basic_json::basic_json ( CompatibleType &&  val)
inlinenoexcept

This is a "catch all" constructor for all compatible JSON types; that is, types for which a to_json() method exsits. The constructor forwards the parameter val to that method (to json_serializer<U>to_json method with U = uncvref_t<CompatibleType>, to be exact).

Template type CompatibleType includes, but is not limited to, the following types:

  • arrays: array_t and all kinds of compatible containers such as std::vector, std::deque, std::list, std::forward_list, std::array, std::set, std::unordered_set, std::multiset, and unordered_multiset with a value_type from which a basic_json value can be constructed.
  • objects: object_t and all kinds of compatible associative containers such as std::map, std::unordered_map, std::multimap, and std::unordered_multimap with a key_type compatible to string_t and a value_type from which a basic_json value can be constructed.
  • strings: string_t, string literals, and all compatible string containers can be used.
  • numbers: number_integer_t, number_unsigned_t, number_float_t, and all convertible number types such as int, size_t, int64_t, float or double can be used.
  • boolean: boolean_t / bool can be used.

See the examples below.

Template Parameters
CompatibleTypea type such that:
U= uncvref_t<CompatibleType>
Parameters
[in]valthe value to be forwarded
Complexity
Usually linear in the size of the passed val, also depending on the implementation of the called to_json() method.
Exceptions
whatjson_serializer<U>to_json() throws
Example
The following code shows the constructor with several compatible types.
1 #include <json.hpp>
2 #include <deque>
3 #include <list>
4 #include <forward_list>
5 #include <set>
6 #include <unordered_map>
7 #include <unordered_set>
8 
9 using json = nlohmann::json;
10 
11 int main()
12 {
13  // ============
14  // object types
15  // ============
16 
17  // create an object from an object_t value
18  json::object_t object_value = { {"one", 1}, {"two", 2} };
19  json j_object_t(object_value);
20 
21  // create an object from std::map
22  std::map<std::string, int> c_map
23  {
24  {"one", 1}, {"two", 2}, {"three", 3}
25  };
26  json j_map(c_map);
27 
28  // create an object from std::unordered_map
29  std::unordered_map<const char*, double> c_umap
30  {
31  {"one", 1.2}, {"two", 2.3}, {"three", 3.4}
32  };
33  json j_umap(c_umap);
34 
35  // create an object from std::multimap
36  std::multimap<std::string, bool> c_mmap
37  {
38  {"one", true}, {"two", true}, {"three", false}, {"three", true}
39  };
40  json j_mmap(c_mmap); // only one entry for key "three" is used
41 
42  // create an object from std::unordered_multimap
43  std::unordered_multimap<std::string, bool> c_ummap
44  {
45  {"one", true}, {"two", true}, {"three", false}, {"three", true}
46  };
47  json j_ummap(c_ummap); // only one entry for key "three" is used
48 
49  // serialize the JSON objects
50  std::cout << j_object_t << '\n';
51  std::cout << j_map << '\n';
52  std::cout << j_umap << '\n';
53  std::cout << j_mmap << '\n';
54  std::cout << j_ummap << "\n\n";
55 
56 
57  // ===========
58  // array types
59  // ===========
60 
61  // create an array from an array_t value
62  json::array_t array_value = {"one", "two", 3, 4.5, false};
63  json j_array_t(array_value);
64 
65  // create an array from std::vector
66  std::vector<int> c_vector {1, 2, 3, 4};
67  json j_vec(c_vector);
68 
69  // create an array from std::deque
70  std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
71  json j_deque(c_deque);
72 
73  // create an array from std::list
74  std::list<bool> c_list {true, true, false, true};
75  json j_list(c_list);
76 
77  // create an array from std::forward_list
78  std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
79  json j_flist(c_flist);
80 
81  // create an array from std::array
82  std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
83  json j_array(c_array);
84 
85  // create an array from std::set
86  std::set<std::string> c_set {"one", "two", "three", "four", "one"};
87  json j_set(c_set); // only one entry for "one" is used
88 
89  // create an array from std::unordered_set
90  std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
91  json j_uset(c_uset); // only one entry for "one" is used
92 
93  // create an array from std::multiset
94  std::multiset<std::string> c_mset {"one", "two", "one", "four"};
95  json j_mset(c_mset); // both entries for "one" are used
96 
97  // create an array from std::unordered_multiset
98  std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
99  json j_umset(c_umset); // both entries for "one" are used
100 
101  // serialize the JSON arrays
102  std::cout << j_array_t << '\n';
103  std::cout << j_vec << '\n';
104  std::cout << j_deque << '\n';
105  std::cout << j_list << '\n';
106  std::cout << j_flist << '\n';
107  std::cout << j_array << '\n';
108  std::cout << j_set << '\n';
109  std::cout << j_uset << '\n';
110  std::cout << j_mset << '\n';
111  std::cout << j_umset << "\n\n";
112 
113 
114  // ============
115  // string types
116  // ============
117 
118  // create string from a string_t value
119  json::string_t string_value = "The quick brown fox jumps over the lazy dog.";
120  json j_string_t(string_value);
121 
122  // create a JSON string directly from a string literal
123  json j_string_literal("The quick brown fox jumps over the lazy dog.");
124 
125  // create string from std::string
126  std::string s_stdstring = "The quick brown fox jumps over the lazy dog.";
127  json j_stdstring(s_stdstring);
128 
129  // serialize the JSON strings
130  std::cout << j_string_t << '\n';
131  std::cout << j_string_literal << '\n';
132  std::cout << j_stdstring << "\n\n";
133 
134 
135  // ============
136  // number types
137  // ============
138 
139  // create a JSON number from number_integer_t
140  json::number_integer_t value_integer_t = -42;
141  json j_integer_t(value_integer_t);
142 
143  // create a JSON number from number_unsigned_t
144  json::number_integer_t value_unsigned_t = 17;
145  json j_unsigned_t(value_unsigned_t);
146 
147  // create a JSON number from an anonymous enum
148  enum { enum_value = 17 };
149  json j_enum(enum_value);
150 
151  // create values of different integer types
152  short n_short = 42;
153  int n_int = -23;
154  long n_long = 1024;
155  int_least32_t n_int_least32_t = -17;
156  uint8_t n_uint8_t = 8;
157 
158  // create (integer) JSON numbers
159  json j_short(n_short);
160  json j_int(n_int);
161  json j_long(n_long);
162  json j_int_least32_t(n_int_least32_t);
163  json j_uint8_t(n_uint8_t);
164 
165  // create values of different floating-point types
166  json::number_float_t v_ok = 3.141592653589793;
167  json::number_float_t v_nan = NAN;
168  json::number_float_t v_infinity = INFINITY;
169 
170  // create values of different floating-point types
171  float n_float = 42.23;
172  float n_float_nan = 1.0f / 0.0f;
173  double n_double = 23.42;
174 
175  // create (floating point) JSON numbers
176  json j_ok(v_ok);
177  json j_nan(v_nan);
178  json j_infinity(v_infinity);
179  json j_float(n_float);
180  json j_float_nan(n_float_nan);
181  json j_double(n_double);
182 
183  // serialize the JSON numbers
184  std::cout << j_integer_t << '\n';
185  std::cout << j_unsigned_t << '\n';
186  std::cout << j_enum << '\n';
187  std::cout << j_short << '\n';
188  std::cout << j_int << '\n';
189  std::cout << j_long << '\n';
190  std::cout << j_int_least32_t << '\n';
191  std::cout << j_uint8_t << '\n';
192  std::cout << j_ok << '\n';
193  std::cout << j_nan << '\n';
194  std::cout << j_infinity << '\n';
195  std::cout << j_float << '\n';
196  std::cout << j_float_nan << '\n';
197  std::cout << j_double << "\n\n";
198 
199 
200  // =============
201  // boolean types
202  // =============
203 
204  // create boolean values
205  json j_truth = true;
206  json j_falsity = false;
207 
208  // serialize the JSON booleans
209  std::cout << j_truth << '\n';
210  std::cout << j_falsity << '\n';
211 }
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:1618
basic_json<> json
default JSON class
Definition: json.hpp:12369
ObjectType< StringType, basic_json, std::less< StringType >, AllocatorType< std::pair< const StringType, basic_json > >> object_t
a type for an object
Definition: json.hpp:1282
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:1479
ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition: json.hpp:1328
StringType string_t
a type for a string
Definition: json.hpp:1381
Output (play with this example online):
{"one":1,"two":2}
{"one":1,"three":3,"two":2}
{"one":1.2,"three":3.4,"two":2.3}
{"one":true,"three":false,"two":true}
{"one":true,"three":false,"two":true}

["one","two",3,4.5,false]
[1,2,3,4]
[1.2,2.3,3.4,5.6]
[true,true,false,true]
[12345678909876,23456789098765,34567890987654,45678909876543]
[1,2,3,4]
["four","one","three","two"]
["four","three","two","one"]
["four","one","one","two"]
["four","two","one","one"]

"The quick brown fox jumps over the lazy dog."
"The quick brown fox jumps over the lazy dog."
"The quick brown fox jumps over the lazy dog."

-42
17
17
42
-23
1024
-17
8
3.14159265358979
null
null
42.2299995422363
null
23.42

true
false
The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/basic_json__CompatibleType.cpp -o basic_json__CompatibleType 
Since
version 2.1.0

Definition at line 2006 of file json.hpp.