I have a vector* of class* (vector
I am using MSVS 2017. Thank you in advance for your help!
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 adl_serizalizer on pointer types (I would recommend supporting Doctor* to begin with though).
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 Doctor* and Doctor&, you have to write two to_json overloads.
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!
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).
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 -fpermissive though, there are some errors unrelated to json).
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 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.
Ah im dumb! Thanks a lot. Was that the problem from the start or were the class* json methods required?
Please be sure to review the code of conduct and be respectful of other users. cc/ @nlohmann
Keep in mind, this repository uses the Contributor Covenant.
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?