jsons
jsons copied to clipboard
dump(s) with a cls does not serialize as that cls
Dumping an object with a different cls reference fails.
from dataclasses import dataclass
import jsons
@dataclass
class Parent:
x: int
@dataclass
class Child(Parent):
y: int
c = Child(1, 2)
jsons.dump(c, Child)
Results in:
{'x': 1, 'y': 2}
Expected:
{'y': 2}
Same goes with regular Python classes. See also #109
To me, it seems more intuitive that {'x': 1, 'y': 2}
should be the result, as simply specifying the Child
class as a parameter does not intuitively convey that attributes will be discarded. I'd suggest a flag should be added that allows the exclusion of parent attributes like a 'smart' version of strip_attr
, or add support for types in the strip_attr
flag so that supplying strip_attr=Parent
ignores all attributes defined in the Parent
class.
I like the idea of allowing strip_attr
to take a class. 👍