jsons
jsons copied to clipboard
In nested objects, `load` is only called for the root object instead of being called for each one
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!
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.