nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
43.08k stars 6.73k forks source link

Want to write a class member variable and a struct variable ( this structure is inside the class) to the json file #1145

Closed quicksilver345 closed 6 years ago

quicksilver345 commented 6 years ago

I have a class and inside that class i have a struct, now i want to write the class variable "name" and the structure member "a" in the json file but i am not able to do so, can you please help me with the problem. :-

Code snipet ->

class ABC
            {
            public:
                struct ex
                {
                public:
                    int a;
                };
                static void registerclass()
                {
                    ClassMetaInfo<ABC>::registerMember("name", &ABC::name);
                    ClassMetaInfo<ex>::registerMember("int a ", &ex::a);
                }
                std::string name;
                ex var = { };

            };

            ABC::registerclass();
            ABC p;
           p.name = "JOHN";
            json j = ClassMetaInfo<ABC>::deserialize(p);

json file output :-

{
    "name": "JOHN"
}

By the above code i am getting the class variable "name" written in the json file but cannot see the structure variable "a" in the json file.

I have tried to put the registerclass() method inside the structure also but still i am not getting the stucture variable "a" written in my json file

(I want to write both the class variable and the structure variable in the json file, as per the above example i am expecting something like :-

{
    "name": "JOHN"
    " int a" : 32 
}

to be the json output )

Can you please help me regarding the issue (Compiling on a windows machine in visual studio 2017)

nlohmann commented 6 years ago

Did you have a look at the section https://github.com/nlohmann/json#arbitrary-types-conversions ? It describes how arbitrary types can be serialized.

quicksilver345 commented 6 years ago

Yes, i tried that but still i cannot see the structure variable in the json format, since my scenario is a structure within a class.

nlohmann commented 6 years ago

How does your to_json function look like?

quicksilver345 commented 6 years ago
json toJson(const Class& obj) const override
{
    return json(obj.*ptr);
}
nlohmann commented 6 years ago

As described in https://github.com/nlohmann/json#arbitrary-types-conversions you should rather define a function with exactly the signature

void to_json(json& j, const Class& obj);

in the namespace where Class is defined.

quicksilver345 commented 6 years ago

i have tried doing that but its not working. Actually i am referring this code -> https://gist.github.com/eliasdaler/213d42051958ae21185adbc7edbff915

and in place of -> struct Person { std::string name; int age; }; in the code as in the link above, i am having this scenario ( nested structure)->

struct second { int secondID; }; struct Person { public: int firstID; second objsec; }; ( I have changed only the structure from the link given above to a nested structure ) Now i want my output to be something like this -> { "firstID": 2 "secondID": 3 }

nlohmann commented 6 years ago

You need to follow the README for this. The functions need to be called exactly from_json and to_json. They must be in the same namespace as your data type. They must not be member functions.

nlohmann commented 6 years ago

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

Assume you have any class like this:

namespace ns {
    // a simple struct to model a person
    struct person {
        std::string name;
        std::string address;
        int age;
    };
} // namespace ns

you need to implement the following functions (Name and signature must be exactly this. ALso note the namespace):

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) {
        p.name = j.at("name").get<std::string>();
        p.address = j.at("address").get<std::string>();
        p.age = j.at("age").get<int>();
    }
} // namespace ns

Then the compiler knows how to translate between ns::person and nlohmann::json, and vice versa:

// create a person
ns::person p {"Ned Flanders", "744 Evergreen Terrace", 60};

// conversion: person -> json
json j = p;

std::cout << j << std::endl;
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}

// conversion: json -> person
ns::person p2 = j;
nlohmann commented 6 years ago

@quicksilver345 Do you need further assistance?

quicksilver345 commented 6 years ago

Hi

No no it solved my issue , thanks for the solution

Regards

On Thu, Jul 5, 2018, 10:40 AM Niels Lohmann notifications@github.com wrote:

@quicksilver345 https://github.com/quicksilver345 Do you need further assistance?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nlohmann/json/issues/1145#issuecomment-402607861, or mute the thread https://github.com/notifications/unsubscribe-auth/Aj4P40GfYxq3_xB8qHWQH6AIFvd37Ps5ks5uDZ_DgaJpZM4U1iWe .