diff --git a/modules/io/common/src/main/java/org/locationtech/jts/io/geojson/GeoJsonReader.java b/modules/io/common/src/main/java/org/locationtech/jts/io/geojson/GeoJsonReader.java index 0bc95e0fcb..ad7c48fdc0 100644 --- a/modules/io/common/src/main/java/org/locationtech/jts/io/geojson/GeoJsonReader.java +++ b/modules/io/common/src/main/java/org/locationtech/jts/io/geojson/GeoJsonReader.java @@ -98,32 +98,31 @@ public Geometry read(String json) throws ParseException { * @return The resulting JTS Geometry * * @throws ParseException - * throws a ParseException if the JSON string cannot be parsed + * throws a ParseException if the JSON string cannot be parsed as a Geometry */ + @SuppressWarnings("unchecked") public Geometry read(Reader reader) throws ParseException { - - Geometry result = null; - + Map geometryMap = null; JSONParser parser = new JSONParser(); try { - @SuppressWarnings("unchecked") - Map geometryMap = (Map) parser - .parse(reader); - - GeometryFactory geometryFactory = null; - if (this.gf == null) { - geometryFactory = this.getGeometryFactory(geometryMap); - } else { - geometryFactory = this.gf; - } - - result = create(geometryMap, geometryFactory); - - } catch (org.json.simple.parser.ParseException e) { + Object obj = parser.parse(reader); + geometryMap = (Map) obj; + } catch (ClassCastException e) { + throw new ParseException("Could not parse Geometry from Json string."); + }catch (org.json.simple.parser.ParseException e) { throw new ParseException(e); } catch (IOException e) { throw new ParseException(e); } + + GeometryFactory geometryFactory = null; + if (this.gf == null) { + geometryFactory = this.getGeometryFactory(geometryMap); + } else { + geometryFactory = this.gf; + } + + Geometry result = create(geometryMap, geometryFactory); return result; } diff --git a/modules/io/common/src/test/java/org/locationtech/jts/io/geojson/GeoJsonReaderTest.java b/modules/io/common/src/test/java/org/locationtech/jts/io/geojson/GeoJsonReaderTest.java new file mode 100644 index 0000000000..527952e990 --- /dev/null +++ b/modules/io/common/src/test/java/org/locationtech/jts/io/geojson/GeoJsonReaderTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2020 Martin Davis. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * and Eclipse Distribution License v. 1.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * + * http://www.eclipse.org/org/documents/edl-v10.php. + */ + +package org.locationtech.jts.io.geojson; + +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.io.ParseException; + +import test.jts.GeometryTestCase; + +public class GeoJsonReaderTest extends GeometryTestCase { + + public GeoJsonReader geoJsonRdr; + + public GeoJsonReaderTest(String name) { + super(name); + } + + @Override + public void setUp() throws Exception { + this.geoJsonRdr = new GeoJsonReader(); + } + + public void testEmptyArray() throws ParseException { + runParseEx("[]"); + } + + public void testEmptyObject() throws ParseException { + runParseEx("{}"); + } + + private void runParseEx(String json) { + try { + Geometry geom = geoJsonRdr.read(json); + } + catch (ParseException ex) { + assertTrue(true); + } + } + +}