title: How to include object type in Json for asmx web service using Gson
body: <p>How do we preserve the object type in json string when sending data to asmx web service in .net 2.0?</p>
  
  <p><em>for example:</em></p>
  
  <pre><code>class A{
   string name;
  }
  
  class B : A {
   string address;
  }
  </code></pre>
  
  <p><em>and the web method:</em></p>
  
  <pre><code>[WebMethod]
  public void PushJson(A obj){
    B b = (B) obj;
  }
  </code></pre>
  
  <p>Now in above example scenario, lets say, i send <code>{"obj":{"name":"waqas","address":"sweden"}}</code> then how can i force my json string to act as type of class B, so that it can be accepted by web method as an object of class A and further parsed back into object of class B? in short, how to preserve polymorphism in json?</p>
  
  <p>I've noticed that the compiler throws me <code>System.InvalidCastException</code> when ever i try to execute such kind of pattern</p>
  
  <p>P.S. I've noticed that .net adds <strong>__type</strong> for complex objects while serializing into json. Is it possible that we can include this key to help .net to automatically parse json string with correct class type?</p>
  
  <p>any help/suggestion would be helpful.</p>
  
  <hr>
  
  <h3>Update:</h3>
  
  <p>If we observe carefully the <code>wsdl</code> of an asmx web-service, so the objects whose classes inherits parent classes are containing  something like <code>&lt;s:extension base="tns:ParentClassName"&gt;</code>. I think this extension part is the thing which I may need to convert into Json. Any idea regarding this?</p>