title: Json.Net serialization gives wrong property to custom converter
body: <p>I have this model that I want to serialize and deserialize using Json.Net:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public struct RangeOrValue
  {
      public int Value { get; }
      public int Min { get; }
      public int Max { get; }
      public bool IsRange { get; }
  
      public RangeOrValue(int min, int max)
      {
          Min = min;
          Max = max;
          IsRange = true;
          Value = 0;
      }
  
      public RangeOrValue(int value)
      {
          Min = 0;
          Max = 0;
          Value = value;
          IsRange = false;
      }
  }
  </code></pre>
  
  <p>I have a special requirement for serialization. If the first constructor is used, then the value should be serialized as <code>{ "Min": &lt;min&gt;, "Max": &lt;max&gt; }</code>.
  But if the second constructor is used, then value should be serialized as <code>&lt;value&gt;</code>.  </p>
  
  <p>For example <code>new RangeOrValue(0, 10)</code> needs to be serialized as <code>{ "Min": 0, "Max": 10 }</code> and <code>new RangeOrValue(10)</code> needs to be serialized as <code>10</code>.</p>
  
  <p>I wrote this custom converter to do this task:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class RangeOrValueConverter : JsonConverter&lt;RangeOrValue&gt;
  {
      public override void WriteJson(JsonWriter writer, RangeOrValue value, JsonSerializer serializer)
      {
          if (value.IsRange)
          {
              // Range values are stored as objects
              writer.WriteStartObject();
              writer.WritePropertyName("Min");
              writer.WriteValue(value.Min);
              writer.WritePropertyName("Max");
              writer.WriteValue(value.Max);
              writer.WriteEndObject();
          }
          else
          {
              writer.WriteValue(value.Value);
          }
      }
  
      public override RangeOrValue ReadJson(JsonReader reader, Type objectType, RangeOrValue existingValue, bool hasExistingValue, JsonSerializer serializer)
      {
          reader.Read();
          // If the type is range, then first token should be property name ("Min" property)
          if (reader.TokenType == JsonToken.PropertyName)
          {
              // Read min value
              int min = reader.ReadAsInt32() ?? 0;
              // Read next property name
              reader.Read(); 
              // Read max value
              int max = reader.ReadAsInt32() ?? 0;
              // Read object end
              reader.Read();
              return new RangeOrValue(min, max);
          }
  
          // Read simple int
          return new RangeOrValue(Convert.ToInt32(reader.Value));
      }
  }
  
  </code></pre>
  
  <p>To test the functionality, I wrote this simple test:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[TestFixture]
  public class RangeOrValueConverterTest
  {
      public class Model
      {
          public string Property1 { get; set; }
          public RangeOrValue Value { get; set; }
          public string Property2 { get; set; }
          public RangeOrValue[] Values { get; set; }
          public string Property3 { get; set; }
      }
  
      [Test]
      public void Serialization_Value()
      {
          var model = new Model
          {
              Value = new RangeOrValue(10),
              Values = new[] {new RangeOrValue(30), new RangeOrValue(40), new RangeOrValue(50),},
              Property1 = "P1",
              Property2 = "P2",
              Property3 = "P3"
          };
  
          string json = JsonConvert.SerializeObject(model, new RangeOrValueConverter());
  
          var deserializedModel = JsonConvert.DeserializeObject&lt;Model&gt;(json, new RangeOrValueConverter());
  
          Assert.AreEqual(model, deserializedModel);
      }
  }
  
  </code></pre>
  
  <p>When I run the test, Object serializes successfully. But when it tries to deserialize it back, I receive this error:</p>
  
  <pre><code>Newtonsoft.Json.JsonReaderException : Could not convert string to integer: P2. Path 'Property2', line 1, position 46.
  </code></pre>
  
  <p>The stack trace leads to line <code>int min = reader.ReadAsInt32() ?? 0;</code>.  </p>
  
  <p>I think I'm doing something wrong in converter that causes Json.Net to provide wrong values to the converter. But I can't quite figure it out. Any ideas?</p>