I noticed a problem with serializing a list of objects that derive from a base class as a list of the base class where the data of the derived type properties was missing after it was deserialized. It happened to me a bit ago and I resolved the problem then by some refactoring that fixed it incidentally by avoiding doing it at all in the newer design of that part of the app. Today I ran into wanting to do this again and learned a correct way to. How to serialize properties of derived classes with System.Text.Json shows how to do it (it is simple but I didn’t find this documentation right away). This Stack Overflow answer was what pointed me there too.
In my case, I have a base class Card
with two derived types BasicNote
and ClozeNote
.
This is what a working use of the JsonDerivedType
attribute was for me. Unrelated to the derived classes serialization, the JsonIgnore
attribute on the Deck
property prevents that from being serialized, which would result in an object cycle as the Deck
type has a collection property with its cards.