Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从json序列化中排除特定类型_C#_.net_Wcf_Json.net_Postsharp - Fatal编程技术网

C# 如何从json序列化中排除特定类型

C# 如何从json序列化中排除特定类型,c#,.net,wcf,json.net,postsharp,C#,.net,Wcf,Json.net,Postsharp,我将所有对WCF web服务的请求(包括参数)记录到数据库中。我就是这样做的: 创建一个类WcfMethodEntry,该类派生自PostSharp的aspect on MethodBoundaryAspect 使用WcfMethodEntry属性注释所有WCF方法 在WcfMethodEntry中,我使用JsonConvert.SerializeObject方法将方法参数序列化为json,并将其保存到数据库中 这可以正常工作,但有时参数相当大,例如,一个自定义类包含两个字节数组,带有照片、

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

  • 创建一个类WcfMethodEntry,该类派生自PostSharp的aspect on MethodBoundaryAspect
  • 使用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属性。

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


否则,您也可以尝试这样做:

尝试使用
JsonIgnore
属性。

以下内容允许您从生成的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;
    }
}
输出:

{
  "Name": "Test"
}
更多信息可在此处找到:


是否可以在序列化之前编辑c#对象?@Alexander-hmm。。。我不知道,你的意思是我可以在反射的帮助下向对象添加JsonIgnore属性?还是干脆清理那些财产?可能…@sventevit-
[JsonIgnore]
会解决这个问题。@sventevit我考虑过清除属性,但JsonIgnore看起来是更好的解决方案:)我不能使用JsonIgnore属性,请查看我的编辑。我会检查你的链接,谢谢。我不能使用JsonIgnore属性,请查看我的edit.ha!很乐意帮忙
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"
}