如何使用 JSON.NET 序列化属性时将属性从接口继承到对象

本文关键字:属性 接口 继承 对象 何使用 JSON NET 序列化 | 更新日期: 2023-09-27 17:55:42

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using Newtonsoft.Json.Serialization;
using System.Linq;
using System.Reflection;
public interface IParent
{
    [JsonProperty]
    int Id {get;set;}
}
[JsonObject(MemberSerialization.OptIn)]
public class Parent : IParent
{
    public int Id { get;set; }  
    public string Name {get;set;}   
}
public class Serializer
{
    public static void Main()
    {
        var parent = new Parent() { Id = 1, Name ="Parent"};        
        var sb = new StringBuilder();
                var sw = new StringWriter(sb);
                var settings = new JsonSerializerSettings()
                       {
                           NullValueHandling = NullValueHandling.Ignore                            
                       };
            var output = JsonConvert.SerializeObject(parent, Formatting.None, settings);
                Console.WriteLine(output);
            Console.ReadKey();
    }
}

在上面的代码中,输出为 {}。是否可以序列化并获取输出为 {"Id":1}

如何使用 JSON.NET 序列化属性时将属性从接口继承到对象

这是个坏主意。

话虽如此,Newtonsoft 为您提供了一种更改序列化内容的方法:在这种情况下,您将子类DefaultContractResolver并覆盖CreateProperty

问题是,根据接口的属性决定何时应该选择加入序列化并不容易。首先,一个类可能会实现具有冲突序列化指令的多个接口。此外,将对象反序列化为声明为冲突接口的变量(例如)将不起作用。它很脆弱,也不安全(它允许外部代码指定实例显示哪些数据)。

如果您必须这样做,下面的代码适用于您的情况:

public class InterfaceContractResolver : DefaultContractResolver, IContractResolver
{
    public InterfaceContractResolver() : this(false) { }
    public InterfaceContractResolver(bool shareCache) : base (shareCache) {}
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        var interfaces = member.DeclaringType.GetInterfaces();
        foreach (var @interface in interfaces)
        {
            foreach (var interfaceProperty in @interface.GetProperties())
            {
                // This is weak: among other things, an implementation 
                // may be deliberately hiding an interface member
                if (interfaceProperty.Name == member.Name && interfaceProperty.MemberType == member.MemberType)
                {
                    if (interfaceProperty.GetCustomAttributes(typeof(JsonPropertyAttribute), true).Any())
                    {
                        property.Ignored = false;
                        return property;
                    }
                }
            }
        }
        return property;
    }
}

然后,在创建序列化程序时,在设置中向其传递解析程序的实例:

var settings = new JsonSerializerSettings()
{
    NullValueHandling = NullValueHandling.Ignore,
    ContractResolver = new InterfaceContractResolver(true)
};

看这里...我不认为这行得通。 对于实现接口的对象,将忽略接口上的 JsonProperty 属性。