Skip to content

Commit 6de4151

Browse files
m-reddingCopilot
andauthored
Implement IJsonModel in GeoPoint (Azure#51111)
* first pass * update tests * simplify tests * export API * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Update GeoPoint.cs --------- Co-authored-by: Copilot <[email protected]>
1 parent 012e305 commit 6de4151

File tree

8 files changed

+216
-12
lines changed

8 files changed

+216
-12
lines changed

sdk/core/Azure.Core/api/Azure.Core.net462.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,14 +951,20 @@ public enum GeoObjectType
951951
MultiLineString = 5,
952952
GeometryCollection = 6,
953953
}
954-
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject
954+
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject, System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>, System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>
955955
{
956+
public GeoPoint() { }
956957
public GeoPoint(Azure.Core.GeoJson.GeoPosition position) { }
957958
public GeoPoint(Azure.Core.GeoJson.GeoPosition position, Azure.Core.GeoJson.GeoBoundingBox? boundingBox, System.Collections.Generic.IReadOnlyDictionary<string, object?> customProperties) { }
958959
public GeoPoint(double longitude, double latitude) { }
959960
public GeoPoint(double longitude, double latitude, double? altitude) { }
960961
public Azure.Core.GeoJson.GeoPosition Coordinates { get { throw null; } }
961962
public override Azure.Core.GeoJson.GeoObjectType Type { get { throw null; } }
963+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
964+
void System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
965+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
966+
string System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
967+
System.BinaryData System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
962968
}
963969
public sealed partial class GeoPointCollection : Azure.Core.GeoJson.GeoObject, System.Collections.Generic.IEnumerable<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyCollection<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyList<Azure.Core.GeoJson.GeoPoint>, System.Collections.IEnumerable
964970
{

sdk/core/Azure.Core/api/Azure.Core.net472.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,14 +951,20 @@ public enum GeoObjectType
951951
MultiLineString = 5,
952952
GeometryCollection = 6,
953953
}
954-
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject
954+
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject, System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>, System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>
955955
{
956+
public GeoPoint() { }
956957
public GeoPoint(Azure.Core.GeoJson.GeoPosition position) { }
957958
public GeoPoint(Azure.Core.GeoJson.GeoPosition position, Azure.Core.GeoJson.GeoBoundingBox? boundingBox, System.Collections.Generic.IReadOnlyDictionary<string, object?> customProperties) { }
958959
public GeoPoint(double longitude, double latitude) { }
959960
public GeoPoint(double longitude, double latitude, double? altitude) { }
960961
public Azure.Core.GeoJson.GeoPosition Coordinates { get { throw null; } }
961962
public override Azure.Core.GeoJson.GeoObjectType Type { get { throw null; } }
963+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
964+
void System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
965+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
966+
string System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
967+
System.BinaryData System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
962968
}
963969
public sealed partial class GeoPointCollection : Azure.Core.GeoJson.GeoObject, System.Collections.Generic.IEnumerable<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyCollection<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyList<Azure.Core.GeoJson.GeoPoint>, System.Collections.IEnumerable
964970
{

sdk/core/Azure.Core/api/Azure.Core.net8.0.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,14 +962,20 @@ public enum GeoObjectType
962962
MultiLineString = 5,
963963
GeometryCollection = 6,
964964
}
965-
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject
965+
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject, System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>, System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>
966966
{
967+
public GeoPoint() { }
967968
public GeoPoint(Azure.Core.GeoJson.GeoPosition position) { }
968969
public GeoPoint(Azure.Core.GeoJson.GeoPosition position, Azure.Core.GeoJson.GeoBoundingBox? boundingBox, System.Collections.Generic.IReadOnlyDictionary<string, object?> customProperties) { }
969970
public GeoPoint(double longitude, double latitude) { }
970971
public GeoPoint(double longitude, double latitude, double? altitude) { }
971972
public Azure.Core.GeoJson.GeoPosition Coordinates { get { throw null; } }
972973
public override Azure.Core.GeoJson.GeoObjectType Type { get { throw null; } }
974+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
975+
void System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
976+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
977+
string System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
978+
System.BinaryData System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
973979
}
974980
public sealed partial class GeoPointCollection : Azure.Core.GeoJson.GeoObject, System.Collections.Generic.IEnumerable<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyCollection<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyList<Azure.Core.GeoJson.GeoPoint>, System.Collections.IEnumerable
975981
{

sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,14 +951,20 @@ public enum GeoObjectType
951951
MultiLineString = 5,
952952
GeometryCollection = 6,
953953
}
954-
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject
954+
public sealed partial class GeoPoint : Azure.Core.GeoJson.GeoObject, System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>, System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>
955955
{
956+
public GeoPoint() { }
956957
public GeoPoint(Azure.Core.GeoJson.GeoPosition position) { }
957958
public GeoPoint(Azure.Core.GeoJson.GeoPosition position, Azure.Core.GeoJson.GeoBoundingBox? boundingBox, System.Collections.Generic.IReadOnlyDictionary<string, object?> customProperties) { }
958959
public GeoPoint(double longitude, double latitude) { }
959960
public GeoPoint(double longitude, double latitude, double? altitude) { }
960961
public Azure.Core.GeoJson.GeoPosition Coordinates { get { throw null; } }
961962
public override Azure.Core.GeoJson.GeoObjectType Type { get { throw null; } }
963+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
964+
void System.ClientModel.Primitives.IJsonModel<Azure.Core.GeoJson.GeoPoint>.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
965+
Azure.Core.GeoJson.GeoPoint? System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
966+
string System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
967+
System.BinaryData System.ClientModel.Primitives.IPersistableModel<Azure.Core.GeoJson.GeoPoint>.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
962968
}
963969
public sealed partial class GeoPointCollection : Azure.Core.GeoJson.GeoObject, System.Collections.Generic.IEnumerable<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyCollection<Azure.Core.GeoJson.GeoPoint>, System.Collections.Generic.IReadOnlyList<Azure.Core.GeoJson.GeoPoint>, System.Collections.IEnumerable
964970
{

sdk/core/Azure.Core/src/GeoJson/GeoJsonConverter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal static GeoObject Read(JsonElement element)
121121
}
122122
}
123123

