Skip to content

Commit 8393100

Browse files
abannsunnyAbann Sunny
andauthored
Implement Custom JsonConverter on Feature
* Remove abstract keyword from Feature class * workiing on feature json parser * fix OnDrawingComplete callback Co-authored-by: Abann Sunny <abann.sunny@nucor.com>
1 parent 4d160be commit 8393100

5 files changed

Lines changed: 203 additions & 2 deletions

File tree

samples/AzureMapsControl.Sample/Pages/Drawing/DrawingToolbar.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
Events = AzureMapsControl.Components.Drawing.DrawingToolbarEventActivationFlags.All()
1414
}"
1515
OnDrawingModeChanged="OnDrawingModeChanged"
16+
OnDrawingComplete="OnDrawingComplete"
1617
/>
1718

1819
@code {
1920
public async Task OnDrawingModeChanged(AzureMapsControl.Components.Drawing.DrawingToolbarModeEventArgs eventArgs)
2021
{
2122
Console.WriteLine(eventArgs.NewMode);
2223
}
24+
25+
public async Task OnDrawingComplete(AzureMapsControl.Components.Drawing.DrawingToolbarEventArgs eventArgs)
26+
{
27+
Console.WriteLine(eventArgs.Data);
28+
}
2329
}

src/AzureMapsControl.Components/Atlas/Feature.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text.Json.Serialization;
88

99
[ExcludeFromCodeCoverage]
10+
[JsonConverter(typeof(FeatureJsonConverter))]
1011
public abstract class Feature
1112
{
1213
[JsonConverter(typeof(FeatureIdConverter))]
@@ -55,6 +56,44 @@ public Feature(TGeometry geometry, IDictionary<string, object> properties) : thi
5556
public Feature(string id, TGeometry geometry, IDictionary<string, object> properties) : base(id, properties) => Geometry = geometry;
5657
}
5758

59+
internal class FeatureJsonConverter : JsonConverter<Feature>
60+
{
61+
public override Feature Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
62+
{
63+
var originalDepth = reader.CurrentDepth;
64+
65+
string Id = null;
66+
IDictionary<string, object> Properties = null;
67+
Geometry geometry = null;
68+
69+
while (reader.TokenType != JsonTokenType.EndObject || originalDepth != reader.CurrentDepth)
70+
{
71+
reader.Read();
72+
73+
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "id")
74+
{
75+
reader.Read();
76+
var converter = new FeatureIdConverter();
77+
Id = converter.Read(ref reader, typeof(string), options);
78+
}
79+
80+
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "properties")
81+
{
82+
var converter = new FeaturePropertiesConverter();
83+
Properties = converter.Read(ref reader, typeof(IDictionary<string, object>), options);
84+
}
85+
86+
}
87+
88+
89+
return new Feature<Geometry>(Id, geometry, Properties);
90+
}
91+
92+
public override void Write(Utf8JsonWriter writer, Feature value, JsonSerializerOptions options) => throw new NotSupportedException();
93+
94+
95+
}
96+
5897
internal sealed class FeatureIdConverter : JsonConverter<string>
5998
{
6099
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
@@ -93,4 +132,63 @@ public override void Write(Utf8JsonWriter writer, IDictionary<string, object> va
93132
writer.WriteEndObject();
94133
}
95134
}
135+
136+
internal class FeatureJsonConverter<TGeometry> : JsonConverter<Feature<TGeometry>> where TGeometry : Geometry
137+
{
138+
public FeatureJsonConverter() { }
139+
140+
public override Feature<TGeometry> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
141+
{
142+
var originalDepth = reader.CurrentDepth;
143+
144+
string Id = null;
145+
IDictionary<string, object> Properties = null;
146+
TGeometry geometry = null;
147+
148+
while (reader.TokenType != JsonTokenType.EndObject || originalDepth != reader.CurrentDepth)
149+
{
150+
reader.Read();
151+
152+
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "id")
153+
{
154+
reader.Read();
155+
var converter = new FeatureIdConverter();
156+
Id = converter.Read(ref reader, typeof(string), options);
157+
}
158+
159+
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "geometry")
160+
{
161+
var converter = new GeometryJsonConverter<TGeometry>();
162+
geometry = converter.Read(ref reader, typeof(TGeometry), options);
163+
}
164+
165+
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "properties")
166+
{
167+
var converter = new FeaturePropertiesConverter();
168+
Properties = converter.Read(ref reader, typeof(IDictionary<string, object>), options);
169+
}
170+
171+
}
172+
173+
174+
return new Feature<TGeometry>(Id, geometry, Properties);
175+
}
176+
177+
public override void Write(Utf8JsonWriter writer, Feature<TGeometry> value, JsonSerializerOptions options)
178+
{
179+
writer.WriteStartObject();
180+
writer.WritePropertyName("geometry");
181+
JsonSerializer.Serialize(writer, value.Geometry);
182+
writer.WriteString("id", value.Id);
183+
writer.WritePropertyName("properties");
184+
JsonSerializer.Serialize(writer, value.Properties);
185+
writer.WritePropertyName("bbox");
186+
JsonSerializer.Serialize(writer, value.BBox);
187+
writer.WriteEndObject();
188+
}
189+
190+
}
191+
192+
193+
96194
}

