Azure 文档客户端忽略 JsonProperty 属性
Azure Document Client ignores JsonProperty attribute
我使用 Azure Function 应用程序。在应用程序中,我使用包 Microsoft.Azure.DocumentDB v2.9.2 来处理 CommosDB。我的实体是:
public abstract class Entity
{
[JsonProperty("id")]
public string id { get; set; }
}
public class MyLog : Entity
{
[JsonProperty("createDate")]
public DateTime CreateDate { get; set; }
}
当我用 Newtonsoft.Json
序列化 MyLog
时,我看到 CreateDate
属性 是驼峰式风格,看起来与 JsonProperty
中提到的完全一样,但是当我通过 CreateDocumentAsync
方法将我的实体写入 CosmosDB 我看到 CreateDate
现在是 pascal 大小写并且看起来像 CreateDate
。想提一下,没有使用 ContractResolver
。我找不到可以将 pascal 大小写应用于实体序列化的任何其他地方。
[更新]
当我 运行 测试时,我发现数据库中的 属性 命名是正确的(考虑到 JsonProperty)。仅当 azure func 写入 DB 时出错
var policy = new ConnectionPolicy();
policy.PreferredLocations.Add("Germany North");
documentClient = new DocumentClient(endPoint, key, policy);
await documentClient.OpenAsync();
...
await documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), document);
[/UPDATE]
有人可以建议其他哪些选项会导致这种行为吗?
所以,我找到了这种行为的原因。带有 .NET 4.7.2 的 Azure Function v1 默认使用 DataContractJsonSerializer 并且不考虑 JsonProperty 属性。测试使用 NewtonSoft json 序列化程序,这就是使用属性的原因。
我使用 Azure Function 应用程序。在应用程序中,我使用包 Microsoft.Azure.DocumentDB v2.9.2 来处理 CommosDB。我的实体是:
public abstract class Entity
{
[JsonProperty("id")]
public string id { get; set; }
}
public class MyLog : Entity
{
[JsonProperty("createDate")]
public DateTime CreateDate { get; set; }
}
当我用 Newtonsoft.Json
序列化 MyLog
时,我看到 CreateDate
属性 是驼峰式风格,看起来与 JsonProperty
中提到的完全一样,但是当我通过 CreateDocumentAsync
方法将我的实体写入 CosmosDB 我看到 CreateDate
现在是 pascal 大小写并且看起来像 CreateDate
。想提一下,没有使用 ContractResolver
。我找不到可以将 pascal 大小写应用于实体序列化的任何其他地方。
[更新]
当我 运行 测试时,我发现数据库中的 属性 命名是正确的(考虑到 JsonProperty)。仅当 azure func 写入 DB 时出错
var policy = new ConnectionPolicy();
policy.PreferredLocations.Add("Germany North");
documentClient = new DocumentClient(endPoint, key, policy);
await documentClient.OpenAsync();
...
await documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), document);
[/UPDATE]
有人可以建议其他哪些选项会导致这种行为吗?
所以,我找到了这种行为的原因。带有 .NET 4.7.2 的 Azure Function v1 默认使用 DataContractJsonSerializer 并且不考虑 JsonProperty 属性。测试使用 NewtonSoft json 序列化程序,这就是使用属性的原因。