有没有像JsonIgnore那样的东西?

use*_*943 2 c# json.net

JsonIgnore属性可用于忽略序列化中的某些属性.我想知道是否可以做相反的事情?所以当有特殊属性时,JsonSerializer会忽略每个属性EXCEPT吗?

Aph*_*ion 7

就在这里.使用[JsonObjectAttribute]并使用MemberSerialization.OptIn参数标记类时,成员序列化是选择加入.然后标记您的成员[JsonProperty]以包含它们以进行序列化.

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

    // not serialized because mode is opt-in
    public string Department { get; set; }
}
Run Code Online (Sandbox Code Playgroud)