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

In nested objects, load is only called for the root object instead of being called for each one #179

Open
itaiperi opened this issue Jul 3, 2022 · 1 comment

Comments

@itaiperi
Copy link

itaiperi commented Jul 3, 2022

Hi,
I have a problem where I'm trying to deserialize nested objects, and have each of their respective load functions trigger.

Code example:

from typing import List
import jsons

class B(jsons.JsonSerializable):
    c: List[str]
    
    @classmethod
    def load(cls, json_obj, **kwargs):
        print("Loading", cls)
        return jsons.load(json_obj, cls, **kwargs)

class A(jsons.JsonSerializable):
    b: B
    
    @classmethod
    def load(cls, json_obj, **kwargs):
        print("Loading", cls)
        return jsons.load(json_obj, cls, **kwargs)

obj = {'b': {'c': ['d', 'd']}}

jsons.load(obj, A) # Nothing printed
A.load(obj) # Only "Loading <class '__main__.A'>" is printed

What I would expect as default behavior in this case is to have both Loading <class ...B> and Loading <class ...A> printed.
Is there any way to achieve this behavior?

Thanks!

@itaiperi
Copy link
Author

itaiperi commented Jul 4, 2022

After I've thought about it a little more, the problem is that jsons.load (and I guess also loads) doesn't check if the cls inherits from the jsons.JsonSerializable class, and if it does it should call its respective load / loads methods. Also for dumping I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant