List of usage examples for com.google.gson.stream JsonReader peek
public JsonToken peek() throws IOException
From source file:co.cask.common.internal.io.SchemaTypeAdapter.java
License:Apache License
/** * Reads json value and convert it into {@link Schema} object. * * @param reader Source of json/*w ww. ja va 2 s .c om*/ * @param knownRecords Set of record name already encountered during the reading. * @return A {@link Schema} reflecting the json. * @throws java.io.IOException Any error during reading. */ private Schema read(JsonReader reader, Set<String> knownRecords) throws IOException { JsonToken token = reader.peek(); switch (token) { case NULL: return null; case STRING: { // Simple type or know record type String name = reader.nextString(); if (knownRecords.contains(name)) { return Schema.recordOf(name); } return Schema.of(Schema.Type.valueOf(name.toUpperCase())); } case BEGIN_ARRAY: // Union type return readUnion(reader, knownRecords); case BEGIN_OBJECT: { reader.beginObject(); String name = reader.nextName(); if (!"type".equals(name)) { throw new IOException("Property \"type\" missing."); } Schema.Type schemaType = Schema.Type.valueOf(reader.nextString().toUpperCase()); Schema schema; switch (schemaType) { case ENUM: schema = readEnum(reader); break; case ARRAY: schema = readArray(reader, knownRecords); break; case MAP: schema = readMap(reader, knownRecords); break; case RECORD: schema = readRecord(reader, knownRecords); break; default: schema = Schema.of(schemaType); } reader.endObject(); return schema; } } throw new IOException("Malformed schema input."); }
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 www.j a va2s . 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. *//*from w w w. j a v a 2s . co 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 . jav 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.forsaken.projectindigo.utils.mojangtokens.EnumTypeAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w ww . ja va2 s . c o m public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!type.getRawType().isEnum()) { return null; } final Map<String, T> map = new HashMap<String, T>(); for (T c : (T[]) type.getRawType().getEnumConstants()) { map.put(c.toString().toLowerCase(Locale.US), c); } return new TypeAdapter<T>() { @Override public T read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } String name = reader.nextString(); if (name == null) { return null; } return map.get(name.toLowerCase(Locale.US)); } @Override public void write(JsonWriter writer, T value) throws IOException { if (value == null) { writer.nullValue(); } else { writer.value(value.toString().toLowerCase(Locale.US)); } } }; }
From source file:com.aelitis.azureus.util.ObjectTypeAdapterLong.java
License:Apache License
@Override public Object read(JsonReader in) throws IOException { JsonToken token = in.peek(); switch (token) { case BEGIN_ARRAY: List<Object> list = new ArrayList<Object>(); in.beginArray();// w w w .jav a2 s.c om 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.android.common.ide.common.blame.MessageJsonSerializer.java
License:Apache License
@Override public Message read(JsonReader in) throws IOException { in.beginObject();//from w w w .j a v a 2 s . 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 w w w. j a va2 s. 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.ide.common.blame.MergingLogPersistUtil.java
License:Apache License
@NonNull static Map<SourceFile, Map<SourcePosition, SourceFilePosition>> loadFromMultiFile(@NonNull File folder, @NonNull String shard) {//from ww w. j a va 2 s.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; File file = getSingleFile(folder, shard); if (!file.exists()) { return fileMap; }// w w w . j av a2 s .co 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. } } }