|
◆ 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>
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:
- CompatibleType is not derived from
std::istream ,
- CompatibleType is not basic_json (to avoid hijacking copy/move constructors),
- CompatibleType is not a basic_json nested type (e.g., json_pointer, iterator, etc ...)
- @ref json_serializer has a
to_json(basic_json_t&, CompatibleType&&) method
|
U | = uncvref_t<CompatibleType> |
- Parameters
-
[in] | val | the value to be forwarded |
- Complexity\n 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\n The following code shows the constructor with several
- compatible types.
6#include <unordered_map>
7#include <unordered_set>
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}
51 std::cout << j_map << '\n';
52 std::cout << j_umap << '\n';
53 std::cout << j_mmap << '\n';
66 std::vector<int> c_vector {1, 2, 3, 4};
70 std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
74 std::list<bool> c_list { true, true, false, true};
78 std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
82 std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
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"};
103 std::cout << j_vec << '\n';
105 std::cout << j_list << '\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";
126 std::string s_stdstring = "The quick brown fox jumps over the lazy dog.";
186 std::cout << j_enum << '\n';
188 std::cout << j_int << '\n';
189 std::cout << j_long << '\n';
192 std::cout << j_ok << '\n';
193 std::cout << j_nan << '\n';
static basic_json array(std::initializer_list< basic_json > init=std::initializer_list< basic_json >()) explicitly create an array from an initializer list
StringType string_t a type for a string
NumberFloatType number_float_t a type for a number (floating-point)
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
a class to store JSON values
basic_json<> json default JSON class
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.
|