Example usage for com.google.gson.stream JsonToken END_ARRAY

List of usage examples for com.google.gson.stream JsonToken END_ARRAY

Introduction

In this page you can find the example usage for com.google.gson.stream JsonToken END_ARRAY.

Prototype

JsonToken END_ARRAY

To view the source code for com.google.gson.stream JsonToken END_ARRAY.

Click Source Link

Document

The closing of a JSON array.

Usage

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

License:Apache License

/**
 * Constructs {@link Schema.Type#UNION UNION} 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#UNION UNION}.
 * @throws java.io.IOException When fails to construct a valid schema from the input.
 *///from w  w w .j  av a  2s .c  om
private Schema readUnion(JsonReader reader, Set<String> knownRecords) throws IOException {
    ImmutableList.Builder<Schema> unionSchemas = ImmutableList.builder();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        unionSchemas.add(read(reader, knownRecords));
    }
    reader.endArray();
    return Schema.unionOf(unionSchemas.build());
}

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

License:Apache License

/**
 * Constructs {@link Schema.Type#ENUM ENUM} type schema from the json input.
 *
 * @param reader The {@link JsonReader} for streaming json input tokens.
 * @return A {@link Schema} of type {@link Schema.Type#ENUM ENUM}.
 * @throws java.io.IOException When fails to construct a valid schema from the input.
 *///  ww  w  . j ava2  s .  c o  m
private Schema readEnum(JsonReader reader) throws IOException {
    if (!"symbols".equals(reader.nextName())) {
        throw new IOException("Property \"symbols\" missing for enum.");
    }
    ImmutableList.Builder<String> enumValues = ImmutableList.builder();
    reader.beginArray();
    while (reader.peek() != JsonToken.END_ARRAY) {
        enumValues.add(reader.nextString());
    }
    reader.endArray();
    return Schema.enumWith(enumValues.build());
}

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.
 *///from   w ww  .  java 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: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  . ja  v a  2s. c  o m*/
    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;/*  ww w.jav a2s . c  om*/
    File file = getSingleFile(folder, shard);
    if (!file.exists()) {
        return fileMap;
    }
    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.cinchapi.concourse.util.Convert.java

License:Apache License

/**
 * Takes a JSON string representation of an object or an array of JSON
 * objects and returns a list of {@link Multimap multimaps} with the
 * corresponding data. Unlike {@link #jsonToJava(String)}, this method will
 * allow the top level element to be an array in the {code json} string.
 * //w  w w .  ja  v a  2s .co  m
 * @param json
 * @return A list of Java objects
 */
public static List<Multimap<String, Object>> anyJsonToJava(String json) {
    List<Multimap<String, Object>> result = Lists.newArrayList();
    try (JsonReader reader = new JsonReader(new StringReader(json))) {
        reader.setLenient(true);
        if (reader.peek() == JsonToken.BEGIN_ARRAY) {
            try {
                reader.beginArray();
                while (reader.peek() != JsonToken.END_ARRAY) {
                    result.add(jsonToJava(reader));
                }
                reader.endArray();
            } catch (IllegalStateException e) {
                throw new JsonParseException(e.getMessage());
            }
        } else {
            result.add(jsonToJava(reader));
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    return result;
}

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./* w w w. ja v a  2s  .  c o 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.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java

License:Apache License

private List<?> readList(JsonReader in) throws IOException {
    List<Object> result = new ArrayList<Object>();
    in.beginArray();//from  w  w w. j  a va  2 s .c o  m
    while (in.peek() != JsonToken.END_ARRAY) {
        final Object element;

        if (in.peek() == JsonToken.BEGIN_OBJECT) {
            element = gson.getAdapter(MailChimpObject.class).read(in);
        } else if (in.peek() == JsonToken.BEGIN_ARRAY) {
            element = readList(in);
        } else {
            element = gson.getAdapter(Object.class).read(in);
        }

        result.add(element);
    }
    in.endArray();
    return result;
}

From source file:com.facebook.buck.httpserver.TracesHelper.java

License:Apache License

private Optional<String> parseCommandFrom(Path pathToTrace) {
    try (InputStream input = projectFilesystem.newFileInputStream(pathToTrace);
            JsonReader jsonReader = new JsonReader(new InputStreamReader(input))) {
        jsonReader.beginArray();/*from   w  w w. j av a 2s. c  om*/
        Gson gson = new Gson();

        // Look through the first few elements to see if one matches the schema for an event that
        // contains the command that the user ran.
        for (int i = 0; i < 4; i++) {
            // If END_ARRAY is the next token, then there are no more elements in the array.
            if (jsonReader.peek().equals(JsonToken.END_ARRAY)) {
                break;
            }

            JsonObject json = gson.fromJson(jsonReader, JsonObject.class);
            Optional<String> command = tryToFindCommand(json);
            if (command.isPresent()) {
                return command;
            }
        }

        // Oh well, we tried.
        return Optional.absent();
    } catch (IOException e) {
        logger.error(e);
        return Optional.absent();
    }
}

