json 从遗留属性名称反序列化

时间:2023-04-26
本文介绍了json 从遗留属性名称反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何设置 Newtonsoft.Json 以使用旧成员名称反序列化对象,但使用当前成员名称对其进行序列化?

How can I setup Newtonsoft.Json to deserialize an object using legacy member names but serialize it using the current member name?

要求是从正在序列化/反序列化的类中删除过时的成员.

这是一个需要序列化和反序列化的示例对象.我给了一个属性一个属性,其中包含一个名称列表,它可能在过去被序列化过.

Here's an example object that needs to be serialized and deserialized. I've given a property an attribute containing a list of names that it may have been serialized under in the past.

[DataContract]
class TestObject {
    [LegacyDataMemberNames("alpha", "omega")]
    [DataMember(Name = "a")]
    public int A { get; set; }
}

我想始终使用名称a"进行 json 序列化,但能够从任何旧名称(包括alpha"和omega"以及当前名称a")反序列化为一个属性

I'd like to json serialize always using name "a" but be able to deserialize to the one property from any legacy name including "alpha" and "omega" as well as the current name, "a"

推荐答案

这可以通过自定义 IContractResolver 通过扩展 DefaultContractResolver:

This can be done with a custom IContractResolver created by extending DefaultContractResolver:

[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class LegacyDataMemberNamesAttribute : Attribute
{
    public LegacyDataMemberNamesAttribute() : this(new string[0]) { }

    public LegacyDataMemberNamesAttribute(params string[] names) { this.Names = names; }

    public string [] Names { get; set; }
}

public class LegacyPropertyResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var properties = base.CreateProperties(type, memberSerialization);

        for (int i = 0, n = properties.Count; i < n; i++)
        {
            var property = properties[i];
            if (!property.Writable)
                continue;
            var attrs = property.AttributeProvider.GetAttributes(typeof(LegacyDataMemberNamesAttribute), true);
            if (attrs == null || attrs.Count == 0)
                continue;
            // Little kludgy here: use MemberwiseClone to clone the JsonProperty.
            var clone = property.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            foreach (var name in attrs.Cast<LegacyDataMemberNamesAttribute>().SelectMany(a => a.Names))
            {
                if (properties.Any(p => p.PropertyName == name))
                {
                    Debug.WriteLine("Duplicate LegacyDataMemberNamesAttribute: " + name);
                    continue;
                }
                var newProperty = (JsonProperty)clone.Invoke(property, new object[0]);
                newProperty.Readable = false;
                newProperty.PropertyName = name;
                properties.Add(newProperty);
            }
        }

        return properties;
    }
}

然后将属性添加到您的类型中,如问题所示:

Then add attributes to your type as shown in the question:

[DataContract]
class TestObject
{
    [LegacyDataMemberNames("alpha", "omega")]
    [DataMember(Name = "a")]
    public int A { get; set; }
}

构造和配置 LegacyPropertyResolver 的实例,例如如下:

Construct and configure an instance of LegacyPropertyResolver, e.g. as follows:

static IContractResolver legacyResolver = new LegacyPropertyResolver 
{ 
    // Configure as required, e.g. 
    // NamingStrategy = new CamelCaseNamingStrategy() 
};

然后在settings:

var settings = new JsonSerializerSettings { ContractResolver = legacyResolver };
var deserialized = JsonConvert.DeserializeObject<TestObject>(jsonString, settings);

注意事项:

  • 这个实现不需要类有明确的数据契约属性注解.如果您愿意,可以添加该限制.

  • This implementation doesn't require that the class have explicit data contract attribute annotation. You could add that restriction, if you prefer.

您应该缓存和重用合约解析器的实例 以获得最佳性能.

You should cache and reuse instances of contract resolvers for best performance.

演示小提琴这里.

这篇关于json 从遗留属性名称反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:反序列化具有以数字开头的某些属性名称的 json 下一篇:从流中加载多个连接的 JSON 对象

相关文章