-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Comments
Are the methods in the right name space? Can you try without any pointers just to make sure this works? |
It does work when the vector is for the class Doctor and not for a pointer. |
Maybe @theodelrieu has an idea. |
Pointer to user-defined types conversion is not supported by the library. But you can do it, you have to specialize |
Thanks @theodelrieu ! We should put this into the README at some point... |
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 |
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! |
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 |
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. |
@VoidBlaze Any news on this? |
Not yet, I will have more soon. |
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. |
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? |
For sure. I uploaded the project here https://github.com/voidblaze/CPP-Appointment-Scheduler. All the methods lie inside the DataHandler class. |
I could compile the project with latest GCC on Linux (with |
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. |
You're trying to serialize a Changing the problematic line to Personally, I would keep pointers for virtual classes, and use references everywhere else. |
Ah im dumb! Thanks a lot. Was that the problem from the start or were the class* json methods required? |
This comment has been minimized.
This comment has been minimized.
It was only a part of the issue, you had to write the methods as well. |
Okay then. Be sure to add that to the documentation if possible, someone may need it someday. Thanks again for your great help! |
Thanks @theodelrieu for helping out! @VoidBlaze What would you add to the documentation? |
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!
The text was updated successfully, but these errors were encountered: