title: Force conversion of empty JSON array to Dictionary type
body: <p>The problem is, that <code>json_encode()</code> function in PHP leaves ambiguity for tools which are reading its output. In PHP both lists and dictionaries are same type of <code>array</code>.</p>
  
  <pre><code>echo json_encode([]); // []
  echo json_encode(["5" =&gt; "something"]); // {"5": "something"}
  </code></pre>
  
  <p>In JSON.NET I want to force both <code>[]</code> and <code>{"5": "something"}</code> to convert to <code>Dictionary&lt;string, string&gt;</code> type. However it recognizes <code>[]</code> as prohibited structure for Dictionary and throws an Exception.</p>
  
  <p>Can I quickly leave empty JSON arrays nulled or force them to convert to empty Dictionary type?</p>
  
  <p><strong>FINAL SOLUTION</strong></p>
  
  <p>I modified accepted answer to make it generic and reusable for other types.</p>
  
  <pre><code>public class DictionaryOrEmptyArrayConverter&lt;T,F&gt; : JsonConverter
  {
      public override bool CanWrite { get { return false; } }
      public override bool CanConvert(Type objectType)
      {
          return objectType == typeof(Dictionary&lt;T, F&gt;);
      }
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          switch (reader.TokenType)
          {
              case JsonToken.StartArray:
                  reader.Read();
                  if (reader.TokenType == JsonToken.EndArray)
                      return new Dictionary&lt;T, F&gt;();
                  else
                      throw new JsonSerializationException("Non-empty JSON array does not make a valid Dictionary!");
              case JsonToken.Null:
                  return null;
              case JsonToken.StartObject:
                  var tw = new System.IO.StringWriter();
                  var writer = new JsonTextWriter(tw);
                  writer.WriteStartObject();
                  int initialDepth = reader.Depth;
                  while (reader.Read() &amp;&amp; reader.Depth &gt; initialDepth)
                  {
                      writer.WriteToken(reader);
                  }
                  writer.WriteEndObject();
                  writer.Flush();
                  return JsonConvert.DeserializeObject&lt;Dictionary&lt;T, F&gt;&gt;(tw.ToString());
              default:
                  throw new JsonSerializationException("Unexpected token!");
          }
      }
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>You should use it with your JSON object as in example below:</p>
  
  <pre><code>public class Company
  {
      public string industry_name { get; set; }
  
      [JsonConverter(typeof(DictionaryOrEmptyArrayConverter&lt;int, Upgrade&gt;))]
      public Dictionary&lt;int, Upgrade&gt; upgrades { get; set; }
  }
  
  public class Upgrade
  {
      public int level { get; set; }
  }
  </code></pre>
  
  <p>It allows you to quickly convert JSON string to objects with <code>DeserializeObject</code> method:</p>
  
  <pre><code>var result = JsonConvert.DeserializeObject&lt;Company&gt;(jsonString);
  </code></pre>