List of usage examples for com.google.gson.stream JsonReader JsonReader
public JsonReader(Reader in)
From source file:com.suny.whereami.service.google.Places.java
License:Open Source License
/** * Get a reader for the URL./* ww w . jav a2 s . c o m*/ */ private static JsonReader reader(String url) throws IOException { URLConnection con = HttpClient.openConnection(new URL(url)); return new JsonReader(new InputStreamReader(con.getInputStream(), "UTF-8")); }
From source file:com.synopsys.integration.hub.bdio.BdioReader.java
License:Apache License
public BdioReader(final Gson gson, final Reader reader) throws IOException { this.gson = gson; this.jsonReader = new JsonReader(reader); jsonReader.beginArray();/*from w w w.ja v a2 s . c o m*/ }
From source file:com.synopsys.integration.hub.bdio.BdioReader.java
License:Apache License
public BdioReader(final Gson gson, final InputStream inputStream) throws IOException { this.gson = gson; this.jsonReader = new JsonReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); jsonReader.beginArray();// w w w . j a va2s .c om }
From source file:com.tapchatapp.android.client.TapchatService.java
License:Apache License
private void oobInclude(String path) throws Exception { mLoadingOobBacklog = true;// w w w . j a v a2 s . co m Response response = mAPI.oobInclude(path.substring(1)); JsonReader reader = new JsonReader(new InputStreamReader(response.getBody().in())); reader.beginArray(); while (reader.hasNext()) { Message message = mGson.fromJson(reader, Message.class); handleMessage(message); } reader.endArray(); reader.close(); mLoadingOobBacklog = false; handleMessageCache(); }
From source file:com.teotigraphix.caustk.tone.ToneUtils.java
License:Apache License
/** * Reads and returns the {@link ToneType} from the JSON data. * //from w ww . j a v a2 s . c o 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; }
From source file:com.thebluealliance.androidclient.datafeed.retrofit.LenientGsonConverter.java
License:Apache License
@Override public T fromBody(ResponseBody body) throws IOException { Reader in = body.charStream(); try {/*w ww . j a v a2 s . c om*/ JsonReader reader = new JsonReader(in); reader.setLenient(true); return typeAdapter.read(reader); } finally { try { in.close(); } catch (IOException ignored) { } } }
From source file:com.trellmor.berrymotes.sync.SubredditEmoteDownloader.java
License:Open Source License
private List<EmoteImage> downloadEmoteList() throws URISyntaxException, IOException, InterruptedException { checkInterrupted();/*from www . j a va2 s . c o m*/ Log.debug("Downloading " + mSubreddit + EMOTES); HttpRequestBase request = new HttpGet(); request.setURI(new URI(EmoteDownloader.HOST + mSubreddit + EMOTES)); request.setHeader("If-Modified-Since", DateUtils.formatDate(mLastModified)); mEmoteDownloader.checkCanDownload(); HttpResponse response = mEmoteDownloader.getHttpClient().execute(request); switch (response.getStatusLine().getStatusCode()) { case 200: Log.debug(mSubreddit + EMOTES + " loaded"); // Download ok Header[] lastModified = response.getHeaders("last-modified"); if (lastModified.length > 0) { try { mLastModified = DateUtils.parseDate(lastModified[0].getValue()); } catch (DateParseException e) { Log.error("Error parsing last-modified header", e); } } HttpEntity entity = response.getEntity(); if (entity != null) { checkInterrupted(); File tmpFile = File.createTempFile(mSubreddit, null, mContext.getCacheDir()); try { InputStream is = entity.getContent(); try { mEmoteDownloader.checkStorageAvailable(); StreamUtils.saveStreamToFile(is, tmpFile); } finally { StreamUtils.closeStream(is); } FileInputStream fis = null; BufferedInputStream bis = null; GZIPInputStream zis = null; Reader isr = null; JsonReader jsonReader = null; checkInterrupted(); try { fis = new FileInputStream(tmpFile); bis = new BufferedInputStream(fis); zis = new GZIPInputStream(bis); isr = new InputStreamReader(zis, "UTF-8"); jsonReader = new JsonReader(isr); jsonReader.beginArray(); Gson gson = new Gson(); ArrayList<EmoteImage> emotes = new ArrayList<EmoteImage>(); while (jsonReader.hasNext()) { EmoteImage emote = gson.fromJson(jsonReader, EmoteImage.class); emotes.add(emote); } jsonReader.endArray(); Log.info("Loaded " + mSubreddit + EMOTES + ", size: " + Integer.toString(emotes.size())); return emotes; } finally { StreamUtils.closeStream(jsonReader); StreamUtils.closeStream(isr); StreamUtils.closeStream(zis); StreamUtils.closeStream(bis); StreamUtils.closeStream(fis); } } finally { tmpFile.delete(); } } break; case 304: Log.info(mSubreddit + EMOTES + " already up to date (HTTP 304)"); break; case 403: case 404: Log.info(mSubreddit + " missing on server, removing emotes"); mEmoteDownloader.deleteSubreddit(mSubreddit, mContentResolver); break; default: throw new IOException("Unexpected HTTP response: " + response.getStatusLine().getReasonPhrase()); } return null; }
From source file:com.triarc.sync.SyncAdapter.java
License:Apache License
@SuppressLint("NewApi") private void readResponse(SyncTypeCollection typeCollection, InputStream inputStream, HttpResponse response) throws UnsupportedEncodingException, IOException, JSONException, Exception { long lastUpdate = 0; Header header = response.getFirstHeader("X-Application-Timestamp"); if (header != null) { String value = header.getValue(); lastUpdate = Long.parseLong(value); }// w w w .ja v a2 s .c om // json is UTF-8 by default BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); JsonStreamParser parser = new JsonStreamParser(reader); JsonReader jsonReader = new JsonReader(reader); jsonReader.beginObject(); jsonReader.nextName(); jsonReader.beginObject(); while (jsonReader.hasNext()) { String syncName = jsonReader.nextName(); SyncType syncType = this.GetSyncType(typeCollection, syncName); JsonElement fromJson = new Gson().fromJson(jsonReader, JsonElement.class); updateLocalTypeData(fromJson.getAsJsonObject(), syncType, typeCollection, syncResult); notificationQueue.add(new SyncNotificationMessage(syncType, fromJson.toString())); // String path = jsonReader.getPath(); // String nextName = jsonReader.nextName(); // jsonReader.endObject(); } jsonReader.endObject(); // String json = getStringForReader(reader); // // // JSONObject syncChangeSets = new JSONObject(json) // .getJSONObject("changeSetPerType"); // // // first save all // for (SyncType syncType : typeCollection.getTypes()) { // syncResult.madeSomeProgress(); // String name = syncType.getName(); // // if (syncChangeSets.has(name)) { // JSONObject changeSetObject = syncChangeSets.getJSONObject(name); // updateLocalTypeData(changeSetObject, syncType, typeCollection, // syncResult); // notificationMap.put(syncType, changeSetObject); // } else { // Log.w(TAG, "Server does not support syncing of " + name); // sendLogs(); // } // } // store collection update timestamp PreferenceManager.getDefaultSharedPreferences(this.getContext()).edit() .putLong(typeCollection.getName(), lastUpdate).commit(); // then notify all notifyWebApp(); }
From source file:com.ushahidi.java.sdk.api.tasks.BaseTask.java
License:Open Source License
/** * Deserialize the JSON from a stream object * //w w w. j a v a 2 s. c o m * @param s The stream * @param cls The class for the model * @return The related object */ public static <T> T fromStream(InputStream s, Class<T> cls) { return cls.cast(gson.fromJson(new JsonReader(new InputStreamReader(s)), cls)); }
From source file:com.utad.flume.interceptor.InterceptorTwitter.java
License:Apache License
private byte[] readJsonStream(InputStream is) { byte[] body = null; try {/*from w w w .j av a 2 s . c om*/ JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8")); try { long id = 0L; String text = null; String userName = null; String userScreenName = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("id")) { id = reader.nextLong(); } else if (name.equals("text")) { text = reader.nextString(); } else if (name.equals("user")) { reader.beginObject(); while (reader.hasNext()) { name = reader.nextName(); if (name.equals("name")) { userName = reader.nextString(); } else if (name.equals("screen_name")) { userScreenName = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); } else { reader.skipValue(); } } reader.endObject(); StringBuilder builder = new StringBuilder("ID:"); builder.append(id); builder.append("||"); if (outputText) { builder.append("TEXT:").append(text).append("||"); } if (outputUserName) { builder.append("USER:").append(userName).append("||"); } if (outputUserScreenName) { builder.append("SCREEN:").append(userScreenName).append("||"); } logger.debug("id: {}", id); logger.debug("text: {}", text); logger.debug("username: {}", userName); logger.debug("screenName: {}", userScreenName); body = builder.toString().getBytes("UTF-8"); } finally { reader.close(); } } catch (UnsupportedEncodingException e) { logger.error("UTF-8 is not supported on this runtime", e); } catch (IOException e) { logger.error("Caught an IOException", e); } return body; }