JavaScriptSerializer. How to ignore property
Asked Answered
S

3

21

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?

Selhorst answered 13/5, 2011 at 19:30 Comment(0)
C
8

Best possible answer I have is to make your own JavaScriptConverter and parse the property based on your own condition(s).

public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
  //...
  if (!object.ReferenceEquals(dictionary["MyProperty"],null)){
    // My Code
  }
  //...
}
Culmination answered 13/5, 2011 at 19:41 Comment(2)
I decided to use a method that will return a dictionary which contains only those objects that need to be serialized. And then serialize that dictionary.Selhorst
@Agzam: Overkill? Debatable. If you wanted full control over the serialization of the objects, this is the route to go. The JavaScriptSerializer class returns a native dictionary anyways, this just gives you the freedom of placing it in your own custom object. When I'm dealing with abstract information, i'll usually use the dynamic keyword and parse the data myself.Culmination
F
42

https://learn.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;
}
Folly answered 5/9, 2015 at 17:30 Comment(2)
I haven't upvoted this because although it helped me out it doesn't answer the asked question.Led
Does it work on properties that were implemented for an Interface? since I still get an error in JSON de-serialize? Since it didn't work in my caseBrittanybritte
C
8

Best possible answer I have is to make your own JavaScriptConverter and parse the property based on your own condition(s).

public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
  //...
  if (!object.ReferenceEquals(dictionary["MyProperty"],null)){
    // My Code
  }
  //...
}
Culmination answered 13/5, 2011 at 19:41 Comment(2)
I decided to use a method that will return a dictionary which contains only those objects that need to be serialized. And then serialize that dictionary.Selhorst
@Agzam: Overkill? Debatable. If you wanted full control over the serialization of the objects, this is the route to go. The JavaScriptSerializer class returns a native dictionary anyways, this just gives you the freedom of placing it in your own custom object. When I'm dealing with abstract information, i'll usually use the dynamic keyword and parse the data myself.Culmination
K
3

I used internal instead of public properties and it worked for me

Kohinoor answered 13/5, 2015 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.