Can Newtonsoft Json.NET skip serializing empty lists?

前端 未结 4 1928
花落未央
花落未央 2020-11-28 11:59

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:

相关标签:
4条回答
  • 2020-11-28 12:03

    Just to be pendantic commonorgarden, I structured the if test to be:

    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.

    0 讨论(0)
  • 2020-11-28 12:04

    In case you didn't find a solution to this, the answer is remarkably simple when you manage to track it down.

    If you are permitted to extend the original class then add a ShouldSerializePropertyName function 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):

    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 IContractResolver approach described on the the linked page is the way to go.

    0 讨论(0)
  • 2020-11-28 12:07

    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.

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 12:26

    Regarding David Jones' suggestion to use IContractResolver, this works for me to cover all IEnumerables variations without explicitly modifying the class that needs to be serialized:

    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);
    
    0 讨论(0)
提交回复
热议问题