Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json serialization of geometries #12440

Merged
merged 9 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CollectionGeometry(System.Collections.Generic.IEnumerable<Azure.Core.Spat
public CollectionGeometry(System.Collections.Generic.IEnumerable<Azure.Core.Spatial.Geometry> geometries, Azure.Core.Spatial.GeometryProperties properties) : base (default(Azure.Core.Spatial.GeometryProperties)) { }
public System.Collections.Generic.IReadOnlyList<Azure.Core.Spatial.Geometry> Geometries { get { throw null; } }
}
public partial class Geometry
public abstract partial class Geometry
{
protected Geometry(Azure.Core.Spatial.GeometryProperties properties) { }
public Azure.Core.Spatial.GeometryProperties Properties { get { throw null; } }
Expand All @@ -74,6 +74,7 @@ protected Geometry(Azure.Core.Spatial.GeometryProperties properties) { }
public partial class GeometryJsonConverter : System.Text.Json.Serialization.JsonConverter<Azure.Core.Spatial.Geometry>
{
public GeometryJsonConverter() { }
public override bool CanConvert(System.Type typeToConvert) { throw null; }
public override Azure.Core.Spatial.Geometry Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
public override void Write(System.Text.Json.Utf8JsonWriter writer, Azure.Core.Spatial.Geometry value, System.Text.Json.JsonSerializerOptions options) { }
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core.Experimental/src/Spatial/Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Azure.Core.Spatial
/// <summary>
/// A base type for all spatial types.
/// </summary>
public class Geometry
public abstract class Geometry
{
internal static readonly GeometryProperties DefaultProperties = new GeometryProperties();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class GeometryJsonConverter : JsonConverter<Geometry>
private const string CoordinatesProperty = "coordinates";
private const string BBoxProperty = "bbox";

/// <inheritdoc />
public override bool CanConvert(Type typeToConvert)
{
return typeof(Geometry).IsAssignableFrom(typeToConvert);
}

/// <inheritdoc />
public override Geometry Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand Down
15 changes: 14 additions & 1 deletion sdk/core/Azure.Core.Experimental/tests/SpatialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,20 @@ private T AssertRoundtrip<T>(string json) where T: Geometry
var element2 = JsonDocument.Parse(memoryStreamOutput.ToArray()).RootElement;
var geometry2 = GeometryJsonConverter.Read(element2);

return (T)geometry2;
var options = new JsonSerializerOptions()
{
Converters = { new GeometryJsonConverter() }
};

// Serialize and deserialize as a base class
var bytes = JsonSerializer.SerializeToUtf8Bytes(geometry2, typeof(Geometry), options);
var geometry3 = JsonSerializer.Deserialize<Geometry>(bytes, options);

// Serialize and deserialize as a concrete class
var bytes2 = JsonSerializer.SerializeToUtf8Bytes(geometry3, options);
var geometry4 = JsonSerializer.Deserialize<T>(bytes2, options);

return geometry4;
}
}
}