<bdo id='wOwxn'></bdo><ul id='wOwxn'></ul>

  1. <small id='wOwxn'></small><noframes id='wOwxn'>

    <tfoot id='wOwxn'></tfoot>

  2. <i id='wOwxn'><tr id='wOwxn'><dt id='wOwxn'><q id='wOwxn'><span id='wOwxn'><b id='wOwxn'><form id='wOwxn'><ins id='wOwxn'></ins><ul id='wOwxn'></ul><sub id='wOwxn'></sub></form><legend id='wOwxn'></legend><bdo id='wOwxn'><pre id='wOwxn'><center id='wOwxn'></center></pre></bdo></b><th id='wOwxn'></th></span></q></dt></tr></i><div id='wOwxn'><tfoot id='wOwxn'></tfoot><dl id='wOwxn'><fieldset id='wOwxn'></fieldset></dl></div>

      <legend id='wOwxn'><style id='wOwxn'><dir id='wOwxn'><q id='wOwxn'></q></dir></style></legend>

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

      时间:2023-08-22

        <small id='ux77I'></small><noframes id='ux77I'>

        <tfoot id='ux77I'></tfoot>
          <tbody id='ux77I'></tbody>
        <i id='ux77I'><tr id='ux77I'><dt id='ux77I'><q id='ux77I'><span id='ux77I'><b id='ux77I'><form id='ux77I'><ins id='ux77I'></ins><ul id='ux77I'></ul><sub id='ux77I'></sub></form><legend id='ux77I'></legend><bdo id='ux77I'><pre id='ux77I'><center id='ux77I'></center></pre></bdo></b><th id='ux77I'></th></span></q></dt></tr></i><div id='ux77I'><tfoot id='ux77I'></tfoot><dl id='ux77I'><fieldset id='ux77I'></fieldset></dl></div>
      • <legend id='ux77I'><style id='ux77I'><dir id='ux77I'><q id='ux77I'></q></dir></style></legend>
              <bdo id='ux77I'></bdo><ul id='ux77I'></ul>

                本文介绍了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 从遗留属性名称反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                相关文章

                最新文章

                  • <bdo id='LZm6E'></bdo><ul id='LZm6E'></ul>

                1. <i id='LZm6E'><tr id='LZm6E'><dt id='LZm6E'><q id='LZm6E'><span id='LZm6E'><b id='LZm6E'><form id='LZm6E'><ins id='LZm6E'></ins><ul id='LZm6E'></ul><sub id='LZm6E'></sub></form><legend id='LZm6E'></legend><bdo id='LZm6E'><pre id='LZm6E'><center id='LZm6E'></center></pre></bdo></b><th id='LZm6E'></th></span></q></dt></tr></i><div id='LZm6E'><tfoot id='LZm6E'></tfoot><dl id='LZm6E'><fieldset id='LZm6E'></fieldset></dl></div>
                  <tfoot id='LZm6E'></tfoot>

                    <small id='LZm6E'></small><noframes id='LZm6E'>

                    <legend id='LZm6E'><style id='LZm6E'><dir id='LZm6E'><q id='LZm6E'></q></dir></style></legend>