Example usage for com.google.gson JsonObject getAsNumber

List of usage examples for com.google.gson JsonObject getAsNumber

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsNumber.

Prototype

public Number getAsNumber() 

Source Link

Document

convenience method to get this element as a Number .

Usage

From source file:com.ibm.common.geojson.as2.GeoAdapter.java

License:Apache License

@Override
public GeoObject deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    GeoObject.Builder geo = null;/*ww w . j  a  v  a 2s  .  co  m*/
    checkArgument(element.isJsonObject());
    JsonObject obj = element.getAsJsonObject();
    checkArgument(obj.has("type"));
    GeoObject.Type et = Enums.getIfPresent(GeoObject.Type.class, obj.get("type").getAsString().toUpperCase())
            .orNull();
    checkArgument(et != null);
    switch (et) {
    case FEATURE:
        geo = GeoMakers.feature();
        break;
    case FEATURECOLLECTION:
        geo = GeoMakers.featureCollection();
        type = Feature.class;
        break;
    case GEOMETRYCOLLECTION:
        geo = GeoMakers.geometryCollection();
        type = Geometry.class;
        break;
    case LINESTRING:
        geo = GeoMakers.linestring();
        type = Position.class;
        break;
    case MULTILINESTRING:
        geo = GeoMakers.multiLineString();
        type = LineString.class;
        break;
    case MULTIPOINT:
        geo = GeoMakers.multipoint();
        type = Position.class;
        break;
    case MULTIPOLYGON:
        geo = GeoMakers.multiPolygon();
        type = Polygon.class;
        break;
    case POINT:
        geo = GeoMakers.point();
        type = null;
        break;
    case POLYGON:
        geo = GeoMakers.polygon();
        type = LineString.class;
        break;
    }

    for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
        JsonElement el = entry.getValue();
        String name = entry.getKey();
        if ("crs".equals(name)) {
            CRS.Builder cb = new CRS.Builder();
            JsonObject o = el.getAsJsonObject();
            if (o.has("type"))
                cb.type(o.get("type").getAsString());
            if (o.has("properties")) {
                JsonObject p = o.get("properties").getAsJsonObject();
                for (Map.Entry<String, JsonElement> e : p.entrySet()) {
                    cb.set(e.getKey(), context.deserialize(e.getValue(), Object.class));
                }
            }
            geo.crs(cb.get());
        } else if ("properties".equals(name)) {
            geo.set("properties", context.deserialize(el, Map.class));
        } else if ("bbox".equals(name)) {
            BoundingBox.Builder bb = new BoundingBox.Builder();
            float[] points = context.deserialize(el, float[].class);
            bb.add(points);
            geo.boundingBox(bb.get());
        } else if ("features".equals(name)) {
            Feature[] features = context.deserialize(el, Feature[].class);
            FeatureCollection.Builder fcb = (FeatureCollection.Builder) geo;
            for (Feature f : features)
                fcb.add(f);
        } else if ("coordinates".equals(name)) {
            switch (et) {
            case LINESTRING: {
                LineString.Builder lsb = (LineString.Builder) geo;
                float[][] positions = context.deserialize(el, float[][].class);
                boolean ring = ring(positions);
                if (ring)
                    lsb.linearRing();
                for (int n = 0; n < positions.length; n++) {
                    if (!ring || (ring && n < positions.length - 1))
                        lsb.add(toPosition(positions[n]));
                }
                break;
            }
            case MULTIPOINT: {
                MultiPoint.Builder lsb = (MultiPoint.Builder) geo;
                float[][] positions = context.deserialize(el, float[][].class);
                for (float[] pos : positions)
                    lsb.add(toPosition(pos));
                break;
            }
            case MULTILINESTRING: {
                MultiLineString.Builder mlb = (MultiLineString.Builder) geo;
                float[][][] positions = context.deserialize(el, float[][][].class);
                for (float[][] lines : positions) {
                    LineString.Builder lsb = GeoMakers.linestring();
                    boolean ring = ring(lines);
                    if (ring)
                        lsb.linearRing();
                    for (int n = 0; n < lines.length; n++) {
                        if (!ring || (ring && n < lines.length - 1))
                            lsb.add(toPosition(lines[n]));
                    }
                    for (float[] pos : lines)
                        lsb.add(toPosition(pos));
                    mlb.add(lsb);
                }
                break;
            }
            case POLYGON: {
                Polygon.Builder mlb = (Polygon.Builder) geo;
                float[][][] positions = context.deserialize(el, float[][][].class);
                for (float[][] lines : positions) {
                    LineString.Builder lsb = GeoMakers.linestring();
                    for (float[] pos : lines)
                        lsb.add(toPosition(pos));
                    mlb.add(lsb);
                }
                break;
            }
            case MULTIPOLYGON: {
                MultiPolygon.Builder mpb = (MultiPolygon.Builder) geo;
                float[][][][] positions = context.deserialize(el, float[][][][].class);
                for (float[][][] polygons : positions) {
                    Polygon.Builder pb = GeoMakers.polygon();
                    for (float[][] lines : polygons) {
                        LineString.Builder lsb = GeoMakers.linestring();
                        for (float[] pos : lines)
                            lsb.add(toPosition(pos));
                        pb.add(lsb);
                    }
                    mpb.add(pb);
                }
                break;
            }
            case POINT:
                Point.Builder pb = (Point.Builder) geo;
                float[] position = context.deserialize(el, float[].class);
                pb.position(toPosition(position));
                break;
            default:
                break;
            }
        } else if ("geometries".equals(name)) {
            Geometry[] geos = context.deserialize(el, Geometry[].class);
            GeometryCollection.Builder fcb = (GeometryCollection.Builder) geo;
            for (Geometry<?, ?> g : geos)
                fcb.add(g);
        } else {
            if (el.isJsonArray()) {
                geo.set(name, context.deserialize(el, Object.class));
            } else if (el.isJsonObject()) {
                geo.set(name, context.deserialize(el, GeoObject.class));
            } else if (el.isJsonPrimitive()) {
                JsonPrimitive p = el.getAsJsonPrimitive();
                if (p.isBoolean())
                    geo.set(name, p.getAsBoolean());
                else if (p.isNumber())
                    geo.set(name, p.getAsNumber());
                else if (p.isString())
                    geo.set(name, p.getAsString());
            }
        }
    }

    return geo.get();
}