title: How to deserialize JSON with dependant properties?
body: <p>I was wondering if I have an object that contains a field which has its deserialization process dependant on another field, how can I deserialize the parent object?</p>
  
  <p><strong>Container</strong></p>
  
  <pre><code>class Container
  { 
      public int Id { get; set; }
      public object Data { get; set; } //deserialization depends on first field
  }
  </code></pre>
  
  <p><strong>Hierarchy</strong></p>
  
  <pre><code>class FieldType1
  {
      public string Value { get; set; }
  }
  
  class FieldType2
  {
      public int Numbers { get; set; }
  }
  </code></pre>
  
  <p>Given the example above if I have a <code>Dictionary&lt;int,Type&gt;</code> how can I deserialize an object that comes as a string like the one below?:</p>
  
  <pre><code>var container = new Container { Data = new FieldType1 { Value = "sata" }};
  var str = JsonConvert.SerializeObject(container);
  var clone = JsonConvert.DeserializeObject&lt;Container&gt;(str);//has dependant field on another field
  </code></pre>
  
  <p>As you can see in my example above I always have the same <code>container</code> type.but one property differs.</p>
  
  <p><strong>Update</strong></p>
  
  <p>After some answers here could it be possible to keep only one type of parent object and instead have a base type for the second field ?</p>
  
  <pre><code>  [JsonSubTypes.KnownSubType(typeof(Child1),1)]
    [JsonSubTypes.KnownSubType(typeof(Child2),2)]
    public abstract Child
    {
    }
  
  
   public class Parent{
   public int Id;
   public Child child;
  
   }
  </code></pre>
  
  <p>Can i decorate somehow the parent to know how to deserialize its second field (similar to <code>JsonSubTypes</code>)? </p>
  
  <p><br>Summing it up i do not want to have <code>P,P1,P2..Pn</code> types for parent.
  I want to have  one type <code>P</code> for parent with <code>F1,F2...Fn</code> types for its second field.So that when i deserialize  i would just say <code>JsonConvert.DeserializeObject&lt;P&gt;</code> while the converter takes care of which concrete type is the second field:<br></p>
  
  <pre><code> Parent c1=new P{ id=1,child=new Child1()};
   Parent c2=new P{ id=2,child=newChild2()};
   List&lt;Parent&gt; items=new List&lt;Parent&gt;{c1,c2};
   var str=JsonConvert.SerializeObject(items);
  
   var clone=JsonConvert.DeserializeObject&lt;List&lt;Parent&gt;&gt;(str);
  </code></pre>