【问题标题】:How to exclude specific type from json serialization如何从 json 序列化中排除特定类型
【发布时间】:2023-04-05 07:04:01
【问题描述】:

我正在将所有对我的 WCF Web 服务的请求(包括参数)记录到数据库中。我就是这样做的:

  • 创建一个派生自 PostSharp 方面 OnMethodBoundaryAspect 的 WcfMethodEntry 类,
  • 使用 WcfMethodEntry 属性注释所有 WCF 方法,
  • 在 WcfMethodEntry 中,我使用 JsonConvert.SerializeObject 方法将方法参数序列化为 json 并将其保存到数据库中。

这没问题,但有时参数很大,例如一个自定义类,其中包含几个字节数组,包括照片、指纹等。我想从序列化中排除所有这些字节数组数据类型,什么是最好的方法?

序列化 json 示例:

[
   {
      "SaveCommand":{
         "Id":5,
         "PersonalData":{
            "GenderId":2,
            "NationalityCode":"DEU",
            "FirstName":"John",
            "LastName":"Doe",
         },
         "BiometricAttachments":[
            {
               "BiometricAttachmentTypeId":1,
               "Parameters":null,
               "Content":"large Base64 encoded string"
            }
         ]
      }
   }
]

期望的输出:

[
   {
      "SaveCommand":{
         "Id":5,
         "PersonalData":{
            "GenderId":2,
            "NationalityCode":"DEU",
            "FirstName":"John",
            "LastName":"Doe",
         },
         "BiometricAttachments":[
            {
               "BiometricAttachmentTypeId":1,
               "Parameters":null,
               "Content":"..."
            }
         ]
      }
   }
]

编辑:我不能更改用作 Web 服务方法参数的类 - 这也意味着我不能使用 JsonIgnore 属性。

【问题讨论】:

  • 是否可以在序列化之前编辑你的 c# 对象?
  • @Alexander 嗯...我不知道,你的意思是我可以在反射的帮助下为对象添加 JsonIgnore 属性?或者只是清除这些属性?大概……
  • @sventevit - [JsonIgnore] 会解决它。
  • @sventevit 我考虑过财产清算,但 JsonIgnore 看起来是更好的解决方案 :)

标签: c# .net wcf json.net postsharp


【解决方案1】:

以下允许您排除要从生成的 json 中排除的特定数据类型。它的使用和实现非常简单,是根据底部的链接改编的。

你可以使用它,因为你不能改变实际的类:

public class DynamicContractResolver : DefaultContractResolver
{

    private Type _typeToIgnore;
    public DynamicContractResolver(Type typeToIgnore)
    {
        _typeToIgnore = typeToIgnore;
    }

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);

        properties = properties.Where(p => p.PropertyType != _typeToIgnore).ToList();

        return properties;
    }
}

用法和样例:

public class MyClass
{
    public string Name { get; set; }
    public byte[] MyBytes1 { get; set; }
    public byte[] MyBytes2 { get; set; }
}

MyClass m = new MyClass
{
    Name = "Test",
    MyBytes1 = System.Text.Encoding.Default.GetBytes("Test1"),
    MyBytes2 = System.Text.Encoding.Default.GetBytes("Test2")
};



JsonConvert.SerializeObject(m, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new DynamicContractResolver(typeof(byte[])) });

输出:

{
  "Name": "Test"
}

更多信息可以在这里找到:

Reducing Serialized JSON Size

【讨论】:

  • 哈!很高兴为您提供帮助
【解决方案2】:

您可以将 [JsonIgnore] 用于此特定属性。

[JsonIgnore]
public Byte[] ByteArray { get; set; }

否则你也可以试试这个:Exclude property from serialization via custom attribute (json.net)

【讨论】:

  • 我无法使用 JsonIgnore 属性,请查看我的编辑。我会检查你的链接,谢谢。
【解决方案3】:

尝试使用JsonIgnore 属性。

【讨论】:

  • 我无法使用 JsonIgnore 属性,请查看我的编辑。
猜你喜欢
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 2019-02-04
  • 1970-01-01
相关资源
最近更新 更多