Newtonsoft.Json: AllowNull bug?



im试图编写序列化器,该序列化器将序列化/避免到完全相同类结构。因此,即使未提供默认值,也应该丢弃错误。我认为我通过设置成员处理和合同解决方案来完成的工作,但它行不通。如果在合同解析器中明确指定了例外情况。

明确指定它。

这是我的序列化器:

public sealed class JsonSerializer : ISerializer
{
    private readonly JsonSerializerSettings _settings = new JsonSerializerSettings
    {
        ContractResolver = new RequireObjectPropertiesContractResolver(),//Everything required. Null are allowed.
        MissingMemberHandling = MissingMemberHandling.Error, //If something missing in target class - it should throw error.
        Formatting = Formatting.Indented
    };
    public object Deserialize(string input, Type type)
    {
        return JsonConvert.DeserializeObject(input, type, _settings);
    }
    public string Serialize(object input)
    {
        return JsonConvert.SerializeObject(input, _settings);
    }
    private class RequireObjectPropertiesContractResolver : DefaultContractResolver
    {
        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            var contract = base.CreateObjectContract(objectType);
            contract.ItemRequired = Required.AllowNull;
            return contract;
        }
    }
}

这是我无法工作的测试用例:

   [DataContract]
    private class TestInput
    {
        [DataMember]
        public string Value0 { get; set; }
        [DataMember]
        public string Value1 { get; set; }
        [DataMember]
        public Inner Inner { get; set; }
    }
    [DataContract]
    private class Inner
    {
        [DataMember]
        public string NewValue0 { get; set; }
    }
    [TestMethod]
    public void TestSerialization()
    {
        using (var kernel = new StandardKernel(new MyModule()))
        {
            var serializer = kernel.Get<ISerializer>();
            //It should throw error because Inner not provided as null (not thrown)
            string json = "{"Value0":"test0", "Value1":"test1"}";
            Extensions.Throws<Exception> (()=>serializer.Deserialize(json, typeof(TestInput)));
            //It should throw error because nothing were provided as null (not thrown)
            json = "{}";
            Extensions.Throws<Exception>(() => serializer.Deserialize(json, typeof(TestInput)));
            //this one is correct one (no problems here)
            json = "{"Value0":"test0", "Value1":"test1", "Inner":null}";
            Extensions.NotThrows(()=>serializer.Deserialize(json, typeof(TestInput)));
        }
    }

这两个测试不会通过。

一段时间后,我设法使其正常工作。将数据合同应用于我的课程,因此我只是重新分配了它们正在设置的值:

    private class RequireObjectPropertiesContractResolver : DefaultContractResolver
    {
        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            var contract = base.CreateObjectContract(objectType);
            contract.ItemRequired = Required.AllowNull;
            return contract;
        }
        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            var properties = base.CreateProperties(type, memberSerialization);
            foreach (var p in properties)
            {
                p.Required = Required.AllowNull;
            }
            return properties;
        }
    }

刚刚设置了所有属性,而不是合同本身。

相关内容

  • 没有找到相关文章

最新更新