Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScriptSerializer. How to ignore property

Tags:

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?

like image 386
iLemming Avatar asked May 13 '11 19:05

iLemming


People also ask

How do I ignore JSON?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

What is the use of JavaScriptSerializer in C#?

Json. The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server.


1 Answers

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;
}
like image 196
akdora Avatar answered Sep 21 '22 05:09

akdora