开发者

JavaScriptSerializer. How to ignore property

开发者 https://www.devze.com 2023-03-06 00:30 出处:网络
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 contai开

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 contai开发者_C百科n any value?


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;
}


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
  }
  //...
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消