Skip to content

Commit e51f9b5

Browse files
committed
Restore support for serializer options when reading positions
1 parent 0ec813b commit e51f9b5

File tree

6 files changed

+104
-5
lines changed

6 files changed

+104
-5
lines changed

src/GeoJSON.Text.Test.Unit/GeoJSON.Text.Test.Unit.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<None Remove="Feature\GenericFeatureTests_Can_Deserialize_Typed_Point_Feature.json" />
2323
<None Remove="Feature\GenericFeatureTests_Can_Serialize_Typed_Point_Feature.json" />
2424
<None Remove="Geometry\LineStringTests_Can_Deserialize.json" />
25+
<None Remove="Geometry\LineStringTests_Can_Deserialize_Strings.json" />
26+
<None Remove="Geometry\LineStringTests_Can_Deserialize_String_Literals.json" />
2527
<None Remove="Geometry\LineStringTests_Can_Deserialize_With_Altitude.json" />
2628
<None Remove="Geometry\LineStringTests_Can_Serialize.json" />
2729
<None Remove="Geometry\MultiLineStringTests_Can_Deserialize.json" />
@@ -49,6 +51,8 @@
4951
<EmbeddedResource Include="Feature\GenericFeatureTests_Can_Deserialize_Typed_Point_Feature.json" />
5052
<EmbeddedResource Include="Feature\GenericFeatureTests_Can_Serialize_Typed_Point_Feature.json" />
5153
<EmbeddedResource Include="Geometry\LineStringTests_Can_Deserialize.json" />
54+
<EmbeddedResource Include="Geometry\LineStringTests_Can_Deserialize_Strings.json" />
55+
<EmbeddedResource Include="Geometry\LineStringTests_Can_Deserialize_String_Literals.json" />
5256
<EmbeddedResource Include="Geometry\LineStringTests_Can_Serialize.json" />
5357
<EmbeddedResource Include="Geometry\MultiLineStringTests_Can_Deserialize.json" />
5458
<EmbeddedResource Include="Geometry\MultiLineStringTests_Can_Serialize.json" />

src/GeoJSON.Text.Test.Unit/Geometry/LineStringTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ public void Can_Deserialize()
8383
Assert.AreEqual(expectedLineString.Coordinates[0].Longitude, actualLineString.Coordinates[0].Longitude);
8484
}
8585

86+
[Test]
87+
public void Can_Deserialize_Strings()
88+
{
89+
var coordinates = new List<IPosition>
90+
{
91+
new Position(52.370725881211314, 4.889259338378906),
92+
new Position(52.3711451105601, 4.895267486572266),
93+
new Position(52.36931095278263, 4.892091751098633),
94+
new Position(52.370725881211314, 4.889259338378906)
95+
};
96+
97+
var expectedLineString = new LineString(coordinates);
98+
99+
var json = GetExpectedJson();
100+
var options = new JsonSerializerOptions { NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString };
101+
var actualLineString = JsonSerializer.Deserialize<LineString>(json, options);
102+
103+
Assert.AreEqual(expectedLineString, actualLineString);
104+
105+
Assert.AreEqual(4, actualLineString.Coordinates.Count);
106+
Assert.AreEqual(expectedLineString.Coordinates[0].Latitude, actualLineString.Coordinates[0].Latitude);
107+
Assert.AreEqual(expectedLineString.Coordinates[0].Longitude, actualLineString.Coordinates[0].Longitude);
108+
}
109+
86110
[Test]
87111
public void Can_Deserialize_With_Altitude()
88112
{
@@ -108,6 +132,34 @@ public void Can_Deserialize_With_Altitude()
108132
Assert.AreEqual(expectedLineString.Coordinates[2].Altitude, actualLineString.Coordinates[2].Altitude);
109133
}
110134

