title: Serializing a dictionary with IConvertible values using Json.NET with the TypeNameHandling-flag
body: <p>I have the following dictionary that I'd very much like to serialize using Json.Net. The dictionary contains items of the <code>IConvertible</code> interface allowing me to add whatever primitive type I need to the dictionary. </p>
  
  <pre><code>    var dic = new Dictionary&lt;string, IConvertible&gt;();
      dic.Add("bool2", false);
      dic.Add("int2", 235);
      dic.Add("string2", "hellohello");
  </code></pre>
  
  <p>I have the following implementation for serializing the list using Json.net:</p>
  
  <pre><code>    var settings = new JsonSerializerSettings();
      settings.TypeNameHandling = TypeNameHandling.Objects;
      var dicString = JsonConvert.SerializeObject(dic,    Newtonsoft.Json.Formatting.Indented, settings);
  </code></pre>
  
  <p>This gives me the following output:</p>
  
  <pre><code>    {
        "$type": "System.Collections.Generic.Dictionary`2[[System.String,         mscorlib],[System.IConvertible, mscorlib]], mscorlib",
        "bool2": false,
        "int2": 235,
        "string2": "hellohello"
      }
  </code></pre>
  
  <p>However. When trying to deserialize as such:</p>
  
  <pre><code>    var dic2 = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, IConvertible&gt;&gt;(dicString);
  </code></pre>
  
  <p>... I get the following error:</p>
  
  <pre><code>    Error converting value False to type 'System.IConvertible'. Path 'bool2', line 3, position 16.
  </code></pre>
  
  <p>I've looked around and found the following; but setting the typeNameHandling didn't solve it. Nor can I decorate the <code>IConvertible</code> value with a type name attribute seeing as it is a dictionary. </p>
  
  <p><a href="https://stackoverflow.com/questions/5780888/casting-interfaces-for-deserialization-in-json-net">Casting interfaces for deserialization in JSON.NET</a></p>
  
  <p>I haven't found any other information on the topic so some help would be greatly appreciated! </p>
  
  <p>I also found this solution but it involves creating a ExpandableObjectConverter which isn't a very elegant solution.</p>
  
  <p><a href="https://stackoverflow.com/questions/17560085/problems-using-json-net-with-expandableobjectconverter">Problems using JSON.NET with ExpandableObjectConverter</a></p>