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

Serialization of struct object. #1877

Closed
AlexRos101 opened this issue Dec 23, 2019 · 3 comments
Closed

Serialization of struct object. #1877

AlexRos101 opened this issue Dec 23, 2019 · 3 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@AlexRos101
Copy link

Hi, nlohmann!

Thank you for your project.

I don't want to set the key and value manually.
So I have used the code below.

// 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
auto p2 = j.get<ns::person>();

// that's it
assert(p == p2);

But this code has some errors.

no suitable user-defined conversion from "ns::person" to "nlohmann::json" exists	
	
no instance of overloaded function "nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::get [with ObjectType=std::map, ArrayType=std::vector, StringType=std::string, BooleanType=bool, NumberIntegerType=int64_t, NumberUnsignedType=uint64_t, NumberFloatType=double, AllocatorType=std::allocator, JSONSerializer=nlohmann::adl_serializer]" matches the argument list

How can I fix these errors?

I hope your help.
Thanks.

@nlohmann
Copy link
Owner

nlohmann commented Dec 23, 2019 via email

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Dec 23, 2019
@AlexRos101
Copy link
Author

Hi!
Thank you for your reply.

So there is no way to convert directly without to_json/from_json function?

Thanks.

@nlohmann
Copy link
Owner

No, you need to, at some point, tell the library how to read/write JSON for your type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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