Microsoft.Azure.Cosmos.Spatial反序列化点坐标时出错

Microsoft.Azure.Cosmos.Spatial Error while deserializing the point coordinates

我正在尝试将地理空间数据存储在我名为 Trips 的 cosmos 数据库容器中,并从我的 .net 核心网站 api (.net 5) 检索项目。我正在使用 Microsoft.Azure.Cosmos (3.19.0) 并且没有为序列化明确配置任何内容。保存有效,但当我尝试检索时出现此错误:

Newtonsoft.Json.JsonSerializationException:无法反序列化几何对象,因为 'type' 属性 不存在或具有无效值。

Cosmos 客户端实例:

   var options = new CosmosClientOptions()
    {
       AllowBulkExecution = true,
       SerializerOptions = new CosmosSerializationOptions()
       {
          PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
       }
    };
    var cosmosClient = new CosmosClient(account, key, options);

型号:

public class Trip
    {
        [JsonPropertyName("id"), Required]
        public string Id { get; set; }

        [JsonPropertyName("vehicleId"), Required]
        public string VehicleId { get; set; }

        [JsonPropertyName("startDateTime"), Required]
        public DateTime StartDateTime { get; set; }

        [JsonPropertyName("endDateTime"), Required]
        public DateTime EndDateTime { get; set; }

        [JsonPropertyName("wayPoints"), Required]
        public List<WayPoint> WayPoints { get; set; }
    }

    public class WayPoint
    {
        [JsonPropertyName("timeStamp"), Required]
        public DateTime TimeStamp { get; set; }

        [JsonPropertyName("point"), Required]
        public Point Point { get; set; }
    }

服务:

public async Task<IEnumerable<Trip>> GetMultipleAsync(string vehicleId = "")
{
   var queryDefinition = new QueryDefinition($"Select * from c where c.vehicleId = \"{vehicleId}\"");
   var queryIterator = _container.GetItemQueryIterator<Trip>(queryDefinition);
    
   var trips = new List<Trip>();
   while(queryIterator.HasMoreResults)
   {
      var response = await queryIterator.ReadNextAsync();
      trips.AddRange(response.ToList());
   }
   return trips;
}

控制器:

using FileStream createStream = System.IO.File.Create(@"C:\repos\TripGenerator\trips.json");
await JsonSerializer.SerializeAsync(
 createStream,
 trips);

Json样本:

{
    "id": "a9153ca0-e171-4fe8-bcfe-733ac75f6b85",
    "vehicleId": "599abc63-eafb-4015-ac65-fc6aed48d9aa",
    "startDateTime": "2021-06-17T00:00:00Z",
    "endDateTime": "2021-06-17T23:55:00Z",
    "wayPoints": [
        {
            "timeStamp": "2021-06-17T00:00:00Z",
            "point": {
                "Position": {
                    "Coordinates": [
                        51.23156579100001,
                        -0.603818000999999
                    ],
                    "Longitude": 77.23156579100001,
                    "Latitude": 12.016038180009999,
                    "Altitude": null
                },
                "Crs": {
                    "Type": 0
                },
                "Type": 0,
                "BoundingBox": null,
                "AdditionalProperties": {}
            }
        },
        {
            "timeStamp": "2021-06-17T00:05:00Z",
            "point": {
                "Position": {
                    "Coordinates": [
                        51.23159449100001,
                        -0.01703846700999
                    ],
                    "Longitude": 77.23159449100001,
                    "Latitude": 12.603846700999998,
                    "Altitude": null
                },
                "Crs": {
                    "Type": 0
                },
                "Type": 0,
                "BoundingBox": null,
                "AdditionalProperties": {}
            }
        },
        ////////////
        {
            "timeStamp": "2021-06-17T23:55:00Z",
            "point": {
                "Position": {
                    "Coordinates": [
                        51.23980269100042,
                        -0.01961205490099
                    ],
                    "Longitude": 77.23980269100042,
                    "Latitude": 12.612054900999901,
                    "Altitude": null
                },
                "Crs": {
                    "Type": 0
                },
                "Type": 0,
                "BoundingBox": null,
                "AdditionalProperties": {}
            }
        }
    ],
}

如有任何帮助,我们将不胜感激。谢谢!

问题似乎出在您使用的外壳配置上:

PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase

您的 Json 显示“点”的“类型”属性 存在,但错误中的大小写为“类型”(小写 T)。

V3 SDK (https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/Spatial/Geometry.cs#L70) 中的几何类型定义为“类型”:

[DataMember(Name = "type")]
[JsonProperty("type", Required = Required.Always, Order = 0)]
[JsonConverter(typeof(StringEnumConverter))]
public GeometryType Type { get; private set; }

所以通常我们希望它是 serialized/saved 作为“类型”,而不是“类型”。

在保存文档时 PropertyNamingPolicy 使其序列化为“类型”,或者文档是使用不同的 SDK/工具保存的,该工具将几何序列化为“类型”。

这就是引发此异常的原因。