title: Deserializing a JSON object hierarchy into a hierarchy of Dictionary<string, object>
body: <p>I'm in <strong>.NET for WinRT (C#)</strong> and I'd like to deserialize a JSON string to a <code>Dictionary&lt;string, object&gt;</code>, where the dictionary value can later be converted to the actual type.
  The JSON string can contain an object hierarchy and I'd like to have child objects in <code>Dictionary&lt;string, object&gt;</code> as well.</p>
  
  <p>Here's a sample JSON it should be able to handle:</p>
  
  <pre><code>{
    "Name":"John Smith",
    "Age":42,
    "Parent":
    {
      "Name":"Brian Smith",
      "Age":65,
      "Parent":
      {
         "Name":"James Smith",
         "Age":87,
      }
    }
  }
  </code></pre>
  
  <p>I tried with the <strong>DataContractJsonSerializer</strong> doing as so:</p>
  
  <pre><code>using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
  {
      DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
      settings.UseSimpleDictionaryFormat = true;
  
      DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Dictionary&lt;string, object&gt;), settings);
      Dictionary&lt;string, object&gt; results = (Dictionary&lt;string, object&gt;)serializer.ReadObject(ms);
  }
  </code></pre>
  
  <p>This actually works fine for the first level, but then <em>"Parent"</em> is just an object which cannot be casted to a <code>Dictionary&lt;string, object&gt;</code>:</p>
  
  <pre><code>Dictionary&lt;string, object&gt; parent = (Dictionary&lt;string, object&gt;)results["Parent"];
  Cannot cast 'results["Parent"]' (which has an actual type of 'object') to 'System.Collections.Generic.Dictionary&lt;string,object&gt;'
  </code></pre>
  
  <p>I then tried using <strong>Json.NET</strong> but child objects are JObject themselves being <code>IDictionary&lt;string, JToken&gt;</code>, which forces me to iterate through the full hierarchy and convert them over again.</p>
  
  <p>Would someone know how to solve this problem using an existing serializer?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I'm using <code>Dictionary&lt;string, object&gt;</code> because my objects vary from one server call to another (e.g. the <em>"Id"</em> property might be <em>"id"</em>, *"cust_id"* or <em>"customerId"</em> depending on the request) and as my app isn't the only app using those services, I can't change that, at least for now.</p>
  
  <p>Therefore, I found it inconvenient to use <em>DataContractAttribute</em> and <em>DataMemberAttribute</em> in this situation.
  Instead I'd like to store everything in a generic dictionary, and have a single strongly-typed property "Id" which looks for "id", "cust_id" or "customerId" in the dictionary making it transparent for the UI.</p>
  
  <p>This system works great with JSON.NET, however if ever the server returns an object hierarchy, sub-objects will be stored as JObjects in my dictionary instead of another dictionary.</p>
  
  <p>To sum-up, I'm looking for an efficient system to transform an object hierarchy into a hierarchy of <code>Dictionary&lt;string, object&gt;</code> using a JSON serializer available in WinRT.</p>