Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
quicksilver345 opened this issue Jun 25, 2018 · 10 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@quicksilver345
Copy link

quicksilver345 commented Jun 25, 2018

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

How does your to_json function look like?

@quicksilver345
Copy link
Author

json toJson(const Class& obj) const override
{
    return json(obj.*ptr);
}

@nlohmann
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

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 nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 28, 2018
@nlohmann
Copy link
Owner

nlohmann commented Jul 5, 2018

@quicksilver345 Do you need further assistance?

@quicksilver345
Copy link
Author

quicksilver345 commented Jul 5, 2018 via email

@nlohmann nlohmann closed this as completed Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants