| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Buffers; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Collections.ObjectModel; |
| | 7 | | using System.IO; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Text; |
| | 10 | | using System.Text.Json; |
| | 11 | |
|
| | 12 | | namespace Azure.Core.GeoJson |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// A base type for all spatial types. |
| | 16 | | /// </summary> |
| | 17 | | public abstract class GeoObject |
| | 18 | | { |
| 2 | 19 | | internal static readonly IReadOnlyDictionary<string, object?> DefaultProperties = new ReadOnlyDictionary<string, |
| | 20 | | private string? _serialized; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of <see cref="GeoObject"/>. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="boundingBox">The <see cref="GeoBoundingBox"/> to use.</param> |
| | 26 | | /// <param name="additionalProperties">The set of additional properties associated with the <see cref="GeoObject |
| 370 | 27 | | protected GeoObject(GeoBoundingBox? boundingBox, IReadOnlyDictionary<string, object?> additionalProperties) |
| | 28 | | { |
| 370 | 29 | | Argument.AssertNotNull(additionalProperties, nameof(additionalProperties)); |
| | 30 | |
|
| 370 | 31 | | BoundingBox = boundingBox; |
| 370 | 32 | | AdditionalProperties = additionalProperties; |
| 370 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Represents information about the coordinate range of the <see cref="GeoObject"/>. |
| | 37 | | /// </summary> |
| 158 | 38 | | public GeoBoundingBox? BoundingBox { get; } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets a dictionary of additional properties associated with the <see cref="GeoObject"/>. |
| | 42 | | /// </summary> |
| 162 | 43 | | public IReadOnlyDictionary<string, object?> AdditionalProperties { get; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Converts an instance of <see cref="GeoObject"/> to a GeoJSON representation. |
| | 47 | | /// </summary> |
| | 48 | | /// <returns></returns> |
| | 49 | | public override string ToString() |
| | 50 | | { |
| 0 | 51 | | if (_serialized == null) |
| | 52 | | { |
| 0 | 53 | | using MemoryStream stream = new MemoryStream(); |
| 0 | 54 | | using Utf8JsonWriter writer = new Utf8JsonWriter(stream); |
| 0 | 55 | | GeoJsonConverter.Write(writer, this); |
| 0 | 56 | | _serialized = Encoding.UTF8.GetString(stream.ToArray()); |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | return _serialized; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Parses an instance of see <see cref="GeoObject"/> from provided JSON representation. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="json">The GeoJSON representation of an object.</param> |
| | 66 | | /// <returns>The resulting <see cref="GeoObject"/> object.</returns> |
| | 67 | | public static GeoObject Parse(string json) |
| | 68 | | { |
| 0 | 69 | | using JsonDocument jsonDocument = JsonDocument.Parse(json); |
| 0 | 70 | | return GeoJsonConverter.Read(jsonDocument.RootElement); |
| 0 | 71 | | } |
| | 72 | | } |
| | 73 | | } |