0

There is a method that allows skipping empty lists by creating a special method for every list property Can Newtonsoft Json.NET skip serializing empty lists?, but is there any method to skip all empty lists in the serialized objects graph?

PS I have to repeat this once again, the answer in the linked question is not suitable.

Community
  • 1
  • 1
user626528
  • 13,999
  • 30
  • 78
  • 146
  • Custom [serializer](http://stackoverflow.com/a/8031283/1997232). Or `ShouldSerializeXXX` method for each property containing such list (which you seems don't like). To me second one looks ok (feels same as e.g. adding line containing attribute to the property), just one line per property: `public bool ShouldSerializeXXX() => xxx?.Count > 0 ?? false;` – Sinatr Aug 09 '16 at 10:12
  • @mwilczynski, some people really can't read the questions. – user626528 Aug 09 '16 at 11:09
  • 1
    Use a custom contract resolver like the one in [How to define in JSON.net a default value to an empty list or dictionary](https://stackoverflow.com/questions/18471864). – dbc Aug 09 '16 at 14:33
  • @dbc, why don't make this an answer? – user626528 Aug 25 '16 at 16:03

0 Answers0