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:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private List<FuxiJob> loadJobsFromStream(InputStream in) throws ODPSConsoleException {
    boolean debug = true;
    ArrayList<FuxiJob> jobs = new ArrayList<FuxiJob>();
    if (debug) {//from   www.  j  av a 2  s.  c om
        try {
            JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("mapReduce")) {
                    reader.beginObject();
                    while (reader.hasNext()) {
                        String nameInMapReduce = reader.nextName();
                        if (nameInMapReduce.equals("jobs")) {
                            reader.beginArray();
                            while (reader.hasNext()) {
                                jobs.add(getFuxiJobFromJson(reader));
                            }
                            reader.endArray();
                        } else if (nameInMapReduce.equals("jsonSummary")) {
                            getInfoFromJsonSummary(jobs, reader.nextString());
                        } else {
                            reader.skipValue();
                        }
                    }
                    reader.endObject();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ODPSConsoleException("Bad json format");
        }
    }

    return jobs;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiJob getFuxiJobFromJson(JsonReader reader) throws IOException {
    FuxiJob job = new FuxiJob();

    reader.beginObject();/*from   w  w  w  .j a  v a 2s.c  o  m*/
    while (reader.hasNext()) {
        String nameInJob = reader.nextName();
        if (nameInJob.equals("name")) {
            job.name = reader.nextString();
        } else if (nameInJob.equals("tasks")) {
            reader.beginArray();
            job.tasks = new ArrayList<FuxiTask>();
            while (reader.hasNext()) {
                job.tasks.add(getFuxiTaskFromJson(reader));
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return job;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiTask getFuxiTaskFromJson(JsonReader reader) throws IOException {
    FuxiTask task = new FuxiTask();

    reader.beginObject();/*from w  w  w.j  av a 2  s.c  o m*/
    while (reader.hasNext()) {
        String nameInTask = reader.nextName();
        if (nameInTask.equals("name")) {
            task.name = reader.nextString();
        } else if (nameInTask.equals("instances")) {
            task.instances = new ArrayList<FuxiInstance>();
            task.upTasks = new ArrayList<FuxiTask>();
            task.downTasks = new ArrayList<FuxiTask>();
            reader.beginArray();
            while (reader.hasNext()) {
                task.instances.add(getFuxiInstanceFromJson(reader));
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return task;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private void getInfoFromJsonSummary(List<FuxiJob> fuxiJobs, String jsonSummaryContent) throws IOException {
    jsonSummaryContent = jsonSummaryContent.replaceAll("\n", " ");
    jsonSummaryContent = jsonSummaryContent.replaceAll("\t", " ");
    jsonSummaryContent = jsonSummaryContent.replace("\\\"", "\"");

    JsonReader reader = new JsonReader(
            new InputStreamReader(new ByteArrayInputStream(jsonSummaryContent.getBytes())));

    reader.beginObject();/*from  ww  w.j  av  a 2  s.  com*/
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("jobs")) {
            reader.beginArray();

            int jobCount = 0;
            // Get more info for each job
            while (reader.hasNext()) {
                reader.beginObject();
                FuxiJob job = fuxiJobs.get(jobCount);
                while (reader.hasNext()) {
                    String nameInJobs = reader.nextName();
                    if (nameInJobs.equals("tasks")) {
                        reader.beginObject();

                        int taskCount = 0;
                        // Get more info for each task
                        while (reader.hasNext()) {
                            String taskName = reader.nextName();
                            FuxiTask task = job.tasks.get(taskCount);

                            // Get the downstream tasks info
                            reader.beginObject();
                            while (reader.hasNext()) {
                                if (reader.nextName().equals("output_record_counts")) {
                                    List<String> downTasks = new ArrayList<String>();
                                    reader.beginObject();
                                    while (reader.hasNext()) {
                                        downTasks.add(reader.nextName());
                                        reader.skipValue();
                                    }
                                    reader.endObject();
                                    addUpAndDownTasks(job, task.name, downTasks);
                                } else {
                                    reader.skipValue();
                                }
                            }
                            reader.endObject();
                            taskCount++;
                        }
                        reader.endObject();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
                jobCount++;
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
}

From source file:com.android.common.ide.common.blame.MessageJsonSerializer.java

License:Apache License

@Override
public Message read(JsonReader in) throws IOException {
    in.beginObject();/*  w  w w .j a v  a  2  s .c  om*/
    Message.Kind kind = Message.Kind.UNKNOWN;
    String text = "";
    String rawMessage = null;
    Optional<String> toolName = Optional.absent();
    ImmutableList.Builder<SourceFilePosition> positions = new ImmutableList.Builder<SourceFilePosition>();
    SourceFile legacyFile = SourceFile.UNKNOWN;
    SourcePosition legacyPosition = SourcePosition.UNKNOWN;
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.equals(KIND)) {
            //noinspection StringToUpperCaseOrToLowerCaseWithoutLocale
            Message.Kind theKind = KIND_STRING_ENUM_MAP.inverse().get(in.nextString().toLowerCase());
            kind = (theKind != null) ? theKind : Message.Kind.UNKNOWN;
        } else if (name.equals(TEXT)) {
            text = in.nextString();
        } else if (name.equals(RAW_MESSAGE)) {
            rawMessage = in.nextString();
        } else if (name.equals(TOOL_NAME)) {
            toolName = Optional.of(in.nextString());
        } else if (name.equals(SOURCE_FILE_POSITIONS)) {
            switch (in.peek()) {
            case BEGIN_ARRAY:
                in.beginArray();
                while (in.hasNext()) {
                    positions.add(mSourceFilePositionTypeAdapter.read(in));
                }
                in.endArray();
                break;
            case BEGIN_OBJECT:
                positions.add(mSourceFilePositionTypeAdapter.read(in));
                break;
            default:
                in.skipValue();
                break;
            }
        } else if (name.equals(LEGACY_SOURCE_PATH)) {
            legacyFile = new SourceFile(new File(in.nextString()));
        } else if (name.equals(LEGACY_POSITION)) {
            legacyPosition = mSourcePositionTypeAdapter.read(in);
        } else {
            in.skipValue();
        }
    }
    in.endObject();

    if (legacyFile != SourceFile.UNKNOWN || legacyPosition != SourcePosition.UNKNOWN) {
        positions.add(new SourceFilePosition(legacyFile, legacyPosition));
    }
    if (rawMessage == null) {
        rawMessage = text;
    }
    ImmutableList<SourceFilePosition> sourceFilePositions = positions.build();
    if (!sourceFilePositions.isEmpty()) {
        return new Message(kind, text, rawMessage, toolName, sourceFilePositions);
    } else {
        return new Message(kind, text, rawMessage, toolName, ImmutableList.of(SourceFilePosition.UNKNOWN));
    }
}

From source file:com.android.ide.common.blame.MergingLogPersistUtil.java

License:Apache License

@NonNull
static Map<SourceFile, Map<SourcePosition, SourceFilePosition>> loadFromMultiFile(@NonNull File folder,
        @NonNull String shard) {/*  w  w w  . j av  a  2s .c  om*/
    Map<SourceFile, Map<SourcePosition, SourceFilePosition>> map = Maps.newConcurrentMap();
    JsonReader reader;
    File file = getMultiFile(folder, shard);
    if (!file.exists()) {
        return map;
    }
    try {
        reader = new JsonReader(Files.newReader(file, Charsets.UTF_8));
    } catch (FileNotFoundException e) {
        // Shouldn't happen unless it disappears under us.
        return map;
    }
    try {
        reader.beginArray();
        while (reader.peek() != JsonToken.END_ARRAY) {
            reader.beginObject();
            SourceFile toFile = SourceFile.UNKNOWN;
            Map<SourcePosition, SourceFilePosition> innerMap = Maps.newLinkedHashMap();
            while (reader.peek() != JsonToken.END_OBJECT) {
                final String name = reader.nextName();
                if (name.equals(KEY_OUTPUT_FILE)) {
                    toFile = mSourceFileJsonTypeAdapter.read(reader);
                } else if (name.equals(KEY_MAP)) {
                    reader.beginArray();
                    while (reader.peek() != JsonToken.END_ARRAY) {
                        reader.beginObject();
                        SourceFilePosition from = null;
                        SourcePosition to = null;
                        while (reader.peek() != JsonToken.END_OBJECT) {
                            final String innerName = reader.nextName();
                            if (innerName.equals(KEY_FROM)) {
                                from = mSourceFilePositionJsonTypeAdapter.read(reader);
                            } else if (innerName.equals(KEY_TO)) {
                                to = mSourcePositionJsonTypeAdapter.read(reader);
                            } else {
                                throw new IOException(String.format("Unexpected property: %s", innerName));
                            }
                        }
                        if (from == null || to == null) {
                            throw new IOException("Each record must contain both from and to.");
                        }
                        innerMap.put(to, from);
                        reader.endObject();
                    }
                    reader.endArray();
                } else {
                    throw new IOException(String.format("Unexpected property: %s", name));
                }
            }
            map.put(toFile, innerMap);
            reader.endObject();
        }
        reader.endArray();
        return map;
    } catch (IOException e) {
        // TODO: trigger a non-incremental merge if this happens.
        throw new RuntimeException(e);
    } finally {
        try {
            reader.close();
        } catch (Throwable e2) {
            // well, we tried.
        }
    }
}

From source file:com.android.ide.common.blame.MergingLogPersistUtil.java

License:Apache License

@NonNull
static Map<SourceFile, SourceFile> loadFromSingleFile(@NonNull File folder, @NonNull String shard) {
    Map<SourceFile, SourceFile> fileMap = Maps.newConcurrentMap();
    JsonReader reader;
    File file = getSingleFile(folder, shard);
    if (!file.exists()) {
        return fileMap;
    }/*from  w w  w. j a va 2s. c o m*/
    try {
        reader = new JsonReader(Files.newReader(file, Charsets.UTF_8));
    } catch (FileNotFoundException e) {
        // Shouldn't happen unless it disappears under us.
        return fileMap;
    }
    try {
        reader.beginArray();
        while (reader.peek() != JsonToken.END_ARRAY) {
            reader.beginObject();
            SourceFile merged = SourceFile.UNKNOWN;
            SourceFile source = SourceFile.UNKNOWN;
            while (reader.peek() != JsonToken.END_OBJECT) {
                String name = reader.nextName();
                if (name.equals(KEY_MERGED)) {
                    merged = mSourceFileJsonTypeAdapter.read(reader);
                } else if (name.equals(KEY_SOURCE)) {
                    source = mSourceFileJsonTypeAdapter.read(reader);
                } else {
                    throw new IOException(String.format("Unexpected property: %s", name));
                }
            }
            reader.endObject();
            fileMap.put(merged, source);
        }
        reader.endArray();
        return fileMap;
    } catch (IOException e) {
        // TODO: trigger a non-incremental merge if this happens.
        throw new RuntimeException(e);
    } finally {
        try {
            reader.close();
        } catch (Throwable e) {
            // well, we tried.
        }
    }
}

From source file:com.android.ide.common.blame.MessageJsonSerializer.java

License:Apache License

@Override
public Message read(JsonReader in) throws IOException {
    in.beginObject();//from ww  w.  j av  a 2s  . c  o  m
    Message.Kind kind = Message.Kind.UNKNOWN;
    String text = "";
    String rawMessage = null;
    ImmutableList.Builder<SourceFilePosition> positions = new ImmutableList.Builder<SourceFilePosition>();
    SourceFile legacyFile = SourceFile.UNKNOWN;
    SourcePosition legacyPosition = SourcePosition.UNKNOWN;
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.equals(KIND)) {
            //noinspection StringToUpperCaseOrToLowerCaseWithoutLocale
            Message.Kind theKind = KIND_STRING_ENUM_MAP.inverse().get(in.nextString().toLowerCase());
            kind = (theKind != null) ? theKind : Message.Kind.UNKNOWN;
        } else if (name.equals(TEXT)) {
            text = in.nextString();
        } else if (name.equals(RAW_MESSAGE)) {
            rawMessage = in.nextString();
        } else if (name.equals(SOURCE_FILE_POSITIONS)) {
            switch (in.peek()) {
            case BEGIN_ARRAY:
                in.beginArray();
                while (in.hasNext()) {
                    positions.add(mSourceFilePositionTypeAdapter.read(in));
                }
                in.endArray();
                break;
            case BEGIN_OBJECT:
                positions.add(mSourceFilePositionTypeAdapter.read(in));
                break;
            default:
                in.skipValue();
                break;
            }
        } else if (name.equals(LEGACY_SOURCE_PATH)) {
            legacyFile = new SourceFile(new File(in.nextString()));
        } else if (name.equals(LEGACY_POSITION)) {
            legacyPosition = mSourcePositionTypeAdapter.read(in);
        } else {
            in.skipValue();
        }
    }
    in.endObject();

    if (legacyFile != SourceFile.UNKNOWN || legacyPosition != SourcePosition.UNKNOWN) {
        positions.add(new SourceFilePosition(legacyFile, legacyPosition));
    }
    if (rawMessage == null) {
        rawMessage = text;
    }
    ImmutableList<SourceFilePosition> sourceFilePositions = positions.build();
    if (!sourceFilePositions.isEmpty()) {
        return new Message(kind, text, rawMessage, sourceFilePositions);
    } else {
        return new Message(kind, text, rawMessage, ImmutableList.of(SourceFilePosition.UNKNOWN));
    }
}

From source file:com.arbalest.net.converter.GsonConverter.java

License:Open Source License

@SuppressWarnings("unchecked")
private <T> T convertArray(InputStream in, Class<T> clazz)
        throws ArbalestNetworkException, ArbalestResponseException {
    JsonReader reader = null;
    try {/*from  w  ww . j  a v  a2 s  .  com*/
        reader = new JsonReader(new InputStreamReader(in, DEFAULT_CHARSET));
        Class<?> type = clazz.getComponentType();
        Gson gson = mDefaultGsonBuilder.create();
        List<Object> list = new ArrayList<Object>();

        reader.beginArray();

        while (reader.hasNext()) {
            list.add(gson.fromJson(reader, type));
        }

        reader.endArray();

        return (T) list.toArray((T[]) Array.newInstance(type, list.size()));
    } catch (JsonSyntaxException e) {
        throw new ArbalestResponseException(e);
    } catch (IOException e) {
        throw new ArbalestNetworkException(e);
    } finally {
        CloseableUtils.close(reader);
    }
}

From source file:com.battlelancer.seriesguide.dataliberation.JsonImportTask.java

License:Apache License

private void importFromJson(@JsonExportTask.BackupType int type, FileInputStream in)
        throws JsonParseException, IOException, IllegalArgumentException {
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();/* ww  w  .j  a  va2  s  .c  om*/

    if (type == JsonExportTask.BACKUP_SHOWS) {
        while (reader.hasNext()) {
            Show show = gson.fromJson(reader, Show.class);
            addShowToDatabase(show);
        }
    } else if (type == JsonExportTask.BACKUP_LISTS) {
        while (reader.hasNext()) {
            List list = gson.fromJson(reader, List.class);
            addListToDatabase(list);
        }
    } else if (type == JsonExportTask.BACKUP_MOVIES) {
        while (reader.hasNext()) {
            Movie movie = gson.fromJson(reader, Movie.class);
            addMovieToDatabase(movie);
        }
    }

    reader.endArray();
    reader.close();
}