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

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

Introduction

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

Prototype

public void skipValue() throws IOException 

Source Link

Document

Skips the next value recursively.

Usage

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 2s . com
    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.common.ide.common.blame.SourceFileJsonTypeAdapter.java

License:Apache License

@Override
public SourceFile read(JsonReader in) throws IOException {
    switch (in.peek()) {
    case BEGIN_OBJECT:
        in.beginObject();/*from  ww w  .ja v  a2s  .c  o m*/
        String filePath = null;
        String description = null;
        while (in.hasNext()) {
            String name = in.nextName();
            if (name.equals(PATH)) {
                filePath = in.nextString();
            } else if (DESCRIPTION.equals(name)) {
                description = in.nextString();
            } else {
                in.skipValue();
            }
        }
        in.endObject();
        if (!Strings.isNullOrEmpty(filePath)) {
            File file = new File(filePath);
            if (!Strings.isNullOrEmpty(description)) {
                return new SourceFile(file, description);
            } else {
                return new SourceFile(file);
            }
        } else {
            if (!Strings.isNullOrEmpty(description)) {
                return new SourceFile(description);
            } else {
                return SourceFile.UNKNOWN;
            }
        }
    case STRING:
        String fileName = in.nextString();
        if (Strings.isNullOrEmpty(fileName)) {
            return SourceFile.UNKNOWN;
        }
        return new SourceFile(new File(fileName));
    default:
        return SourceFile.UNKNOWN;
    }

}

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

License:Apache License

@Override
public SourceFilePosition read(JsonReader in) throws IOException {
    in.beginObject();/*  w ww. j  a  va  2  s  .  co  m*/
    SourceFile file = SourceFile.UNKNOWN;
    SourcePosition position = SourcePosition.UNKNOWN;
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.equals(FILE)) {
            file = mSourceFileJsonTypeAdapter.read(in);
        } else if (name.equals(POSITION)) {
            position = mSourcePositionJsonTypeAdapter.read(in);
        } else {
            in.skipValue();
        }
    }
    in.endObject();
    return new SourceFilePosition(file, position);
}

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

License:Apache License

@Override
public SourcePosition read(JsonReader in) throws IOException {
    int startLine = -1, startColumn = -1, startOffset = -1;
    int endLine = -1, endColumn = -1, endOffset = -1;
    in.beginObject();//from ww w  . j  a  v  a  2  s.c  om
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.equals(START_LINE)) {
            startLine = in.nextInt();
        } else if (name.equals(START_COLUMN)) {
            startColumn = in.nextInt();
        } else if (name.equals(START_OFFSET)) {
            startOffset = in.nextInt();
        } else if (name.equals(END_LINE)) {
            endLine = in.nextInt();
        } else if (name.equals(END_COLUMN)) {
            endColumn = in.nextInt();
        } else if (name.equals(END_OFFSET)) {
            endOffset = in.nextInt();
        } else {
            in.skipValue();
        }
    }
    in.endObject();

    endLine = (endLine != -1) ? endLine : startLine;
    endColumn = (endColumn != -1) ? endColumn : startColumn;
    endOffset = (endOffset != -1) ? endOffset : startOffset;
    return new SourcePosition(startLine, startColumn, startOffset, endLine, endColumn, endOffset);
}

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

License:Apache License