src/AzureMapsControl.Components/Drawing/DrawingToolbarEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[ExcludeFromCodeCoverage]
99
public sealed class DrawingToolbarEventArgs : MapEventArgs
1010
{
11-
public Feature Data { get; }
11+
public Feature<Geometry> Data { get; }
1212

1313
internal DrawingToolbarEventArgs(Map map, DrawingToolbarJsEventArgs eventArgs) : base(map, eventArgs.Type) => Data = eventArgs.Data;
1414
}

src/AzureMapsControl.Components/Drawing/DrawingToolbarJsEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ internal class DrawingToolbarJsEventArgs
99
{
1010
public string Type { get; set; }
1111
public string NewMode { get; set; }
12-
public Feature Data { get; set; }
12+
public Feature<Geometry> Data { get; set; }
1313
}
1414
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
namespace AzureMapsControl.Components.Tests.Atlas
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Text.Json;
8+
using System.Threading.Tasks;
9+
10+
using AzureMapsControl.Components.Atlas;
11+
using AzureMapsControl.Components.Tests.Json;
12+
13+
using Xunit;
14+
public class FeaturePolygonJsonConverterTests : JsonConverterTests<Feature<Polygon>>
15+
{
16+
JsonSerializerOptions _jsonSerializerOptions;
17+
public FeaturePolygonJsonConverterTests() : base(new FeatureJsonConverter<Polygon>())
18+
{
19+
_jsonSerializerOptions = new JsonSerializerOptions {
20+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
21+
};
22+
}
23+
24+
[Fact]
25+
public void Should_ReadFeature()
26+
{
27+
var feature = new Feature<Polygon>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Polygon() {
28+
GeometryType = "Polygon",
29+
Coordinates = new[] {
30+
new [] {
31+
new Position(0, 1),
32+
new Position(2, 3)
33+
}
34+
},
35+
Id = "abcd-abcd-abcd-abcd-abcd"
36+
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
37+
var json = JsonSerializer.Serialize(feature, _jsonSerializerOptions);
38+
var result = Read(json);
39+
Assert.IsType<Feature<Polygon>>(result);
40+
Assert.Equal(json, JsonSerializer.Serialize(result, _jsonSerializerOptions));
41+
}
42+
43+
[Fact]
44+
public void Should_Write()
45+
{
46+
var geometry = new Feature<Polygon>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Polygon() {
47+
GeometryType = "Polygon",
48+
Coordinates = new[] {
49+
new [] {
50+
new Position(0, 1),
51+
new Position(2, 3)
52+
}
53+
},
54+
Id = "abcd-abcd-abcd-abcd-abcd"
55+
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
56+
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry, _jsonSerializerOptions));
57+
}
58+
59+
}
60+
61+
public class FeaturePointJsonConverterTests : JsonConverterTests<Feature<Point>>
62+
{
63+
JsonSerializerOptions _jsonSerializerOptions;
64+
public FeaturePointJsonConverterTests() : base(new FeatureJsonConverter<Point>())
65+
{
66+
_jsonSerializerOptions = new JsonSerializerOptions {
67+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
68+
};
69+
}
70+
71+
[Fact]
72+
public void Should_ReadFeature()
73+
{
74+
var feature = new Feature<Point>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Point() {
75+
GeometryType = "Point",
76+
Coordinates = new Position(0, 1),
77+
Id = "abcd-abcd-abcd-abcd-abcd"
78+
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
79+
var json = JsonSerializer.Serialize(feature, _jsonSerializerOptions);
80+
var result = Read(json);
81+
Assert.IsType<Feature<Point>>(result);
82+
Assert.Equal(json, JsonSerializer.Serialize(result, _jsonSerializerOptions));
83+
}
84+
85+
[Fact]
86+
public void Should_Write()
87+
{
88+
var feature = new Feature<Point>("b9d9f4b7-e5cc-42da-8b22-7bb36fe9fa23", new Point() {
89+
GeometryType = "Point",
90+
Coordinates = new Position(0, 1),
91+
Id = "abcd-abcd-abcd-abcd-abcd"
92+
}, new Dictionary<string, object> { { "_azureMapsShapeId", "efgh-efgh-efgh-efgh-efgh" } });
93+
TestAndAssertWrite(feature, JsonSerializer.Serialize(feature, _jsonSerializerOptions));
94+
}
95+
96+
}
97+
}

0 commit comments

Comments
 (0)