List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
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 av a 2 s. com*/ * @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; }
From source file:com.fatboyindustrial.gsonjavatime.InstantConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the specified type. * <p>// w w w . j a v a 2s . c o m * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any non-trivial field of * the returned object. However, you should never invoke it on the the same type passing {@code json} since that will cause * an infinite loop (Gson will call your call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return getFormatter().parse(json.getAsString(), Instant::from); }
From source file:com.fatboyindustrial.gsonjavatime.LocalDateConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the specified type. * <p>//from w w w. j a v a 2 s .c o m * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any non-trivial field of * the returned object. However, you should never invoke it on the the same type passing {@code json} since that will cause * an infinite loop (Gson will call your call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return getFormatter().parse(json.getAsString(), LocalDate::from); }
From source file:com.fatboyindustrial.gsonjavatime.LocalDateTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the specified type. * <p>/*from w ww .j a v a2 s. co m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any non-trivial field of * the returned object. However, you should never invoke it on the the same type passing {@code json} since that will cause * an infinite loop (Gson will call your call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return getFormatter().parse(json.getAsString(), LocalDateTime::from); }
From source file:com.fatboyindustrial.gsonjavatime.LocalTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the specified type. * <p>//from w ww . j a v a 2s.c o m * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any non-trivial field of * the returned object. However, you should never invoke it on the the same type passing {@code json} since that will cause * an infinite loop (Gson will call your call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return getFormatter().parse(json.getAsString(), LocalTime::from); }
From source file:com.fatboyindustrial.gsonjavatime.OffsetDateTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the specified type. * <p>//from w w w. j ava 2 s .co m * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any non-trivial field of * the returned object. However, you should never invoke it on the the same type passing {@code json} since that will cause * an infinite loop (Gson will call your call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public OffsetDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return getFormatter().parse(json.getAsString(), OffsetDateTime::from); }
From source file:com.fatboyindustrial.gsonjavatime.OffsetTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the specified type. * <p>/*from w ww . j av a 2s . co m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any non-trivial field of * the returned object. However, you should never invoke it on the the same type passing {@code json} since that will cause * an infinite loop (Gson will call your call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public OffsetTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return getFormatter().parse(json.getAsString(), OffsetTime::from); }
From source file:com.fatboyindustrial.gsonjavatime.ZonedDateTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the specified type. * <p>/*w w w . j ava 2s . co m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any non-trivial field of * the returned object. However, you should never invoke it on the the same type passing {@code json} since that will cause * an infinite loop (Gson will call your call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return getFormatter().parse(json.getAsString(), ZonedDateTime::from); }
From source file:com.fatboyindustrial.gsonjodatime.DateMidnightConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>//from w w w. ja v a2s .c o m * * In the implementation of this call-back method, you should consider invoking * {@link com.google.gson.JsonDeserializationContext#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws com.google.gson.JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public DateMidnight deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); return new DateMidnight(fmt.parseDateTime(json.getAsString())); }
From source file:com.fatboyindustrial.gsonjodatime.DateTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>//from w w w .j a va2 s . com * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); return fmt.parseDateTime(json.getAsString()); }