How to mark a property as non serializable for json?

前端 未结 3 1839
悲哀的现实
悲哀的现实 2021-02-07 07:44

As the title says, I want to mark a property as non serializable by the JavascriptSerializer. How can achive that ? Thanks in advance.

相关标签:
3条回答
  • 2021-02-07 08:01

    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;
    
    0 讨论(0)
  • 2021-02-07 08:05

    I think you just want to apply the ScriptIgnoreAttribute:

    [ScriptIgnore]
    public string IgnoreThis { get; set; }
    
    0 讨论(0)
  • 2021-02-07 08:08

    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 讨论(0)
提交回复
热议问题