|
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 andnot std::is_same< U, basic_json_t >::value andnot detail::is_basic_json_nested_type< basic_json_t, U >::value anddetail::has_to_json< basic_json, U >::value, int > = 0>
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
-
CompatibleType | a type such that:
|
U | = uncvref_t<CompatibleType> |
- Parameters
-
[in] | val | the 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
-
what | json_serializer<U>to_json() throws |
- Example
- The following code shows the constructor with several compatible types.
4 #include <forward_list> 6 #include <unordered_map> 7 #include <unordered_set> 19 json j_object_t(object_value); 22 std::map<std::string, int> c_map 24 { "one", 1}, { "two", 2}, { "three", 3} 29 std::unordered_map<const char*, double> c_umap 31 { "one", 1.2}, { "two", 2.3}, { "three", 3.4} 36 std::multimap<std::string, bool> c_mmap 38 { "one", true}, { "two", true}, { "three", false}, { "three", true} 43 std::unordered_multimap<std::string, bool> c_ummap 45 { "one", true}, { "two", true}, { "three", false}, { "three", true} 47 json j_ummap(c_ummap); 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"; 63 json j_array_t(array_value); 66 std::vector<int> c_vector {1, 2, 3, 4}; 70 std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6}; 71 json j_deque(c_deque); 74 std::list<bool> c_list { true, true, false, true}; 78 std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; 79 json j_flist(c_flist); 82 std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; 83 json j_array(c_array); 86 std::set<std::string> c_set { "one", "two", "three", "four", "one"}; 90 std::unordered_set<std::string> c_uset { "one", "two", "three", "four", "one"}; 94 std::multiset<std::string> c_mset { "one", "two", "one", "four"}; 98 std::unordered_multiset<std::string> c_umset { "one", "two", "one", "four"}; 99 json j_umset(c_umset); 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"; 119 json::string_t string_value = "The quick brown fox jumps over the lazy dog."; 120 json j_string_t(string_value); 123 json j_string_literal( "The quick brown fox jumps over the lazy dog."); 126 std::string s_stdstring = "The quick brown fox jumps over the lazy dog."; 127 json j_stdstring(s_stdstring); 130 std::cout << j_string_t << '\n'; 131 std::cout << j_string_literal << '\n'; 132 std::cout << j_stdstring << "\n\n"; 141 json j_integer_t(value_integer_t); 145 json j_unsigned_t(value_unsigned_t); 148 enum { enum_value = 17 }; 149 json j_enum(enum_value); 155 int_least32_t n_int_least32_t = -17; 156 uint8_t n_uint8_t = 8; 159 json j_short(n_short); 162 json j_int_least32_t(n_int_least32_t); 163 json j_uint8_t(n_uint8_t); 171 float n_float = 42.23; 172 float n_float_nan = 1.0f / 0.0f; 173 double n_double = 23.42; 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); 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"; 206 json j_falsity = false; 209 std::cout << j_truth << '\n'; 210 std::cout << j_falsity << '\n'; NumberFloatType number_float_t a type for a number (floating-point)
basic_json<> json default JSON class
NumberIntegerType number_integer_t a type for a number (integer)
ObjectType< StringType, basic_json, std::less< StringType >, AllocatorType< std::pair< const StringType, basic_json >>> object_t a type for an object
ArrayType< basic_json, AllocatorType< basic_json >> array_t a type for an array
StringType string_t a type for a string
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 withg++ -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.
|