-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Microsoft.Spatial unconditionally in Search (#15387)
* Fix README for Microsoft.Azure.Core.NewtonsoftJson * Add Json.NET converters for Microsoft.Spatial Resolves #13165 * Rename converter to match Json.NET ObjectSerializer It may be long, but it's consistent. * Add GeometryFactory and adapter Uses reflection to loosely bind Microsoft.Spatial types. * Rename spatial adapters to proxies * Proxy all supported Microsoft.Spatial types * Update Search for current Azure.Core.GeoJson * Encode spatial types for SearchFilter Fixes #15299 * Implement IEquatable<GeoPosition> on GeoPosition GeoPosition already implemented the interface, just without declaring it. May as well, so the struct doesn't have to be boxed when using EqualityComparer.Default, for example. * Resolve PR build issues * Resolve build analysis issues Filed #15423 to remove exclusions later. * Move Microsoft.Spatial support for Json.NET to core * Remove unused methods * Always run tests against Microsoft.Spatial * Change GeometryPoint to GeographyPoint * Fix README validation failure * Resolve PR feedback * Resolve PR feedback
- Loading branch information
Showing
44 changed files
with
1,631 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
sdk/core/Microsoft.Azure.Core.NewtonsoftJson/src/Microsoft.Azure.Core.NewtonsoftJson.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Release History | ||
|
||
## 1.0.0-preview.1 (Unreleased) | ||
|
||
### Added | ||
|
||
- Added `NewtonsoftJsonGeographyPointConverter` to serialize `Microsoft.Spatial.GeographyPoint` objects. |
7 changes: 7 additions & 0 deletions
7
sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/Directory.Build.props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<IsClientLibrary>true</IsClientLibrary> | ||
</PropertyGroup> | ||
|
||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.props))\Directory.Build.props" /> | ||
</Project> |
52 changes: 52 additions & 0 deletions
52
sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Newtonsoft.Json support for Microsoft.Spatial library for .NET | ||
|
||
The [Microsoft.Spatial package][microsoft_spatial_package] contains classes and methods that support geographic and geometric operations. | ||
This library contains converters dependent on the [Newtonsoft.Json package][newtonsoft_json_package] for use with Microsoft.Spatial | ||
when using the Azure SDK for .NET. | ||
|
||
## Getting started | ||
|
||
TODO | ||
|
||
### Install the package | ||
|
||
TODO | ||
|
||
### Prerequisites | ||
|
||
TODO | ||
|
||
### Authenticate the client | ||
|
||
TODO | ||
|
||
## Key concepts | ||
|
||
TODO | ||
|
||
## Examples | ||
|
||
TODO | ||
|
||
## Troubleshooting | ||
|
||
TODO | ||
|
||
## Next steps | ||
|
||
TODO | ||
|
||
## Contributing | ||
|
||
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit <https://cla.microsoft.com>. | ||
|
||
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. | ||
|
||
This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][code_of_conduct_faq] or contact opencode@microsoft.com with any additional questions or comments. | ||
|
||
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-net%2Fsdk%2Fcore%2FMicrosoft.Azure.Core.NewtonsoftJson%2FREADME.png) | ||
|
||
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct | ||
[code_of_conduct_faq]: https://opensource.microsoft.com/codeofconduct/faq/ | ||
[microsoft_spatial_package]: https://www.nuget.org/packages/Microsoft.Spatial/ | ||
[newtonsoft_json_package]: https://www.nuget.org/packages/Newtonsoft.Json/ |
10 changes: 10 additions & 0 deletions
10
....Spatial.NewtonsoftJson/api/Microsoft.Azure.Core.Spatial.NewtonsoftJson.netstandard2.0.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Azure.Core.Serialization | ||
{ | ||
public partial class NewtonsoftJsonGeographyPointConverter : Newtonsoft.Json.JsonConverter | ||
{ | ||
public NewtonsoftJsonGeographyPointConverter() { } | ||
public override bool CanConvert(System.Type objectType) { throw null; } | ||
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { throw null; } | ||
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { } | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/src/GeoJsonExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.Spatial; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Azure.Core.Serialization | ||
{ | ||
/// <summary> | ||
/// Defines extension methods for various JSON.NET types that make it easier to recognize and read Geo-JSON. | ||
/// </summary> | ||
internal static class GeoJsonExtensions | ||
{ | ||
private const string Coordinates = "coordinates"; | ||
private const string Crs = "crs"; | ||
private const string Name = "name"; | ||
private const string Point = "Point"; | ||
private const string Properties = "properties"; | ||
private const string Type = "type"; | ||
private const string WorldGeodeticSystem1984 = "EPSG:4326"; // See https://epsg.io/4326 | ||
|
||
private static readonly IEnumerable<string> CrsOnly = new[] { Crs }; | ||
private static readonly IEnumerable<string> NameOnly = new[] { Name }; | ||
private static readonly IEnumerable<string> TypeAndCoordinates = new[] { Type, Coordinates }; | ||
private static readonly IEnumerable<string> TypeAndProperties = new[] { Type, Properties }; | ||
|
||
/// <summary> | ||
/// Reads a Geo-JSON point into a <see cref="GeographyPoint" /> instance, or throws | ||
/// <see cref="JsonSerializationException" /> if the reader is not positioned on the | ||
/// beginning of a valid Geo-JSON point. | ||
/// </summary> | ||
/// <param name="reader">The JSON reader from which to read a Geo-JSON point.</param> | ||
/// <returns>A <see cref="GeographyPoint" /> instance.</returns> | ||
public static GeographyPoint ReadGeoJsonPoint(this JsonReader reader) | ||
{ | ||
// Check for null first. | ||
if (reader.TokenType == JsonToken.Null) | ||
{ | ||
return null; | ||
} | ||
|
||
GeographyPoint result = null; | ||
|
||
reader.ReadObject( | ||
requiredProperties: TypeAndCoordinates, | ||
optionalProperties: CrsOnly, | ||
readProperty: (r, propertyName) => | ||
{ | ||
switch (propertyName) | ||
{ | ||
case Type: | ||
r.ExpectAndAdvance(JsonToken.String, Point); | ||
break; | ||
case Coordinates: | ||
result = ReadCoordinates(r); | ||
break; | ||
case Crs: | ||
ReadCrs(r); | ||
break; | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Writes a <see cref="GeographyPoint" /> instance as Geo-JSON format. | ||
/// </summary> | ||
/// <param name="writer">The JSON writer to which to write the Geo-JSON point.</param> | ||
/// <param name="point">The <see cref="GeographyPoint" /> instance to write.</param> | ||
public static void WriteGeoJsonPoint(this JsonWriter writer, GeographyPoint point) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName(Type); | ||
writer.WriteValue(Point); | ||
writer.WritePropertyName(Coordinates); | ||
writer.WriteStartArray(); | ||
writer.WriteValue(point.Longitude); | ||
writer.WriteValue(point.Latitude); | ||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
private static bool IsCrsProperties(JObject possibleProperties) => | ||
possibleProperties.IsValid( | ||
requiredProperties: NameOnly, | ||
isPropertyValid: property => property.Name == Name && property.Value.IsString(WorldGeodeticSystem1984)); | ||
|
||
private static void ReadCrsProperties(JsonReader propertiesReader) => | ||
propertiesReader.ReadObjectAndAdvance( | ||
requiredProperties: NameOnly, | ||
readProperty: (r, _) => r.ExpectAndAdvance(JsonToken.String, WorldGeodeticSystem1984)); | ||
|
||
private static void ReadCrs(JsonReader crsReader) => | ||
crsReader.ReadObjectAndAdvance( | ||
requiredProperties: TypeAndProperties, | ||
readProperty: (r, propertyName) => | ||
{ | ||
switch (propertyName) | ||
{ | ||
case Type: | ||
r.ExpectAndAdvance(JsonToken.String, Name); | ||
break; | ||
case Properties: | ||
ReadCrsProperties(r); | ||
break; | ||
} | ||
}); | ||
|
||
private static GeographyPoint ReadCoordinates(JsonReader coordinatesReader) | ||
{ | ||
coordinatesReader.ExpectAndAdvance(JsonToken.StartArray); | ||
|
||
double ReadFloatOrInt() | ||
{ | ||
switch (coordinatesReader.TokenType) | ||
{ | ||
case JsonToken.Integer: | ||
return coordinatesReader.ExpectAndAdvance<long>(JsonToken.Integer); | ||
|
||
// Treat all other cases as Float and let ExpectAndAdvance() handle any errors. | ||
default: | ||
return coordinatesReader.ExpectAndAdvance<double>(JsonToken.Float); | ||
} | ||
} | ||
|
||
double longitude = ReadFloatOrInt(); | ||
double latitude = ReadFloatOrInt(); | ||
|
||
coordinatesReader.ExpectAndAdvance(JsonToken.EndArray); | ||
return GeographyPoint.Create(latitude, longitude); | ||
} | ||
} | ||
} |
Oops, something went wrong.