如何根据条件添加(可能为空)JSON 属性?

昨天,我需要在 JSON 文件中添加一个属性,我已使用 DefaultHandling 完成此操作,如 this previous question 中所述:

[JsonProperty("DIAGNOSTICS",DefaultvalueHandling = DefaultvalueHandling.Populate)]
public bool DIAGNOSTICS { get; set; }

现在我正在处理一个更复杂的情况:

摘自我的源代码:

[JsonProperty("NAME")]
public string NAME { get; set; }
[JsonProperty("i1.ENABLE")]
public bool i1ENABLE { get; set; }
[JsonProperty("i2.ENABLE")]
public bool i2ENABLE { get; set; }
[JsonProperty("i3ENABLE")]
public bool i3ENABLE { get; set; }
...

所需的 JSON 结果:

"NAME": "i1","i1.ENABLE": false,/* i2.ENABLE and i3.ENABLE are not shown */
...
"NAME": "i2","i2.ENABLE": false,/* i1.ENABLE and i3.ENABLE are not shown */
...
"NAME": "i3","i3.ENABLE": false,/* i1.ENABLE and i2.ENABLE are not shown */
...

所以我的问题是,这是否可能(伪代码),如果是,如何?

[JsonProperty("i1.ENABLE",DefaultvalueHandling = (IF(NAME=="i1") THEN DefaultvalueHandling.Populate))]
public bool i1ENABLE { get; set; }
[JsonProperty("i2.ENABLE",DefaultvalueHandling = (IF(NAME=="i2") THEN DefaultvalueHandling.Populate))]
public bool i2ENABLE { get; set; }
[JsonProperty("i3.ENABLE",DefaultvalueHandling = (IF(NAME=="i3") THEN DefaultvalueHandling.Populate))]
public bool i3ENABLE { get; set; }

编辑(在 Laurent 的第一个回答之后)

我的序列化代码如下:

JsonSerializerSettings js = new JsonSerializerSettings();
js.DefaultvalueHandling = DefaultvalueHandling.Ignore;
string temp = JsonConvert.SerializeObject(root,Formatting.Indented,js);

万一在属性声明中做不到,可以在这里做吗?

SSBB999SA 回答:如何根据条件添加(可能为空)JSON 属性?

您的属性中不能有条件,请尝试在常规代码中设置这些条件默认值。 (属性只能有常量值)

,

你可以利用 json.net 的 Conditional Property Serialization

class CC {
  [JsonProperty("NAME")]
  public string NAME { get; set; }

  [JsonProperty("i1.ENABLE")]
  public bool i1ENABLE { get; set; }

  [JsonProperty("i2.ENABLE")]
  public bool i2ENABLE { get; set; }

  [JsonProperty("i3.ENABLE")]
  public bool i3ENABLE { get; set; }

  public bool ShouldSerializei1ENABLE() {
    bool r = NAME == "i1";
    return r;
  }

  public bool ShouldSerializei2ENABLE() {
    bool r = NAME == "i2";
    return r;
  }

  public bool ShouldSerializei3ENABLE() {
    bool r = NAME == "i3";
    return r;
  }

}

所以一个属性只有在 ShouldSerialize[PropertyName]() 返回 true 时才会被序列化;

另见this fiddle

编辑

是的,您的 DefaultValueHandling.Ignore 中的 JsonSerializerSettings 确实可能会影响序列化的结果。即,即使 ShouldSerialize... 返回 true,如果属性等于默认值,它也不会被序列化(在这种特殊情况下,bool 永远不会被序列化,如果它是 {{ 1}} 和 false 已设置)。

因此,如果您总是想在 DefaultValueHandling.Ignore 等于 i1ENABLE 时序列化 NAME,您必须覆盖此属性的 i1

DefaultValueHandling

所以当你序列化如下

class CC {
  [JsonProperty("NAME")]
  public string NAME { get; set; }

  [JsonProperty("i1.ENABLE",DefaultValueHandling=DefaultValueHandling.Populate)]
  public bool i1ENABLE { get; set; }

  public bool ShouldSerializei1ENABLE() {
    bool r = NAME == "i1";
    return r;
  }
}

属性 JsonSerializerSettings js = new JsonSerializerSettings(); js.DefaultValueHandling = DefaultValueHandling.Ignore; string temp = JsonConvert.SerializeObject(root,Formatting.Indented,js); 总是被序列化,当 i1ENABLE从不被序列化,当 NAME == "i1"

NAME != "i1" 可能是最早的过滤器之一,它是在属性序列化期间完成的。如果它返回 ShouldSerialize...,则永远不会序列化此属性。但是如果它返回 false,还有其他检查。其中之一是true。因此,如果 DefaultValueHandling 设置为 DefaultValueHandling,则值为 Ignore 的布尔值不会被序列化,即使 false 返回 true。

另请参阅此updated fiddle

本文链接:https://www.f2er.com/29026.html

大家都在问