List of usage examples for com.google.gson.stream JsonReader close
public void close() throws IOException
From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java
License:Apache License
public SparseArray<Stop> fetchStops() throws IOException { // Download stop data. URL stopsURL = new URL(mStopsUrl); URLConnection stopsConnection = stopsURL.openConnection(); stopsConnection.setRequestProperty("User-Agent", mUserAgent); InputStream in = new BufferedInputStream(stopsConnection.getInputStream()); JsonReader reader = new JsonReader(new InputStreamReader(in)); mStops.clear();/*from w ww. j a va 2 s. co m*/ mRouteStopsMap.clear(); reader.beginArray(); while (reader.hasNext()) { Stop stop = readStop(reader); mStops.put(stop.id, stop); } reader.endArray(); reader.close(); return mStops; }
From source file:edu.rpi.shuttles.data.RPIShuttleDataProvider.java
License:Apache License
public SparseArray<Vehicle> updateVehicleLocations() throws IOException { // Download vehicle location data. URL vehicleLocationURL = new URL(mVehicleLocationsUrl); URLConnection vehicleLocationConnection = vehicleLocationURL.openConnection(); vehicleLocationConnection.setRequestProperty("User-Agent", mUserAgent); InputStream in = new BufferedInputStream(vehicleLocationConnection.getInputStream()); JsonReader reader = new JsonReader(new InputStreamReader(in)); SparseArray<Vehicle> updated_vehicles = new SparseArray<Vehicle>(); reader.beginArray();// w w w .ja v a 2 s . co m while (reader.hasNext()) { Vehicle shuttle = readVehicleLocation(reader); updated_vehicles.put(shuttle.id, shuttle); mVehicles.put(shuttle.id, shuttle); } reader.endArray(); reader.close(); return updated_vehicles; }
From source file:edu.washington.cs.cupid.usage.CupidDataCollector.java
License:Open Source License
public synchronized String getAllJson(String indent, boolean includeNow) throws IOException { List<SessionLog> logs = Lists.newArrayList(); for (File file : logDirectory.listFiles()) { if (file.getName().endsWith(".json")) { JsonReader reader = new JsonReader(new FileReader(file)); logs.add((SessionLog) gson.fromJson(reader, SessionLog.class)); reader.close(); }//from w w w. j a v a2 s . c o m } if (init) { logs.add(new SessionLog(uuid(), system, sessionLog)); } StringWriter result = new StringWriter(); JsonWriter writer = new JsonWriter(result); if (indent != null) { writer.setIndent(indent); } gson.toJson(logs, new TypeToken<List<SessionLog>>() { }.getType(), writer); writer.close(); return result.toString(); }
From source file:eu.pinnoo.garbagecalendar.util.parsers.CalendarParser.java
License:Apache License
@Override protected ArrayList downloadData() throws IOException { ArrayList<PrimitiveCollection> list = new ArrayList<PrimitiveCollection>(); InputStream inp = Network.getStream(getURL()); JsonReader reader = new JsonReader(new InputStreamReader(inp, LocalConstants.ENCODING)); PrimitiveCollectionList results = new GsonBuilder().create().fromJson(reader, PrimitiveCollectionList.class); list.addAll((java.util.Collection<PrimitiveCollection>) results.list); reader.close(); return list;/*from w w w . j av a 2 s . c om*/ }
From source file:eu.pinnoo.garbagecalendar.util.parsers.StreetsParser.java
License:Apache License
@Override protected ArrayList downloadData() throws IOException { ArrayList<PrimitiveAddress> list = new ArrayList<PrimitiveAddress>(); InputStream inp = Network.getStream(getURL()); JsonReader reader = new JsonReader(new InputStreamReader(inp, LocalConstants.ENCODING)); PrimitiveAddressList results = new GsonBuilder().create().fromJson(reader, PrimitiveAddressList.class); list.addAll((Collection<PrimitiveAddress>) results.list); reader.close(); return list;// ww w . j av a2s. c o m }
From source file:gov.pnnl.goss.gridappsd.testmanager.CompareResults.java
License:Open Source License
public SimulationOutput getOutputProperties(String path) { Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); JsonReader jsonReader; SimulationOutput testScript = null;//from w ww.java 2 s .com try { jsonReader = new JsonReader(new FileReader(path)); jsonReader.setLenient(true); testScript = gson.fromJson(new FileReader(path), SimulationOutput.class); jsonReader.close(); } catch (Exception e) { e.printStackTrace(); } return testScript; }
From source file:ilearnrw.prototype.application.JsonHandler.java
License:Open Source License
public Object fromJson(java.lang.reflect.Type type) { Object obj = null;// w w w . ja va 2s.c o m try { InputStream inputStream = ResourceLoader.getInstance().getInputStream(this.type, path); JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8")); obj = gson.fromJson(reader, type); reader.close(); } catch (IOException e) { e.printStackTrace(); return obj; } return obj; }
From source file:io.grpc.internal.JsonParser.java
License:Apache License
/** * Parses a json string, returning either a {@code Map<String, ?>}, {@code List<?>}, * {@code String}, {@code Double}, {@code Boolean}, or {@code null}. *//*from ww w . j a v a 2s. c om*/ public static Object parse(String raw) throws IOException { JsonReader jr = new JsonReader(new StringReader(raw)); try { return parseRecursive(jr); } finally { try { jr.close(); } catch (IOException e) { logger.log(Level.WARNING, "Failed to close", e); } } }
From source file:io.motown.chargingstationconfiguration.viewmodel.restapi.providers.GsonJsonProvider.java
License:Apache License
@Override public Object readFrom(Class<Object> objectClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> stringStringMultivaluedMap, InputStream inputStream) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(inputStream, Charset.forName(UTF_8))); Object obj = gson.fromJson(reader, type); reader.close(); return obj;/* w w w . ja va 2s. c o m*/ }
From source file:it.bradipao.berengar.DbTool.java
License:Apache License
public static int gson2db(SQLiteDatabase mDB, File jsonFile) { // vars/*from w ww . j a v a2 s. c om*/ int iTableNum = 0; FileReader fr = null; BufferedReader br = null; JsonReader jr = null; String name = null; String val = null; String mTable = null; String mTableSql = null; ArrayList<String> aFields = null; ArrayList<String> aValues = null; ContentValues cv = null; // file readers try { fr = new FileReader(jsonFile); br = new BufferedReader(fr); jr = new JsonReader(br); } catch (FileNotFoundException e) { Log.e(LOGTAG, "error in gson2db file readers", e); } // parsing try { // start database transaction mDB.beginTransaction(); // open root { jr.beginObject(); // iterate through root objects while (jr.hasNext()) { name = jr.nextName(); if (jr.peek() == JsonToken.NULL) jr.skipValue(); // number of tables else if (name.equals("tables_num")) { val = jr.nextString(); iTableNum = Integer.parseInt(val); if (GOLOG) Log.d(LOGTAG, "TABLE NUM : " + iTableNum); } // iterate through tables array else if (name.equals("tables")) { jr.beginArray(); while (jr.hasNext()) { // start table mTable = null; aFields = null; jr.beginObject(); while (jr.hasNext()) { name = jr.nextName(); if (jr.peek() == JsonToken.NULL) jr.skipValue(); // table name else if (name.equals("table_name")) { mTable = jr.nextString(); } // table sql else if (name.equals("table_sql")) { mTableSql = jr.nextString(); if ((mTable != null) && (mTableSql != null)) { mDB.execSQL("DROP TABLE IF EXISTS " + mTable); mDB.execSQL(mTableSql); if (GOLOG) Log.d(LOGTAG, "DROPPED AND CREATED TABLE : " + mTable); } } // iterate through columns name else if (name.equals("cols_name")) { jr.beginArray(); while (jr.hasNext()) { val = jr.nextString(); if (aFields == null) aFields = new ArrayList<String>(); aFields.add(val); } jr.endArray(); if (GOLOG) Log.d(LOGTAG, "COLUMN NAME : " + aFields.toString()); } // iterate through rows else if (name.equals("rows")) { jr.beginArray(); while (jr.hasNext()) { jr.beginArray(); // iterate through values in row aValues = null; cv = null; while (jr.hasNext()) { val = jr.nextString(); if (aValues == null) aValues = new ArrayList<String>(); aValues.add(val); } jr.endArray(); // add to database cv = new ContentValues(); for (int j = 0; j < aFields.size(); j++) cv.put(aFields.get(j), aValues.get(j)); mDB.insert(mTable, null, cv); if (GOLOG) Log.d(LOGTAG, "INSERT IN " + mTable + " : " + aValues.toString()); } jr.endArray(); } else jr.skipValue(); } // end table jr.endObject(); } jr.endArray(); } else jr.skipValue(); } // close root } jr.endObject(); jr.close(); // successfull transaction mDB.setTransactionSuccessful(); } catch (IOException e) { Log.e(LOGTAG, "error in gson2db gson parsing", e); } finally { mDB.endTransaction(); } return iTableNum; }