I am using the JsonConvert.DeserializeObject<T>(string value)
method to deserialize an object of type T
from a string
.
In a custom class (which I am attempting to deserialize) I perform checks on the arguments supplied to the constructor and can throw an ArgumentNullException
. However, this exception does not bubble up through the deserializer and to the original caller, and therefore the exception stays unhandled inside the constructor.
This is the generic method I am using in a helper class:
public static T FromJsonString<T>(string json)
{
try
{
return JsonConvert.DeserializeObject<T>(json);
}
catch(ArgumentNullException)
{
// Would like to handle exception here, but never reached
}
}
My class constructor example:
[JsonConstructor]
public Profile(string name, ...)
{
if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException("name"); }
// ...
}
I have attempted using JsonSerializerSettings
when calling DeserializeObject<T>()
, for example the Error
property, however this made no difference.
How can I make the exception bubble up and not stay inside the constructor of my class?