Example usage for com.google.gson.stream JsonReader endArray

List of usage examples for com.google.gson.stream JsonReader endArray

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader endArray.

Prototype

public void endArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current array.

Usage

From source file:edu.isi.karma.modeling.alignment.GraphUtil.java

License:Apache License

private static Set<String> readModelIds(JsonReader reader) throws IOException {

    Set<String> modelIds = new HashSet<>();

    reader.beginArray();// w  ww . j  a  va2 s.c o m
    while (reader.hasNext()) {
        modelIds.add(reader.nextString());
    }
    reader.endArray();

    return modelIds;
}

From source file:edu.isi.karma.modeling.alignment.SemanticModel.java

License:Apache License

private static SemanticModel readModel(JsonReader reader) throws IOException {

    String id = null;/*w  w w  .  ja v a 2s  . com*/
    String name = null;
    String description = null;
    DirectedWeightedMultigraph<Node, DefaultLink> graph = null;
    List<ColumnNode> sourceColumns = null;
    HashMap<String, ColumnNode> sourceColumnIds = null;

    Map<String, String> mappingToSourceColumnsIds = null;

    reader.beginObject();
    while (reader.hasNext()) {
        String key = reader.nextName();
        if (key.equals("id") && reader.peek() != JsonToken.NULL) {
            id = reader.nextString();
        } else if (key.equals("name") && reader.peek() != JsonToken.NULL) {
            name = reader.nextString();
        } else if (key.equals("description") && reader.peek() != JsonToken.NULL) {
            description = reader.nextString();
        } else if (key.equals("sourceColumns") && reader.peek() != JsonToken.NULL) {
            sourceColumns = new LinkedList<>();
            sourceColumnIds = new HashMap<>();
            reader.beginArray();
            while (reader.hasNext()) {
                ColumnNode cn = readSourceColumn(reader);
                if (cn != null) {
                    sourceColumns.add(cn);
                    sourceColumnIds.put(cn.getId(), cn);
                }
            }
            reader.endArray();
        } else if (key.equals("mappingToSourceColumns") && reader.peek() != JsonToken.NULL) {
            mappingToSourceColumnsIds = new HashMap<>();
            Map<String, String> oneEntryMapping;
            reader.beginArray();
            while (reader.hasNext()) {
                oneEntryMapping = readMappingToSourceColumn(reader);
                if (oneEntryMapping != null && oneEntryMapping.size() == 1)
                    mappingToSourceColumnsIds.putAll(oneEntryMapping);
            }
            reader.endArray();
        } else if (key.equals("graph") && reader.peek() != JsonToken.NULL) {
            graph = GraphUtil.readGraph(reader);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    Map<ColumnNode, ColumnNode> mappingToSourceColumns = new HashMap<>();
    if (graph != null && mappingToSourceColumnsIds != null && !mappingToSourceColumnsIds.isEmpty()) {
        for (Node n : graph.vertexSet()) {
            if (n instanceof ColumnNode) {
                ColumnNode cn = (ColumnNode) n;
                String sourceColumnId = mappingToSourceColumnsIds.get(cn.getId());
                if (sourceColumnId != null) {
                    ColumnNode sourceColumn = sourceColumnIds.get(sourceColumnId);
                    if (sourceColumn != null)
                        mappingToSourceColumns.put(cn, sourceColumn);
                }
            }
        }
    }

    SemanticModel semanticModel = new SemanticModel(id, GraphUtil.asLabeledGraph(graph), sourceColumns,
            mappingToSourceColumns);
    semanticModel.setName(name);
    semanticModel.setDescription(description);

    return semanticModel;
}

From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java

License:Apache License

public SparseArray<Stop> fetchStops() throws IOException {
    // Download stop data.
    URL stopsURL = new URL(mStopsUrl);
    URLConnection stopsConnection = stopsURL.openConnection();
    stopsConnection.setRequestProperty("User-Agent", mUserAgent);
    InputStream in = new BufferedInputStream(stopsConnection.getInputStream());
    JsonReader reader = new JsonReader(new InputStreamReader(in));
    mStops.clear();//from  w  w w .  j  a va 2  s .  c om
    mRouteStopsMap.clear();

    reader.beginArray();
    while (reader.hasNext()) {
        Stop stop = readStop(reader);
        mStops.put(stop.id, stop);
    }
    reader.endArray();

    reader.close();
    return mStops;
}

From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java

License:Apache License

private Stop readStop(JsonReader reader) throws IOException {
    Stop stop = new Stop();
    ArrayList<Integer> routes = new ArrayList<Integer>();
    reader.beginObject();/*from   w ww .  ja  v  a2  s .  co  m*/
    reader.nextName();
    reader.beginObject();
    while (reader.hasNext()) {
        String key = reader.nextName();
        if (key.equals("id")) {
            stop.id = reader.nextInt();
        } else if (key.equals("name")) {
            stop.name = reader.nextString();
        } else if (key.equals("short_name")) {
            stop.short_name = reader.nextString();
        } else if (key.equals("enabled")) {
            stop.enabled = reader.nextBoolean();
        } else if (key.equals("latitude")) {
            stop.latitude = reader.nextDouble();
        } else if (key.equals("longitude")) {
            stop.longitude = reader.nextDouble();
        } else if (key.equals("routes")) {
            reader.beginArray();
            while (reader.hasNext()) {
                reader.beginObject();
                while (reader.hasNext()) {
                    if (reader.nextName().equals("id")) {
                        routes.add(reader.nextInt());
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
            }
            reader.endArray();
        } else {
            stop.extraAttributes.put(key, reader.nextString());
        }

    }
    reader.endObject();
    reader.endObject();
    Log.d("RPIDataProvider", String.format("Pulling stop %S (%S)...", Integer.toString(stop.id), stop.name));
    for (int i = 0; i < routes.size(); i++) {
        ArrayList<Integer> route = mRouteStopsMap.get(routes.get(i), new ArrayList<Integer>());
        route.add(stop.id);
        mRouteStopsMap.put(routes.get(i), route);
    }
    return stop;
}

From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java

License:Apache License

public SparseArray<Route> fetchRoutes() throws IOException {
    URL routesURL = new URL(mRoutesUrl);
    URLConnection routesConnection = routesURL.openConnection();
    routesConnection.setRequestProperty("User-Agent", mUserAgent);
    InputStream in = new BufferedInputStream(routesConnection.getInputStream());

    URL routePathsURL = new URL(mRoutePathsUrl);
    URLConnection routePathsConnection = routePathsURL.openConnection();
    routePathsConnection.setRequestProperty("User-Agent", mUserAgent);
    InputStream pathInputStream = new BufferedInputStream(routePathsConnection.getInputStream());

    mRoutes.clear();// ww w  .j  a  v a 2s.c o  m
    JsonReader routesReader = new JsonReader(new InputStreamReader(in));
    routesReader.beginArray();
    while (routesReader.hasNext()) {
        Route route = readRoute(routesReader);
        mRoutes.put(route.id, route);
    }
    routesReader.endArray();

    JsonReader routePathReader = new JsonReader(new InputStreamReader(pathInputStream));
    routePathReader.beginObject();
    while (routePathReader.hasNext()) {
        Log.d("RouteDataProvider", "Entering L1.");
        String key = routePathReader.nextName();
        Log.d("RouteDataProvider", String.format("L1: %s", key));
        if (key.equals("routes")) {
            Log.d("RouteDataProvider", "Found 'routes' tag.");
            routePathReader.beginArray();
            while (routePathReader.hasNext()) {
                populateRoutePath(routePathReader);
            }
            routePathReader.endArray();
        } else {
            Log.d("RouteDataProvider", "Found other tag.");
            routePathReader.skipValue();
        }
    }
    routePathReader.endObject();

    return mRoutes;
}

From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java

License:Apache License

private void populateRoutePath(JsonReader reader) throws IOException {
    ArrayList<Coordinate> path = new ArrayList<Coordinate>();
    Integer path_id = 0;//from   w w  w  .ja va 2  s  .c  om
    reader.beginObject();
    while (reader.hasNext()) {
        // Process attributes of a route.
        // We have the array of coordinates making up the path of the route on a map.
        String key = reader.nextName();
        if (key.equals("coords")) {
            Double latitude = 0.0;
            Double longitude = 0.0;
            reader.beginArray();
            // Read coordinate attributes and add to path.
            while (reader.hasNext()) {
                reader.beginObject();
                // Read coordinate attributes.
                while (reader.hasNext()) {
                    String coordinate_key = reader.nextName();
                    if (coordinate_key.equals("latitude")) {
                        latitude = reader.nextDouble();
                    } else if (coordinate_key.equals("longitude")) {
                        longitude = reader.nextDouble();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
                // Add coordinate to path.
                path.add(new Coordinate(latitude, longitude));
                Log.d("CreatePoint",
                        String.format("Inserting point %S, %S", latitude.toString(), longitude.toString()));
            }
            reader.endArray();
        } else if (key.equals("id")) {
            path_id = reader.nextInt();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    Route route = mRoutes.get(path_id);
    route.map_polyline = path;
    mRoutes.put(path_id, route);
    Log.d("RouteDataProvider", String.format("Pulling route %S (%S) with %s coordinates...",
            Integer.toString(route.id), route.name, Integer.valueOf(route.map_polyline.size()).toString()));
}

From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java

License:Apache License

public SparseArray<Vehicle> updateVehicleLocations() throws IOException {
    // Download vehicle location data.
    URL vehicleLocationURL = new URL(mVehicleLocationsUrl);
    URLConnection vehicleLocationConnection = vehicleLocationURL.openConnection();
    vehicleLocationConnection.setRequestProperty("User-Agent", mUserAgent);
    InputStream in = new BufferedInputStream(vehicleLocationConnection.getInputStream());
    JsonReader reader = new JsonReader(new InputStreamReader(in));
    SparseArray<Vehicle> updated_vehicles = new SparseArray<Vehicle>();

    reader.beginArray();/*from   www .  j ava2  s  .c  o m*/
    while (reader.hasNext()) {
        Vehicle shuttle = readVehicleLocation(reader);
        updated_vehicles.put(shuttle.id, shuttle);
        mVehicles.put(shuttle.id, shuttle);
    }
    reader.endArray();

    reader.close();
    return updated_vehicles;
}

From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java

License:Apache License

@SuppressLint("SimpleDateFormat")
private Vehicle readVehicleLocation(JsonReader reader) throws IOException {
    Vehicle shuttle = new Vehicle();
    reader.beginObject();//from   w  w w.jav  a2 s .  co m
    reader.nextName();
    reader.beginObject();
    while (reader.hasNext()) {
        String key = reader.nextName();
        if (key.equals("id")) {
            shuttle.id = reader.nextInt();
        } else if (key.equals("name")) {
            shuttle.name = reader.nextString();
        } else if (key.equals("latest_position")) {
            reader.beginObject();
            while (reader.hasNext()) {
                key = reader.nextName();
                if (key.equals("heading")) {
                    shuttle.heading = reader.nextInt();
                } else if (key.equals("latitude")) {
                    shuttle.latitude = reader.nextDouble();
                } else if (key.equals("longitude")) {
                    shuttle.longitude = reader.nextDouble();
                } else if (key.equals("speed")) {
                    shuttle.speed = reader.nextInt();
                } else if (key.equals("timestamp")) {
                    SimpleDateFormat iso_format = new SimpleDateFormat("yyyy-MM-dd HH:mmZ");
                    try {
                        shuttle.timestamp = iso_format.parse(reader.nextString().replace("T", " "));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else if (key.equals("public_status_message")) {
                    shuttle.description = reader.nextString();
                } else if (key.equals("cardinal_point")) {
                    shuttle.cardinalPoint = reader.nextString();
                }
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    reader.endObject();
    Log.d("RPIDataProvider",
            String.format("Updated Shuttle %S (%S) location...", Integer.toString(shuttle.id), shuttle.name));
    return shuttle;
}

From source file:es.chatclient.server.messages.adapters.ConversDataMessageTypeAdapter.java

@Override
public ConversDataMessage read(JsonReader in) throws IOException {

    final ConversDataMessage conversDate = new ConversDataMessage();
    final List<ConverData> converDataList = new ArrayList();

    in.beginObject();//  www.  j  a va2  s .c o  m

    in.nextName();

    in.beginArray();

    while (in.hasNext()) {
        in.beginObject();
        final ConverData converData = new ConverData();

        while (in.hasNext()) {
            switch (in.nextName()) {

            case "converID":
                converData.setConverID(in.nextString());

                break;

            case "converName":
                converData.setConverName(in.nextString());
                break;

            case "arrayMessages":

                final List<Message> msgList = new ArrayList();

                in.beginArray();
                while (in.hasNext()) {
                    in.beginObject();
                    final Message msg = new Message();

                    while (in.hasNext()) {
                        switch (in.nextName()) {
                        case "msgID":
                            msg.setMsgID(in.nextString());
                            break;

                        case "msgType":
                            msg.setMsgType(in.nextString());
                            break;

                        case "msgText":
                            msg.setMsgText(in.nextString());
                            break;

                        case "msgDate":
                            msg.setMsgDate(in.nextString());
                            break;

                        case "clientId":
                            msg.setClientId(in.nextString());
                            break;

                        case "converId":
                            msg.setConverId(in.nextString());
                            break;

                        case "userNick":
                            msg.setUserNick(in.nextString());
                            break;

                        } //fin switch

                    }

                    msgList.add(msg);
                    in.endObject();

                }

                in.endArray();

                converData.setConverMessages(msgList);

                break;

            default:

                break;

            }

        }

        converDataList.add(converData);
        in.endObject();

    }

    in.endArray();
    conversDate.setConverDataArray(converDataList);

    in.endObject();

    return conversDate;

}

From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java

License:Apache License

static Collection<Object> parseArray(final JsonReader jsonReader) throws IOException {
    assert null != jsonReader : "DEBUG: The json Reader is null";

    Collection<Object> l = new ArrayList<>();
    JsonToken tok;//from   ww  w  .j a v a 2  s  .  c om

    //
    while (jsonReader.hasNext()) {

        tok = jsonReader.peek();
        // we reach the end of this array
        if (JsonToken.END_ARRAY == tok) {
            jsonReader.endArray();
            // return
            break;
        }
        // what token
        switch (tok) {
        // if array/map - parse and append
        case BEGIN_ARRAY:
            l.add(parseArray(jsonReader));
            break;
        case BEGIN_OBJECT:
            l.add(parseObject(jsonReader));
            break;
        // if raw type
        case STRING:
            l.add(jsonReader.nextString());
            break;
        case BOOLEAN:
            l.add(jsonReader.nextBoolean());
            break;
        case NUMBER:
            l.add(jsonReader.nextDouble());
            break;
        // if null , add null and consume
        case NULL:
            l.add(null);
            jsonReader.nextNull();
            break;

        // all other cases are errors
        default:
            throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array");
        }
    }

    return l;
}