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

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

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Usage

From source file:co.cask.common.internal.io.SchemaTypeAdapter.java

License:Apache License

/**
 * Constructs {@link Schema.Type#RECORD RECORD} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @param knownRecords Set of record name already encountered during the reading.
 * @return A {@link Schema} of type {@link Schema.Type#RECORD RECORD}.
 * @throws java.io.IOException When fails to construct a valid schema from the input.
 *///w  ww  .j  av a  2  s.c o m
private Schema readRecord(JsonReader reader, Set<String> knownRecords) throws IOException {
    if (!"name".equals(reader.nextName())) {
        throw new IOException("Property \"name\" missing for record.");
    }

    String recordName = reader.nextString();

    // Read in fields schemas
    if (!"fields".equals(reader.nextName())) {
        throw new IOException("Property \"fields\" missing for record.");
    }

    knownRecords.add(recordName);

    ImmutableList.Builder<Schema.Field> fieldBuilder = ImmutableList.builder();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        reader.beginObject();
        if (!"name".equals(reader.nextName())) {
            throw new IOException("Property \"name\" missing for record field.");
        }
        String fieldName = reader.nextString();
        fieldBuilder.add(Schema.Field.of(fieldName, readInnerSchema(reader, "type", knownRecords)));
        reader.endObject();
    }
    reader.endArray();
    return Schema.recordOf(recordName, fieldBuilder.build());
}

From source file:co.moonmonkeylabs.flowmortarexampleapp.common.flow.GsonParceler.java

License:Apache License

private Object decode(String json) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(json));

    try {/*from   w  w  w . j  a  va 2 s .co  m*/
        reader.beginObject();

        Class<?> type = Class.forName(reader.nextName());
        return gson.fromJson(reader, type);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        reader.close();
    }
}

From source file:com.aelitis.azureus.util.ObjectTypeAdapterLong.java

License:Apache License

@Override
public Object read(JsonReader in) throws IOException {
    JsonToken token = in.peek();/*from   w w  w  .j  a  v  a2 s  .  c  o  m*/
    switch (token) {
    case BEGIN_ARRAY:
        List<Object> list = new ArrayList<Object>();
        in.beginArray();
        while (in.hasNext()) {
            list.add(read(in));
        }
        in.endArray();
        return list;

    case BEGIN_OBJECT:
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        in.beginObject();
        while (in.hasNext()) {
            map.put(in.nextName(), read(in));
        }
        in.endObject();
        return map;

    case STRING:
        return in.nextString();

    case NUMBER: {
        String value = in.nextString();
        if (value.indexOf('.') >= 0) {
            return Double.parseDouble(value);
        } else {
            return Long.parseLong(value);
        }
    }

    case BOOLEAN:
        return in.nextBoolean();

    case NULL:
        in.nextNull();
        return null;

    default:
        throw new IllegalStateException();
    }
}

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) {// w  w w.  jav  a2 s. c  o  m
        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();
    while (reader.hasNext()) {
        String nameInJob = reader.nextName();
        if (nameInJob.equals("name")) {
            job.name = reader.nextString();
        } else if (nameInJob.equals("tasks")) {
            reader.beginArray();//from w w  w.j  a  v  a2  s .c o  m
            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();
    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();/* w w  w.  j av a 2s .  c  o m*/
            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 FuxiInstance getFuxiInstanceFromJson(JsonReader reader) throws IOException {
    FuxiInstance inst = new FuxiInstance();

    reader.beginObject();
    long endTime = 0;
    while (reader.hasNext()) {
        String nameInInstance = reader.nextName();
        if (nameInInstance.equals("id")) {
            inst.id = reader.nextString();
        } else if (nameInInstance.equals("logId")) {
            inst.logid = reader.nextString();
            inst.IpAndPath = this.decodeLogId(inst.logid);
        } else if (nameInInstance.equals("startTime")) {
            inst.startTime = reader.nextLong();
        } else if (nameInInstance.equals("endTime")) {
            endTime = reader.nextLong();
        } else if (nameInInstance.equals("status")) {
            inst.status = reader.nextString();
        } else {/*w  ww . ja v  a 2  s.  co  m*/
            reader.skipValue();
        }
    }
    inst.duration = (int) (endTime - inst.startTime);
    reader.endObject();
    return inst;
}

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();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("jobs")) {
            reader.beginArray();//from  w w  w . j a  v  a  2s. c om

            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.andrewreitz.onthisday.ui.flow.GsonParcer.java

License:Apache License

private T decode(String json) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(json));

    try {//from w  w w  .jav  a 2 s . c o m
        reader.beginObject();

        Class<?> type = Class.forName(reader.nextName());
        return gson.fromJson(reader, type);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        reader.close();
    }
}

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

License:Apache License

@Override
public Message read(JsonReader in) throws IOException {
    in.beginObject();
    Message.Kind kind = Message.Kind.UNKNOWN;
    String text = "";
    String rawMessage = null;//from ww  w.ja  va  2  s .co m
    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));
    }
}