Json.NET需要反序列化的所有属性 [英] Json.NET require all properties on deserialization

查看:116
本文介绍了Json.NET需要反序列化的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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的可能值为:

  • 默认:该属性不是必需的.默认状态.
  • 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() };

注意:

  • 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需要反序列化的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