One of the things you can do with Json.NET is:
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
JsonConvert.SerializeObject(entity, Formatting.Indented, settings);
The TypeNameHandling
flag will add a $type
property to the JSON, which allows Json.NET to know which concrete type it needs to deserialize the object into. This allows you to deserialize an object while still fulfilling an interface or abstract base class.
The downside, however, is that this is very Json.NET-specific. The $type
will be a fully-qualified type, so if you’re serializing it with type info,, the deserializer needs to be able to understand it as well.
Documentation: Serialization Settings with Json.NET