#include "json.hpp"
#include <memory>
#include <vector>
#include <iostream>
struct json_node;
using json_node_ptr = std::shared_ptr<json_node>;
struct json_node
{
int id;
std::vector<json_node_ptr> children;
json_node(int _id)
: id{ _id }
{
}
};
void to_json(nlohmann::json& j, const json_node_ptr& node)
{
j = {{"ID", node->id}};
if (!node->children.empty()) {
j.push_back( nlohmann::json {"children", node->children});
//j["children"] = node->children;
}
}
int main()
{
}
I am getting the following error. How do I resolve this? What is the problem behind it?
Any easy workaround? It is not easy to change client library.
error: no matching function for call to ‘basic_json<>::push_back(<brace-enclosed initializer list>)’
j.push_back( {"children", node->children} );
Header file is here: https://github.com/nlohmann/json/blob/v2.0.10/src/json.hpp
UPDATE: THIS PROBLEM OCCURS WITH OLDER VERSIONS OF THE LIBRARY. IT IS FIXED IN LATEST VERSION.
Here the code that converts the struct to string and then parses string back to json object
Output
Here is another vesion of the to_json - that does not use to string serialization