|
| 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 | +} |
0 commit comments