Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Newtonsoft Json.NET skip serializing empty lists?

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": [] }

I would like to get: { "Identity": 123 }

That is, I would like to skip any lists, collections, arrays, or such things that are empty.

like image 361
Phill Campbell Avatar asked Jul 04 '12 00:07

Phill Campbell


People also ask

Is Newtonsoft JSON obsolete?

Yet Newtonsoft. Json was basically scrapped by Microsoft with the coming of . NET Core 3.0 in favor of its newer offering designed for better performance, System.

Can JSON serialize a list?

Json.NET has excellent support for serializing and deserializing collections of objects. To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for.

How does Newtonsoft JSON work?

Newtonsoft. Json uses reflection to get constructor parameters and then tries to find closest match by name of these constructor parameters to object's properties. It also checks type of property and parameters to match. If there is no match found, then default value will be passed to this parameterized constructor.

Is Newtonsoft JSON thread safe?

Correct, JsonSerializer is threadsafe. No state is shared while serializing but if you change a setting on the JsonSerializer while in the middle of serializing an object then those will automatically be used.


1 Answers

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.

like image 112
David Jones - iPushPull Avatar answered Sep 18 '22 19:09

David Jones - iPushPull