【问题标题】:Deserialize IndexingPolicy(Azure Cosmos DB) from JSON从 JSON 反序列化 IndexingPolicy(Azure Cosmos DB)
【发布时间】: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


    【解决方案1】:

    使用默认的 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);
    

    请记住,您在示例中使用的索引定义似乎不正确,dataTypekind 是数字,似乎必须是实际名称。

    【讨论】:

    • 这确实解决了问题。谢谢!你知道为什么它在 Newtonsoft.Json 中有效,但在 System.Text.Json 中无效吗?对于您对dataTypekind 的评论,正如您在原始帖子中看到的那样,我尝试了这两个选项:)
    • 如果我在ip.LoadFrom(reader); 之前添加Index.Range(DataType.String, -1); 它可以正常工作。但是它不适用于JsonSerializer.Deserialize
    • 它不适用于 System.Text.Json,因为 SDK 当前与 Newtonsoft.Json 绑定,并且这些是 SDK 类(不是用户数据类),因此使用 Newtonsoft.Json 装饰器和转换器,参考:github.com/Azure/azure-cosmos-dotnet-v3/blob/master/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多