JSON for Modern C++  2.0.3
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 andnot std::is_same< CompatibleArrayType, typename basic_json_t::const_iterator >::value andnot std::is_same< CompatibleArrayType, typename basic_json_t::reverse_iterator >::value andnot std::is_same< CompatibleArrayType, typename basic_json_t::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>
nlohmann::basic_json::basic_json ( const CompatibleArrayType &  val)
inline

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
CompatibleArrayTypeAn 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]vala value for the array
Complexity
Linear in the size of the passed val.
Exceptions
std::bad_allocif allocation for array value fails
Example
The following code shows the constructor with several compatible array type parameters.
1 #include <json.hpp>
2 #include <deque>
3 #include <list>
4 #include <forward_list>
5 #include <set>
6 #include <unordered_set>
7 
8 using json = nlohmann::json;
9 
10 int main()
11 {
12  // create an array from std::vector
13  std::vector<int> c_vector {1, 2, 3, 4};
14  json j_vec(c_vector);
15 
16  // create an array from std::deque
17  std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
18  json j_deque(c_deque);
19 
20  // create an array from std::list
21  std::list<bool> c_list {true, true, false, true};
22  json j_list(c_list);
23 
24  // create an array from std::forward_list
25  std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
26  json j_flist(c_flist);
27 
28  // create an array from std::array
29  std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
30  json j_array(c_array);
31 
32  // create an array from std::set
33  std::set<std::string> c_set {"one", "two", "three", "four", "one"};
34  json j_set(c_set); // only one entry for "one" is used
35 
36  // create an array from std::unordered_set
37  std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
38  json j_uset(c_uset); // only one entry for "one" is used
39 
40  // create an array from std::multiset
41  std::multiset<std::string> c_mset {"one", "two", "one", "four"};
42  json j_mset(c_mset); // only one entry for "one" is used
43 
44  // create an array from std::unordered_multiset
45  std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
46  json j_umset(c_umset); // both entries for "one" are used
47 
48  // serialize the JSON arrays
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';
58 }
basic_json<> json
default JSON class
Definition: json.hpp:10122
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 with
g++ -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 1215 of file json.hpp.