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

  1. <legend id='vZkxp'><style id='vZkxp'><dir id='vZkxp'><q id='vZkxp'></q></dir></style></legend>
      <bdo id='vZkxp'></bdo><ul id='vZkxp'></ul>
    <tfoot id='vZkxp'></tfoot>

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

      如何使用 JsonConverter 仅序列化类的继承属性

      时间:2023-08-23
      • <tfoot id='iJvbt'></tfoot>

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

              <tbody id='iJvbt'></tbody>

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

              <i id='iJvbt'><tr id='iJvbt'><dt id='iJvbt'><q id='iJvbt'><span id='iJvbt'><b id='iJvbt'><form id='iJvbt'><ins id='iJvbt'></ins><ul id='iJvbt'></ul><sub id='iJvbt'></sub></form><legend id='iJvbt'></legend><bdo id='iJvbt'><pre id='iJvbt'><center id='iJvbt'></center></pre></bdo></b><th id='iJvbt'></th></span></q></dt></tr></i><div id='iJvbt'><tfoot id='iJvbt'></tfoot><dl id='iJvbt'><fieldset id='iJvbt'></fieldset></dl></div>
                <bdo id='iJvbt'></bdo><ul id='iJvbt'></ul>
                本文介绍了如何使用 JsonConverter 仅序列化类的继承属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试仅使用 json.net 序列化类的继承属性.我知道 [JsonIgnore] 属性,但我只想在某些情况下忽略它们,所以我使用了自定义 JsonConverter.

                I'm trying to serialize only the inherited properties of a class using json.net. I'm aware of the [JsonIgnore] attribute, but I only want to do ignore them on certain occasion, so I used a custom JsonConverter instead.

                这是我的课:

                public class EverythingButBaseJsonConverter : JsonConverter
                {
                    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                    {
                        // Find properties of inherited class
                        var classType = value.GetType();
                        var classProps = classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
                
                        // Remove the overrided properties
                        classProps.RemoveAll(t =>
                        {
                            var getMethod = t.GetGetMethod(false);
                            return (getMethod.GetBaseDefinition() != getMethod);
                        });
                
                        // Get json data
                        var o = (JObject)JToken.FromObject(value);
                
                        // Write only properties from inhertied class
                        foreach (var p in o.Properties().Where(p => classProps.Select(t => t.Name).Contains(p.Name)))
                            p.WriteTo(writer);
                    }
                
                    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                    {
                        throw new NotImplementedException("");
                    }
                
                    public override bool CanRead
                    {
                        get { return false; }
                    }
                
                    public override bool CanConvert(Type objectType)
                    {
                        return true;
                    }
                }
                

                当做一个简单的 o.WriteTo(writer);它给出与不使用转换器相同的结果.遍历属性并在属性上使用 WriteTo 时,它适用于基本类型(int、string 等),但我遇到了集合问题.

                When doing a simple o.WriteTo(writer); it give the same result as not using a converter. When iterating through properties and using WriteTo on the properties, it works fine for base type (int, string, etc), but I'm having problem with collections.

                预期:

                {
                  "Type": 128,
                  "Time": [
                    1,
                    2,
                  ],
                  "Pattern": 1,
                  "Description": ""
                }
                

                得到:

                  "Type": 128,
                  "Time": [
                    1,
                    2,
                  ]"Pattern": 1,
                  "Description": ""
                

                如您所见,该集合缺少,"和结束行部分.我也缺少整个对象的全局 { }.

                As you can see, the collection is missing the "," and endline portion. I'm also missing the global { } for the whole object.

                我做事的方式正确吗?有没有更简单的方法来获得我想要的结果?

                I am doing things the correct way? Is there an easier way to get the result I want?

                推荐答案

                不确定您的代码为什么不起作用(可能是 Json.NET 错误?).相反,您可以从 JObject 中删除不需要的属性,并在一次调用中编写整个内容:

                Not sure why your code doesn't work (maybe a Json.NET bug?). Instead, you can remove the properties you don't want from the JObject and write the entire thing in one call:

                    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                    {
                        // Find properties of inherited class
                        var classType = value.GetType();
                        var classProps = classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
                
                        // Remove the overrided properties
                        classProps.RemoveAll(t =>
                        {
                            var getMethod = t.GetGetMethod(false);
                            return (getMethod.GetBaseDefinition() != getMethod);
                        });
                
                        // Get json data
                        var o = (JObject)JToken.FromObject(value);
                
                        // Remove all base properties
                        foreach (var p in o.Properties().Where(p => !classProps.Select(t => t.Name).Contains(p.Name)).ToList())
                            p.Remove();
                
                        o.WriteTo(writer);
                    }
                

                或者,您可以创建自己的 合约解析器并过滤掉基础属性和成员:

                Alternatively, you could create your own contract resolver and filter out base properties and members:

                public class EverythingButBaseContractResolver : DefaultContractResolver
                {
                    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
                    {
                        if (member.ReflectedType != member.DeclaringType)
                            return null;
                        if (member is PropertyInfo)
                        {
                            var getMethod = ((PropertyInfo)member).GetGetMethod(false);
                            if (getMethod.GetBaseDefinition() != getMethod)
                                return null;
                        }
                        var property = base.CreateProperty(member, memberSerialization);
                        return property;
                    }
                }
                

                然后像这样使用它:

                        var settings = new JsonSerializerSettings { ContractResolver = new EverythingButBaseContractResolver() };
                        var json = JsonConvert.SerializeObject(rootObject, Formatting.Indented, settings);
                

                这篇关于如何使用 JsonConverter 仅序列化类的继承属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:当值可以是对象或空数组时反序列化 JSON 下一篇:使用 JSON.net,如何防止在基类上下文中使用派生

                相关文章

                最新文章

                  <bdo id='tzWYB'></bdo><ul id='tzWYB'></ul>
                <tfoot id='tzWYB'></tfoot>

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

                1. <legend id='tzWYB'><style id='tzWYB'><dir id='tzWYB'><q id='tzWYB'></q></dir></style></legend>

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