问题描述
在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. (不能通过常规方式在代码中将它们分配为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()
推荐答案
如果您的模型具有您的 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.
Required
的可能值为:
该设置是继承的,因此可以添加到通用基类中.
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属性,请参见此答案这个问题,以及问题 Json.NET根据属性类型来确定是否需要属性 .
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.
如问题所述,设置 MissingMemberHandling = MissingMemberHandling.Error
解决了一个补充问题:如果您的 JSON 可能具有模型所忽略的属性,而您想将其作为错误,请使用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需要反序列化的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!