开发者

How to mark a property as non serializable for json?

开发者 https://www.devze.com 2023-02-13 16:54 出处:网络
As the title says, I want to mark a property as non serializable by the JavascriptSerializer. How ca开发者_StackOverflow社区n achive that ? Thanks in advance.I think you just want to apply the ScriptI

As the title says, I want to mark a property as non serializable by the JavascriptSerializer. How ca开发者_StackOverflow社区n achive that ? Thanks in advance.


I think you just want to apply the ScriptIgnoreAttribute:

[ScriptIgnore]
public string IgnoreThis { get; set; }


If you are needing this for ASP.NET Core or even before that, you should be using:

[JsonIgnore]

you'll need to reference:

using Newtonsoft.Json;


As of .NET 3.0, you can use DataContract instead of Serializable. With the DataContract though, you will need to either "opt-in" by marking the serializable fields with the DataMember attribute; or "opt-out" using the IgnoreDataMember.

The main difference between opt-in vs opt-out is that opt-out by default will only serialize public members, while opt-in will only serialize the marked members (regardless of protection level).

Note that if you are using Newtonsoft.Json for your serialization (like in my case), it supports DataContract as well.

0

精彩评论

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