是否有与JsonIgnore相反的东西? [英] Is there something like the opposite of a JsonIgnore?

查看:77
本文介绍了是否有与JsonIgnore相反的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JsonIgnore属性可用于忽略序列化中的某些属性.我想知道是否可以做相反的事情?因此,当JsonSerializer上具有特殊属性时,它将忽略除每个属性以外的所有属性?

The JsonIgnore attribute can be used to ignore certain properties in serialization. I was wondering if it is possible to do the opposite of that? So a JsonSerializer would ignore every property EXCEPT when there is a special attribute on it?

推荐答案

是的.当您用[JsonObjectAttribute]标记类并传递MemberSerialization.OptIn参数时,成员序列化选择加入.然后用[JsonProperty]标记您的成员以包括它们以进行序列化.

Yes there is. When you mark your class with [JsonObjectAttribute] and pass the MemberSerialization.OptIn parameter, member serialization is opt-in. Then mark your members with [JsonProperty] to include them for serialization.

[JsonObject(MemberSerialization.OptIn)]
public class Person
{
    [JsonProperty]
    public string Name { get; set; }

    // not serialized because mode is opt-in
    public string Department { get; set; }
}

这篇关于是否有与JsonIgnore相反的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