I have a Json string for a list of some object, this Json string does not include one of the properties from my object. I am looking for a way to set that missing property value while deserializing the Json string to object.I have a work around to set it after completion of deserialization but would prefer it if I can set it while deserialization.
I tried using custom JsonConverter but it does not get executed as a respective property not a part of Json string, any other way is also fine if I can inject value in missing property while deserialization.
Class
public class TestClass
{
[JsonConverter(typeof(InternalIDConverter))]
public int InternalID { get; set; }
public int IdRemittance { get; set; }
public string IDCountry { get; set; }
public string OperationAction { get; set; }
public bool BusinessToBusiness { get; set; }
}
Custom JsonConverter
public class InternalIDConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException("Not implemented yet");
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//Doing my work here
}
public override bool CanWrite
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return false;
}
}
Deserialization
private void button11_Click(object sender, EventArgs e)
{
string json = "[{\"IDCountry\":\"AK\",\"BusinessToBusiness\":false,\"IdRemittance\":2,\"OperationAction\":\"U\"},{\"IDCountry\":\"AK\",\"BusinessToBusiness\":true,\"IdRemittance\":14,\"OperationAction\":\"U\"}]";
var data = JsonConvert.DeserializeObject<List<TestClass>>(json);
}