I have a complex object graph that I am serializing/deserializing with Json.NET. Some of the objects derive from an abstract class, so in order for the deserialization to work properly, I needed to create a custom JsonConverter
. Its only role is to select the appropriate concrete implementation of the abstract class at deserialization-time and allow Json.NET to continue on its way.
My problem comes when I want to serialize. I don't need to do anything custom at all. I want to get exactly the same behavior as I would get using JsonConvert.SerializeObject
with no custom JsonConverter
.
However, since I'm using the custom JsonConverter class for my deserialization needs, I'm forced to supply a WriteJson
implementation. Since WriteJson is abstract, I can't just call base.WriteJson
, but I want to do essentially that. So my question is, what do I put in that method to get the plain-Jane, default behavior? In other words:
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// What goes here to get default processing?
}
TypeNameHandling
toAuto
in your serializer settings? This will add a "$type" property if an instance type is different than its declaring property type (which will be an interface or base class type). Isn't this exactly what you want? – Catafalque