C# Newtonsoft Json.NET 可以跳过序列化空列表吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11320968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Newtonsoft Json.NET skip serializing empty lists?
提问by Phill Campbell
I am trying to serialize some legacy objects that "lazy creates" various lists. I can not change the legacy behavior.
我正在尝试序列化一些“懒惰创建”各种列表的遗留对象。我无法改变遗留行为。
I have boiled it down to this simple example:
我把它归结为这个简单的例子:
public class Junk
{
protected int _id;
[JsonProperty( PropertyName = "Identity" )]
public int ID
{
get
{
return _id;
}
set
{
_id = value;
}
}
protected List<int> _numbers;
public List<int> Numbers
{
get
{
if( null == _numbers )
{
_numbers = new List<int>( );
}
return _numbers;
}
set
{
_numbers = value;
}
}
}
class Program
{
static void Main( string[] args )
{
Junk j = new Junk( ) { ID = 123 };
string newtonSoftJson = JsonConvert.SerializeObject( j, Newtonsoft.Json.Formatting.Indented );
Console.WriteLine( newtonSoftJson );
}
}
The current results are: { "Identity": 123, "Numbers": [] }
当前的结果是:{ "Identity": 123, "Numbers": [] }
I would like to get: { "Identity": 123 }
我想得到:{“身份”:123}
That is, I would like to skip any lists, collections, arrays, or such things that are empty.
也就是说,我想跳过任何列表、集合、数组或此类空的东西。
采纳答案by David Jones - iPushPull
In case you didn't find a solution to this, the answeris remarkably simple when you manage to track it down.
如果您没有找到解决方案,那么当您设法追踪它时,答案非常简单。
If you are permitted to extend the original class then add a ShouldSerializePropertyNamefunction to it. This should return a Boolean indicating whether or not that property should be serialized for the current instance of the class. In your example this might look like this (not tested but you should get the picture):
如果您被允许扩展原始类,则向其添加一个ShouldSerializePropertyName函数。这应该返回一个布尔值,指示是否应该为类的当前实例序列化该属性。在您的示例中,这可能如下所示(未经测试,但您应该得到图片):
public bool ShouldSerializeNumbers()
{
return _numbers.Count > 0;
}
This approach works for me (albeit in VB.NET). If you're not allowed to modify the original class then the IContractResolverapproach described on the the linked page is the way to go.
这种方法对我有用(尽管在 VB.NET 中)。如果您不允许修改原始类,那么IContractResolver链接页面上描述的方法就是要走的路。
回答by Eryn
Just to be pendantic commonorgarden, I structured the if test to be:
只是为了挂起 commonorgarden,我将 if 测试构建为:
public bool ShouldSerializecommunicationmethods()
{
if (communicationmethods != null && communicationmethods.communicationmethod != null && communicationmethods.communicationmethod.Count > 0)
return true;
else
return false;
}
As an empty list will often be null too. Thanks for posting the solution. ATB.
由于空列表通常也为空。感谢您发布解决方案。ATB。
回答by J Bryan Price
Regarding David Jones' suggestion to use IContractResolver, this works for me to cover all IEnumerablesvariations without explicitly modifying the class that needs to be serialized:
关于大卫琼斯的使用建议IContractResolver,这对我有用,IEnumerables无需显式修改需要序列化的类即可涵盖所有变体:
public class ShouldSerializeContractResolver : DefaultContractResolver
{
public static readonly ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) {
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (property.PropertyType != typeof(string)) {
if (property.PropertyType.GetInterface(nameof(IEnumerable)) != null)
property.ShouldSerialize =
instance => (instance?.GetType().GetProperty(property.PropertyName).GetValue(instance) as IEnumerable<object>)?.Count() > 0;
}
return property;
}
}
Then I build it into my settings object:
然后我将它构建到我的设置对象中:
static JsonSerializerSettings JsonSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = ShouldSerializeContractResolver.Instance,
};
and use it like this:
并像这样使用它:
JsonConvert.SerializeObject(someObject, JsonSettings);
回答by Buvy
Bryan you are most the way there you don't need the overhead of the instance variable and you need to trap both field & member instances plus I would not run the count operation which requires the enumerable to exhaust the entire collection you can simply run the MoveNext() function.
布赖恩你是最不需要实例变量的开销的方式,你需要捕获字段和成员实例加上我不会运行计数操作,这需要枚举来耗尽整个集合,你可以简单地运行MoveNext() 函数。
public class IgnoreEmptyEnumerableResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member,
MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.PropertyType != typeof(string) &&
typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
property.ShouldSerialize = instance =>
{
IEnumerable enumerable = null;
// this value could be in a public field or public property
switch (member.MemberType)
{
case MemberTypes.Property:
enumerable = instance
.GetType()
.GetProperty(member.Name)
?.GetValue(instance, null) as IEnumerable;
break;
case MemberTypes.Field:
enumerable = instance
.GetType()
.GetField(member.Name)
.GetValue(instance) as IEnumerable;
break;
}
return enumerable == null ||
enumerable.GetEnumerator().MoveNext();
// if the list is null, we defer the decision to NullValueHandling
};
}
return property;
}
}

