1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So this is partially working, by using this approach i am able to remove the assembly name / namespace. The problem however is that it is also messing with the List<BaseClass> initial type at the start of the json. This is all i have at the start of the json before it dives into the list: {"$type":"List`1","$values":
fixed it. Needed to change TypeNameHandling.All to TypeNameHandling.Object; thanks for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.