In my C# project, I have occasion to deserialize JSON that may not be correct. Hence, errors are possible. At the moment, I am wrapping it in a try block, and catching exceptions by returning null. This works, but it would be nicer to tell the library not to throw an exception in the first place. Is that possible?
My current code:
public static NodeModel FromJsonString(string json)
{
NodeModel r = null;
JsonConverter converter = JsonConverters.ReferenceHierarchyCreation;
try
{
r = JsonConvert.DeserializeObject<NodeModel>(json, converter);
}
catch
{
}
return r;
}
nullfor a valid JSON string (ie if it was trying to deserialise"null")? Right now what you're doing is probably the best you can do, since it's in your layer where you can make the decision that"null"andthis: 'isn't, valid [jsonshould be treated the same.