如何从 json 序列化中排除特定类型
How to exclude specific type from json serialization
我正在将对我的 WCF Web 服务的所有请求(包括参数)记录到数据库中。我是这样做的:
- 创建一个 class WcfMethodEntry,它派生自 PostSharp 的方面 OnMethodBoundaryAspect,
- 用 WcfMethodEntry 属性注释所有 WCF 方法,
- 在 WcfMethodEntry 中,我使用 JsonConvert.SerializeObject 方法将方法参数序列化为 json 并将其保存到数据库中。
这工作正常,但有时参数非常大,例如自定义 class 带有几个字节数组,照片,指纹等。我想从中排除所有这些字节数组数据类型序列化,最好的方法是什么?
序列化示例 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 服务方法参数的 classes - 这也意味着我无法使用 JsonIgnore 属性。
对于这个特定的 属性,您可以只使用 [JsonIgnore]。
[JsonIgnore]
public Byte[] ByteArray { get; set; }
否则你也可以试试这个:Exclude property from serialization via custom attribute (json.net)
尝试使用 JsonIgnore
属性。
以下允许您排除要从结果 json 中排除的特定数据类型。它的使用和实现非常简单,改编自底部的 link。
你可以使用它,因为你不能改变实际的 类:
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"
}
可在此处找到更多信息:
我正在将对我的 WCF Web 服务的所有请求(包括参数)记录到数据库中。我是这样做的:
- 创建一个 class WcfMethodEntry,它派生自 PostSharp 的方面 OnMethodBoundaryAspect,
- 用 WcfMethodEntry 属性注释所有 WCF 方法,
- 在 WcfMethodEntry 中,我使用 JsonConvert.SerializeObject 方法将方法参数序列化为 json 并将其保存到数据库中。
这工作正常,但有时参数非常大,例如自定义 class 带有几个字节数组,照片,指纹等。我想从中排除所有这些字节数组数据类型序列化,最好的方法是什么?
序列化示例 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 服务方法参数的 classes - 这也意味着我无法使用 JsonIgnore 属性。
对于这个特定的 属性,您可以只使用 [JsonIgnore]。
[JsonIgnore]
public Byte[] ByteArray { get; set; }
否则你也可以试试这个:Exclude property from serialization via custom attribute (json.net)
尝试使用 JsonIgnore
属性。
以下允许您排除要从结果 json 中排除的特定数据类型。它的使用和实现非常简单,改编自底部的 link。
你可以使用它,因为你不能改变实际的 类:
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"
}
可在此处找到更多信息: