diff --git a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs index dead24a..5d8a99d 100644 --- a/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs +++ b/src/NetTopologySuite.IO.GeoJSON4STJ/Converters/StjFeatureConverter.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using NetTopologySuite.Features; @@ -107,7 +108,12 @@ public override IFeature Read(ref Utf8JsonReader reader, Type objectType, JsonSe break; case JsonTokenType.Number: - throw new JsonException("Number value cannot be boxed as a decimal: " + reader.GetString()); + // "number" is technically SUPPOSED to be a float, so even though we + // prefer reading it as a decimal, it should at least be safe enough + // to use GetDouble() for the exception message, and certainly safer + // than GetString() which an older version of this code used to use, + // which ALWAYS throws for Number tokens (airbreather 2023-09-14). + throw new JsonException("Number value cannot be boxed as a decimal: " + reader.GetDouble()); case JsonTokenType.String: feature.Id = reader.GetString(); diff --git a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs index e0dbe04..7dbc9e3 100644 --- a/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs +++ b/test/NetTopologySuite.IO.GeoJSON4STJ.Test/Converters/FeatureConverterTest.cs @@ -227,7 +227,25 @@ public void TestFeatureIdSerializedToRoot() }; string expected = "{\"type\":\"Feature\",\"id\":1,\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]},\"properties\":{\"name\":\"Test feature\"}}"; - Assert.That(expected, Is.EqualTo(expected)); + Assert.That(JsonSerializer.Serialize(feature, options), Is.EqualTo(expected)); + } + + [GeoJsonIssueNumber(132)] + [Test] + public void TestNumericFeatureIdMustBeValidDecimal() + { + string serialized = $@" +{{ + ""type"": ""Feature"", + ""id"": {double.MaxValue}, + ""geometry"": {{ + ""type"": ""Point"", + ""coordinates"": [0, 0] + }} +}} + "; + + Assert.That(() => JsonSerializer.Deserialize(serialized, DefaultOptions), Throws.InstanceOf()); } } }