124-
private static GeoBoundingBox? ReadBoundingBox(in JsonElement element)
124+
internal static GeoBoundingBox? ReadBoundingBox(in JsonElement element)
125125
{
126126
GeoBoundingBox? bbox = null;
127127

@@ -172,7 +172,7 @@ internal static GeoObject Read(JsonElement element)
172172
return bbox;
173173
}
174174

175-
private static IReadOnlyDictionary<string, object?> ReadAdditionalProperties(in JsonElement element, string knownProperty = CoordinatesProperty)
175+
internal static IReadOnlyDictionary<string, object?> ReadAdditionalProperties(in JsonElement element, string knownProperty = CoordinatesProperty)
176176
{
177177
Dictionary<string, object?>? additionalProperties = null;
178178
foreach (var property in element.EnumerateObject())
@@ -234,7 +234,7 @@ internal static GeoObject Read(JsonElement element)
234234
}
235235
}
236236

237-
private static IReadOnlyList<GeoPosition> ReadCoordinates(JsonElement coordinatesElement)
237+
internal static IReadOnlyList<GeoPosition> ReadCoordinates(JsonElement coordinatesElement)
238238
{
239239
GeoPosition[] coordinates = new GeoPosition[coordinatesElement.GetArrayLength()];
240240

@@ -248,7 +248,7 @@ private static IReadOnlyList<GeoPosition> ReadCoordinates(JsonElement coordinate
248248
return coordinates;
249249
}
250250

251-
private static GeoPosition ReadCoordinate(JsonElement coordinate)
251+
internal static GeoPosition ReadCoordinate(JsonElement coordinate)
252252
{
253253
var arrayLength = coordinate.GetArrayLength();
254254
if (arrayLength < 2 || arrayLength > 3)
@@ -415,7 +415,7 @@ void WritePositions(GeoArray<GeoPosition> positions)
415415

416416
writer.WriteEndObject();
417417
}
418-
private static void WriteAdditionalPropertyValue(Utf8JsonWriter writer, object? value)
418+
internal static void WriteAdditionalPropertyValue(Utf8JsonWriter writer, object? value)
419419
{
420420
switch (value)
421421
{
@@ -463,7 +463,7 @@ private static void WriteAdditionalPropertyValue(Utf8JsonWriter writer, object?
463463
}
464464
}
465465

466-
private static JsonElement GetRequiredProperty(JsonElement element, string name)
466+
internal static JsonElement GetRequiredProperty(JsonElement element, string name)
467467
{
468468
if (!element.TryGetProperty(name, out JsonElement property))
469469
{
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.ClientModel.Primitives;
6+
using System.Collections.Generic;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
10+
namespace Azure.Core.GeoJson
11+
{
12+
/// <summary>
13+
/// Represents a point geometry.
14+
/// </summary>
15+
/// <example>
16+
/// Creating a point:
17+
/// <code snippet="Snippet:CreatePoint" language="csharp">
18+
/// var point = new GeoPoint(-122.091954, 47.607148);
19+
/// </code>
20+
/// </example>
21+
public sealed partial class GeoPoint : IJsonModel<GeoPoint>
22+
{
23+
private const string PointType = "Point";
24+
private const string TypeProperty = "type";
25+
private const string GeometriesProperty = "geometries";
26+
private const string CoordinatesProperty = "coordinates";
27+
// cspell:ignore bbox
28+
private const string BBoxProperty = "bbox";
29+
30+
void IJsonModel<GeoPoint>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
31+
{
32+
var format = options.Format == "W" ? ((IPersistableModel<GeoPoint>)this).GetFormatFromOptions(options) : options.Format;
33+
if (format != "J")
34+
{
35+
throw new FormatException($"The model {nameof(GeoPoint)} does not support '{format}' format.");
36+
}
37+
38+
GeoJsonConverter.Write(writer, this);
39+
}
40+
41+
GeoPoint? IJsonModel<GeoPoint>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
42+
{
43+
var format = options.Format == "W" ? ((IPersistableModel<GeoPoint>)this).GetFormatFromOptions(options) : options.Format;
44+
if (format != "J")
45+
{
46+
throw new FormatException($"The model {nameof(GeoPoint)} does not support '{format}' format.");
47+
}
48+
49+
var document = JsonDocument.ParseValue(ref reader);
50+
return Create(document.RootElement);
51+
}
52+
53+
BinaryData IPersistableModel<GeoPoint>.Write(ModelReaderWriterOptions options)
54+
{
55+
var format = options.Format == "W" ? ((IPersistableModel<GeoPoint>)this).GetFormatFromOptions(options) : options.Format;
56+
if (format != "J")
57+
{
58+
throw new FormatException($"The model {nameof(GeoPoint)} does not support '{format}' format.");
59+
}
60+
61+
return ModelReaderWriter.Write(this, options, AzureCoreContext.Default);
62+
}
63+
64+
GeoPoint? IPersistableModel<GeoPoint>.Create(BinaryData data, ModelReaderWriterOptions options)
65+
{
66+
var format = options.Format == "W" ? ((IPersistableModel<GeoPoint>)this).GetFormatFromOptions(options) : options.Format;
67+
if (format != "J")
68+
{
69+
throw new FormatException($"The model {nameof(GeoPoint)} does not support '{format}' format.");
70+
}
71+
72+
using var document = JsonDocument.Parse(data);
73+
var element = document.RootElement;
74+
return Create(element);
75+
}
76+
77+
private static GeoPoint Create(JsonElement element)
78+
{
79+
JsonElement typeProperty = GeoJsonConverter.GetRequiredProperty(element, TypeProperty);
80+
81+
string? type = typeProperty.GetString();
82+
83+
GeoBoundingBox? boundingBox = GeoJsonConverter.ReadBoundingBox(element);
84+
85+
IReadOnlyDictionary<string, object?> additionalProperties = GeoJsonConverter.ReadAdditionalProperties(element);
86+
JsonElement coordinates = GeoJsonConverter.GetRequiredProperty(element, CoordinatesProperty);
87+
88+
switch (type)
89+
{
90+
case PointType:
91+
return new GeoPoint(GeoJsonConverter.ReadCoordinate(coordinates), boundingBox, additionalProperties);
92+
default:
93+
throw new NotSupportedException($"Type '{type}' is not GeoPoint");
94+
}
95+
}
96+
97+
string IPersistableModel<GeoPoint>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
98+
}
99+
}

sdk/core/Azure.Core/src/GeoJson/GeoPoint.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Text.Json.Serialization;
67

@@ -16,14 +17,21 @@ namespace Azure.Core.GeoJson
1617
/// </code>
1718
/// </example>
1819
[JsonConverter(typeof(GeoJsonConverter))]
19-
public sealed class GeoPoint : GeoObject
20+
public sealed partial class GeoPoint : GeoObject
2021
{
22+
/// <summary>
23+
/// Initializes new instance of <see cref="GeoPoint"/>.
24+
/// </summary>
25+
public GeoPoint() : this(default, default)
26+
{
27+
}
28+
2129
/// <summary>
2230
/// Initializes new instance of <see cref="GeoPoint"/>.
2331
/// </summary>
2432
/// <param name="longitude">The longitude of the point.</param>
2533
/// <param name="latitude">The latitude of the point.</param>
26-
public GeoPoint(double longitude, double latitude): this(new GeoPosition(longitude, latitude), null, DefaultProperties)
34+
public GeoPoint(double longitude, double latitude) : this(new GeoPosition(longitude, latitude), null, DefaultProperties)
2735
{
2836
}
2937

0 commit comments

Comments
 (0)