1

Duplicate of Using a custom type discriminator to tell JSON.net which type of a class hierarchy to deserialize

and JsonConverter how to deserialize to generic object

Thx https://stackoverflow.com/users/3744182/dbc

Consumer application knows Class A, I will serve Class B that inherited from Class A.Class B is defined in server application.

public class A {} // Both Application know it
public class B:A{} // It is defined in Server Application
public A GetAInstanceById(){ // It is written in Server Application, but both application know its method signature, consumer application calls it by simple Web Call
   ..............
   return new B();
}

B response is serialized by JsonConvert.SerializeObject method, with this settings.

settings.TypeNameHandling = TypeNameHandling.All;

When consumer application get the response from server application, it tries to deserialize response to A (because of method signature)

Json.Net could not find B class and convert instance as "object" and throw "object Must Implement IConvertible" exception.

So I want to serialize response as $type:"A" . How can I change $type:"B" to $type:"A"

Community
  • 1
  • 1
Oguz Karadenizli
  • 3,449
  • 6
  • 38
  • 73
  • A more generic approach would be *not sharing any assembly between client&server* .... Server respondes with a json (without those `$type`s) and client should decide how to handle that json. This is the common case when you use Google/Twitter/Facebook/Instagram etc. APIs – L.B Nov 18 '16 at 21:03
  • I agree with you but Enterprise Service Bus of my company works typed always and I could not change it :(, so I have to change $type , Is there any way to change it? – Oguz Karadenizli Nov 18 '16 at 21:10
  • (at client side)read the json make the changes and then use it... – L.B Nov 18 '16 at 21:14
  • I think I could not explain, when ESB try to deserialize Json data it throws exception, "could not find B Type and convert instance as "object" and throw "object Must Implement IConvertible" exception." – Oguz Karadenizli Nov 18 '16 at 21:50
  • 1
    Use a custom `SerializationBinder` as explained in [Using a custom type discriminator to tell JSON.net which type of a class hierarchy to deserialize](https://stackoverflow.com/questions/11099466/using-a-custom-type-discriminator-to-tell-json-net-which-type-of-a-class-hierarc/12203624#12203624). See also [JsonConverter how to deserialize to generic object](https://stackoverflow.com/questions/35325664/jsonconverter-how-to-deserialize-to-generic-object/35349393#35349393). – dbc Nov 18 '16 at 22:02
  • Sanitizing your types with a custom `SerializationBinder` when deserializing is recommended practice, by the way. From the [docs](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_TypeNameHandling.htm): *TypeNameHandling should be used with caution when your application deserializes JSON from an external source. Incoming types should be validated with a custom SerializationBinder when deserializing with a value other than TypeNameHandling.None.* – dbc Nov 18 '16 at 22:07
  • 1
    When I saw dbc comment I recognize that my answer is duplicated, Thx dbc. It seems I could not find true keywords when I was searching answers. I'm closing my question with duplicate info. – Oguz Karadenizli Nov 18 '16 at 22:26

0 Answers0