Attach to Process

serialization

Recently I ran into an issue where I needed to exclude a property from getting serialized using Json.NET. The easy answer is to add a [JsonIgnore] attribute to the property. The problem with doing that is it will also ignore the same property during deserialization. So I needed a solution that allows me to ignore a property using serialization, but still set that property's value during deserialization. Thankfully I found a blog post from 2013 that explains exactly how to do that. I would have wasted more hours searching for an answer had I not found this solution right away.

There's a little known feature of Json.NET that lets you determine at runtime whether or not to serialize a particular object member: On the object you're serializing, you have to define a public method named ShouldSerialize{MemberName} returning a boolean value. – Marius Schulz

Visit Original Post: Conditionally Serializing Fields and Properties with Json.NET

It was only after I found Marius' blog post that I then found the documentation talking about conditional property serialization on the Newtonsoft website.

This is one of the rare instances where I didn't find the answer in StackOverflow. It makes me grateful for the developers who are still cranking out blog posts and sharing solutions to problems on their personal blogs/websites.

#Bookmarks #JsonDotNet #DotNet #Serialization

Discuss... or leave a comment below.