Json.NET 行为可以通过属性定义:如果 json 负载不包含必需的属性,则使用默认值或仅抛出异常。
Yet System.Text.Json
序列化器默默地什么也不做。
正在上课:
public sealed class Foo
{
[Required]
public int Prop {get;set;} = 10;
}
并反序列化空对象:
JsonSerializer.Deserialize<Foo>("{}");
我只是得到一个实例Foo
with Prop=10
。
我找不到任何设置JsonSerializerOptions https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions?view=netcore-3.1强制它抛出异常。是否可以?
如果没有收到目标类型的属性之一的值,System.Text.Json 不会引发异常。您需要实现一个自定义转换器。
参考:https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#required-properties https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#required-properties
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)