title: Deserialize object based on value type in property
body: <p><strong>TL;DR</strong>: Is there an easy way in json.net to inspect the type of a property and create an instance based on that?</p>
  
  <p>I have the following two objects in JSON, these are relation objects in the <a href="http://jsonapi.org" rel="noreferrer">JSON API</a>:</p>
  
  <pre><code>{ "data": { "type": "Test", "id": "1" } }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{ "data": [{ "type": "Test", "id": "1" }, { "type": "Test", "id": "2" }]}
  </code></pre>
  
  <p>Using json.net, I want to serialize these objects to either the class ToOneRelation, or ToManyRelation. The classes look like this:</p>
  
  <pre><code>[JsonConverter(typeof(RelationshipConverter))]
  abstract class Relation
  {
      [JsonProperty("meta")]
      public Dictionary&lt;string, object&gt; Meta { get; set; }
  }
  class ToOneRelation : Relation
  {
      [JsonProperty("data")]
      public object Data { get; set; }
  }
  class ToManyRelation : Relation
  {
      [JsonProperty("data")]
      public List&lt;object&gt; Data { get; set; }
  }
  </code></pre>
  
  <p>Generally I only know that I want to have a Relation, so I would do the following:</p>
  
  <pre><code>var result = JsonConvert.DeserializeObject&lt;Relation&gt;(json);
  </code></pre>
  
  <p>In order to instantiate the correct object, I need a custom converter, where I check the type of the "data" property. When the type is an array, I use the ToManyRelation, otherwise I use the ToOneRelation.</p>
  
  <p>Currently I created a custom converter that will walk over all properties and deserialize them. </p>
  
  <pre><code>object toOneData = null;
  List&lt;object&gt; toManyRelationData = null;
  
  Dictionary&lt;string, object&gt; meta = null;
  
  reader.Read();
  while (reader.TokenType == JsonToken.PropertyName)
  {
      string propertyName = reader.Value.ToString();
      reader.Read();
      if (string.Equals(propertyName, "data", StringComparison.OrdinalIgnoreCase))
      {
          if (reader.TokenType == JsonToken.StartObject)
          {
              toOneData = serializer.Deserialize&lt;object&gt;(reader);
          }
          else if (reader.TokenType == JsonToken.StartArray)
          {
              toManyRelationData = serializer.Deserialize&lt;List&lt;object&gt;&gt;(reader);
          }
      }
      else if (string.Equals(propertyName, "meta", StringComparison.OrdinalIgnoreCase))
      {
          meta = serializer.Deserialize&lt;Dictionary&lt;string, object&gt;&gt;(reader);
      }
      else
      {
          reader.Skip();
      }
      reader.Read();
  }
  </code></pre>
  
  <p>Although this works, I have the problem that this code isn't really maintainable. As soon as an extra property is introduced to the Relation object (or the other objects) I need to modify this code to reflect those changes.</p>
  
  <p>So my question is is, is there a better way to inspect a property to identify which type to create and than use <code>serializer.Populate</code> method to do the rest?
  (The CustomCreationConverter doesn't work, because it can't inspect the properties before creating the object is created...)</p>
  
  <p>(Note, a working example can be found <a href="https://dotnetfiddle.net/dP43KC" rel="noreferrer">here</a>)</p>