问题
I'm trying to parse a GeoJSON using Gson, and I have an JSON that looks something like this (simplified):
{"type": "FeatureCollection",
"features": [
{ "type": "Feature", "name": "Afghanistan", "geometry":
{ "type": "Polygon", "coordinates":
<<a 3-dimensional double array (double[][][] in Java)>>
}
},
{ "type": "Feature", "name": "Indonesia", "geometry":
{ "type": "MultiPolygon", "coordinates":
<<a 4-dimensional double array (double[][][][] in Java)>>
}
},
//etc...
]
}
I need to have a java class that can be correlated with the Gson object in order to use it, but I'm struggling on what to with an array of similar objects whose variable coordinates
is not the same. I have the equivalent of:
class FeatureCollection{
String type;
Feature[] features;
}
class Feature{
String type,name;
Shape geometry;
}
class Shape{
String type;
??? coordinates;
}
- When I try to use
double[][][]
instead of???
, I get acom.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a double but was BEGIN_ARRAY at line 6 column 146
- When I try making
Shape
an abstract class and use subclasses forMultiPolygon
andPolygon
,Gson
tries to instantiate aShape
and errors.
Can I use generics or something else sneaky to get around this?
回答1:
You'll need to have your own custom JsonDeserializer since the coordinates
variable doesn't have a set of defined array dimensions. I recommend using an interface for the shape, then writing a deserializer for it, like so:
public interface Shape {
ShapeType getType();
enum ShapeType { Polygon, MultiPolygon }
}
Then implementations for each type. ShapeType.Polygon
:
public class PolygonShape implements Shape {
private final ShapeType type = ShapeType.Polygon;
private double[][][] coordinates;
public ShapeType getType() {
return type;
}
public double[][][] getCoordinates() {
return coordinates;
}
public void setCoordinates(double[][][] coordinates) {
this.coordinates = coordinates;
}
}
And ShapeType.MultiPolygon
:
public class MultiPolygonShape implements Shape {
private final ShapeType type = ShapeType.MultiPolygon;
private double[][][][] coordinates;
public ShapeType getType() {
return type;
}
public double[][][][] getCoordinates() {
return coordinates;
}
public void setCoordinates(double[][][][] coordinates) {
this.coordinates = coordinates;
}
}
Then lastly, your deserializer will rely on the type
from each implementation:
public class ShapeDeserializer implements JsonDeserializer<Shape> {
@Override
public Shape deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
ShapeType type = context.deserialize(jsonObject.get("type"), ShapeType.class);
switch (type) {
case Polygon:
return context.deserialize(json, PolygonShape.class);
case MultiPolygon:
return context.deserialize(json, MultiPolygonShape.class);
default:
throw new JsonParseException("Unrecognized shape type: " + type);
}
}
}
Using this, you can also create other implementations based on the type of shape and add them to the switch
to support them. For example, to support a new Line
type:
case Line:
return context.deserialize(json, LineShape.class);
Don't forget to register it with the GsonBuilder.registerTypeAdapter method:
GsonBuilder builder;
// ...
builder.registerTypeAdapter(Shape.class, new ShapeDeserializer());
来源:https://stackoverflow.com/questions/18551587/java-object-with-variable-dimensional-array