I have a bunch of domain objects that I serialize, send to other applications, and then deserialize with Json.Net
. These objects may have properties.
- Defined as an abstract base class with multiple derived classes.
- Dynamic properties
I used TypeNameHandling.Auto
one that adds a property $type
to types that are different from the declared type. However, this parameter has an undesirable side effect on my dynamic properties, namely that their types are also declared.
In the example below model
, this is a dynamic property defined as public dynamic Model { get; set; }
in my C # code.
"model":{"$type":"<>f__AnonymousType0`3[[System.String, mscorlib],[System.String, mscorlib],[System.String, mscorlib]], ExampleAssembly","link":"http://www.google.com","name":"John"}
When trying to deserialize this line in another assembly, Json.Net cannot (of course) find it ExampleAssembly
. Using a property TypeNameHandling.None
gives the following serialization of properties
"model": {"link":"http://www.google.com","name":"John"}
What you can successfully deserialize to dynamic
. However, this disrupts the deserialization of derived types.
Any ideas on how to get this to work without implementing custom IContractResolver
and possibly other custom code?
I donβt own domain objects, so I canβt decorate them or their properties with attributes or let them implement interfaces, etc. What I'm looking for is some kind of setup in a serializer that doesn't use types for dynamics
.
IMHO, this must be configured through the settings somehow, I just did not find it.