Java Code Examples for com.google.gson.stream.JsonReader#peek()
The following examples show how to use
com.google.gson.stream.JsonReader#peek() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Gson.java From gson with Apache License 2.0 | 6 votes |
private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) { if (serializeSpecialFloatingPointValues) { return TypeAdapters.DOUBLE; } return new TypeAdapter<Number>() { @Override public Double read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return in.nextDouble(); } @Override public void write(JsonWriter out, Number value) throws IOException { if (value == null) { out.nullValue(); return; } double doubleValue = value.doubleValue(); checkValidFloatingPoint(doubleValue); out.value(value); } }; }
Example 2
Source File: DistanceAdapter.java From google-maps-services-java with Apache License 2.0 | 6 votes |
/** * Read a distance object from a Directions API result and convert it to a {@link Distance}. * * <p>We are expecting to receive something akin to the following: * * <pre> * { * "value": 207, "text": "0.1 mi" * } * </pre> */ @Override public Distance read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } Distance distance = new Distance(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("text")) { distance.humanReadable = reader.nextString(); } else if (name.equals("value")) { distance.inMeters = reader.nextLong(); } } reader.endObject(); return distance; }
Example 3
Source File: DurationAdapter.java From google-maps-services-java with Apache License 2.0 | 6 votes |
/** * Read a distance object from a Directions API result and convert it to a {@link Distance}. * * <p>We are expecting to receive something akin to the following: * * <pre> * { * "value": 207, * "text": "0.1 mi" * } * </pre> */ @Override public Duration read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } Duration duration = new Duration(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("text")) { duration.humanReadable = reader.nextString(); } else if (name.equals("value")) { duration.inSeconds = reader.nextLong(); } } reader.endObject(); return duration; }
Example 4
Source File: JSON.java From oxd with Apache License 2.0 | 6 votes |
@Override public java.sql.Date read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; default: String date = in.nextString(); try { if (dateFormat != null) { return new java.sql.Date(dateFormat.parse(date).getTime()); } return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime()); } catch (ParseException e) { throw new JsonParseException(e); } } }
Example 5
Source File: StateTypeAdapter.java From smarthome with Eclipse Public License 2.0 | 6 votes |
@Override public State read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } String value = reader.nextString(); String[] parts = value.split(TYPE_SEPARATOR); String valueTypeName = parts[0]; String valueAsString = parts[1]; try { @SuppressWarnings("unchecked") Class<? extends State> valueType = (Class<? extends State>) Class.forName(valueTypeName); List<Class<? extends State>> types = Collections.singletonList(valueType); return TypeParser.parseState(types, valueAsString); } catch (Exception e) { logger.warn("Couldn't deserialize state '{}': {}", value, e.getMessage()); } return null; }
Example 6
Source File: SpeechWordConfidenceTypeAdapter.java From java-sdk with Apache License 2.0 | 6 votes |
@Override public SpeechWordConfidence read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } final SpeechWordConfidence speechWordConfidence = new SpeechWordConfidence(); reader.beginArray(); if (reader.peek() == JsonToken.STRING) { speechWordConfidence.setWord(reader.nextString()); } if (reader.peek() == JsonToken.NUMBER) { speechWordConfidence.setConfidence(reader.nextDouble()); } reader.endArray(); return speechWordConfidence; }
Example 7
Source File: TypeAdapters.java From gson with Apache License 2.0 | 5 votes |
@Override public String read(JsonReader in) throws IOException { JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); return null; } /* coerce booleans to strings for backwards compatibility */ if (peek == JsonToken.BOOLEAN) { return Boolean.toString(in.nextBoolean()); } return in.nextString(); }
Example 8
Source File: LatLngAdapter.java From android-places-demos with Apache License 2.0 | 5 votes |
/** * Reads in a JSON object and try to create a LatLng in one of the following formats. * * <pre>{ * "lat" : -33.8353684, * "lng" : 140.8527069 * } * * { * "latitude": -33.865257570508334, * "longitude": 151.19287000481452 * }</pre> */ @Override public LatLng read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } double lat = 0; double lng = 0; boolean hasLat = false; boolean hasLng = false; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("lat".equals(name) || "latitude".equals(name)) { lat = reader.nextDouble(); hasLat = true; } else if ("lng".equals(name) || "longitude".equals(name)) { lng = reader.nextDouble(); hasLng = true; } } reader.endObject(); if (hasLat && hasLng) { return new LatLng(lat, lng); } else { return null; } }
Example 9
Source File: DateAdapters.java From sumk with Apache License 2.0 | 5 votes |
@Override public SumkDate read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } String v = in.nextString(); return SumkDate.of(v); }
Example 10
Source File: LocalDateTimeISOAdapter.java From uyuni with GNU General Public License v2.0 | 5 votes |
@Override public LocalDateTime read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { throw new JsonParseException("null is not a valid value for LocalDateTime"); } String dateStr = jsonReader.nextString(); try { ZonedDateTime p = ZonedDateTime.parse(dateStr); ZoneId zoneId = Context.getCurrentContext().getTimezone().toZoneId(); return LocalDateTime.ofInstant(p.toInstant(), zoneId); } catch (DateTimeParseException e) { return LocalDateTime.parse(dateStr); } }
Example 11
Source File: TypeAdapters.java From gson with Apache License 2.0 | 5 votes |
@Override public Number read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } try { int intValue = in.nextInt(); return (byte) intValue; } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } }
Example 12
Source File: TimeTypeAdapter.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@Override public synchronized Time read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } try { Date date = format.parse(in.nextString()); return new Time(date.getTime()); } catch (ParseException e) { throw new JsonSyntaxException(e); } }
Example 13
Source File: JSON.java From android with MIT License | 5 votes |
@Override public LocalDate read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; default: String date = in.nextString(); return LocalDate.parse(date, formatter); } }
Example 14
Source File: ChannelReader.java From packagedrone with Eclipse Public License 1.0 | 5 votes |
private Map<String, String> readAspects ( final JsonReader jr ) throws IOException { final Map<String, String> result = new HashMap<> (); jr.beginObject (); while ( jr.hasNext () ) { switch ( jr.nextName () ) { case "map": jr.beginObject (); while ( jr.hasNext () ) { final String id = jr.nextName (); String value = null; if ( jr.peek () == JsonToken.STRING ) { value = jr.nextString (); } else { jr.skipValue (); } result.put ( id, value ); } jr.endObject (); break; } } jr.endObject (); return result; }
Example 15
Source File: TypeAdapters.java From letv with Apache License 2.0 | 5 votes |
public Number read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } try { return Byte.valueOf((byte) in.nextInt()); } catch (Throwable e) { throw new JsonSyntaxException(e); } }
Example 16
Source File: JsonLdSerializer.java From schemaorg-java with Apache License 2.0 | 5 votes |
private List<SchemaOrgType> readInternal(JsonReader reader, boolean acceptNull) throws IOException { List<SchemaOrgType> list = new ArrayList<>(); switch (reader.peek()) { case STRING: list.add(Text.of(reader.nextString())); break; case NUMBER: list.add(Number.of(reader.nextString())); break; case BOOLEAN: if (reader.nextBoolean()) { list.add(BooleanEnum.TRUE); } else { list.add(BooleanEnum.FALSE); } break; case NULL: if (acceptNull) { reader.nextNull(); list.add(null); } else { throw new JsonLdSyntaxException("Found null value for schema.org property value"); } break; case BEGIN_OBJECT: list.add(readObject(reader)); break; case BEGIN_ARRAY: list.addAll(readArray(reader)); break; case END_DOCUMENT: throw new JsonLdSyntaxException("Meet end of document"); case END_OBJECT: case END_ARRAY: case NAME: break; } return list; }
Example 17
Source File: TupleTypeAdapters.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
@Override public Two<F, S> read(final JsonReader in) throws IOException { JsonToken next = in.peek(); if (next == JsonToken.NULL) { in.nextNull(); return null; } in.beginArray(); F f = first.read(in); S s = second.read(in); Two<F, S> result = new Two<F, S>(f, s); in.endArray(); return result; }
Example 18
Source File: TypeAdapters.java From letv with Apache License 2.0 | 5 votes |
public Calendar read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } in.beginObject(); int year = 0; int month = 0; int dayOfMonth = 0; int hourOfDay = 0; int minute = 0; int second = 0; while (in.peek() != JsonToken.END_OBJECT) { String name = in.nextName(); int value = in.nextInt(); if (YEAR.equals(name)) { year = value; } else if (MONTH.equals(name)) { month = value; } else if (DAY_OF_MONTH.equals(name)) { dayOfMonth = value; } else if (HOUR_OF_DAY.equals(name)) { hourOfDay = value; } else if (MINUTE.equals(name)) { minute = value; } else if (SECOND.equals(name)) { second = value; } } in.endObject(); return new GregorianCalendar(year, month, dayOfMonth, hourOfDay, minute, second); }
Example 19
Source File: StreamMetadataJsonAdapter.java From esjc with MIT License | 4 votes |
@Override public StreamMetadata read(JsonReader reader) throws IOException { StreamMetadata.Builder builder = StreamMetadata.newBuilder(); if (reader.peek() == JsonToken.NULL) { return null; } reader.beginObject(); while (reader.peek() != JsonToken.END_OBJECT && reader.hasNext()) { String name = reader.nextName(); switch (name) { case MAX_COUNT: builder.maxCount(reader.nextLong()); break; case MAX_AGE: builder.maxAge(Duration.ofSeconds(reader.nextLong())); break; case TRUNCATE_BEFORE: builder.truncateBefore(reader.nextLong()); break; case CACHE_CONTROL: builder.cacheControl(Duration.ofSeconds(reader.nextLong())); break; case ACL: StreamAcl acl = streamAclJsonAdapter.read(reader); if (acl != null) { builder.aclReadRoles(acl.readRoles); builder.aclWriteRoles(acl.writeRoles); builder.aclDeleteRoles(acl.deleteRoles); builder.aclMetaReadRoles(acl.metaReadRoles); builder.aclMetaWriteRoles(acl.metaWriteRoles); } break; default: switch (reader.peek()) { case NULL: reader.nextNull(); builder.customProperty(name, (String) null); break; case BEGIN_ARRAY: List<Object> values = new ArrayList<>(); reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { switch (reader.peek()) { case NULL: reader.nextNull(); values.add(null); break; case BOOLEAN: values.add(reader.nextBoolean()); break; case NUMBER: values.add(new BigDecimal(reader.nextString())); break; case STRING: values.add(reader.nextString()); } } reader.endArray(); if (values.stream().anyMatch(v -> v instanceof Boolean)) { builder.customProperty(name, values.stream().toArray(Boolean[]::new)); } else if (values.stream().anyMatch(v -> v instanceof Number)) { builder.customProperty(name, values.stream().toArray(Number[]::new)); } else { builder.customProperty(name, values.stream().toArray(String[]::new)); } break; case BOOLEAN: builder.customProperty(name, reader.nextBoolean()); break; case NUMBER: builder.customProperty(name, new BigDecimal(reader.nextString())); break; case STRING: builder.customProperty(name, reader.nextString()); break; default: reader.skipValue(); } } } reader.endObject(); return builder.build(); }
Example 20
Source File: ProteusTypeAdapterFactory.java From proteus with Apache License 2.0 | 4 votes |
@Override public Value read(JsonReader in) throws IOException { switch (in.peek()) { case STRING: return compileString(getContext(), in.nextString()); case NUMBER: String number = in.nextString(); return new Primitive(new LazilyParsedNumber(number)); case BOOLEAN: return new Primitive(in.nextBoolean()); case NULL: in.nextNull(); return Null.INSTANCE; case BEGIN_ARRAY: Array array = new Array(); in.beginArray(); while (in.hasNext()) { array.add(read(in)); } in.endArray(); return array; case BEGIN_OBJECT: ObjectValue object = new ObjectValue(); in.beginObject(); if (in.hasNext()) { String name = in.nextName(); if (ProteusConstants.TYPE.equals(name) && JsonToken.STRING.equals(in.peek())) { String type = in.nextString(); if (PROTEUS_INSTANCE_HOLDER.isLayout(type)) { Layout layout = LAYOUT_TYPE_ADAPTER.read(type, PROTEUS_INSTANCE_HOLDER.getProteus(), in); in.endObject(); return layout; } else { object.add(name, compileString(getContext(), type)); } } else { object.add(name, read(in)); } } while (in.hasNext()) { object.add(in.nextName(), read(in)); } in.endObject(); return object; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }