List of usage examples for com.google.gson.stream JsonReader JsonReader
public JsonReader(Reader in)
From source file:com.pets.core.petstore.data.store.JSONSerializer.java
License:Apache License
/** * Deserialize the given JSON string to Java object. * * @param <T> Type/*ww w. j a v a2 s .c om*/ * @param body The JSON string * @param returnType The type to deserialize inot * @return The deserialized Java object */ @SuppressWarnings("unchecked") public <T> T deserialize(String body, Type returnType) { try { if (apiClient.isLenientOnJson()) { JsonReader jsonReader = new JsonReader(new StringReader(body)); // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean) jsonReader.setLenient(true); return gson.fromJson(jsonReader, returnType); } else { return gson.fromJson(body, returnType); } } catch (JsonParseException e) { // Fallback processing when failed to parse JSON from response body: // return the raw body return (T) body; } }
From source file:com.pocketbeer.presentation.flow.GsonParceler.java
License:Apache License
private Object decode(String json) throws IOException { JsonReader reader = new JsonReader(new StringReader(json)); try {// www .j a v a2 s . co m reader.beginObject(); Class<?> type = Class.forName(reader.nextName()); return mGson.fromJson(reader, type); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } finally { reader.close(); } }
From source file:com.psygate.vanguard.storage.VanguardFileStorage.java
License:Open Source License
@Override public PlayerSettings getPlayerSettings(UUID playerid) { if (!Files.exists(getUserFile(playerid))) { throw new IllegalArgumentException(); }// w w w.j av a 2 s. c o m try (JsonReader writer = new JsonReader(new FileReader(getUserFile(playerid).toFile()))) { return bgson.create().fromJson(writer, PlayerSettings.class); } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:com.ptapp.sync.PTAppDataHandler.java
License:Open Source License
/** * Processes a conference data body and calls the appropriate data type handlers * to process each of the objects represented therein. * * @param dataBody The body of data to process * @throws java.io.IOException If there is an error parsing the data. *///w w w. j av a 2s . com private void processDataBody(String dataBody) throws IOException { JsonReader reader = new JsonReader(new StringReader(dataBody)); JsonParser parser = new JsonParser(); try { reader.setLenient(true); // To err is human // the whole file is a single JSON object reader.beginObject(); while (reader.hasNext()) { // the key is "educators", "courses", "students", "events" etc. String key = reader.nextName(); if (mHandlerForKey.containsKey(key)) { // pass the value to the corresponding handler mHandlerForKey.get(key).process(parser.parse(reader)); } else { LOGW(TAG, "Skipping unknown key in ptapp data json: " + key); reader.skipValue(); } } reader.endObject(); } finally { reader.close(); } }
From source file:com.publicobject.roundsweb.PostServlet.java
License:Apache License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { URL requestUrl = new URL(request.getRequestURL().toString()); Reader reader = new InputStreamReader(request.getInputStream(), "UTF-8"); JsonReader jsonReader = new JsonReader(reader); Game game = gson.fromJson(jsonReader, Game.class); long gameId = storeGame(game); response.setContentType("text/plain"); Writer writer = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); URL gameUrl = new URL(requestUrl, String.format("/%d", gameId)); writer.write(gameUrl.toString());// ww w .j a v a2 s . c o m writer.close(); }
From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java
License:Open Source License
/** * can parse json like {"items":[{"id":6782}]} * @param in/* ww w . j a va 2s. c o m*/ * @param iJoBj * @return count all, count new items * @throws IOException */ public static int[] readJsonStreamV2(InputStream in, IHandleJsonObject iJoBj) throws IOException { List<String> allowedArrays = Arrays.asList("feeds", "folders", "items"); int count = 0; int newItemsCount = 0; JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginObject(); String currentName; while (reader.hasNext() && (currentName = reader.nextName()) != null) { if (allowedArrays.contains(currentName)) break; else reader.skipValue(); } reader.beginArray(); while (reader.hasNext()) { JSONObject e = getJSONObjectFromReader(reader); if (iJoBj.performAction(e)) newItemsCount++; count++; } if (iJoBj instanceof InsertItemIntoDatabase) ((InsertItemIntoDatabase) iJoBj).performDatabaseBatchInsert(); //Save pending buffer //reader.endArray(); //reader.endObject(); reader.close(); return new int[] { count, newItemsCount }; }
From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java
License:Open Source License
/** * can parse json like {"items":[{"id":6782}]} * @param in//from w w w . j ava 2s.c o m * @param iJoBj * @return new int[] { count, newItemsCount } * @throws IOException */ public static int[] readJsonStreamV1(InputStream in, IHandleJsonObject iJoBj) throws IOException { int count = 0; int newItemsCount = 0; JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginObject();//{ reader.nextName();//"ocs" reader.beginObject();//{ reader.nextName();//meta getJSONObjectFromReader(reader);//skip status etc. reader.nextName();//data reader.beginObject();//{ reader.nextName();//folders etc.. reader.beginArray(); while (reader.hasNext()) { //reader.beginObject(); JSONObject e = getJSONObjectFromReader(reader); if (iJoBj.performAction(e)) newItemsCount++; //reader.endObject(); count++; } if (iJoBj instanceof InsertItemIntoDatabase) ((InsertItemIntoDatabase) iJoBj).performDatabaseBatchInsert(); //Save pending buffer //reader.endArray(); //reader.endObject(); reader.close(); return new int[] { count, newItemsCount }; }
From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java
License:Open Source License
/** * can read json like {"version":"1.101"} * @param in/* w w w . j a v a 2s . co m*/ * @param iJoBj * @return * @throws IOException */ private static int readJsonStreamSimple(InputStream in, IHandleJsonObject iJoBj) throws IOException { int count = 0; JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); //reader.setLenient(true); //JsonToken token = reader.peek(); //while(token.equals(JsonToken.STRING)) // reader.skipValue(); JSONObject e = getJSONObjectFromReader(reader); iJoBj.performAction(e); reader.close(); return count; }
From source file:com.razza.apps.iosched.sync.ConferenceDataHandler.java
License:Open Source License
/** * Processes a conference data body and calls the appropriate data type handlers * to process each of the objects represented therein. * * @param dataBody The body of data to process * @throws IOException If there is an error parsing the data. *//*from w w w.ja va2 s . c om*/ private void processDataBody(String dataBody) throws IOException { JsonReader reader = new JsonReader(new StringReader(dataBody)); JsonParser parser = new JsonParser(); try { reader.setLenient(true); // To err is human // the whole file is a single JSON object reader.beginObject(); while (reader.hasNext()) { // the key is "rooms", "speakers", "tracks", etc. String key = reader.nextName(); if (mHandlerForKey.containsKey(key)) { // pass the value to the corresponding handler mHandlerForKey.get(key).process(parser.parse(reader)); } else { LogUtils.LOGW(TAG, "Skipping unknown key in conference data json: " + key); reader.skipValue(); } } reader.endObject(); } finally { reader.close(); } }
From source file:com.registrodevisitas.services.CausasServices.java
public void generarListaDeCausasDeCache() throws FileNotFoundException, IOException { logger.info("CausasServices:generarListaDeCausasDeCache: BEGIN "); Gson gson = new Gson(); JsonReader jsonReader = new JsonReader(new FileReader(this.cacheFile)); final ListaDeCausas listaDecausas = gson.fromJson(jsonReader, ListaDeCausas.class); jsonReader.close();//from w ww . j a v a2s . c o m listaDecausas.sortCauses(); causas.addAll(listaDecausas.getCausas()); listaDecausas.getCausas().clear(); logger.info("CausasServices:generarListaDeCausasDeCache: END "); }