名称为 "formattable" 的 C# JsonProperty

C# JsonProperty with "formattable" name

我有一个这样注释的模型:

[DataMember(EmitDefaultValue = false)]
[JsonProperty("extension_{0}_name")]
public string name {get; set;}

此数据是通过对 Azure 域的查询检索的,通过 Postman,我可以看到实际 属性 被填充到特定字段中:

{
     "extension_129af129412_name":"John",
     ...
}

其中 129af129412 匹配 AzurePropertyExtensionId 属性,在我的 Web.Config 中定义为:

<appSettings>
    <add key="AzurePropertyExtensionId" value="129af129412" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>

但是,当我尝试使用 JsonConvert.DeserializeObject<> 反序列化对象时,未检索到该字段。我怀疑这是因为绑定是在字段 name_{0}_property 而不是 name_129af129412_property.

上进行的

我想 Web.Config 的 xdt:Transform 属性出了点问题:写它的人都认为 JsonProperty 名称会被正确重写,但这不是案件。使用 JsonConvert 序列化后,该字段将序列化为 extension_{0}_name。这里出了什么问题?

旁注:Azure 有一个有趣的行为,我们将带有 {0} 占位符的序列化模型发送到 Azure,并且该字段无论如何都保存在 Azure 上。所以,虽然我们在WebServer上因为字段名不匹配而无法反序列化字段,但Azure是有能力自行解决这个问题的。我很想了解这是怎么可能的。

事实证明,有一个 ContractResolver 实例负责 JsonProperty 字段名称的转换,但未作为 JsonSerializerSettings 的一部分传递。将解析器实例添加到(反)序列化步骤后,该字段已成功检索。