Skip to content

Commit

Permalink
Fix GeoJsonReader to handle empty arrays (#600)
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Davis <mtnclimb@gmail.com>
  • Loading branch information
dr-jts committed Sep 22, 2020
1 parent 3b594d9 commit 95c809f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> geometryMap = null;
JSONParser parser = new JSONParser();
try {
@SuppressWarnings("unchecked")
Map<String, Object> geometryMap = (Map<String, Object>) 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<String, Object>) 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}

0 comments on commit 95c809f

Please sign in to comment.