jsons icon indicating copy to clipboard operation
jsons copied to clipboard

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

Open itaiperi opened this issue 3 years ago • 1 comments

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 avatar Jul 03 '22 22:07 itaiperi

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.

itaiperi avatar Jul 04 '22 11:07 itaiperi