【发布时间】:2020-11-20 11:45:59
【问题描述】:
我正在尝试从 json IndexingPolicy 反序列化以更新 Azure Cosmos DB。 虽然 IndexingPolicy 及其所有内部类都是 JsonSerializable 的子类,但无法反序列化。
Index 是 IndexingPolicy 的内部类之一,缺少空构造函数。因此,反序列化失败。但是,我很难相信框架的开发人员没有对其进行适当的测试。
我尝试了两种反序列化的方法,
var jsonString = @"{
'indexingMode': 'consistent',
'automatic': true,
'includedPaths': [
{
'path': '/PartitionKey/?',
'indexes': [
{
'kind': 'Range',
'dataType': 'String',
'precision': -1
}
]
}
],
'excludedPaths': [
{
'path': '/*'
}
]
}";
JsonSerializerOptions options = new JsonSerializerOptions()
{
IgnoreNullValues = true,
IgnoreReadOnlyProperties = true,
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = false,
MaxDepth = 1000,
PropertyNameCaseInsensitive = true
};
IndexingPolicy index = System.Text.Json.JsonSerializer.Deserialize<IndexingPolicy>(jsonString, options);
也可以通过LoadFrom()方法,
IndexingPolicy ip = new IndexingPolicy();
using (var reader = new JsonTextReader(new StringReader(jsonString)))
{
ip.LoadFrom(reader);
}
为了安全起见,我也尝试了这个 JSON
{
"indexingMode": 0,
"automatic": true,
"includedPaths": [
{
"path": "/PartitionKey/?",
"indexes": [
{
"kind": 1,
"dataType": 1,
"precision": -1
}
]
}
],
"excludedPaths": [
{
"path": "/*"
}
]
}
【问题讨论】:
标签: json azure azure-cosmosdb