title: How deserialize [JsonExtensionData] Dictionary<string, object> with casting
body: <p>[JsonExtensionData] allows you to do is to serialize elements of a JSON document which does not have matching properties on the destination object to the dictionary which is decorated with the [JsonExtensionData] attribute.</p>
  
  <p>How to create a dictionary of objects cast into the appropriate type??</p>
  
  <p>For example:</p>
  
  <pre><code>var json = "{\r\n \"sampleClass\":{ \"name\":\"Name\"} ,             
  \"sampleOtherClass\":{ \"name\":\"OtherName\"} ,\r\n  \"X\": \"Jan\"\r\n}";
  </code></pre>
  
  <p>and</p>
  
  <pre><code>var result  =JsonConvert.DeserializeObject&lt;Test&gt;(json);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class Test
  {
      public string X { get; set; }
  
      [JsonExtensionData]
      public Dictionary&lt;string, object&gt; Y { get; set; }
  }
  </code></pre>
  
  <p>The dictionary should contain such elements:</p>
  
  <pre><code>Dictionary&lt;string, object&gt; students = new Dictionary&lt;string, object&gt;()
  {
      { "sampleClass", new ClassName { Name="MyName" } },
      { "sampleOtherClass", new SampleOtherClass { Name="MyName" } }
  };
  </code></pre>
  
  <p>Which means that for node sampleClass we want to create object SampleClass and next to add the dictionary Y.</p>
  
  <p>Currently, the value in the DictionaryEntry is a string e.g. { name: "Name" }</p>
  
  <p>The source:
  <a href="https://dotnetfiddle.net/mhU6ME" rel="nofollow noreferrer">https://dotnetfiddle.net/mhU6ME</a></p>
  
  <p><strong>Update:</strong></p>
  
  <p>Now I used the below approach to deserialize, but when I want to have one     [JsonExtensionData] Dictionary to deserialize/serialize  it brings problems to have two collections.</p>
  
  <pre><code>public class Class1
  {
      public string Code { get; set; }
  
      public string Name { get; set; }
  
      public bool IsActive { get; set; }
  
      [JsonExtensionData]
      public Dictionary&lt;string, JToken&gt; _JTokenProperty { get; set; }
  
      public Dictionary&lt;string, PropertiesClass&gt; Properties1 { get; set; } = new Dictionary&lt;string, PropertiesClass&gt;();
  }
  </code></pre>