I'm using the json.net package to serialize/deserialize settings to file. I'd like a way to ensure that when I deserialize, that a value was found in the json string for every property in my class. There is a way to have json.net throw an exception if a property is found that is not a member of the class, but the reverse isn't true. For example:
public class Params
{
public double ReqPress = 5.0;
public double LaserTransmission = 0.0;
public int LaserShotCount = 1;
public override string ToString()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
public static OperationalParams FromString(string jsonData)
{
var s = new JsonSerializerSettings();
s.MissingMemberHandling = MissingMemberHandling.Error;
return JsonConvert.DeserializeObject<OperationalParams>(jsonData, s);
}
}
If I call
OperationalParams.FromString("{ ReqPress : 5.0, LaserTransmission : 0.0 }"),
then I want to get an error telling me that LaserShotCount was missing and did not get initialized from the json data. Ideally, JsonConvert.DeserializeObject would throw an exception if one or more members were missing, but that doesn't seem to be supported. Is there another way to do this?