gpt4 book ai didi

c# - 发生错误时,我们如何访问 ServiceStack 的 JSON Serializer 中的数据?

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:37 25 4
gpt4 key购买 nike

我们如何在 ServiceStack's JSON Serializer 时获取有关 JSON 反序列化异常的更多信息?配置为引发异常:

JsConfig.ThrowOnDeserializationError = true;

默认情况下,JSON Serializer 将记录并忽略非关键异常,或者可以配置为抛出上述配置。

最佳答案

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com