JSON for Modern C++ 3.7.3

accept
accept
accept
allocator_type
array
array_t
at
at
at
at
at
at
back
back
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
begin
begin
binary
binary
binary
binary
binary_t
boolean_t
cbegin
cend
clear
const_iterator
const_pointer
const_reference
const_reverse_iterator
contains
contains
count
crbegin
crend
detail::external_constructor
diff
difference_type
dump
emplace
emplace_back
empty
end
end
erase
erase
erase
erase
error_handler_t
exception
find
find
flatten
from_bson
from_bson
from_bson
from_cbor
from_cbor
from_cbor
from_msgpack
from_msgpack
from_msgpack
from_ubjson
from_ubjson
from_ubjson
front
front
get
get
get
get
get
get
get_allocator
get_binary
get_binary
get_ptr
get_ptr
get_ref
get_ref
get_to
get_to
initializer_list_t
input_format_t
insert
insert
insert
insert
insert
insert
insert_iterator
invalid_iterator
is_array
is_binary
is_boolean
is_discarded
is_null
is_number
is_number_float
is_number_integer
is_number_unsigned
is_object
is_primitive
is_string
is_structured
items
items
iterator
iterator_wrapper
iterator_wrapper
json_pointer
json_sax_t
json_serializer
max_size
merge_patch
meta
number_float_t
number_integer_t
number_unsigned_t
object
object_comparator_t
object_t
operator value_t
operator ValueType
operator!=
operator!=
operator!=
operator+=
operator+=
operator+=
operator+=
operator<
operator<
operator<
operator<<
operator<<
operator<=
operator<=
operator<=
operator=
operator==
operator==
operator==
operator>
operator>
operator>
operator>=
operator>=
operator>=
operator>>
operator>>
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
other_error
out_of_range
parse
parse
parse
parse_error
parse_event_t
parser_callback_t
patch
pointer
push_back
push_back
push_back
push_back
rbegin
rbegin
reference
rend
rend
reverse_iterator
sax_parse
sax_parse
sax_parse
size
size_type
string_t
swap
swap
swap
swap
swap
swap
to_bson
to_bson
to_bson
to_cbor
to_cbor
to_cbor
to_msgpack
to_msgpack
to_msgpack
to_ubjson
to_ubjson
to_ubjson
type
type_error
type_name
unflatten
update
update
value
value
value
value
value_t
value_type
~basic_json
| ## ◆ basic_json() [3/10] 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. binary: binary_t / `std::vector<uint8_t>` may be used, unfortunately because string literals cannot be distinguished from binary character arrays by the C++ type system, all types compatible with `const char*` will be directed to the string constructor instead. This is both for backwards compatibility, and due to the fact that a binary type is not a standard JSON type. 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. 1 #include <iostream> 2 #include <deque> 3 #include <list> 4 #include <forward_list> 5 #include <set> 6 #include <unordered_map> 7 #include <unordered_set> 8 #include <valarray> 9 #include <nlohmann/json.hpp> 10 11 using json = nlohmann::json; 12 13 int main() 14 { 15 // ============ 16 // object types 17 // ============ 18 19 // create an object from an object_t value 20 json::object_t object_value = { {"one", 1}, {"two", 2} }; 21 json j_object_t(object_value); 22 23 // create an object from std::map 24 std::map<std::string, int> c_map 25 { 26 {"one", 1}, {"two", 2}, {"three", 3} 27 }; 28 json j_map(c_map); 29 30 // create an object from std::unordered_map 31 std::unordered_map<const char*, double> c_umap 32 { 33 {"one", 1.2}, {"two", 2.3}, {"three", 3.4} 34 }; 35 json j_umap(c_umap); 36 37 // create an object from std::multimap 38 std::multimap<std::string, bool> c_mmap 39 { 40 {"one", true}, {"two", true}, {"three", false}, {"three", true} 41 }; 42 json j_mmap(c_mmap); // only one entry for key "three" is used 43 44 // create an object from std::unordered_multimap 45 std::unordered_multimap<std::string, bool> c_ummap 46 { 47 {"one", true}, {"two", true}, {"three", false}, {"three", true} 48 }; 49 json j_ummap(c_ummap); // only one entry for key "three" is used 50 51 // serialize the JSON objects 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"; 57 58 59 // =========== 60 // array types 61 // =========== 62 63 // create an array from an array_t value 64 json::array_t array_value = {"one", "two", 3, 4.5, false}; 65 json j_array_t(array_value); 66 67 // create an array from std::vector 68 std::vector<int> c_vector {1, 2, 3, 4}; 69 json j_vec(c_vector); 70 71 // create an array from std::valarray 72 std::valarray<short> c_valarray {10, 9, 8, 7}; 73 json j_valarray(c_valarray); 74 75 // create an array from std::deque 76 std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6}; 77 json j_deque(c_deque); 78 79 // create an array from std::list 80 std::list<bool> c_list {true, true, false, true}; 81 json j_list(c_list); 82 83 // create an array from std::forward_list 84 std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; 85 json j_flist(c_flist); 86 87 // create an array from std::array 88 std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; 89 json j_array(c_array); 90 91 // create an array from std::set 92 std::set<std::string> c_set {"one", "two", "three", "four", "one"}; 93 json j_set(c_set); // only one entry for "one" is used 94 95 // create an array from std::unordered_set 96 std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"}; 97 json j_uset(c_uset); // only one entry for "one" is used 98 99 // create an array from std::multiset 100 std::multiset<std::string> c_mset {"one", "two", "one", "four"}; 101 json j_mset(c_mset); // both entries for "one" are used 102 103 // create an array from std::unordered_multiset 104 std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"}; 105 json j_umset(c_umset); // both entries for "one" are used 106 107 // serialize the JSON arrays 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"; 119 120 121 // ============ 122 // string types 123 // ============ 124 125 // create string from a string_t value 126 json::string_t string_value = "The quick brown fox jumps over the lazy dog."; 127 json j_string_t(string_value); 128 129 // create a JSON string directly from a string literal 130 json j_string_literal("The quick brown fox jumps over the lazy dog."); 131 132 // create string from std::string 133 std::string s_stdstring = "The quick brown fox jumps over the lazy dog."; 134 json j_stdstring(s_stdstring); 135 136 // serialize the JSON strings 137 std::cout << j_string_t << '\n'; 138 std::cout << j_string_literal << '\n'; 139 std::cout << j_stdstring << "\n\n"; 140 141 142 // ============ 143 // number types 144 // ============ 145 146 // create a JSON number from number_integer_t 147 json::number_integer_t value_integer_t = -42; 148 json j_integer_t(value_integer_t); 149 150 // create a JSON number from number_unsigned_t 151 json::number_integer_t value_unsigned_t = 17; 152 json j_unsigned_t(value_unsigned_t); 153 154 // create a JSON number from an anonymous enum 155 enum { enum_value = 17 }; 156 json j_enum(enum_value); 157 158 // create values of different integer types 159 short n_short = 42; 160 int n_int = -23; 161 long n_long = 1024; 162 int_least32_t n_int_least32_t = -17; 163 uint8_t n_uint8_t = 8; 164 165 // create (integer) JSON numbers 166 json j_short(n_short); 167 json j_int(n_int); 168 json j_long(n_long); 169 json j_int_least32_t(n_int_least32_t); 170 json j_uint8_t(n_uint8_t); 171 172 // create values of different floating-point types 173 json::number_float_t v_ok = 3.141592653589793; 174 json::number_float_t v_nan = NAN; 175 json::number_float_t v_infinity = INFINITY; 176 177 // create values of different floating-point types 178 float n_float = 42.23; 179 float n_float_nan = 1.0f / 0.0f; 180 double n_double = 23.42; 181 182 // create (floating point) JSON numbers 183 json j_ok(v_ok); 184 json j_nan(v_nan); 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); 189 190 // serialize the JSON numbers 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"; 205 206 207 // ============= 208 // boolean types 209 // ============= 210 211 // create boolean values 212 json j_truth = true; 213 json j_falsity = false; 214 215 // serialize the JSON booleans 216 std::cout << j_truth << '\n'; 217 std::cout << j_falsity << '\n'; 218 } 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 with
      g++ -std=c++11 -Isingle_include doc/examples/basic_json__CompatibleType.cpp -o basic_json__CompatibleType
Since version 2.1.0 Definition at line 17227 of file json.hpp.

nlohmann::basic_json::number_integer_t
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:16488
nlohmann::basic_json::array_t
ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition: json.hpp:16337
nlohmann::basic_json::object_t
ObjectType< StringType, basic_json, object_comparator_t, AllocatorType< std::pair< const StringType, basic_json > >> object_t
a type for an object
Definition: json.hpp:16291
nlohmann::basic_json::string_t
StringType string_t
a type for a string
Definition: json.hpp:16390
nlohmann::basic_json::number_float_t
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:16627
nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:2774