Serialization / deserialization of derived objects using JSON.NET without using JsonProperty?

I use the NewtonSoft JSON.NET library to serialize the next class, where DTOBase can contain derived instances.

public class Command { public DTOBase CommandDTO { get; set; } } 

Per in this article, you need to enable the JsonProperty attribute so that derived instances deserialize correctly

 public class Command { [JsonProperty(TypeNameHandling = TypeNameHandling.All)] public DTOBase CommandDTO { get; set; } } 

The question is, is there any other way than using the attribute to get the same result? I would prefer not to get attached to the NewtonSoft library and json serialization, in particular at the class level. Is there a way to specify some parameters in the Serialize / Deserialize methods of the library in general to get the same result?

+4
source share
1 answer

The TypeNameHandling property can be set to JsonSerializerSettings by calling JsonConvert.SerializeObject(value, settings) .

If you want the name included for derived objects to set TypeNameHandling to TypeNameHandling.Auto .

+4
source

All Articles