Json.NET 需要反序列化的所有属性 [英] Json.NET require all properties on deserialization
问题描述
在 Json.NET 中,如何使反序列化时需要的所有属性?我知道我可以使用消息的属性来做到这一点,但我不想这样做.主要是因为它需要我的消息库承担外部依赖.
In Json.NET, how do I make ALL properties required upon deserialization? I know that I can do this with attributes on the messages, but I don't want to do that. Mainly because it would require my message library to take on an external dependency.
我尝试了 MissingMemberHandling.Error 设置,但它与我想要的相反.我对具有额外属性的 JSON 没意见.当 JSON 中缺少任何目标对象属性时,我希望它失败.
I tried the MissingMemberHandling.Error setting, but it does the opposite of what I want. I'm okay with the JSON having extra properties. I want it to fail when any target object properties are missing in the JSON.
我实际上是在反序列化为 F# 记录,而且属性通常不能为空.(它们不能通过代码中的正常方式分配为 null.)但是当数据丢失时,Json.NET 很高兴地默认属性为 null.
I'm actually deserializing to F# records, and the properties can't ordinarily be null anyway. (They can't be assigned null by normal means in code.) But Json.NET happily defaults properties to null under the covers when data is missing.
接受答案的 F# 版本
解析器
open System
open Newtonsoft.Json
open Newtonsoft.Json.Serialization
type RequireAllPropertiesContractResolver() =
inherit DefaultContractResolver()
override me.CreateObjectContract(objectType:Type) =
let contract = base.CreateObjectContract(objectType)
contract.ItemRequired <- new Nullable<Required>(Required.Always)
contract
在设置中
let settings = new JsonSerializerSettings() // default settings
...
settings.ContractResolver <- new RequireAllPropertiesContractResolver()
推荐答案
如果您的 model 具有您的 JSON 可能忽略的属性,并且您希望这是一个错误,添加属性 [JsonObject(ItemRequired=Required.Always)]代码>
到你的类:
If your model has properties that your JSON may omit, and you want that to be an error, add the attribute [JsonObject(ItemRequired=Required.Always)]
to your classes:
类型:必需
一个值,指示是否需要对象的属性.
A value indicating whether the object's properties are required.
必需
的可能值是:
- 默认:该属性不是必需的.默认状态.
- AllowNull:该属性必须在 JSON 中定义,但可以为空值.
- 始终:该属性必须在 JSON 中定义,并且不能为空值.
- DisallowNull:该属性不是必需的,但它不能是空值 [如果存在].(Json.NET 8.0.1 及更高版本.)李>
- Default: The property is not required. The default state.
- AllowNull: The property must be defined in JSON but can be a null value.
- Always: The property must be defined in JSON and cannot be a null value.
- DisallowNull: The property is not required but it cannot be a null value [if present]. (Json.NET 8.0.1 and later.)
该设置是继承的,因此可以添加到通用基类中.
The setting is inherited so can be added to a generic base class.
更新
要对所有对象全局执行此操作,请将 DefaultContractResolver
并将 ItemRequired
标志添加到所有对象协定:
To do it globally for all objects, subclass the DefaultContractResolver
and add the ItemRequired
flag to all object contracts:
public class RequireObjectPropertiesContractResolver : DefaultContractResolver
{
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
var contract = base.CreateObjectContract(objectType);
contract.ItemRequired = Required.Always;
return contract;
}
}
然后,在设置中:
var settings = new JsonSerializerSettings { ContractResolver = new RequireObjectPropertiesContractResolver() };
注意事项:
如果您不想在 f# 成员为
optional
时要求 JSON 属性,请参阅 this answer 对这个问题以及问题 Json.NET make property required based on property type.
If you don't want to require JSON properties when your f# member is
optional
see this answer to this question and also the question Json.NET make property required based on property type.
此合约解析器将默认设置 Required.Always
应用于所有对象属性,但不会覆盖 JsonProperty.AttributeRequired
直接应用时.如果您需要,请参阅例如如何覆盖Required.Always"在 newtonsoft json 中.
This contract resolver applies a default setting of Required.Always
to all object properties, but will not override JsonProperty.AttributeRequired
when applied directly. If you need that, see e.g. How to override the "Required.Always" in newtonsoft json.
如问题所述,设置 MissingMemberHandling =MissingMemberHandling.Error
解决了一个补充问题:如果您的 JSON 可能具有您的 model 省略的属性,并且您希望这是一个错误,使用 MissingMemberHandling.Error
.请参阅:MissingMemberHandling 设置.
As stated in the question, the setting MissingMemberHandling = MissingMemberHandling.Error
solves a complimentary problem: if your JSON may have properties that your model omits, and you want that to be an error, use MissingMemberHandling.Error
. See: MissingMemberHandling setting.
您可能希望缓存合约解析器以获得最佳性能.
这篇关于Json.NET 需要反序列化的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!