Problem serializing classes

Oct 28, 2011 at 1:25 PM


I have a class consisting of properties only and need to serialize it. This is no problem so far and works fine with the JsonConvert.SerializeObject function.

But now it is important for me to NOT serialize properties that have not been given a value.I have tried to fix that by setting the NullValueHandling.ignore option of the SerializeObject settings. But this seems to work only for a part of the properties.

String Properties are indeed ignored, while numeric properties (like Integer or Long) will be serialized (and set to '0').

What am I doing wrong?


Thank you very much!

Regards, Tom



Oct 29, 2011 at 10:59 AM

OK, I was able to fix it.

At first, I found this Article:

The Article describes the usage of DefaultValueHandling.Ignore. I had already tried that before, and it did not work, so I was not surprised when it did not at the second attempt.

Then I realized I was still using the R2 version of JSON.NET and updated to R3. After that everything is working fine now.


I hope this is useful to someone...

Regards, Tom

Nov 2, 2011 at 6:28 AM

Thanks for posting it Tom! It was very helpful for me :)