title: Serialize Dictionary<TKey, TValue> to JSON with DataContractJsonSerializer
body: <p>I have an object tree that I'm serializing to JSON with <code>DataContractJsonSerializer</code>. <code>Dictionary&lt;TKey, TValue&gt;</code> gets serialized but I don't like the markup - the items are not rendered like this:</p>
  
  <pre><code>{key1:value, key2:value2}
  </code></pre>
  
  <p>but rather like an array of serialized <code>KeyValuePair&lt;TKey, TValue&gt;</code> objects:</p>
  
  <pre><code>[{
      "__type":"KeyValuePairOfstringanyType:#System.Collections.Generic",
      "key":"key1",
      "value":"value1"
   },     
   {
      "__type":"KeyValuePairOfstringanyType:#System.Collections.Generic",
      "key":"key2",
      "value":"value2"
   }]
  </code></pre>
  
  <p>Ugly, isn't it?</p>
  
  <p>So, I avoid this by wrapping the generic Dictionary in a custom object that implements <code>ISerializable</code>, and I implement my custom serialization in the <code>GetObjectData</code> method (and it takes just 3 lines).</p>
  
  <p>Now the problem - I can't make my class derive from <code>Dictionary&lt;TKey, TValue&gt;</code>, so I implement all logic (<code>Add</code>, <code>Clear</code>, etc.) in my custom class, being applied to a private <code>Dictionary&lt;TKey, TValue&gt;</code> field. Inheritance would be preferable as I'll have all generic Dictionary functionality at my disposal when using my custom object.</p>
  
  <p>The problem with inheritance is that <code>Dictionary&lt;TKey, TValue&gt;</code> implements <code>ISerializable</code> on its own, and <code>DataContractJsonSerializer</code> seems to prefer this implementation even if I implement <code>ISerializable</code> explicitly from my custom class, like this:</p>
  
  <pre><code>public class MyClass : Dictionary&lt;string, object&gt;, ISerializable
  {
      public override void GetObjectData(SerializationInfo info, 
          StreamingContext context)
  }
  </code></pre>
  
  <p>I was actually surprised that this is possible as it allows me to implement the same interface twice without being obviously able to use explicit interface implementation - so I analyzed the situation in more detail in a blog post <a href="http://stefankiryazov.blogspot.com/2011/09/luke-whos-your-father-multiple.html" rel="noreferrer">about multiple interface implementation</a></p>
  
  <p>So, according to the experiments I did there, the serializer should be calling my implementation of ISerializable, no matter what type of casting is used internally - </p>
  
  <pre><code>((ISerializable)((Dictionary&lt;,&gt;)obj)).GetObjectData(...)
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>((ISerializable)obj).GetObjectData(...)
  </code></pre>
  
  <p>but it apparently isn't happening as I see in the resulting JSON that the <code>KeyValuePair&lt;TKey, TValue&gt;</code> serializer still being called. What might be happening that I'm missing?</p>
  
  <p>Update:
  The answers and comments I'm getting so far are pretty much only suggesting workarounds. I noted, however, that I have a workaround that works quite well so with asking this question I have 2 objectives:</p>
  
  <ol>
  <li><p>Eventually make it work with the original design - and I'm not going to change serialization logic just for that, there is a lot of code and logic dependent on it</p></li>
  <li><p>To unserstand the mystery of why isn't the <code>DataContractJsonSerializer</code> using my serialization code - as can be seen in the blog post I referred, I have made all kinds of experiments with interface implementation and inheritance and I was confident that I'm grasping all the ins and outs of the process, so I'm troubled by failing to understand what's hapenning in this case</p></li>
  </ol>