title: Using a custom type discriminator to tell JSON.net which type of a class hierarchy to deserialize
body: <p>Suppose I have the following class hierarchy:</p>
  
  <pre><code>public abstract class Organization
  {
      /* properties related to all organizations */
  }
  
  public sealed class Company : Organization
  {
       /* properties related to companies */
  } 
  
  public sealed class NonProfitOrganization : Organization
  {
       /* properties related to non profit organizations */
  }
  </code></pre>
  
  <p>Is it possible to have <a href="http://json.codeplex.com/">json.net</a> use property (say "type" or "discriminator") to determine which type the object when it deserializes the organization? For example, the following should deserialize an instance of Company.</p>
  
  <pre><code>{
     "type": "company"
     /* other properties related to companies */
  }
  </code></pre>
  
  <p>And the following should deserialize an instance of NonProfitOrganization. </p>
  
  <pre><code>{
     "type": "non-profit"
     /* other properties related to non profit */
  }
  </code></pre>
  
  <p>When I call the following:</p>
  
  <pre><code>Organization organization = JsonConvert.DeserializeObject&lt;Organization&gt;(payload);
  </code></pre>
  
  <p>where payload is the above JSON snippets.  I had a look at setting the "<a href="http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_TypeNameHandling.htm">TypeNameHandling</a>" on properties or classes but it serializes the whole .NET type, which isn't "portable" between the client and server when the classes are defined in different namespaces and assemblies. </p>
  
  <p>I'd rather define the type is a neutral manner which clients written in any language can use to determine the actual type of the object type being serialized.</p>