Skip to content

Reading null into std::vector<> (empty as array) #2587

Answered by nlohmann
ignus2 asked this question in Q&A
Discussion options

You must be logged in to vote

The problem was indeed your old code. Instead of

std::vector<SomeClass> arr = ...; // array of stuff
nlohmann::json jarr;
for (const auto& a : arr) jarr.push_back(a.myToJson());

you should have initialized the jarr as an array:

std::vector<SomeClass> arr = ...; // array of stuff
nlohmann::json jarr = nlohmann::json::array();
for (const auto& a : arr) jarr.push_back(a.myToJson());

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@ignus2
Comment options

@nlohmann
Comment options

@ignus2
Comment options

@nlohmann
Comment options

@ignus2
Comment options

Answer selected by nlohmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants