Reputation: 619
I have two applications which are communicating via rest API, serialized via Json.NET
The part that i am having difficulties with is passing an inheritance structure between the two applications:
ChildClass1 : BaseClass
ChildClass2 : BaseClass
I need to be able to deserialize a list of BaseClasses that have each individually been deserialized into their correct relative type:
JsonConvert.DeserializeObject<IEnumerable<BaseClass>>(json)
I am aware of the TypeNameHandling.All Serializer setting, but the problem i have is that the class on each application are in different namespaces:
Application 1:
App1.NameSpace.ChildClass1
Application 2:
App2.DifferentNameSpace.ChildClass1
Due to this it is not automatically converting them. It is not possible to change the namespaces or use a contract library for these classes.
Do i need to write a custom JsonReader? or is there a setting i can use to override the class name when being serialized / deserialized so that i can remove the namespace?
Upvotes: 1
Views: 1993
Reputation: 17405
You can use an ISerializationBinder
you set on the settings that allow you to specify the name to use in JSON during serialization and what type to use on deserialization.
That way you have full control over the names to use.
An example can be found here.
Upvotes: 2