【问题标题】:c# json.net custom serialization of subobjectsc# json.net 自定义子对象序列化
【发布时间】:2013-08-14 15:30:55
【问题描述】:

我正在使用 JSON.NET 将一个类序列化为 JSON。该类包含一个由项目列表组成的属性,我想以自定义方式序列化项目本身(通过使用自定义的 ContractResolver 仅动态包含某些属性)。所以基本上我想以标准方式序列化父类本身,使用 DefaultContractResolver,但以自定义方式序列化这个属性,使用我自己的 ContractResolver。

JSON.NET 有可能允许这样做的方法,但文档相当粗略。任何帮助将不胜感激。

【问题讨论】:

    标签: json.net


    【解决方案1】:

    我使用 ContractResolver 解决了这个问题。我要序列化的对象列表是异构的,所以我必须向它传递两个参数,一个要序列化的属性列表,以及一个属性列表适用的类型列表。所以它看起来像这样:

        public class DynamicContractResolver : DefaultContractResolver
        {
            private List<string> mPropertiesToSerialize = null;
            private List<string> mItemTypeNames = new List<string>();
    
            public DynamicContractResolver( List<string> propertiesToSerialize,
                List<string> itemTypeNames )
            {
                this.mPropertiesToSerialize = propertiesToSerialize;
                this.mItemTypeNames = itemTypeNames;
            }
    
            protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization )
            {
                IList<JsonProperty> properties = base.CreateProperties( type, memberSerialization );
                if( this.mItemTypeNames.Contains( type.Name ) )
                    properties = properties.Where( p => mPropertiesToSerialize.Contains( p.PropertyName ) ).ToList();
                return properties;
            }
        }
    

    它是这样称呼的:

                DynamicContractResolver contractResolver = new DynamicContractResolver( propsToSerialize, GetItemTypeNames() );
                json = JsonConvert.SerializeObject( this, Formatting.None,
                    new JsonSerializerSettings { ContractResolver = contractResolver } );
    

    GetItemTypeNames() 对列表中我要序列化的每个项目调用 GetType().Name 并将它们清楚地写入列表。

    抱歉,我最初的问题含糊不清,措辞不当,如果有人有更好的解决方案,我当然不会接受这个。

    【讨论】:

      【解决方案2】:

      这是一个更好的版本。它将类型名称与属性相关联,因此您可以指定希望在每个级别上序列化的属性。字典的键是类型名称;该值是要为每种类型序列化的属性列表。

      class PropertyContractResolver : DefaultContractResolver
      {
          public PropertyContractResolver( Dictionary<string, IEnumerable<string>> propsByType )  
          {
              PropertiesByType = propsByType;
          }
      
          protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization )
          {
              IList<JsonProperty> properties = base.CreateProperties( type, memberSerialization );
              if( this.PropertiesByType.ContainsKey( type.Name ) )
              {
                  IEnumerable<string> propsToSerialize = this.PropertiesByType[ type.Name ];
                  properties = properties.Where( p => propsToSerialize.Contains( p.PropertyName ) ).ToList();
              }
              return properties;
          }
      
          private Dictionary<string, IEnumerable<string>> PropertiesByType
          {
              get;
              set;
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        相关资源
        最近更新 更多