Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoJsonReader.read and ClassCastException #598

Closed
karol-bujacek opened this issue Sep 22, 2020 · 2 comments
Closed

GeoJsonReader.read and ClassCastException #598

karol-bujacek opened this issue Sep 22, 2020 · 2 comments

Comments

@karol-bujacek
Copy link

I am using GeoJsonReader from jts-io-common 1.17.1 for reading (and validating) input string in geoJson format. (Previously I used version 1.16.0 but then tried the latest version).

It works correctly for most ‘bad inputs’ and a ParseException is thrown.
In one case (I am not able to find more at this time) the read method throws ClassCastException instead of ParseException: for input string "[]".

The problem is in this method:
org.locationtech.jts.io.geojson.GeoJsonReader#read(java.io.Reader)

and line

Map<String, Object> geometryMap = (Map)parser.parse(reader);

JSONParser understands the "[]" string as a JSONArray, but JSONArray cannot be casted to java.util.Map.
I believe that the result of parser.parse(reader) should be checked for object type and fail (throw ParseException) if it is not JSONObject.

    public Geometry read(Reader reader) throws ParseException {
        Geometry result = null;
        JSONParser parser = new JSONParser();

        try {
            Map<String, Object> geometryMap = (Map)parser.parse(reader);
            GeometryFactory geometryFactory = null;
            if (this.gf == null) {
                geometryFactory = this.getGeometryFactory(geometryMap);
            } else {
                geometryFactory = this.gf;
            }

            result = this.create(geometryMap, geometryFactory);
            return result;
        } catch (org.json.simple.parser.ParseException var6) {
            throw new ParseException(var6);
        } catch (IOException var7) {
            throw new ParseException(var7);
        }
    }
@dr-jts
Copy link
Contributor

dr-jts commented Sep 22, 2020

Makes sense. Thanks for the issue report.

@dr-jts
Copy link
Contributor

dr-jts commented Sep 22, 2020

Fixed by #600

@dr-jts dr-jts closed this as completed Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants