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
When decoding a json string, jsonpickle does not re-initialize the class instance. #442
Comments
I agree that such an option should exist. Hopefully I can get around to implementing it over the summer (I'm finishing up my final exams over the next few days). That being said, it might be very difficult, and there are a lot of bugs that are a higher priority at the moment. If you'd like it sooner, I'd be happy to review and merge a PR, but I can't promise that I'll be able to do the work myself over the next few months (since I'm taking summer classes and have a full-time research job). |
jsonpickle does not currently have an explicit method to ensure that an object constructor must be run. Add a test that demonstrates how to use __new__ alongside the existing default_factory functionality to create an object that will always run its constructor during jsonpickle object initialization. Classes must opt-in to this behavior by providing both self.default_factory and an implementation of __new__ to force jsonpickle into using the user-provided constructor. Related-to: jsonpickle#442 Signed-off-by: David Aguilar <davvid@gmail.com>
Perhaps slightly magical, but I added a test case in #445 that demonstrates how you can accomplish this using existing jsonpickle features. The basic gist of it is that you can provide a boilerplate This does mean that your constructor must function without any arguments, but since you implement After construction jsonpickle will fill in any data that was present in the json payload, so if you have json with old attributes those will continue to be present in new instances. Hopefully this approach works for your use case. |
jsonpickle does not currently have an explicit method to ensure that an object constructor must be run. Add a test that demonstrates how to use __new__ alongside the existing default_factory functionality to create an object that will always run its constructor during jsonpickle object initialization. Classes must opt-in to this behavior by providing both self.default_factory and an implementation of __new__ to force jsonpickle into using the user-provided constructor. Related-to: jsonpickle#442 Signed-off-by: David Aguilar <davvid@gmail.com>
Thanks so much davvid! |
Lets say I have this class
When I encode this json with JSON pickle it will save all the attributes and class definition. And after encoding, lets say I change the class to following.
Now after changing the line item, if I decode the existing json with JSON pickle. The newly decoded class instance of LineItem will not have attribute var2. So if I try to access it, it will throw an error.
In my opinion, there should be an option to reinitialize the class instance.
The text was updated successfully, but these errors were encountered: