Raaj
Raaj

Reputation: 57

conversion from class to json using nlohmann in c++

I have been trying to convert my class to json in visual studio 2017 but there are linker errors C2440. "

Severity    Code    Description Project File    Line    Suppression State
Error   C2440   'initializing': cannot convert from 'ns::person' to 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>'   jason reader writer c:\users\lengdon\source\repos\jason reader writer\jason reader writer\objecttojason.h   15  

My code is: Class 1:

#include<iostream>
#include<string>
#include"nlohmann/json.hpp"
#include"nlohmann/adl_serializer.hpp"
using namespace std;
using json = nlohmann::json;
namespace ns {
    // a simple struct to model a person
    class person {
    public:
        string name;
        string address;
        int age;
        void to_json(json& j, const person& p) {
            j = json{ {"name", p.name}, {"address", p.address}, {"age", p.age} };
        }
        void from_json(const json& j, person& p) {
            j.at("name").get_to(p.name);
            j.at("address").get_to(p.address);
            j.at("age").get_to(p.age);
        }
    };    
}

class 2:

#include"Student.h"
#include"nlohmann/json.hpp"
#include"nlohmann/adl_serializer.hpp"
using json = nlohmann::json;
using namespace std;
class ObjectToJason
{
public:
    json convert()
    {
        ns::person p{ "Ned Flanders", "744 Evergreen Terrace", 60 };
        json j=p;

    }
};

In my code for class 2, json j= has errors "no suitable user defined conversions fron ns::person to json exist". I am new to c++ and json so please try to explain in plain English. Thank you in advance

Upvotes: 1

Views: 7180

Answers (2)

ritesh sangani
ritesh sangani

Reputation: 340

I would like to quote from the documentation

https://github.com/nlohmann/json#arbitrary-types-conversions

Those methods MUST be in your type's namespace (which can be the global namespace), or the library will not be able to locate them (in this example, they are in namespace ns, where person is defined).

Which means that the two functions

void to_json(json& j, const person& p)
void from_json(const json& j, person& p)

should be inside the same namespace in which the custom object exists. Which in your case is class person.

So, your code for Class 1 will be:

#include<iostream>
#include<string>
#include"nlohmann/json.hpp"
#include"nlohmann/adl_serializer.hpp"
using namespace std;
using json = nlohmann::json;
namespace ns {
    void to_json(json& j, const person& p) {
        j = json{ {"name", p.name}, {"address", p.address}, {"age", p.age} };
    }
    void from_json(const json& j, person& p) {
        j.at("name").get_to(p.name);
        j.at("address").get_to(p.address);
        j.at("age").get_to(p.age);
    }

    // a simple struct to model a person
    class person {
    public:
        string name;
        string address;
        int age;

    };    
}

And your code for Class 2 will be:

#include"Student.h"
#include"nlohmann/json.hpp"
#include"nlohmann/adl_serializer.hpp"
using json = nlohmann::json;
using namespace std;
namespace ns {
    class ObjectToJason
    {
        public:
        json convert()
        {
            ns::person p{ "Ned Flanders", "744 Evergreen Terrace", 60 };
            json j=p;
        }
    };
}

I hope this helps.

Upvotes: 3

vinod sahu
vinod sahu

Reputation: 67

Check the requirements here https://github.com/nlohmann/json#arbitrary-types-conversions. Move to_json and from_json out of the class.

Upvotes: 1

Related Questions