I have this JSON :
{
"Foo": "A value",
"Bar": 42
}
To deserialize it, I have this class :
public class SomeClass
{
public string Foo { get; set; }
public int Bar { get; set; }
}
I'm deserializing it using :
string json1 = "{\"Foo\":\"A value\",\"Bar\":42}";
var MyObj1 = JsonConvert.DeserializeObject<SomeClass>(json1);
Debug.WriteLine("Foo1 : " + MyObj1.Foo); //Foo1 : A value
Debug.WriteLine("Bar1 : " + MyObj1.Bar); //Bar1 : 42
I need a specific treatment when a field is missing from JSON.
In example :
{
"Foo": "Another value"
}
When using the same code than above, Bar is set to 0. It could have been set to 0 in the original JSON, so I can't tell with this value if it was existing or not.
string json2 = "{\"Foo\":\"Another value\"}";
var MyObj2 = JsonConvert.DeserializeObject<SomeClass>(json2);
Debug.WriteLine("Foo2 : " + MyObj2.Foo); //Foo2 : Another value
Debug.WriteLine("Bar2 : " + MyObj2.Bar); //Bar2 : 0
How can I achieve it?