ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
288 stars 40 forks source link

Serializing a list of objects loses type info #102

Open aecay opened 4 years ago

aecay commented 4 years ago

Here's a test program:

import jsons
from dataclasses import dataclass

@dataclass
class Animal:
    name: str

@dataclass
class Dog(Animal):
    pass

@dataclass
class Person(Animal):
    pet: Animal

dave = Person(name="dave", pet=Dog(name="fido"))

print(jsons.load(jsons.dump([dave], verbose=True)))
print(jsons.load(jsons.dump(dave, verbose=True)))

Here's the output:

[Person(name='dave', pet=Animal(name='fido'))]
Person(name='dave', pet=Dog(name='fido'))

When we round-trip the list through jsons, the info that Dave's pet is a dog (as opposed to a generic Animal) is lost.

ramonhagenaars commented 4 years ago

Thanks @aecay for reporting this and for your pull request. I'll look into it in more detail as soon as I have the time. :-)