Json.net有没有指定只序列化想要的属性的方法?或者根据绑定标志(例如仅声明)序列化某些属性?目前我正在使用JObject.FromObject(MainObj.SubObj);获取SubObj的所有属性,它是一个遵循ISubObject接口的类的实例:
public interface ISubObject
{
}
public class ParentSubObject : ISubObject
{
public string A { get; set; }
}
public class SubObjectWithOnlyDeclared : ParentSubObject
{
[JsonInclude] // This is fake, but what I am wishing existed
public string B { get; set; }
[JsonInclude] // This is fake, but what I am wishing existed
public string C { get; set; }
}
public class NormalSubObject: ParentSubObject
{
public string B { get; set; }
}
如果 MainObj.SubObj
是一个NormalSubObject
,它会序列化 A 和 B,但如果它是SubObjectWithOnlyDeclared
,它只会序列化 B 和 C,并忽略父属性。
[JsonIgnore]
? - Eser