List of usage examples for com.google.gson.stream JsonReader peek
public JsonToken peek() throws IOException
From source file:com.google.maps.internal.DistanceAdapter.java
License:Open Source License
/** * 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>/* w w w . ja v a 2s . c om*/ * { * "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; }
From source file:com.google.maps.internal.DurationAdapter.java
License:Open Source License
/** * 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>// w w w . j a v a2 s . c om * { * "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; }
From source file:com.google.maps.internal.FareAdapter.java
License:Open Source License
/** * Read a Fare object from the Directions API and convert to a {@link com.google.maps.model.Fare} * * <pre>{/*from w ww. ja v a 2s . c o m*/ * "currency": "USD", * "value": 6 * }</pre> */ @Override public Fare read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } Fare fare = new Fare(); reader.beginObject(); while (reader.hasNext()) { String key = reader.nextName(); if ("currency".equals(key)) { fare.currency = Currency.getInstance(reader.nextString()); } else if ("value".equals(key)) { // this relies on nextString() being able to coerce raw numbers to strings fare.value = new BigDecimal(reader.nextString()); } else { // Be forgiving of unexpected values reader.skipValue(); } } reader.endObject(); return fare; }
From source file:com.google.maps.internal.GeolocationResponseAdapter.java
License:Open Source License
@Override public GeolocationApi.Response read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();/*from ww w. java 2 s . c o m*/ return null; } GeolocationApi.Response response = new GeolocationApi.Response(); LatLngAdapter latLngAdapter = new LatLngAdapter(); reader.beginObject(); // opening { while (reader.hasNext()) { String name = reader.nextName(); // two different objects could be returned a success object containing "location" and "accuracy" // keys or an error object containing an "error" key if (name.equals("location")) { // we already have a parser for the LatLng object so lets use that response.location = latLngAdapter.read(reader); } else if (name.equals("accuracy")) { response.accuracy = reader.nextDouble(); } else if (name.equals("error")) { reader.beginObject(); // the error key leads to another object... while (reader.hasNext()) { String errName = reader.nextName(); // ...with keys "errors", "code" and "message" if (errName.equals("code")) { response.code = reader.nextInt(); } else if (errName.equals("message")) { response.message = reader.nextString(); } else if (errName.equals("errors")) { reader.beginArray(); // its plural because its an array of errors... while (reader.hasNext()) { reader.beginObject();// ...and each error array element is an object... while (reader.hasNext()) { errName = reader.nextName(); // ...with keys "reason", "domain", "debugInfo", "location", "locationType", and "message" (again) if (errName.equals("reason")) { response.reason = reader.nextString(); } else if (errName.equals("domain")) { response.domain = reader.nextString(); } else if (errName.equals("debugInfo")) { response.debugInfo = reader.nextString(); } else if (errName.equals("message")) { // have this already reader.nextString(); } else if (errName.equals("location")) { reader.nextString(); } else if (errName.equals("locationType")) { reader.nextString(); } } reader.endObject(); } reader.endArray(); } } reader.endObject(); // closing } } } reader.endObject(); return response; }
From source file:com.google.maps.internal.InstantAdapter.java
License:Open Source License
/** * Read a time from the Places API and convert to a {@link Instant} *//* ww w . j ava 2 s . c o m*/ @Override public Instant read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } if (reader.peek() == JsonToken.NUMBER) { // Number is the number of seconds since Epoch. return new Instant(reader.nextLong() * 1000L); } throw new UnsupportedOperationException("Unsupported format"); }
From source file:com.google.maps.internal.LatLngAdapter.java
License:Open Source License
/** * Reads in a JSON object and try to create a LatLng in one of the following formats. * * <pre>{/* w w w . j a v a 2 s . c o m*/ * "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; } }
From source file:com.google.maps.internal.LocalTimeAdapter.java
License:Open Source License
/** * Read a time from the Places API and convert to a {@link LocalTime} *//*from w w w.jav a 2 s.co m*/ @Override public LocalTime read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } if (reader.peek() == JsonToken.STRING) { DateTimeFormatter dtf = DateTimeFormat.forPattern("HHmm"); return LocalTime.parse(reader.nextString(), dtf); } throw new UnsupportedOperationException("Unsupported format"); }
From source file:com.google.maps.internal.PriceLevelAdapter.java
License:Open Source License
@Override public PriceLevel read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();/*from ww w . jav a2s. c o m*/ return null; } if (reader.peek() == JsonToken.NUMBER) { int priceLevel = reader.nextInt(); switch (priceLevel) { case 0: return PriceLevel.FREE; case 1: return PriceLevel.INEXPENSIVE; case 2: return PriceLevel.MODERATE; case 3: return PriceLevel.EXPENSIVE; case 4: return PriceLevel.VERY_EXPENSIVE; } } return PriceLevel.UNKNOWN; }
From source file:com.google.maps.internal.SafeEnumAdapter.java
License:Open Source License
@Override public E read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();// w w w. j a v a 2 s . com return null; } String value = reader.nextString(); try { return Enum.valueOf(clazz, value.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException iae) { log.warning(String.format("Unknown type for enum %s: '%s'", clazz.getName(), value)); return unknownValue; } }
From source file:com.google.maps.internal.TravelModeAdapter.java
License:Open Source License
/** * Read a travel mode from a Directions API result and convert it to a {@link TravelMode}. *///from w w w . j a v a 2 s. co m @Override public TravelMode read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } return TravelMode.lookup(reader.nextString()); }