There is some data that I receive from a service in json format. A part of the data is an object of class BASE (say). There are many classes that derive from it:
Now the server sends a fixed structure json, and an object of type BASE lies in it. Some sample responses I am listing down here:
"{"Status":"Success","B":{"$type":"B1","id":"123"},"C":null}"
"{"Status":"Success","A":{"$type":"A3","name":"Jon"},"B":{"$type":"B2","id":"A34J"}}"
I have the same class hierarchy defined in my client-side Models and I am using NewtonSoft Json Library to deserialize the content. As you can see, the server is sending the $type information of the every object that's derived from BASE class, I want to be able to load those objects during client-side deserialization into their respective type. I deserliaze the json into an object of type Response
class Response
{
string Status;
BASE object1;
BASE object2;
}
Response resp = JsonConvert.DeserializeObject<Response>( jsonServiceMsg, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, Binder = mycustombinder });
As you can see, I am not specifying the exact type of object B1 or A3 or B2, in the Response object, I want the Newtonsoft lib to init the object1 and object2, with appropriate type based on $type value in the json message.
Note that the Service has these classes (Base and all derivetives) defined in a different namespace and I (client) in different namespace. But for simplicity the service is not binding the namespace by using a custom binder while serializing into a json response.
I know I can achieve this by defining a custom binder (class derived from SerializationBinder) and overriding the BindToName and BindToType properties (refer link)
But the issue is that we do not want to use Json.Net SerializationBinder from System.Runtime.Serialization. Our module can only use Newtonsoft lib. Any solutions to this?