Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to dump a two-dimensional array to "[[null],[1,2,3]]"? #1146

Closed
GingerMoon opened this issue Jun 26, 2018 · 2 comments
Closed
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@GingerMoon
Copy link

Is it possible to dump a two-dimensional array to "[[null],[1,2,3]]"?

I tried the code below:
my_json j;
std::vector a[2];
std::vector a1;
a1.push_back(1);
a[1] = a1;
j["a"] = a;

The output is:
{"a":[[],[1]]}
Is it possible to dump a two-dimensional array to "[[null],[1,2,3]]"?

@nlohmann
Copy link
Owner

You could try:

// direct approach
json j = { {nullptr}, {1,2,3} };

// indirect approach
json j_array, j_array1, j_array2;
j_array1.push_back(nullptr);
j_array2.push_back(1);
j_array2.push_back(2);
j_array2.push_back(3);
j_array.push_back(j_array1);
j_array.push_back(j_array2);

I haven't run the code, but j and j_array should both serialize to [[null],[1,2,3]].

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 26, 2018
@GingerMoon
Copy link
Author

Thanks a lot, the output below is what I am expecting:
[[null],[1,2,3]]
[[null],[1,2,3]]
[null]
[1,2,3]


// direct approach
json j = { { nullptr },{ 1,2,3 } };

// indirect approach
json j_array, j_array1, j_array2;
j_array1.push_back(nullptr);
j_array2.push_back(1);
j_array2.push_back(2);
j_array2.push_back(3);
j_array.push_back(j_array1);
j_array.push_back(j_array2);

std::cout << j.dump() << std::endl;
std::cout << j_array.dump() << std::endl;
std::cout << j_array1.dump() << std::endl;
std::cout << j_array2.dump() << std::endl;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants