List of usage examples for com.google.gson.stream JsonReader nextString
public String nextString() throws IOException
From source file:com.singhinderjeet.gracenoteapi.json.UtcDateTypeAdapter.java
License:Apache License
@SuppressWarnings("deprecation") @Override/*from w w w. j a va 2 s.c om*/ public Date read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; default: String date = in.nextString(); // Instead of using iso8601Format.parse(value), we use Jackson's date parsing // This is because Android doesn't support XXX because it is JDK 1.6 try { return parse(date, new ParsePosition(0)); } catch (ParseException e) { try { return new Date(date); } catch (IllegalArgumentException iae) { try { return dateFormat.get().parse(date); } catch (ParseException pe) { throw new JsonParseException(pe); } } } } }
From source file:com.smartling.api.sdk.util.DateTypeAdapter.java
License:Apache License
public Date read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();//from w w w . j a v a 2 s . com return null; } String value = reader.nextString(); try { return DateFormatter.parse(value); } catch (ParseException e) { throw new IOException(e); } }
From source file:com.solidfire.jsvcgen.serialization.ApiServerExceptionTypeAdapter.java
License:Open Source License
/** * {@inheritDoc}//from w w w.j a v a2 s . c o m */ @Override public ApiServerException read(JsonReader in) throws IOException { String name = null; String code = null; String message = null; in.beginObject(); while (in.hasNext()) { if (in.peek() != JsonToken.NAME) { in.skipValue(); continue; } switch (in.nextName()) { case "name": name = in.nextString(); break; case "code": code = in.nextString(); break; case "message": message = in.nextString(); break; } } in.endObject(); return new ApiServerException(name, code, message); }
From source file:com.solidfire.jsvcgen.serialization.DateTimeAdapter.java
License:Open Source License
/** * Reads a DateTime object.// w ww . j a v a2 s. c o m * * @param reader the JSON reader to read from. * @return The DateTime object that was read. * @throws IOException if the DateTime object cannot be parsed */ @Override public DateTime read(JsonReader reader) throws IOException { return DateTime.parse(reader.nextString()); }
From source file:com.solidfire.jsvcgen.serialization.DurationAdapter.java
License:Open Source License
/** * Reads a Duration object./*from w ww.j av a 2 s .c o m*/ * * @param reader the JSON reader to read from. * @return The Duration object that was read. * @throws IOException if the Duration can not be parsed. */ @Override public Duration read(JsonReader reader) throws IOException { String input = reader.nextString(); Matcher m = inputRegex.matcher(input); while (m.matches()) { long hours = m.group(2) != null ? Long.parseLong(m.group(2)) : 0; long minutes = m.group(3) != null ? Long.parseLong(m.group(3)) : 0; long seconds = m.group(4) != null ? Long.parseLong(m.group(4)) : 0; // Milliseconds are trickier because if the input string is not properly formatted (e.g. ".1") we need to // right pad the input string with zeros -- ".1" is really 100 milliseconds. Likewise, if the string is // larger than 3 characters, we need to cut off extra characters. String millisecondsSource = m.group(5) != null ? m.group(5) : "000"; while (millisecondsSource.length() < 3) millisecondsSource += '0'; millisecondsSource = millisecondsSource.substring(0, 3); long millis = Long.parseLong(millisecondsSource); long millisTotal = ((hours * 60 + minutes) * 60 + seconds) * 1000 + millis; // Negate final output if '-' was on original input if (m.group(1).equals("-")) millisTotal = -millisTotal; return Duration.millis(millisTotal); } throw new RuntimeException("Could not extract duration from \"" + input + "\""); }
From source file:com.squareup.wire.ByteStringTypeAdapter.java
License:Apache License
@Override public ByteString read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();//from w w w .ja v a 2 s. c om return null; } return ByteString.decodeBase64(in.nextString()); }
From source file:com.squareup.wire.MessageTypeAdapter.java
License:Apache License
private void parseUnknownField(JsonReader in, Message.Builder<M> builder, int tag) throws IOException { in.beginArray();/*ww w. j a v a 2 s . c o m*/ UnknownFieldType type = UnknownFieldType.of(in.nextString()); while (in.peek() != JsonToken.END_ARRAY) { switch (type) { case VARINT: builder.addVarint(tag, in.nextInt()); break; case FIXED32: builder.addFixed32(tag, in.nextInt()); break; case FIXED64: builder.addFixed64(tag, in.nextInt()); break; case LENGTH_DELIMITED: builder.addLengthDelimited(tag, ByteString.decodeBase64(in.nextString())); break; default: throw new AssertionError("Unknown field type " + type); } } in.endArray(); }
From source file:com.suny.whereami.service.google.Place.java
License:Open Source License
/** * Read fields from a result object.//from w ww . jav a 2 s . com * * @param fields * to read or 0 if all fields should be read * @param maxResults * maximum number of reviews, events, and photos to return */ Place(JsonReader in, int fields, int maxResults) throws IOException { in.beginObject(); while (in.hasNext()) { Key key = Key.get(in.nextName()); if (key == UNKNOWN || fields != 0 && key.mField != null && !key.mField.in(fields)) { /* unknown field or caller doesn't want it */ in.skipValue(); continue; } switch (key) { case id: mId = in.nextString(); break; case reference: mReference = in.nextString(); break; case icon: mIcon = in.nextString(); break; case url: mUrl = in.nextString(); break; case geometry: in.beginObject(); while (in.hasNext()) { if (in.nextName().equals("location")) { in.beginObject(); while (in.hasNext()) { switch (Key.get(in.nextName())) { case lat: mLat = in.nextDouble(); break; case lng: mLong = in.nextDouble(); break; default: in.skipValue(); } } in.endObject(); } else { in.skipValue(); // "viewport" } } in.endObject(); break; case name: mName = in.nextString(); break; case address_components: mAddress = new Address(in); break; case formatted_address: mFmtAddress = in.nextString(); break; case vicinity: mVicinity = in.nextString(); break; case international_phone_number: mIntlPhone = in.nextString(); break; case formatted_phone_number: mFmtPhone = in.nextString(); break; case website: mWebsite = in.nextString(); break; case types: types(in); break; case price_level: mPrice = in.nextInt(); break; case rating: mRating = (float) in.nextDouble(); break; case reviews: in.beginArray(); while (in.hasNext()) { if (mReviews == null) { int cap = Math.min(Math.max(0, maxResults), MAX_REVIEWS); mReviews = new ArrayList<Review>(cap > 0 ? cap : MAX_REVIEWS); } if (maxResults <= 0 || mReviews.size() < maxResults) { mReviews.add(new Review(in)); } else { in.skipValue(); } } in.endArray(); break; case opening_hours: in.beginObject(); while (in.hasNext()) { switch (Key.get(in.nextName())) { case open_now: mOpen = in.nextBoolean(); break; case periods: in.beginArray(); while (in.hasNext()) { if (mOpenHours == null) { mOpenHours = new ArrayList<OpeningHours>(); } mOpenHours.add(new OpeningHours(in)); } in.endArray(); break; default: in.skipValue(); } } in.endObject(); break; case events: in.beginArray(); while (in.hasNext()) { if (mEvents == null) { int cap = Math.min(Math.max(0, maxResults), MAX_EVENTS); mEvents = new ArrayList<Event>(cap > 0 ? cap : MAX_EVENTS); } if (maxResults <= 0 || mEvents.size() < maxResults) { mEvents.add(new Event(in)); } else { in.skipValue(); } } in.endArray(); break; case utc_offset: mUtcOffset = in.nextInt(); break; case photos: in.beginArray(); while (in.hasNext()) { if (mPhotos == null) { int cap = Math.min(Math.max(0, maxResults), MAX_PHOTOS); mPhotos = new ArrayList<Photo>(cap > 0 ? cap : MAX_PHOTOS); } if (maxResults <= 0 || mPhotos.size() < maxResults) { mPhotos.add(new Photo(in)); } else { in.skipValue(); } } in.endArray(); break; default: in.skipValue(); } } in.endObject(); }
From source file:com.suny.whereami.service.google.Place.java
License:Open Source License
/** * Read field values from a types array. *///from ww w . j a v a 2 s . c o m void types(JsonReader in) throws IOException { in.beginArray(); while (in.hasNext()) { if (mTypes == null) { mTypes = new ArrayList<String>(); } mTypes.add(in.nextString()); } in.endArray(); }
From source file:com.teotigraphix.caustk.tone.ToneUtils.java
License:Apache License
/** * Reads and returns the {@link ToneType} from the JSON data. * /*from w w w .ja v a2 s.co m*/ * @param data Valid serialized Tone data. * @throws IOException */ public static ToneType readToneType(String data) throws IOException { JsonReader reader = new JsonReader(new StringReader(data)); String type = null; try { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("toneType")) { type = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); } finally { reader.close(); } if (type != null) return ToneType.valueOf(type); return null; }