C++ no matching function for call when using push_back in older json::nlohmann library

1.7k views Asked by At
#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.

1

There are 1 answers

6
Artemy Vysotsky On BEST ANSWER

Here the code that converts the struct to string and then parses string back to json object

#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <string>
#include "json.hpp"

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 }
    {
    }
    std::ostringstream& print(std::ostringstream& ss) const
    {
        ss << "{\"ID\" :" << id ;
        if (children.size() > 0) {
            ss << ", \"children\" : [";
            std::string prefix = "";
            for (auto& ch : children) {
                ss << prefix;
                ch->print(ss);
                prefix = ", ";
            }
            ss << "]";
        }
        ss << "}";
        return ss;
    };

};

std::ostringstream& operator<<(std::ostringstream& ss,const json_node & node)
{
    return node.print(ss);
}
void to_json(nlohmann::json& j, const json_node_ptr& node)
{
    std::ostringstream ss;
    ss << *node;
    std::string str = ss.str();
    j = nlohmann::json::parse(str);
}
int main()
{
    json_node_ptr p = std::make_shared<json_node>(1);
    json_node_ptr child_1 = std::make_shared<json_node>(2);
    p->children.push_back(child_1);
    json_node_ptr child_2 = std::make_shared<json_node>(3);
    json_node_ptr child_3 = std::make_shared<json_node>(4);
    child_2->children.push_back(child_3);
    p->children.push_back(child_2);

    nlohmann::json j;
    to_json(j, p);

    std::cout << j << '\n';


    nlohmann::json j1 = nlohmann::json::array();
    j1.push_back(j);
    j1.push_back(p);
    std::cout << j1 << "\n";

}

Output

{"ID":1,"children":[{"ID":2},{"ID":3,"children":[{"ID":4}]}]}

Here is another vesion of the to_json - that does not use to string serialization

void to_json(nlohmann::json& j, const json_node_ptr& node)
{
    j["ID"] = node->id;
    if (node->children.size() > 0)
    {
        j["children"] = nlohmann::json::array();
        for (auto& ch : node->children)
        {
            nlohmann::json j_child;
            to_json(j_child, ch);
            j["children"].push_back(j_child);
        }
    }
}