-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Comments
Did you have a look at the section https://github.com/nlohmann/json#arbitrary-types-conversions ? It describes how arbitrary types can be serialized. |
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. |
How does your |
|
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 |
i have tried doing that but its not working. and in place of -> struct second |
You need to follow the README for this. The functions need to be called exactly |
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 // 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; |
@quicksilver345 Do you need further assistance? |
Hi
No no it solved my issue , thanks for the solution
Regards
…On Thu, Jul 5, 2018, 10:40 AM Niels Lohmann ***@***.***> 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
<#1145 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/Aj4P40GfYxq3_xB8qHWQH6AIFvd37Ps5ks5uDZ_DgaJpZM4U1iWe>
.
|
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 ->
json file output :-
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 :-
to be the json output
)
Can you please help me regarding the issue
(Compiling on a windows machine in visual studio 2017)
The text was updated successfully, but these errors were encountered: