JavaScriptSerializer. How to ignore property

iLemming picture iLemming · May 13, 2011 · Viewed 16.2k times · Source

I know about ScriptIgnoreAttribute.

But what if I want to ignore a property based on criteria. For example how to ignore a nullable property on serialization only if it's null and doesn't contain any value?

Answer

akdora picture akdora · Sep 5, 2015

https://docs.microsoft.com/dotnet/api/system.web.script.serialization.scriptignoreattribute

Use [ScriptIgnore]

using System;
using System.Web.Script.Serialization;

public class Group
{
    // The JavaScriptSerializer ignores this field.
    [ScriptIgnore]
    public string Comment;

    // The JavaScriptSerializer serializes this field.
    public string GroupName;
}