|
◆ basic_json() [3/9]
template<typename CompatibleType , typename U = detail::uncvref_t<CompatibleType>, detail::enable_if_t< not detail::is_basic_json< U >::value and detail::is_compatible_type< basic_json_t, 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 exists. 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::valarray , std::set , std::unordered_set , std::multiset , and std::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
-
CompatibleType | a type such that:
- CompatibleType is not derived from
std::istream ,
- CompatibleType is not basic_json (to avoid hijacking copy/move constructors),
- CompatibleType is not a different basic_json type (i.e. with different template arguments)
- CompatibleType is not a basic_json nested type (e.g., json_pointer, iterator, etc ...)
- json_serializer has a
to_json(basic_json_t&, CompatibleType&&) method
|
U | = uncvref_t<CompatibleType> |
- Parameters
-
[in] | val | the value to be forwarded to the respective constructor |
- Complexity
- Usually linear in the size of the passed val, also depending on the implementation of the called
to_json() method.
- Exception safety
- Depends on the called constructor. For types directly supported by the library (i.e., all types for which no
to_json() function was provided), strong guarantee holds: if an exception is thrown, there are no changes to any JSON value.
- Example
- The following code shows the constructor with several compatible types.
4 #include <forward_list>
6 #include <unordered_map>
7 #include <unordered_set>
9 #include <nlohmann/json.hpp>
21 json j_object_t(object_value);
24 std::map<std::string, int> c_map
26 { "one", 1}, { "two", 2}, { "three", 3}
31 std::unordered_map<const char*, double> c_umap
33 { "one", 1.2}, { "two", 2.3}, { "three", 3.4}
38 std::multimap<std::string, bool> c_mmap
40 { "one", true}, { "two", true}, { "three", false}, { "three", true}
45 std::unordered_multimap<std::string, bool> c_ummap
47 { "one", true}, { "two", true}, { "three", false}, { "three", true}
49 json j_ummap(c_ummap);
52 std::cout << j_object_t << '\n';
53 std::cout << j_map << '\n';
54 std::cout << j_umap << '\n';
55 std::cout << j_mmap << '\n';
56 std::cout << j_ummap << "\n\n";
65 json j_array_t(array_value);
68 std::vector<int> c_vector {1, 2, 3, 4};
72 std::valarray<short> c_valarray {10, 9, 8, 7};
73 json j_valarray(c_valarray);
76 std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
77 json j_deque(c_deque);
80 std::list<bool> c_list { true, true, false, true};
84 std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
85 json j_flist(c_flist);
88 std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
89 json j_array(c_array);
92 std::set<std::string> c_set { "one", "two", "three", "four", "one"};
96 std::unordered_set<std::string> c_uset { "one", "two", "three", "four", "one"};
100 std::multiset<std::string> c_mset { "one", "two", "one", "four"};
104 std::unordered_multiset<std::string> c_umset { "one", "two", "one", "four"};
105 json j_umset(c_umset);
108 std::cout << j_array_t << '\n';
109 std::cout << j_vec << '\n';
110 std::cout << j_valarray << '\n';
111 std::cout << j_deque << '\n';
112 std::cout << j_list << '\n';
113 std::cout << j_flist << '\n';
114 std::cout << j_array << '\n';
115 std::cout << j_set << '\n';
116 std::cout << j_uset << '\n';
117 std::cout << j_mset << '\n';
118 std::cout << j_umset << "\n\n";
126 json::string_t string_value = "The quick brown fox jumps over the lazy dog.";
127 json j_string_t(string_value);
130 json j_string_literal( "The quick brown fox jumps over the lazy dog.");
133 std::string s_stdstring = "The quick brown fox jumps over the lazy dog.";
134 json j_stdstring(s_stdstring);
137 std::cout << j_string_t << '\n';
138 std::cout << j_string_literal << '\n';
139 std::cout << j_stdstring << "\n\n";
148 json j_integer_t(value_integer_t);
152 json j_unsigned_t(value_unsigned_t);
155 enum { enum_value = 17 };
156 json j_enum(enum_value);
162 int_least32_t n_int_least32_t = -17;
163 uint8_t n_uint8_t = 8;
166 json j_short(n_short);
169 json j_int_least32_t(n_int_least32_t);
170 json j_uint8_t(n_uint8_t);
178 float n_float = 42.23;
179 float n_float_nan = 1.0f / 0.0f;
180 double n_double = 23.42;
185 json j_infinity(v_infinity);
186 json j_float(n_float);
187 json j_float_nan(n_float_nan);
188 json j_double(n_double);
191 std::cout << j_integer_t << '\n';
192 std::cout << j_unsigned_t << '\n';
193 std::cout << j_enum << '\n';
194 std::cout << j_short << '\n';
195 std::cout << j_int << '\n';
196 std::cout << j_long << '\n';
197 std::cout << j_int_least32_t << '\n';
198 std::cout << j_uint8_t << '\n';
199 std::cout << j_ok << '\n';
200 std::cout << j_nan << '\n';
201 std::cout << j_infinity << '\n';
202 std::cout << j_float << '\n';
203 std::cout << j_float_nan << '\n';
204 std::cout << j_double << "\n\n";
213 json j_falsity = false;
216 std::cout << j_truth << '\n';
217 std::cout << j_falsity << '\n';
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]
[10,9,8,7]
[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.141592653589793
null
null
42.22999954223633
null
23.42
true
false
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/basic_json__CompatibleType.cpp -o basic_json__CompatibleType
- Since
- version 2.1.0
Definition at line 15843 of file json.hpp.
|