Skip to content

Commit

Permalink
Remove use of ParseException, use JsonReaderException
Browse files Browse the repository at this point in the history
  • Loading branch information
FObermaier committed Sep 15, 2023
1 parent 290175f commit 2215276
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
reader.Read();
if (reader.TokenType != JsonToken.String && (string)reader.Value != "FeatureCollection")
{
throw new ParseException("Expected value 'FeatureCollection' not found");
throw new JsonReaderException("Expected value 'FeatureCollection' not found");
}

read = reader.Read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
reader.Read();
if ((string)reader.Value != "Feature")
{
throw new ParseException("Expected value 'Feature' not found");
throw new JsonReaderException("Expected value 'Feature' not found");
}

read = reader.Read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
break;

case GeoJsonObjectType.GeometryCollection:
throw new ParseException("GeometryCollection is currently not supported.");
throw new JsonReaderException("GeometryCollection is currently not supported.");
}

reader.Read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private Geometry ParseGeometry(JsonReader reader, JsonSerializer serializer)
reader.ReadOrThrow();
geometryType = Enum.TryParse((string)reader.Value, true, out GeoJsonObjectType parsedGeometryType)
? parsedGeometryType
: throw new ParseException($"Unknown geometry type '{(string)reader.Value}'");
: throw new JsonReaderException($"Unknown geometry type '{(string)reader.Value}'");
reader.ReadOrThrow();
}

Expand Down
28 changes: 24 additions & 4 deletions src/NetTopologySuite.IO.GeoJSON/GeoJsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class GeoJsonReader
private readonly GeometryFactory _factory;
private readonly JsonSerializerSettings _serializerSettings;
private readonly int _dimension;
private readonly RingOrientationOption _ringOrientationOption;

/// <summary>
/// Creates an instance of this class
Expand All @@ -30,7 +31,7 @@ public GeoJsonReader()
/// <param name="factory">The factory to use when creating geometries</param>
/// <param name="serializerSettings">The serializer setting</param>
public GeoJsonReader(GeometryFactory factory, JsonSerializerSettings serializerSettings)
: this(factory, serializerSettings, 2)
: this(factory, serializerSettings, GeoJsonSerializer.Dimension)
{
}

Expand All @@ -41,11 +42,26 @@ public GeoJsonReader(GeometryFactory factory, JsonSerializerSettings serializerS
/// <param name="factory">The factory to use when creating geometries</param>
/// <param name="serializerSettings">The serializer setting</param>
/// <param name="dimension">The number of dimensions to handle. Must be 2 or 3.</param>
public GeoJsonReader(GeometryFactory factory, JsonSerializerSettings serializerSettings, int dimension)
public GeoJsonReader(GeometryFactory factory, JsonSerializerSettings serializerSettings, int dimension) :
this(factory, serializerSettings, dimension, GeoJsonSerializer.RingOrientationOption)
{
}

/// <summary>
/// Creates an instance of this class using the provided <see cref="GeometryFactory"/> and
/// <see cref="JsonSerializerSettings"/>.
/// </summary>
/// <param name="factory">The factory to use when creating geometries</param>
/// <param name="serializerSettings">The serializer setting</param>
/// <param name="dimension">The number of dimensions to handle. Must be 2 or 3.</param>
/// <param name="ringOrientationOption"></param>
public GeoJsonReader(GeometryFactory factory, JsonSerializerSettings serializerSettings,
int dimension, RingOrientationOption ringOrientationOption)
{
_factory = factory;
_serializerSettings = serializerSettings;
_dimension = dimension;
_ringOrientationOption = ringOrientationOption;
}

/// <summary>
Expand Down Expand Up @@ -79,14 +95,18 @@ public TObject Read<TObject>(JsonReader json)
throw new ArgumentNullException(nameof(json));
}

var g = GeoJsonSerializer.Create(_serializerSettings, _factory, _dimension);
var g = GeoJsonSerializer.Create(_serializerSettings, _factory, _dimension, _ringOrientationOption);
try
{
return g.Deserialize<TObject>(json);
}
catch (JsonReaderException)
{
throw;
}
catch (Exception ex)
{
throw new ParseException(ex);
throw new JsonReaderException("Failed to correctly read json", ex);
}
}
}
Expand Down
66 changes: 44 additions & 22 deletions test/NetTopologySuite.IO.GeoJSON.Test/GeoJsonReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,41 +319,63 @@ public void TestMalformedPoint()

// Point
Assert.That(() => rdr.Read<Point>("\"type\": \"Point\", \"coordinates\": [99.0, 89.0]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Point>("{\"type\": \"Point\", \"coordinates\": [99.0, 89.0]{"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Point>("{\"type\": \"Point\", \"coordinates\": [99.0, 89.0]"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<EndOfStreamException>());
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Point>("{\"type\": \"Pooint\", \"coordinates\": [99.0, 89.0]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<ParseException>());
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Point>("{\"type\": \"Point\", \"coordinates\": [99.0, B]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Point>("{\"type\": \"Point\", \"coordinates\": 99.0, 89.0]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Point>("{\"type\": \"Point\", \"coordinates\": [99.0, 89.0}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Throws.InstanceOf<JsonReaderException>());
}

[Test]
public void TestMalformedLineString()
{
var rdr = new GeoJsonReader();

// Point
Assert.That(() => rdr.Read<LineString>("\"type\": \"LineString\", \"coordinates\": [99.0, 89.0, 100.0, 89.0]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [99.0, 89.0, 100.0, 89.0]{"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [99.0, 89.0, 100.0, 89.0]"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<EndOfStreamException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineSting\", \"coordinates\": [99.0, 89.0, 100.0, 89.0]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<ParseException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [99.0, B, 100.0, 89.0]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": 99.0, 89.0, 100.0, 89.0]}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [99.0, 89.0, 100.0, 89.0}"),
Throws.InstanceOf<ParseException>().With.InnerException.InstanceOf<JsonReaderException>());
// LineString
Assert.That(() => rdr.Read<LineString>("\"type\": \"LineString\", \"coordinates\": [[99.0, 89.0], 100.0, 89.0]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [[99.0, 89.0], [100.0, 89.0]]{"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [[99.0, 89.0], [100.0, 89.0]]"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineSting\", \"coordinates\": [[99.0, 89.0], [100.0, 89.0]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [[99.0, B], [100.0, 89.0]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [99.0, 89.0], [100.0, 89.0]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<LineString>("{\"type\": \"LineString\", \"coordinates\": [[99.0, 89.0], [100.0, 89.0]}"),
Throws.InstanceOf<JsonReaderException>());
}

[Test]
public void TestMalformedPolygon()
{
var rdr = new GeoJsonReader();

// Polygon
Assert.That(() => rdr.Read<Polygon>("\"type\": \"Polygon\", \"coordinates\": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Polygon>("\"type\": \"Polygon\", \"coordinates\": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]{"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Polygon>("\"type\": \"Polygon\", \"coordinates\": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Polygon>("\"type\": \"Poygon\", \"coordinates\": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Polygon>("\"type\": \"Polygon\", \"coordinates\": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, A.0], [100.0, 0.0] ]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Polygon>("\"type\": \"Polygon\", \"coordinates\": [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]}"),
Throws.InstanceOf<JsonReaderException>());
Assert.That(() => rdr.Read<Polygon>("\"type\": \"Polygon\", \"coordinates\": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]}"),
Throws.InstanceOf<JsonReaderException>());
}
}
}

0 comments on commit 2215276

Please sign in to comment.