List of usage examples for com.google.gson.stream JsonReader beginArray
public void beginArray() throws IOException
From source file:BundleTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* ww w.j a v a 2 s .com*/ public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { if (!Bundle.class.isAssignableFrom(type.getRawType())) { return null; } return (TypeAdapter<T>) new TypeAdapter<Bundle>() { @Override public void write(JsonWriter out, Bundle bundle) throws IOException { if (bundle == null) { out.nullValue(); return; } out.beginObject(); for (String key : bundle.keySet()) { out.name(key); Object value = bundle.get(key); if (value == null) { out.nullValue(); } else { gson.toJson(value, value.getClass(), out); } } out.endObject(); } @Override public Bundle read(JsonReader in) throws IOException { switch (in.peek()) { case NULL: in.nextNull(); return null; case BEGIN_OBJECT: return toBundle(readObject(in)); default: throw new IOException("expecting object: " + in.getPath()); } } private Bundle toBundle(List<Pair<String, Object>> values) throws IOException { Bundle bundle = new Bundle(); for (Pair<String, Object> entry : values) { String key = entry.first; Object value = entry.second; if (value instanceof String) { bundle.putString(key, (String) value); } else if (value instanceof Integer) { bundle.putInt(key, ((Integer) value).intValue()); } else if (value instanceof Long) { bundle.putLong(key, ((Long) value).longValue()); } else if (value instanceof Double) { bundle.putDouble(key, ((Double) value).doubleValue()); } else if (value instanceof Parcelable) { bundle.putParcelable(key, (Parcelable) value); } else if (value instanceof List) { List<Pair<String, Object>> objectValues = (List<Pair<String, Object>>) value; Bundle subBundle = toBundle(objectValues); bundle.putParcelable(key, subBundle); } else { throw new IOException("Unparcelable key, value: " + key + ", " + value); } } return bundle; } private List<Pair<String, Object>> readObject(JsonReader in) throws IOException { List<Pair<String, Object>> object = new ArrayList<Pair<String, Object>>(); in.beginObject(); while (in.peek() != JsonToken.END_OBJECT) { switch (in.peek()) { case NAME: String name = in.nextName(); Object value = readValue(in); object.add(new Pair<String, Object>(name, value)); break; case END_OBJECT: break; default: throw new IOException("expecting object: " + in.getPath()); } } in.endObject(); return object; } private Object readValue(JsonReader in) throws IOException { switch (in.peek()) { case BEGIN_ARRAY: return readArray(in); case BEGIN_OBJECT: return readObject(in); case BOOLEAN: return in.nextBoolean(); case NULL: in.nextNull(); return null; case NUMBER: return readNumber(in); case STRING: return in.nextString(); default: throw new IOException("expecting value: " + in.getPath()); } } private Object readNumber(JsonReader in) throws IOException { double doubleValue = in.nextDouble(); if (doubleValue - Math.ceil(doubleValue) == 0) { long longValue = (long) doubleValue; if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) { return (int) longValue; } return longValue; } return doubleValue; } @SuppressWarnings("rawtypes") private List readArray(JsonReader in) throws IOException { List list = new ArrayList(); in.beginArray(); while (in.peek() != JsonToken.END_ARRAY) { Object element = readValue(in); list.add(element); } in.endArray(); return list; } }; }
From source file:at.univie.sensorium.preferences.Preferences.java
License:Open Source License
private void loadPrefsFromStream(InputStream input) { List<BasicNameValuePair> preferencelist = new LinkedList<BasicNameValuePair>(); try {//from w w w. ja va 2 s .c om InputStreamReader isreader = new InputStreamReader(input); JsonReader reader = new JsonReader(isreader); // String jsonVersion = ""; reader.beginArray(); // do we have an array or just a single object? reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); String value = reader.nextString(); if (name.equalsIgnoreCase(PREFERENCES_VERSION)) currentPrefVersion = Integer.valueOf(value); BasicNameValuePair kv = new BasicNameValuePair(name, value); preferencelist.add(kv); } reader.endObject(); reader.endArray(); reader.close(); if (newerPrefsAvailable()) { Log.d(SensorRegistry.TAG, "Newer preferences available in json, overwriting existing."); for (BasicNameValuePair kv : preferencelist) { putPreference(kv.getName(), kv.getValue()); } // also reset the welcome screen putBoolean(WELCOME_SCREEN_SHOWN, false); } else { Log.d(SensorRegistry.TAG, "Preferences are recent, not overwriting."); } } catch (FileNotFoundException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (IOException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } }
From source file:ca.mcgill.cs.creco.data.CRDeadlinks.java
License:Apache License
private void tryreadingthejson() throws IOException { InputStream in = new FileInputStream(DataPath.get() + "dead_links.json"); JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginArray(); while (reader.hasNext()) { }/*from w ww. j a v a 2s . c o m*/ reader.endArray(); reader.close(); in.close(); }
From source file:ca.mcgill.cs.creco.data.CRDeadlinks.java
License:Apache License
private static void readFile(String pFilePath) throws IOException, InterruptedException { InputStream in = new FileInputStream(pFilePath); JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginArray(); while (reader.hasNext()) { ProductStub prodStub = new Gson().fromJson(reader, ProductStub.class); writeToFile = writeToFile.concat("{\"product_id\":" + prodStub.id + ","); String urltext = prodStub.modelOverviewPageUrl; // Write 404 error if no URL exists if (urltext == null) { writeToFile = writeToFile.concat("\"state\":" + "404" + "},"); continue; }//w w w .j a v a 2s . c o m Thread.sleep(SLEEP); URL url = new URL(urltext); // Attempt a connection and see the resulting response code it returns int responseCode = ((HttpURLConnection) url.openConnection()).getResponseCode(); writeToFile = writeToFile.concat("\"state\":" + responseCode + "},"); } reader.endArray(); reader.close(); in.close(); }
From source file:ca.mcgill.cs.creco.data.json.JsonLoadingService.java
License:Apache License
private static void readFile(String filePath, IDataCollector pCollector) throws IOException { InputStream in = new FileInputStream(filePath); JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginArray(); while (reader.hasNext()) { ProductStub prodStub = new Gson().fromJson(reader, ProductStub.class); pCollector.addProduct(buildProduct(prodStub)); }// w w w. j a v a 2s .com reader.endArray(); reader.close(); in.close(); }
From source file:ca.mcgill.cs.creco.data.json.JsonLoadingService.java
License:Apache License
private void readDeadLinks() throws FileNotFoundException, IOException { // Try to read the dead links file InputStream in;//from w ww . j a va 2 s. co m try { in = new FileInputStream(aPath + aDeadLinksFileName); } catch (FileNotFoundException e) { return; } // Flag that we were succesful finding the deadlins file, so deadlinks // will be checked while building products aDoCheckDeadLinks = true; // Make a json reader for the deadlinks file JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginArray(); // Iterate over each entry in the deadlinks file, putting a records in a HashTable while (reader.hasNext()) { LinkResponseStub responseStub = new Gson().fromJson(reader, LinkResponseStub.class); aDeadLinks.put(responseStub.product_id, responseStub.state); } reader.endArray(); reader.close(); in.close(); }
From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java
License:Open Source License
private Set<CallSite> readCallSites(JsonReader in) throws IOException { Set<CallSite> sites = Sets.newLinkedHashSet(); in.beginArray(); while (in.hasNext()) { sites.add(readCallSite(in));//from www . j av a 2s.c om } in.endArray(); return sites; }
From source file:ch.cyberduck.core.importer.ExpandriveBookmarkCollection.java
License:Open Source License
@Override protected void parse(final Local file) throws AccessDeniedException { try {/*from w ww. j a v a 2 s . c o m*/ final JsonReader reader = new JsonReader(new InputStreamReader(file.getInputStream(), "UTF-8")); reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); final Host current = new Host(new FTPProtocol(), PreferencesFactory.get().getProperty("connection.hostname.default")); while (reader.hasNext()) { final String name = reader.nextName(); switch (name) { case "server": current.setHostname(reader.nextString()); break; case "username": current.getCredentials().setUsername(reader.nextString()); break; case "private_key_file": current.getCredentials().setIdentity(LocalFactory.get(reader.nextString())); break; case "remotePath": current.setDefaultPath(reader.nextString()); break; case "type": final Protocol type = ProtocolFactory.forName(reader.nextString()); if (null != type) { current.setProtocol(type); } break; case "protocol": final Protocol protocol = ProtocolFactory.forName(reader.nextString()); if (null != protocol) { current.setProtocol(protocol); // Reset port to default current.setPort(-1); } break; case "name": current.setNickname(reader.nextString()); break; case "region": current.setRegion(reader.nextString()); break; default: log.warn(String.format("Ignore property %s", name)); reader.skipValue(); break; } } reader.endObject(); this.add(current); } reader.endArray(); } catch (IllegalStateException | IOException e) { throw new LocalAccessDeniedException(e.getMessage(), e); } }
From source file:ch.cyberduck.core.importer.NetDrive2BookmarkCollection.java
License:Open Source License
@Override protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException { try {/*from w w w.j a v a 2 s. c o m*/ final JsonReader reader = new JsonReader(new InputStreamReader(file.getInputStream(), "UTF-8")); reader.beginArray(); String url; String user; boolean ssl; Protocol protocol; while (reader.hasNext()) { reader.beginObject(); boolean skip = false; url = null; ssl = false; protocol = null; user = null; while (reader.hasNext()) { final String name = reader.nextName(); switch (name) { case "url": url = this.readNext(name, reader); if (StringUtils.isBlank(url)) { skip = true; } break; case "ssl": ssl = reader.nextBoolean(); break; case "user": user = this.readNext(name, reader); break; case "type": final String type = this.readNext(name, reader); switch (type) { case "google_cloud_storage": protocol = protocols.forType(Protocol.Type.googlestorage); break; case "gdrive": protocol = protocols.forType(Protocol.Type.googledrive); break; default: protocol = protocols.forName(type); } break; default: log.warn(String.format("Ignore property %s", name)); reader.skipValue(); break; } } reader.endObject(); if (!skip && protocol != null && StringUtils.isNotBlank(user)) { if (ssl) { switch (protocol.getType()) { case ftp: protocol = protocols.forScheme(Scheme.ftps); break; case dav: protocol = protocols.forScheme(Scheme.davs); break; } } this.add(HostParser.parse(protocols, protocol, url)); } } reader.endArray(); } catch (IllegalStateException | IOException e) { throw new LocalAccessDeniedException(e.getMessage(), e); } }
From source file:classifiers.DummyClassifier.java
License:Apache License
public void parseStreamAndClassify(String jsonFile, String resultsFile) throws IOException { String journalName;/*from www . j ava 2 s . com*/ int count = 0; int abstract_count = 0; try { JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile))); JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(resultsFile), "UTF-8")); writer.setIndent(" "); //reader.setLenient(true); reader.beginArray(); writer.beginArray(); while (reader.hasNext()) { reader.beginObject(); writer.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("abstract")) { abstract_count++; reader.skipValue(); } else if (name.equals("pmid")) { String pmid = reader.nextString(); writer.name("labels"); writeLabels(writer); writer.name("pmid").value(pmid); } else if (name.equals("title")) { reader.skipValue(); } else { System.out.println(name); reader.skipValue(); } } reader.endObject(); writer.endObject(); } reader.endArray(); writer.endArray(); System.out.println("Abstracts: " + abstract_count); writer.close(); } catch (FileNotFoundException ex) { } }