Reputation: 405
I know that if I have some integer 'a' and 'b' that I can append them to a Value in Json:
Value root;
Value v = root["int"];
int a = 1;
int b = 2;
v.append(a);
v.append(b);
Resulting Json file:
"int":
[
1,
2
]
But is there also a way to append entire arrays? Perhaps something like this:
Value root;
Value v = root["arrays"];
int a[3] = {1,2,3};
int b[3] = {4,5,6};
v.append(a);
v.append(b);
I'm trying to have my resulting Json file look like:
"arrays":
[
[1, 2, 3],
[4, 5, 6]
]
But instead, Json only appends the pointer address value, which reads as "true":
"arrays":
[
true,
true
]
Upvotes: 0
Views: 4118
Reputation: 62817
The explicit way is to use range-for with a temp Value
.
Value root;
Value v = root["arrays"];
int a[3] = {1,2,3};
int b[3] = {4,5,6};
Value tmp;
for(auto i : a) { tmp.append(i); }
v.append(tmp);
tmp = Value{}; // reset
for(auto i : b) { tmp.append(i); }
v.append(tmp)
A possible helper template function to wrap it nicely:
template<typename RANGE>
Value rangeToValue(RANGE src) {
Value result;
for (const auto &value : src) {
result.append(value);
}
return result;
}
Then this should work:
v.append(rangeToValue(a));
v.append(rangeToValue(b));
Upvotes: 1