如何在派生类中设置Json.Net JsonProperty属性的一部分,而不替换基础中的JsonProperty?

时间:2019-03-21 16:51:16

标签: c# json.net

我试图在基类中设置一个特定的JsonProperty(名称),但还要在派生类中设置另一个(不相关的)JsonProperty。不幸的是,这似乎在序列化和反序列化期间不起作用。派生类中的JsonProperty完全替换了基类中的JsonProperty。

在序列化和反序列化期间,如何仍然可以在派生类中使用JsonProperty,并仍然尊重基类中的JsonProperty?

我有这个基类:

public class BaseNode
{
    [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)]
    public string _sNodeId { get; set; }

    [JsonProperty(PropertyName = "parentid")]
    [DisplayName("Parent Folder")]
    [DefaultValue("000000000000000000000000")]
    [MaxLength(24)]
    [MinLength(24)]
    public virtual string _sNodeParentId { get; set; }
}

该类从其派生:

public class BaseBoardNode : BaseNode
{
    [JsonProperty(PropertyName = "name")]
    [DisplayName("Name")]
    [Description("Logical name for board")]
    [DefaultValue("newboard")]
    [MinLength(1)]
    [MaxLength(128)]
    public virtual string _sNodeName { get; set; }

    [JsonProperty(PropertyName = "ip")]
    [DisplayName("IP")]
    [Description("IP(v4) to use to sync to this board")]
    [DefaultValue("192.168.1.1")]
    [RegularExpression("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")]
    public virtual string _sNodeIP { get; set; }

    [JsonProperty(PropertyName = "port")]
    [DisplayName("Port")]
    [Description("TCP port to use to sync to this board")]
    [DefaultValue(8080)]
    [MinLength(0)]
    [MaxLength(60000)]
    public virtual int _iNodePort { get; set; }

    [JsonProperty(PropertyName = "devicefamily", Required = Required.Default)]
    [DisplayName("Device Family (Read-Only)")]
    [Description("Device family of board (read-only)")]
    public string _sDeviceFamily { get; set; }
}

然后我有2个最终的派生类(可能以后再讲):

public class DefaultingBoard : BaseBoardNode
{
    [JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Populate)]
    public override string _sNodeParentId { get => base._sNodeParentId; set => base._sNodeParentId = value; }

    [JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Populate)]
    public override string _sNodeName { get => base._sNodeName; set => base._sNodeName = value; }

    [JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Populate)]
    public override string _sNodeIP { get => base._sNodeIP; set => base._sNodeIP = value; }

    [JsonProperty(Required = Required.DisallowNull, DefaultValueHandling = DefaultValueHandling.Populate)]
    public override int _iNodePort { get => base._iNodePort; set => base._iNodePort = value; }
}

public class NonDefaultingBoard : BaseBoardNode
{
    [JsonProperty(Required = Required.Always)]
    public override string _sNodeParentId { get => base._sNodeParentId; set => base._sNodeParentId = value; }

    [JsonProperty(Required = Required.Always)]
    public override string _sNodeName { get => base._sNodeName; set => base._sNodeName = value; }

    [JsonProperty(Required = Required.Always)]
    public override string _sNodeIP { get => base._sNodeIP; set => base._sNodeIP = value; }

    [JsonProperty(Required = Required.Always)]
    public override int _iNodePort { get => base._iNodePort; set => base._iNodePort = value; }
}

当我序列化DefaultingBoard时,我得到了:

{
    "_sNodeParentId": "000000000000000000000000",
    "_sNodeName": "newboard",
    "_sNodeIP": "192.168.1.1",
    "_iNodePort": 8080,
    "devicefamily": null,
    "id": "5c93b4b33485788504fcbffb"
}

而不是期望的结果:

{
    "parentid": "000000000000000000000000",
    "name": "newboard",
    "ip": "192.168.1.1",
    "port": 8080,
    "devicefamily": null,
    "id": "5c93b4b33485788504fcbffb"
}

是否有一种直接的方法来获得所需的结果(即如何使JsonProperty.PropertyName不被覆盖或以某种方式从基础中获取)?

1 个答案:

答案 0 :(得分:0)

所以我想我解决了。我对Ryan Wilson的建议(在评论中)提出的问题是,它将导致重复的代码。像JsonProperty(PropertyName =“ keyname”)在几个不同的地方(在每个派生类中,我想使用具有不同属性的相同键)。我担心拼写错误或前后不一会早晚渗入代码中并咬我。

我的解决方法是将键名存储在常量中:

public class BaseNode
{
    [JsonIgnore]
    public const string _constKeyName_sNodeParentId = "parentid";

    [JsonProperty(PropertyName = "id", Required = Required.DisallowNull)]
    public string _sNodeId { get; set; }

    [DisplayName("Parent Folder")]
    [DefaultValue("000000000000000000000000")]
    [MaxLength(24)]
    [MinLength(24)]
    public virtual string _sNodeParentId { get; set; }
}

然后将属性中的常量重新用作键名,而不是直接输入字符串:

public class NonDefaultingBoard : BaseBoardNode
{
    [JsonProperty(PropertyName = BaseNode._constKeyName_sNodeParentId, Required = Required.Always)]
    public override string _sNodeParentId { get => base._sNodeParentId; set => base._sNodeParentId = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_sNodeName, Required = Required.Always)]
    public override string _sNodeName { get => base._sNodeName; set => base._sNodeName = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_sNodeIP, Required = Required.Always)]
    public override string _sNodeIP { get => base._sNodeIP; set => base._sNodeIP = value; }

    [JsonProperty(PropertyName = BaseBoardNode._constKeyName_iNodePort, Required = Required.Always)]
    public override int _iNodePort { get => base._iNodePort; set => base._iNodePort = value; }
}

然后,我可以在一个地方使用更改我的父母姓名的键名,它会在代码中回荡。再也没有犯规。