title: How to ignore $type during deserialization
body: <p>I have 2 classes like this</p>
  
  <pre><code>class A: Base
  {
      public string CommonField;
      public int IntField;
  }
  
  class B: Base
  {
      public string CommonField;
      public double DoubleField;
  }
  </code></pre>
  
  <p>I serialize <code>A</code> instance with type name handling</p>
  
  <pre><code>{
    "$type": "MyApp.A, MyApp",
    "CommonField": "SomeValue",
    "IntField": 123,
    "SomeBaseField": 321
  }
  </code></pre>
  
  <p>What would be the easiest way to get <code>B</code> instance from this? Before you ask why, it's about importing <code>A</code> as <code>B</code> (they have base properties and those what names are matching as you see, so this operation is reasonable).</p>
  
  <p>Trying to deserialize given json as <code>B</code> will throw</p>
  
  <pre><code>Newtonsoft.Json.JsonSerializationException: Type specified in JSON 'MyApp.A, MyApp, Version=0.0.1.1, Culture=neutral, PublicKeyToken=null' is not compatible with 'MyApp.B, MyApp, Version=0.0.1.1, Culture=neutral, PublicKeyToken=null'. Path '$type', line 2, position 42.
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolveTypeName(JsonReader reader, Type&amp; objectType, JsonContract&amp; contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, String qualifiedTypeName)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadMetadataProperties(JsonReader reader, Type&amp; objectType, JsonContract&amp; contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object&amp; newValue, String&amp; id)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
  </code></pre>
  
  <p>I could think of following:</p>
  
  <ol>
  <li>Deserialize json as <code>A</code>, create instance of <code>B</code> and copy all values <strong>(bad)</strong>;</li>
  <li>Substitute (maybe even remove?) <code>$type</code> in json and try to deserialize it as <code>B</code> <strong>(better, still bad)</strong>;</li>
  <li>Use <code>SerializationBinder</code> and deserialize json as <code>B</code> <strong>(good)</strong>:</li>
  </ol>
  
  <p><br></p>
  
  <pre><code>public class JsonABBinder : DefaultSerializationBinder
  {
      public override Type BindToType(string assemblyName, string typeName)
      {
          if (typeName == "MyApp.A")
               return typeof(B);
          return base.BindToType(assemblyName, typeName);
      }
  }
  </code></pre>
  
  <p>Is there better way? In my scenario there are many such types and having hundereds of such binders (as well as reading json first as <code>A</code> to determine which binder to apply) is very tedious.</p>
  
  <p>I am looking for a simple way to ignore <code>$type</code> and specify wanted type directly (generic method), because I know type I need, but I don't know which type is in json and I want to ignore it.</p>