From source file:com.facebook.buck.util.trace.ChromeTraceParser.java

License:Apache License

/**
 * Parses a Chrome trace and stops parsing once all of the specified matchers have been
 * satisfied. This method parses only one Chrome trace event at a time, which avoids loading the
 * entire trace into memory./*from  www . j  a  v  a 2  s . c  om*/
 * @param pathToTrace is a relative path [to the ProjectFilesystem] to a Chrome trace in the
 *     "JSON Array Format."
 * @param chromeTraceEventMatchers set of matchers this invocation of {@code parse()} is trying to
 *     satisfy. Once a matcher finds a match, it will not consider any other events in the trace.
 * @return a {@code Map} where every matcher that found a match will have an entry whose key is
 *     the matcher and whose value is the one returned by
 *     {@link ChromeTraceEventMatcher#test(JsonObject, String)} without the {@link Optional}
 *     wrapper.
 */
public Map<ChromeTraceEventMatcher<?>, Object> parse(Path pathToTrace,
        Set<ChromeTraceEventMatcher<?>> chromeTraceEventMatchers) throws IOException {
    Set<ChromeTraceEventMatcher<?>> unmatchedMatchers = new HashSet<>(chromeTraceEventMatchers);
    Preconditions.checkArgument(!unmatchedMatchers.isEmpty(), "Must specify at least one matcher");
    Map<ChromeTraceEventMatcher<?>, Object> results = new HashMap<>();

    try (InputStream input = projectFilesystem.newFileInputStream(pathToTrace);
            JsonReader jsonReader = new JsonReader(new InputStreamReader(input))) {
        jsonReader.beginArray();
        Gson gson = new Gson();

        featureSearch: while (true) {
            // If END_ARRAY is the next token, then there are no more elements in the array.
            if (jsonReader.peek().equals(JsonToken.END_ARRAY)) {
                break;
            }

            // Verify and extract the name property before invoking any of the matchers.
            JsonObject event = gson.fromJson(jsonReader, JsonObject.class);
            JsonElement nameEl = event.get("name");
            if (nameEl == null || !nameEl.isJsonPrimitive()) {
                continue;
            }
            String name = nameEl.getAsString();

            // Prefer Iterator to Iterable+foreach so we can use remove().
            for (Iterator<ChromeTraceEventMatcher<?>> iter = unmatchedMatchers.iterator(); iter.hasNext();) {
                ChromeTraceEventMatcher<?> chromeTraceEventMatcher = iter.next();
                Optional<?> result = chromeTraceEventMatcher.test(event, name);
                if (result.isPresent()) {
                    iter.remove();
                    results.put(chromeTraceEventMatcher, result.get());

                    if (unmatchedMatchers.isEmpty()) {
                        break featureSearch;
                    }
                }
            }
        }
    }

    // We could throw if !unmatchedMatchers.isEmpty(), but that might be overbearing.
    return results;
}