You could leverage a custom JsonConverter. Decorate your class:
[JsonConverter(typeof(CustomConverter))]
public class MyType
{
[JsonProperty("type")]
[Required]
public string Type { get; set; }
[JsonProperty("value")] // existing field
public int Value { get; set; }
// new field planning to add for new data
public decimal Value2 { get; set; }
}
Where CustomConverter is defined (roughly):
public class CustomConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jObject = JToken.Load(reader) as JObject;
MyType myType = new MyType();
myType.Type = jObject.GetValue("type").ToString();
JToken tokenValue = jObject["value"];
if (tokenValue.Type == JTokenType.Integer)
{
myType.Value = int.Parse(tokenValue.ToString());
}
else if (tokenValue.Type == JTokenType.Float) {
myType.Value2 = decimal.Parse(tokenValue.ToString());
}
return myType;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
Note that one of Value and Value2 is implicitly set to 0 whereas the other property contains deserialized value.
To test the solution execute:
string json = @"{type:""Type1"",value: 27.99}";
MyType temp = JsonConvert.DeserializeObject<MyType>(json);