如何覆盖“始终需要"在newtonsoft json中 [英] How to override the "Required.Always" in newtonsoft json

查看:61
本文介绍了如何覆盖“始终需要"在newtonsoft json中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是基于基于此问题(尽管适用于序列化)的代码的示例.是否有可能将所有Required.Always覆盖为Required.Default(即可选"),并允许我反序列化对象,而与"required"无关.属性?

This is following an example based on code from this question (albeit for Serialization). Is it possible to override all Required.Always to be Required.Default (i.e. "optional"), and allow me to deserialize an object regardless of the "required" attributes?

    public class OverrideToDefaultContractResolver : DefaultContractResolver
    {
        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            var contract = base.CreateObjectContract(objectType);
            contract.ItemRequired = Required.Default;
            return contract;
        }
    }

    public class MyClass
    {
        [JsonProperty("MyRequiredProperty", Required = Required.Always, NullValueHandling = NullValueHandling.Ignore)]
        public string MyRequiredProperty { get; set; }
    }

    public static void Test()
    {
        var settings = new JsonSerializerSettings { ContractResolver = new OverrideToDefaultContractResolver() };
        MyClass obj = JsonConvert.DeserializeObject<MyClass>(@"{""Nope"": ""Hi there""}", settings);
        Console.WriteLine($"Json property: {obj.MyRequiredProperty}");
    }

推荐答案

通常,对于Json.NET,设置的优先级是:

In general, with Json.NET the precedence of settings is:

  1. 应用于属性或参数的属性.
  2. 应用于对象的属性.
  3. 应用于序列化程序的序列化设置.

您的OverrideToDefaultContractResolver取消了在对象级别而不是单个属性级别应用的Required属性.由于后者优先于前者,因此也需要取消它们.以下合同解析器可以完成这项工作:

Your OverrideToDefaultContractResolver cancels Required attributes applied at the object level, but not at the individual property level. As the latter take precedence over the former, they need to be cancelled also. The following contract resolver does the job:

public class OverrideToDefaultContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        property.Required = Required.Default;
        return property;
    }

    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        var contract = base.CreateObjectContract(objectType);
        contract.ItemRequired = Required.Default;
        return contract;
    }
    
    protected override JsonProperty CreatePropertyFromConstructorParameter(JsonProperty matchingMemberProperty, ParameterInfo parameterInfo)
    {
        var property = base.CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
        property.Required = Required.Default;
        return property;
    }
}

注意:

  • 链接的答案中的合同解析器最初旨在强制要求所有属性,除非明确标记否则-f#的合理默认行为,通常不允许空值.

  • The contract resolver in the linked answer was originally designed to force all properties to be required unless explicitly marked otherwise - a reasonable default behavior for f#, which generally disallows null values.

您可能想要最好地缓存合同解析器性能.

演示小提琴此处.

这篇关于如何覆盖“始终需要"在newtonsoft json中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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