135+
[Test]
136+
public void Can_Deserialize_String_Literals()
137+
{
138+
var coordinates = new List<IPosition>
139+
{
140+
new Position(52.370725881211314, 4.889259338378906, double.NegativeInfinity),
141+
new Position(52.3711451105601, 4.895267486572266, double.PositiveInfinity),
142+
new Position(52.36931095278263, 4.892091751098633, double.NaN),
143+
new Position(52.370725881211314, 4.889259338378906, double.NegativeInfinity)
144+
};
145+
146+
var expectedLineString = new LineString(coordinates);
147+
148+
var json = GetExpectedJson();
149+
var options = new JsonSerializerOptions { NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals };
150+
var actualLineString = JsonSerializer.Deserialize<LineString>(json, options);
151+
152+
bool b = expectedLineString.Coordinates[0].Equals(actualLineString.Coordinates[0]);
153+
Assert.AreEqual(expectedLineString, actualLineString);
154+
155+
Assert.AreEqual(4, actualLineString.Coordinates.Count);
156+
Assert.AreEqual(expectedLineString.Coordinates[0].Latitude, actualLineString.Coordinates[0].Latitude);
157+
Assert.AreEqual(expectedLineString.Coordinates[0].Longitude, actualLineString.Coordinates[0].Longitude);
158+
Assert.AreEqual(expectedLineString.Coordinates[0].Altitude, actualLineString.Coordinates[0].Altitude);
159+
Assert.AreEqual(expectedLineString.Coordinates[1].Altitude, actualLineString.Coordinates[1].Altitude);
160+
Assert.AreEqual(expectedLineString.Coordinates[2].Altitude, actualLineString.Coordinates[2].Altitude);
161+
}
162+
111163
[Test]
112164
public void Constructor_No_Coordinates_Throws_Exception()
113165
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"coordinates": [
3+
[4.8892593383789062, 52.370725881211314, "-Infinity"],
4+
[4.8952674865722656, 52.3711451105601, "Infinity"],
5+
[4.8920917510986328, 52.369310952782627, "NaN"],
6+
[4.8892593383789062, 52.370725881211314, "-Infinity"]
7+
],
8+
"type": "LineString"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"coordinates": [
3+
["4.8892593383789062", "52.370725881211314"],
4+
["4.8952674865722656", "52.3711451105601"],
5+
["4.8920917510986328", "52.369310952782627"],
6+
["4.8892593383789062", "52.370725881211314"]
7+
],
8+
"type": "LineString"
9+
}

src/GeoJSON.Text/Converters/PositionConverter.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,48 @@ public override IPosition Read(
5555
{
5656
throw new ArgumentException("Expected number, but got end of data");
5757
}
58+
5859
if (reader.TokenType == JsonTokenType.EndArray)
5960
{
6061
throw new ArgumentException("Expected 2 or 3 coordinates but got 0");
6162
}
62-
if (reader.TokenType != JsonTokenType.Number)
63+
64+
if (reader.TokenType == JsonTokenType.Number)
65+
{
66+
lng = reader.GetDouble();
67+
}
68+
else if (reader.TokenType == JsonTokenType.String)
69+
{
70+
lng = JsonSerializer.Deserialize<double>(ref reader, options);
71+
}
72+
else
6373
{
6474
throw new ArgumentException("Expected number but got other type");
6575
}
66-
lng = reader.GetDouble();
6776

6877
// Read latitude
6978
if (!reader.Read())
7079
{
7180
throw new ArgumentException("Expected number, but got end of data");
7281
}
82+
7383
if (reader.TokenType == JsonTokenType.EndArray)
7484
{
7585
throw new ArgumentException("Expected 2 or 3 coordinates but got 1");
7686
}
77-
if (reader.TokenType != JsonTokenType.Number)
87+
88+
if (reader.TokenType == JsonTokenType.Number)
89+
{
90+
lat = reader.GetDouble();
91+
}
92+
else if (reader.TokenType == JsonTokenType.String)
93+
{
94+
lat = JsonSerializer.Deserialize<double>(ref reader, options);
95+
}
96+
else
7897
{
7998
throw new ArgumentException("Expected number but got other type");
8099
}
81-
lat = reader.GetDouble();
82100

83101
// Read altitude, or return if end of array is found
84102
if (!reader.Read())
@@ -97,6 +115,10 @@ public override IPosition Read(
97115
{
98116
alt = reader.GetDouble();
99117
}
118+
else if (reader.TokenType == JsonTokenType.String)
119+
{
120+
alt = JsonSerializer.Deserialize<double>(ref reader, options);
121+
}
100122
else
101123
{
102124
throw new ArgumentException("Expected number but got other type");

src/GeoJSON.Text/DoubleTenDecimalPlaceComparer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ public class DoubleTenDecimalPlaceComparer : IEqualityComparer<double>
1515
{
1616
public bool Equals(double x, double y)
1717
{
18-
return Math.Abs(x - y) < 0.0000000001;
18+
return (double.IsNaN(x) && double.IsNaN(y)) ||
19+
(double.IsInfinity(x) && double.IsInfinity(y)) ||
20+
(double.IsNegativeInfinity(x) && double.IsNegativeInfinity(y)) ||
21+
Math.Abs(x - y) < 0.0000000001;
1922
}
2023

2124
public int GetHashCode(double obj)

0 commit comments

Comments
 (0)