opendatalab-de / geojson-jackson

GeoJson POJOs for Jackson - serialize and deserialize objects with ease
http://blog.opendatalab.de
Apache License 2.0
263 stars 94 forks source link

Can not construct instance of org.geojson.GeoJsonObject #56

Open porunov opened 4 years ago

porunov commented 4 years ago

Cannot parse a valid GeoJSON string via readValue(geoJson, GeoJsonObject.class);.

Here is the code to reproduce the exception:

String geoJson = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-126.456789,-2.123,0.0],[-123.456789,2.123,0.0],[-124.456789,2.123,0.0],[-125.456789,-2.123,0.0],[-126.456789,-2.123,0.0]]]},\"properties\":{\"myProp\":\"PropData1\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-123.123,55.555,0.0]},\"properties\":{\"myProp\":\"PropData2\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[123.456789,2.123,0.0],[-123.456789,2.123,0.0],[-124.456789,2.123,0.0],[-125.456789,-2.123,0.0],[123.456789,2.123,0.0]]]},\"properties\":{\"myProp\":\"PropData3\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-15.123,75.555,0.0]},\"properties\":{\"myProp\":\"PropData4\"}}]}";

GeoJsonObject object = new ObjectMapper().readValue(geoJson, GeoJsonObject.class);

The log is the next:

Can not construct instance of org.geojson.GeoJsonObject, problem: abstract types can only be instantiated with additional type information
 at [Source: java.io.StringReader@aa0657b; line: 1, column: 1]
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of org.geojson.GeoJsonObject, problem: abstract types can only be instantiated with additional type information
 at [Source: java.io.StringReader@aa0657b; line: 1, column: 1]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:233)
    at org.codehaus.jackson.map.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:60)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)

For convince here is the geoJson which I try to parse:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -126.456789,
              -2.123,
              0.0
            ],
            [
              -123.456789,
              2.123,
              0.0
            ],
            [
              -124.456789,
              2.123,
              0.0
            ],
            [
              -125.456789,
              -2.123,
              0.0
            ],
            [
              -126.456789,
              -2.123,
              0.0
            ]
          ]
        ]
      },
      "properties": {
        "myProp": "PropData1"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -123.123,
          55.555,
          0.0
        ]
      },
      "properties": {
        "myProp": "PropData2"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              123.456789,
              2.123,
              0.0
            ],
            [
              -123.456789,
              2.123,
              0.0
            ],
            [
              -124.456789,
              2.123,
              0.0
            ],
            [
              -125.456789,
              -2.123,
              0.0
            ],
            [
              123.456789,
              2.123,
              0.0
            ]
          ]
        ]
      },
      "properties": {
        "myProp": "PropData3"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -15.123,
          75.555,
          0.0
        ]
      },
      "properties": {
        "myProp": "PropData4"
      }
    }
  ]
}
JoaoEmanoelConinch commented 3 years ago

It happens to me too. Did you solve the problem? Could you help me?