|
§ basic_json() [6/23]
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<class CompatibleArrayType , typename std::enable_if< not std::is_same< CompatibleArrayType, typename basic_json_t::iterator >::value and not std::is_same< CompatibleArrayType, typename basic_json_t::const_iterator >::value and not std::is_same< CompatibleArrayType, typename basic_json_t::reverse_iterator >::value and not std::is_same< CompatibleArrayType, typename basic_json_t::const_reverse_iterator >::value and not std::is_same< CompatibleArrayType, typename array_t::iterator >::value and not std::is_same< CompatibleArrayType, typename array_t::const_iterator >::value and std::is_constructible< basic_json, typename CompatibleArrayType::value_type >::value, int >::type = 0>
Create an array JSON value with a given content. This constructor allows any type CompatibleArrayType that can be used to construct values of type array_t.
- Template Parameters
-
CompatibleArrayType | An object type whose value_type is compatible to array_t. Examples include 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. |
- Parameters
-
[in] | val | a value for the array |
- Complexity
- Linear in the size of the passed val.
- Exceptions
-
std::bad_alloc | if allocation for array value fails |
- Example
- The following code shows the constructor with several compatible array type parameters.
4 #include <forward_list> 6 #include <unordered_set> 13 std::vector<int> c_vector {1, 2, 3, 4}; 17 std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6}; 18 json j_deque(c_deque); 21 std::list<bool> c_list { true, true, false, true}; 25 std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543}; 26 json j_flist(c_flist); 29 std::array<unsigned long, 4> c_array {{1, 2, 3, 4}}; 30 json j_array(c_array); 33 std::set<std::string> c_set { "one", "two", "three", "four", "one"}; 37 std::unordered_set<std::string> c_uset { "one", "two", "three", "four", "one"}; 41 std::multiset<std::string> c_mset { "one", "two", "one", "four"}; 45 std::unordered_multiset<std::string> c_umset { "one", "two", "one", "four"}; 46 json j_umset(c_umset); 49 std::cout << j_vec << '\n'; 50 std::cout << j_deque << '\n'; 51 std::cout << j_list << '\n'; 52 std::cout << j_flist << '\n'; 53 std::cout << j_array << '\n'; 54 std::cout << j_set << '\n'; 55 std::cout << j_uset << '\n'; 56 std::cout << j_mset << '\n'; 57 std::cout << j_umset << '\n'; basic_json<> json default JSON class
Output (play with this example online): [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 example code above can be translated withg++ -std=c++11 -Isrc doc/examples/basic_json__CompatibleArrayType.cpp -o basic_json__CompatibleArrayType
- See also
- basic_json(const array_t&) – create an array value
- Since
- version 1.0.0
Definition at line 1219 of file json.hpp.
|