|
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 = int64_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::iterator >::value andnot std::is_same< CompatibleArrayType, typename __basic_json::const_iterator >::value andnot std::is_same< CompatibleArrayType, typename __basic_json::reverse_iterator >::value andnot std::is_same< CompatibleArrayType, typename __basic_json::const_reverse_iterator >::value andnot std::is_same< CompatibleArrayType, typename array_t::iterator >::value andnot std::is_same< CompatibleArrayType, typename array_t::const_iterator >::value andstd::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 that can be used to construct values of type array_t. Examples include the types std::vector , std::list , and std::set .
- Template Parameters
-
CompatibleArrayType | an object type whose value_type is compatible to array_t |
- Parameters
-
[in] | value | a value for the array |
- Complexity
- Linear in the size of the passed value.
- 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';
a class to store JSON values Definition: json.hpp:130
namespace for Niels Lohmann Definition: json.hpp:55
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&)
|