当 ServiceStack's JSON Serializer 时,我们如何获取有关 JSON 反序列化异常的更多信息配置为抛出异常:
JsConfig.ThrowOnDeserializationError = true;
默认情况下,JSON 序列化器将记录并忽略非关键异常,或者可以配置为使用上述配置抛出异常。
最佳答案
何时 ServiceStack's JSON Serializer配置为在遇到反序列化错误时抛出,其中:
JsConfig.ThrowOnDeserializationError = true;
它捕获能够反序列化的信息,并将它们存储在抛出的序列化异常中的 ex.Data
字典中。
try {
string json = @"{""idBad"":""abc"", ""idGood"":""2"" }";
JsonSerializer.DeserializeFromString(json, typeof(TestDto));
Assert.Fail("Exception should have been thrown.");
} catch (SerializationException ex) {
Assert.That(ex.Data, Is.Not.Null);
Assert.That(ex.Data["propertyName"], Is.EqualTo("idBad"));
Assert.That(ex.Data["propertyValueString"], Is.EqualTo("abc"));
Assert.That(ex.Data["propertyType"], Is.EqualTo(typeof(int)));
}
关于c# - 当发生错误时,我们如何从ServiceStack的JSON Serializer访问数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12171217/