title: Usage-specific serialization for complex type in Dictionary with JSON.Net
body: <p>I have a class</p>
  
  <pre><code>public class MyValue 
  {
      public String Prop1 { get; set; }
      public String Prop2 { get; set; }
  }
  </code></pre>
  
  <p>which I am both using as a type for a normal Property as well as a Dictionary key.</p>
  
  <p>What I need is a way so that when this class is used as a Property, it is serialized as</p>
  
  <pre><code>{"Prop1":"foo","Prop2":"bar"}
  </code></pre>
  
  <p>but when it is used as a Dictionary key, it is serialized in a way that JSON.Net is able to deserialize it properly.</p>
  
  <p>When adding a ToString() method to MyValue, I am able to create a text representation (non-JSON) that allows the usage as Dictionary key but unfortunately, I am not able to deserialize it afterwards. Even adding a JsonConverter for MyValue did not help because it seems to be unable to handle non-JSON as source format (and additionally, when serialized as a property, it IS json, so the converter would need to handle both somehow).</p>