List of usage examples for com.google.gson.stream JsonReader endObject
public void endObject() throws IOException
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 a v a 2 s . co 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; }//from w w w. j a v a 2 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. } } }
From source file:com.android.ide.common.blame.MessageJsonSerializer.java
License:Apache License
@Override public Message read(JsonReader in) throws IOException { in.beginObject();/* ww w . j a va 2 s. 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.atlauncher.adapter.ColorTypeAdapter.java
License:Open Source License
@Override public Color read(JsonReader reader) throws IOException { reader.beginObject();/*from ww w . j ava2s . c om*/ String next = reader.nextName(); if (!next.equalsIgnoreCase("value")) { throw new JsonParseException("Key " + next + " isnt a valid key"); } String hex = reader.nextString(); reader.endObject(); int[] rgb = toRGB(clamp(hex.substring(1))); return new Color(rgb[0], rgb[1], rgb[2]); }
From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java
License:Open Source License
@Override public Receipient read(JsonReader in) throws IOException, IllegalStateException, JsonParseException, NumberFormatException { NginxClojureRT.log.debug(TAG + " invoked..."); Receipient receipient = new Receipient(); if (in.peek() == JsonToken.NULL) { in.nextNull();/*from w w w .j a v a 2s .co m*/ return null; } in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "apns_token": if (in.peek() == JsonToken.NULL) { in.nextNull(); } else receipient.setApns_Token(in.nextString()); break; case "fcm_token": if (in.peek() == JsonToken.NULL) { in.nextNull(); } else receipient.setFcm_Token(in.nextString()); break; case "payload": receipient.setPayload(extractPayload(in)); break; } } in.endObject(); NginxClojureRT.log.debug(TAG + "Deserializing and adding receipient of " + (receipient.getApns_Token() == null ? "fcm--" + receipient.getFcm_Token() : "apns--" + receipient.getApns_Token())); return receipient; }
From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java
License:Open Source License
private MeetingPayload extractPayload(JsonReader in) throws IOException, NumberFormatException, IllegalStateException, JsonParseException { NginxClojureRT.log.debug(TAG + "TypeAdapter extracting Payload..."); MeetingPayload meetingPayload = new MeetingPayload(); if (in.peek() == JsonToken.NULL) { in.nextNull();/*from w w w.ja v a2 s . co m*/ throw new JsonParseException("null Payload"); } in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "aps": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "badge": meetingPayload.getAps().setBadge(in.nextLong()); break; case "sound": meetingPayload.getAps().setSound(in.nextString()); break; case "alert": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "title": meetingPayload.getAps().getAlert().setTitle(in.nextString()); break; case "body": meetingPayload.getAps().getAlert().setBody(in.nextString()); break; case "action-loc-key": meetingPayload.getAps().getAlert().setActionLocKey(in.nextString()); break; } } in.endObject(); break; } } in.endObject(); break; case "dapi": meetingPayload.setDapi(in.nextString()); break; case "acme1": meetingPayload.setAcme1(in.nextString()); break; case "acme2": meetingPayload.setAcme2(in.nextLong()); break; case "acme3": meetingPayload.setAcme3(in.nextLong()); break; case "acme4": NginxClojureRT.log.info(TAG + "TypeAdapter Reader is reading acme4..."); meetingPayload.setAcme4(in.nextLong()); break; case "acme5": meetingPayload.setAcme5(in.nextLong()); break; case "acme6": meetingPayload.setAcme6(in.nextLong()); break; case "acme7": ArrayList<String> attendees = new ArrayList<>(); in.beginArray(); while (in.hasNext()) { attendees.add(in.nextString()); } in.endArray(); meetingPayload.setAcme7(attendees); break; case "acme8": meetingPayload.setAcme8(in.nextString()); break; } } in.endObject(); return meetingPayload; }
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.//from w ww. j av a2 s .c om * * <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();// w w w . j av a 2s. com 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.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
@Override public T read(final JsonReader in) throws IOException { String jobName = ""; String cron = ""; int shardingTotalCount = 0; String shardingItemParameters = ""; String jobParameter = ""; boolean failover = false; boolean misfire = failover; String description = ""; JobProperties jobProperties = new JobProperties(); JobEventConfiguration[] jobEventConfigs = null; JobType jobType = null;//w ww.j a v a 2 s . co m String jobClass = ""; DataflowJobConfiguration.DataflowType dataflowType = null; boolean streamingProcess = false; int concurrentDataProcessThreadCount = 0; String scriptCommandLine = ""; Map<String, Object> customizedValueMap = new HashMap<>(32, 1); in.beginObject(); while (in.hasNext()) { String jsonName = in.nextName(); switch (jsonName) { case "jobName": jobName = in.nextString(); break; case "cron": cron = in.nextString(); break; case "shardingTotalCount": shardingTotalCount = in.nextInt(); break; case "shardingItemParameters": shardingItemParameters = in.nextString(); break; case "jobParameter": jobParameter = in.nextString(); break; case "failover": failover = in.nextBoolean(); break; case "misfire": misfire = in.nextBoolean(); break; case "description": description = in.nextString(); break; case "jobProperties": jobProperties = getJobProperties(in); break; case "jobEventConfigs": jobEventConfigs = getJobEventConfigs(in); break; case "jobType": jobType = JobType.valueOf(in.nextString()); break; case "jobClass": jobClass = in.nextString(); break; case "dataflowType": dataflowType = DataflowJobConfiguration.DataflowType.valueOf(in.nextString()); break; case "streamingProcess": streamingProcess = in.nextBoolean(); break; case "concurrentDataProcessThreadCount": concurrentDataProcessThreadCount = in.nextInt(); break; case "scriptCommandLine": scriptCommandLine = in.nextString(); break; default: addToCustomizedValueMap(jsonName, in, customizedValueMap); break; } } in.endObject(); JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount, shardingItemParameters, jobParameter, failover, misfire, description, jobProperties, jobEventConfigs); JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, dataflowType, streamingProcess, concurrentDataProcessThreadCount, scriptCommandLine); return getJobRootConfiguration(typeConfig, customizedValueMap); }
From source file:com.dangdang.ddframe.job.api.config.impl.AbstractJobConfigurationGsonTypeAdapter.java
License:Apache License
private JobProperties getJobProperties(final JsonReader in) throws IOException { JobProperties result = new JobProperties(); in.beginObject();/*from ww w . j a v a 2 s. c o m*/ while (in.hasNext()) { switch (in.nextName()) { case "job_exception_handler": result.put(JobProperties.JobPropertiesEnum.JOB_EXCEPTION_HANDLER.getKey(), in.nextString()); break; case "executor_service_handler": result.put(JobProperties.JobPropertiesEnum.EXECUTOR_SERVICE_HANDLER.getKey(), in.nextString()); break; default: break; } } in.endObject(); return result; }