-
I used to read/write json arrays with my own code like so:
Now I switched to using simply to_json()/from_json() for everything including vector<>'s (indirectly), so now I simply do:
The problem is that I have existing files written with the previous method, where if the input array was empty, it actually resulted in a null value. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
To answer my own question, I can actually fix this by using my own serialization macros, see #2588 how. |
Beta Was this translation helpful? Give feedback.
-
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 std::vector<SomeClass> arr = ...; // array of stuff
nlohmann::json jarr = nlohmann::json::array();
for (const auto& a : arr) jarr.push_back(a.myToJson()); |
Beta Was this translation helpful? Give feedback.
The problem was indeed your old code. Instead of
you should have initialized the
jarr
as an array: