Skip to content

Commit

Permalink
make coordinate serialization more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
lolochristen committed Feb 16, 2024
1 parent e4c9607 commit e761de4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
25 changes: 23 additions & 2 deletions src/OpenLayers.Blazor/CoordinateConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenLayers.Blazor;

public class CoordinateConverter : JsonConverter<Coordinate>
internal class CoordinateConverter : JsonConverter<Coordinate>
{
public override Coordinate? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand All @@ -14,7 +14,28 @@ public class CoordinateConverter : JsonConverter<Coordinate>
Coordinate.TryParse(val, out var coordinate, CultureInfo.InvariantCulture);
return coordinate;
}

else if (reader.TokenType == JsonTokenType.StartObject)
{
double x = 0, y = 0;
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
var prop = reader.GetString();
if (prop != null && prop.Equals("x", options.PropertyNameCaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
{
reader.Read();
reader.TryGetDouble(out x);
}
else if (prop != null && prop.Equals("y", options.PropertyNameCaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
{
reader.Read();
reader.TryGetDouble(out y);
}
}
}
return new Coordinate(x, y);
}
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions src/OpenLayers.Blazor/wwwroot/openlayers_interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ MapOL.prototype.onMapClick = function (evt, popup, element) {
if (invokeMethod) {
invokeMethod = false;
const coordinate = ol.proj.transform(evt.coordinate, this.Map.getView().getProjection(), this.Options.coordinatesProjection);
const point = coordinate[0].toString() + "/" + coordinate[1].toString();
const point = { x: coordinate[0], y: coordinate[1] };
this.Instance.invokeMethodAsync("OnInternalClick", point);
}
};
Expand All @@ -583,7 +583,7 @@ MapOL.prototype.onMapPointerMove = function (evt, element) {
const coordinate = ol.proj.transform(evt.coordinate,
this.Map.getView().getProjection(),
this.Options.coordinatesProjection);
const point = coordinate[0].toString() + "/" + coordinate[1].toString();
const point = { x: coordinate[0], y: coordinate[1] };
this.Instance.invokeMethodAsync("OnInternalPointerMove", point);
};

Expand All @@ -597,7 +597,7 @@ MapOL.prototype.onMapCenterChanged = function () {
const coordinate = ol.proj.transform(center,
this.Map.getView().getProjection(),
this.Options.coordinatesProjection);
const point = coordinate[0].toString() + "/" + coordinate[1].toString();
const point = { x: coordinate[0], y: coordinate[1] };
this.Instance.invokeMethodAsync("OnInternalCenterChanged", point);
this.onVisibleExtentChanged();
};
Expand Down Expand Up @@ -636,7 +636,7 @@ MapOL.prototype.getCurrentGeoLocation = function () {
const coordinate = ol.proj.transform([position.coords.longitude, position.coords.latitude],
"EPSG:4326",
projection);
const point = coordinate[0].toString() + "/" + coordinate[1].toString();
const point = { x: coordinate[0], y: coordinate[1] };
resolve(point);
});
} else {
Expand Down
19 changes: 19 additions & 0 deletions test/OpenLayers.Blazor.Tests/CoordinateTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Text.Json;

namespace OpenLayers.Blazor.Tests;

Expand Down Expand Up @@ -42,4 +43,22 @@ public void Coordinate_ParseAltCulture_Success()
Assert.Equal(1.1, c.X);
Assert.Equal(2.2, c.Y);
}

[Fact]
public void Coordinate_JsonDeserialize_AsString()
{
string json = "\"1.1/2.2\"";
var c = JsonSerializer.Deserialize<Coordinate>(json, new JsonSerializerOptions(JsonSerializerDefaults.Web));
Assert.Equal(1.1, c.X);
Assert.Equal(2.2, c.Y);
}

[Fact]
public void Coordinate_JsonDeserialize_AsXY()
{
string json = "{\"x\":1.1,\"y\":2.2}";
var c = JsonSerializer.Deserialize<Coordinate>(json, new JsonSerializerOptions(JsonSerializerDefaults.Web));
Assert.Equal(1.1, c.X);
Assert.Equal(2.2, c.Y);
}
}

0 comments on commit e761de4

Please sign in to comment.