When I set Json.NET to serialize with TypeNameHandling set to TypeNameHandling.Auto, it correctly sets $type for child properties of an object but does not do so for the roo
Brian is absolutely correct, Json.NET has no way of knowing the compile-time declared type of the object it's being passed as the value
parameter is declared as an object
. The easy fix for this was if Json.NET added generic serialize methods so that the compile-time declared type would automatically flow over to Json.NET but the library's author has decided against my proposal for this here.
As an alternative, I've wrapped all my json (de)serialization needs in a JsonHelper
class with generic serialize methods which use the typeof
expression to automatically pass the compile-time declared type of the value to be serialized.