java 可变数组实例,具有可变维数组的Java对象

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":

<>

}

},

{ "type": "Feature", "name": "Indonesia", "geometry":

{ "type": "MultiPolygon", "coordinates":

<>

}

},

//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 a com.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 for MultiPolygon and Polygon, Gson tries to instantiate a Shape and errors.

Can I use generics or something else sneaky to get around this?

解决方案

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 {

@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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值