@Override
public Message read(JsonReader in) throws IOException {
    in.beginObject();// w  w  w  .j av  a  2s .  co 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.cinchapi.concourse.util.Convert.java

License:Apache License

/**
 * Convert the next JSON object in the {@code reader} to a mapping that
 * associates each key with the Java objects that represent the
 * corresponding values./*  www. j a va  2  s.  co m*/
 * 
 * <p>
 * This method has the same rules and limitations as
 * {@link #jsonToJava(String)}. It simply uses a {@link JsonReader} to
 * handle reading an array of objects.
 * </p>
 * <p>
 * <strong>This method DOES NOT {@link JsonReader#close()} the
 * {@code reader}.</strong>
 * </p>
 * 
 * @param reader the {@link JsonReader} that contains a stream of JSON
 * @return the JSON data in the form of a {@link Multimap} from keys to
 *         values
 */
private static Multimap<String, Object> jsonToJava(JsonReader reader) {
    Multimap<String, Object> data = HashMultimap.create();
    try {
        reader.beginObject();
        JsonToken peek0;
        while ((peek0 = reader.peek()) != JsonToken.END_OBJECT) {
            String key = reader.nextName();
            peek0 = reader.peek();
            if (peek0 == JsonToken.BEGIN_ARRAY) {
                // If we have an array, add the elements individually. If
                // there are any duplicates in the array, they will be
                // filtered out by virtue of the fact that a HashMultimap
                // does not store dupes.
                reader.beginArray();
                JsonToken peek = reader.peek();
                do {
                    Object value;
                    if (peek == JsonToken.BOOLEAN) {
                        value = reader.nextBoolean();
                    } else if (peek == JsonToken.NUMBER) {
                        value = stringToJava(reader.nextString());
                    } else if (peek == JsonToken.STRING) {
                        String orig = reader.nextString();
                        value = stringToJava(orig);
                        if (orig.isEmpty()) {
                            value = orig;
                        }
                        // If the token looks like a string, it MUST be
                        // converted to a Java string unless it is a
                        // masquerading double or an instance of Thrift
                        // translatable class that has a special string
                        // representation (i.e. Tag, Link)
                        else if (orig.charAt(orig.length() - 1) != 'D'
                                && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) {
                            value = value.toString();
                        }
                    } else if (peek == JsonToken.NULL) {
                        reader.skipValue();
                        continue;
                    } else {
                        throw new JsonParseException("Cannot parse nested object or array within an array");
                    }
                    data.put(key, value);
                } while ((peek = reader.peek()) != JsonToken.END_ARRAY);
                reader.endArray();
            } else {
                Object value;
                if (peek0 == JsonToken.BOOLEAN) {
                    value = reader.nextBoolean();
                } else if (peek0 == JsonToken.NUMBER) {
                    value = stringToJava(reader.nextString());
                } else if (peek0 == JsonToken.STRING) {
                    String orig = reader.nextString();
                    value = stringToJava(orig);
                    if (orig.isEmpty()) {
                        value = orig;
                    }
                    // If the token looks like a string, it MUST be
                    // converted to a Java string unless it is a
                    // masquerading double or an instance of Thrift
                    // translatable class that has a special string
                    // representation (i.e. Tag, Link)
                    else if (orig.charAt(orig.length() - 1) != 'D'
                            && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) {
                        value = value.toString();
                    }
                } else if (peek0 == JsonToken.NULL) {
                    reader.skipValue();
                    continue;
                } else {
                    throw new JsonParseException("Cannot parse nested object to value");
                }
                data.put(key, value);
            }
        }
        reader.endObject();
        return data;
    } catch (IOException | IllegalStateException e) {
        throw new JsonParseException(e.getMessage());
    }
}

From source file:com.confighub.core.utils.Utils.java

License:Open Source License

private static void skipToken(final JsonReader reader) throws IOException {
    final JsonToken token = reader.peek();
    switch (token) {
    case BEGIN_ARRAY:
        reader.beginArray();//  ww w .j a va  2  s .  co  m
        break;
    case END_ARRAY:
        reader.endArray();
        break;
    case BEGIN_OBJECT:
        reader.beginObject();
        break;
    case END_OBJECT:
        reader.endObject();
        break;
    case NAME:
        reader.nextName();
        break;
    case STRING:
    case NUMBER:
    case BOOLEAN:
    case NULL:
        reader.skipValue();
        break;
    case END_DOCUMENT:
    default:
        throw new AssertionError(token);
    }
}

From source file:com.facebook.buck.worker.WorkerProcessProtocolZero.java

License:Apache License

private static void receiveHandshake(JsonReader reader, int messageId, Optional<Path> stdErr)
        throws IOException {
    int id = -1;// w ww . ja va2  s  . co  m
    String type = "";
    String protocolVersion = "";

    try {
        reader.beginArray();
        reader.beginObject();
        while (reader.hasNext()) {
            String property = reader.nextName();
            if (property.equals("id")) {
                id = reader.nextInt();
            } else if (property.equals("type")) {
                type = reader.nextString();
            } else if (property.equals("protocol_version")) {
                protocolVersion = reader.nextString();
            } else if (property.equals("capabilities")) {
                try {
                    reader.beginArray();
                    reader.endArray();
                } catch (IllegalStateException e) {
                    throw new HumanReadableException(
                            "Expected handshake response's \"capabilities\" to " + "be an empty array.");
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    } catch (IOException e) {
        throw new HumanReadableException(e, "Error receiving handshake response from external process.\n"
                + "Stderr from external process:\n%s", getStdErrorOutput(stdErr));
    }

    if (id != messageId) {
        throw new HumanReadableException(String.format(
                "Expected handshake response's \"id\" value " + "to be \"%d\", got \"%d\" instead.", messageId,
                id));
    }
    if (!type.equals(TYPE_HANDSHAKE)) {
        throw new HumanReadableException(
                String.format("Expected handshake response's \"type\" " + "to be \"%s\", got \"%s\" instead.",
                        TYPE_HANDSHAKE, type));
    }
    if (!protocolVersion.equals(PROTOCOL_VERSION)) {
        throw new HumanReadableException(String.format(
                "Expected handshake response's " + "\"protocol_version\" to be \"%s\", got \"%s\" instead.",
                PROTOCOL_VERSION, protocolVersion));
    }
}

From source file:com.flipkart.batchdemo.adapter.CustomTagDataAdapter.java

License:Open Source License

@Override
public CustomTagData read(JsonReader reader) throws IOException {
    if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
        reader.nextNull();//  ww w.jav a 2 s. c  o m
        return null;
    }
    if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
        reader.skipValue();
        return null;
    }
    reader.beginObject();

    Tag tag = null;
    Long eventId = 0L;
    JSONObject event = null;
    while (reader.hasNext()) {
        String name = reader.nextName();
        com.google.gson.stream.JsonToken jsonToken = reader.peek();
        if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
            reader.skipValue();
            continue;
        }
        switch (name) {
        case "tag":
            tag = tagTypeAdapter.read(reader);
            break;
        case "eventId":
            eventId = BatchingTypeAdapters.LONG.read(reader);
            break;
        case "event":
            event = BatchingTypeAdapters.getJSONObjectTypeAdapter(gson).read(reader);
            break;
        default:
            reader.skipValue();
            break;
        }
    }

    reader.endObject();
    CustomTagData customTagData = new CustomTagData(tag, event);
    customTagData.setEventId(eventId);
    return customTagData;
}