jsons icon indicating copy to clipboard operation
jsons copied to clipboard

dump(s) with a cls does not serialize as that cls

Open ramonhagenaars opened this issue 4 years ago • 2 comments

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

ramonhagenaars avatar Aug 04 '20 20:08 ramonhagenaars

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.

alexmirrington avatar Aug 13 '20 02:08 alexmirrington

I like the idea of allowing strip_attr to take a class. 👍

ramonhagenaars avatar Aug 26 '20 18:08 ramonhagenaars