I'm trying to handle deserialization error, but even if I'm able to reach error handling function and set Handled property to true then error is throwing to the main function.
Models:
public class PriceValidity
{
public Date EndDate { get; set; }
public Date StartDate { get; set; }
[OnError]
internal void OnError(StreamingContext context, ErrorContext errorContext)
{
errorContext.Handled = true;
}
}
public class Date
{
[JsonProperty("$date")]
public DateTime Value { get; set; }
}
Calling deserializer:
private void ParseMessage<T>(string message) where T: new()
{
var result = new T();
var jsonSerializer = new Newtonsoft.Json.JsonSerializer();
using (var reader = new StringReader(message))
using (var jsonReader = new JsonTextReader(reader))
{
result = jsonSerializer.Deserialize<T>(jsonReader);
};
}
JSON:
{
"StartDate":{
"$date":"2018-05-07T00:00:00.000Z"
},
"EndDate":{
"$date":{
"$numberLong":"253402214400000"
}
}
}
Error:
After parsing a value an unexpected character was encountered: :. Path 'EndDate.$date',
I don't want to handle also $numberLong scenario but just skip it.