简体   繁体   English

从 JSON 反序列化 IndexingPolicy(Azure Cosmos DB)

[英]Deserialize IndexingPolicy(Azure Cosmos DB) from JSON

I'm trying to deserialize from a json IndexingPolicy in order to update the Azure Cosmos DB.我正在尝试从 json IndexingPolicy 反序列化以更新 Azure Cosmos DB。 Although IndexingPolicy and all its inner class are subclasses of JsonSerializable, it fails to be deserialized.虽然 IndexingPolicy 及其所有内部 class 都是 JsonSerializable 的子类,但它无法反序列化。

Index is one of the inner class of IndexingPolicy and is lack of empty constructor.索引是 IndexingPolicy 的内部 class 之一,并且缺少空构造函数。 Therefore, the deserialization fails.因此,反序列化失败。 However, I find it hard to believe that the developers of the framework haven't tested it properly.但是,我很难相信框架的开发人员没有正确测试它。

I have tried two ways to deserialize,我尝试了两种反序列化方法,

    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); 

And also by LoadFrom() method,并且还通过 LoadFrom() 方法,

        IndexingPolicy ip = new IndexingPolicy();
        using (var reader = new JsonTextReader(new StringReader(jsonString)))
        {
            ip.LoadFrom(reader);
        } 

To be on the safe side I also tried with this JSON为了安全起见,我也尝试了这个 JSON

{
      "indexingMode": 0,
      "automatic": true,
      "includedPaths": [
        {
          "path": "/PartitionKey/?",
          "indexes": [
            {
              "kind": 1,
              "dataType": 1,
              "precision": -1
            }
          ]
        }
      ],
      "excludedPaths": [
        {
          "path": "/*"
        }
      ]
    }

Using the default Newtonsoft.Json settings works without issues:使用默认 Newtonsoft.Json 设置可以正常工作:

string serializedPolicy = @"{
  ""indexingMode"": 0,
  ""automatic"": true,
  ""includedPaths"": [
    {
      ""path"": ""/PartitionKey/?"",
      ""indexes"":[{""dataType"":""Number"",""precision"":-1,""kind"":""Hash""}]
    }
  ],
  ""excludedPaths"": [
    {
       ""path"": ""/*""
    }
  ]
}";
IndexingPolicy policy = JsonConvert.DeserializeObject<IndexingPolicy>(serializedPolicy);

Keep in mind that the index definition you are using in your example seems to be incorrect, the dataType and kind are numbers, which seem to have to be the actual names.请记住,您在示例中使用的索引定义似乎不正确, dataTypekind是数字,似乎必须是实际名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Azure Cosmos DB 中获取 arrays 的扁平 JSON - Getting a flattened JSON of arrays in Azure Cosmos DB 从Azure函数中的Azure服务总线接收JSON(Cosmos DB文档)作为输入 - Receiving JSON (Cosmos DB Documents) as input from Azure Service Bus in Azure Functions 在Azure Cosmos DB或Azure Blob中将JSON转换为CSV - Convert JSON to CSV in Azure Cosmos DB or Azure Blob 从 Java 代码批量上传/导入 JSON 文件到 Azure Cosmos DB - Bulk Upload/Import of JSON files to Azure Cosmos DB from Java Code 使用 C# ASP.NET MVC 显示来自单个数组 JSON(azure Cosmos db) 的数据 - Display data from single array JSON(azure Cosmos db) using C# ASP.NET MVC 如何将Azure cosmos Db中的非结构化json文件转换为结构化表? - How to convert an unstructured json file from Azure cosmos Db to a structured table? Azure + Cosmos数据库: - Azure + Cosmos DB : 如何将Upsert json文件批量添加到Azure Cosmos DB(documentDB模块)? - How to bulk Upsert json files to azure Cosmos DB (documentDB module)? 将“扩展”元数据作为JSON文档存储在Azure Cosmos DB中存储的实体上 - Store “extended” metadata on entities stored in Azure Cosmos DB as JSON documents Json 格式 azure 宇宙图db批量导入 - Json Format for azure cosmos graph db bulk import
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM