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

Serializing a vector of classes? #1136

Closed
DimosthenisK opened this issue Jun 17, 2018 · 22 comments
Closed

Serializing a vector of classes? #1136

DimosthenisK opened this issue Jun 17, 2018 · 22 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@DimosthenisK
Copy link

I have a vector* of class* (vector<Doctor*> doctors) and I need to serialize it. The class Doctor has some other vector of class* that have to be serialized too. The question is, do I need to create to_json and from_json methods for vector*<Doctor*>, and for any other vectors of classes inside Doctor? I have already created the methods for the class doctor and for every other class that needs it, yet I keep getting the "could not find to_json() method in T's namespace".

I am using MSVS 2017. Thank you in advance for your help!

@nlohmann
Copy link
Owner

Are the methods in the right name space? Can you try without any pointers just to make sure this works?

@DimosthenisK
Copy link
Author

It does work when the vector is for the class Doctor and not for a pointer.

@nlohmann
Copy link
Owner

Maybe @theodelrieu has an idea.

@theodelrieu
Copy link
Contributor

Pointer to user-defined types conversion is not supported by the library.

But you can do it, you have to specialize adl_serizalizer on pointer types (I would recommend supporting Doctor* to begin with though).

@nlohmann
Copy link
Owner

Thanks @theodelrieu ! We should put this into the README at some point...

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 17, 2018
@theodelrieu
Copy link
Contributor

In fact it's way simpler, for a single type at least:

namespace my_ns {
  void to_json(json& j, const Doctor* doc) {}
}

Never thought about that before... So if you want to support both Doctor* and Doctor&, you have to write two to_json overloads.

@DimosthenisK
Copy link
Author

But I have more vector to class pointers inside the Doctor class that also need serialization. I have to create a to_json and from_json overload for each of them?

Thank you for the great help btw!

@theodelrieu
Copy link
Contributor

I have to create a to_json and from_json overload for each of them?

Absolutely, that's a bit tedious though. I don't know what is your exact use-case for having vectors of pointers, but I would advise not doing that if you can avoid it.

Unless you're using base classes and inheritance, storing Doctor instead of Doctor* gives you back a real copy (value semantics, instead of reference semantics).

@DimosthenisK
Copy link
Author

I am using inheritance and I am sadly required to have pointers instead of values. I will try to create the methods for class pointers and will report back.

@nlohmann
Copy link
Owner

@VoidBlaze Any news on this?

@DimosthenisK
Copy link
Author

Not yet, I will have more soon.

@DimosthenisK
Copy link
Author

DimosthenisK commented Jun 27, 2018

Sadly same error. I created the to_json methods for every class* and I still get that the to_json method doesnt exist.

It would be very nice if there was a way for me to check where the program hangs instead of the not-so-useful "T's namespace" that I get.

@nlohmann
Copy link
Owner

Unfortunately, the current error is already an improvement, because it is very difficult to indicate errors there. Can you share some code so we can better understand what's going wrong?

@DimosthenisK
Copy link
Author

DimosthenisK commented Jun 28, 2018

For sure. I uploaded the project here https://github.com/voidblaze/CPP-Appointment-Scheduler.

All the methods lie inside the DataHandler class.

@theodelrieu
Copy link
Contributor

I could compile the project with latest GCC on Linux (with -fpermissive though, there are some errors unrelated to json).

@DimosthenisK
Copy link
Author

DimosthenisK commented Jun 28, 2018

Yeah sorry, I forgot to uncomment the line that was causing the error. Its on data handler, line 191. I believe the errors have something to do with the file opening on line 188, on a path that obviously doesn't exist on your machine.

I pushed the "fixed" project.

@theodelrieu
Copy link
Contributor

You're trying to serialize a std::vector<...>**, I would advise passing such argument by reference in the first place.

Changing the problematic line to json jDoctors = *doctors makes the project compile.

Personally, I would keep pointers for virtual classes, and use references everywhere else.

@DimosthenisK
Copy link
Author

Ah im dumb! Thanks a lot. Was that the problem from the start or were the class* json methods required?

@sentiment-bot

This comment has been minimized.

@theodelrieu
Copy link
Contributor

It was only a part of the issue, you had to write the methods as well.

@DimosthenisK
Copy link
Author

Okay then. Be sure to add that to the documentation if possible, someone may need it someday. Thanks again for your great help!

@nlohmann
Copy link
Owner

Thanks @theodelrieu for helping out!

@VoidBlaze What would you add to the documentation?

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

3 participants