Newtonsoft.json: Option to ignore empty collections

Created on 6 Aug 2014  路  2Comments  路  Source: JamesNK/Newtonsoft.Json

I would like to have a serializer option to ignore empty collections. Right now I am using this:

https://gist.github.com/ejsmith/4d60f03d1f1d756c26a9

Most helpful comment

Yeah, that is why I am just checking for a Count property and using that if it exists. You have fast reflection code so this should be pretty cheap. I feel like this is a pretty core thing that would be nice to be included in the core serializer.

All 2 comments

I think this is something people can add themselves like you have do or with a JsonConverter.

By default Json.NET serializers through the IEnumerable interface, which makes it difficult to determine whether a collection is empty or not.

Yeah, that is why I am just checking for a Count property and using that if it exists. You have fast reflection code so this should be pretty cheap. I feel like this is a pretty core thing that would be nice to be included in the core serializer.

Was this page helpful?
0 / 5 - 0 ratings