List of usage examples for com.google.gson.stream JsonReader nextInt
public int nextInt() throws IOException
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private boolean parseArticle(final Article a, final JsonReader reader, final Set<Article.ArticleField> skipNames, final IArticleOmitter filter) throws IOException { boolean skipObject = false; while (reader.hasNext() && reader.peek().equals(JsonToken.NAME)) { if (skipObject) { // field name reader.skipValue();//from www . j a v a2s. c o m // field value reader.skipValue(); continue; } String name = reader.nextName(); Article.ArticleField field = Article.ArticleField.valueOf(name); try { if (skipNames != null && skipNames.contains(field)) { reader.skipValue(); continue; } switch (field) { case id: a.id = reader.nextInt(); break; case title: a.title = reader.nextString(); break; case unread: a.isUnread = reader.nextBoolean(); break; case updated: a.updated = new Date(reader.nextLong() * 1000); break; case feed_id: if (reader.peek() == JsonToken.NULL) reader.nextNull(); else a.feedId = reader.nextInt(); break; case content: a.content = reader.nextString(); break; case link: a.url = reader.nextString(); break; case comments: a.commentUrl = reader.nextString(); break; case attachments: a.attachments = parseAttachments(reader); break; case marked: a.isStarred = reader.nextBoolean(); break; case published: a.isPublished = reader.nextBoolean(); break; case labels: a.labels = parseLabels(reader); break; case author: a.author = reader.nextString(); break; default: reader.skipValue(); continue; } if (filter != null) skipObject = filter.omitArticle(field, a); } catch (IllegalArgumentException | StopJsonParsingException | IOException e) { Log.w(TAG, "Result contained illegal value for entry \"" + field + "\"."); reader.skipValue(); } } return skipObject; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * Retrieves all categories.//from w w w . jav a 2 s. co m * * @return a list of categories. */ public Set<Category> getCategories() { long time = System.currentTimeMillis(); Set<Category> ret = new LinkedHashSet<>(); if (sessionNotAlive()) return ret; Map<String, String> params = new HashMap<>(); params.put(PARAM_OP, VALUE_GET_CATEGORIES); JsonReader reader = null; try { reader = prepareReader(params); if (reader == null) return ret; reader.beginArray(); while (reader.hasNext()) { int id = -1; String title = null; int unread = 0; reader.beginObject(); while (reader.hasNext()) { try { switch (reader.nextName()) { case ID: id = reader.nextInt(); break; case TITLE: title = reader.nextString(); break; case UNREAD: unread = reader.nextInt(); break; default: reader.skipValue(); break; } } catch (IllegalArgumentException e) { e.printStackTrace(); reader.skipValue(); } } reader.endObject(); // Don't handle categories with an id below 1, we already have them in the DB from // Data.updateVirtualCategories() if (id > 0 && title != null) ret.add(new Category(id, title, unread)); } reader.endArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch (IOException e1) { // Empty! } } Log.d(TAG, "getCategories: " + (System.currentTimeMillis() - time) + "ms"); return ret; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * get current feeds from server/*from w ww . ja va 2s . c o m*/ * * @param tolerateWrongUnreadInformation if set to {@code false}, then * lazy server will be updated before * @return set of actual feeds on server */ private Set<Feed> getFeeds(boolean tolerateWrongUnreadInformation) { long time = System.currentTimeMillis(); Set<Feed> ret = new LinkedHashSet<>(); if (sessionNotAlive()) return ret; if (!tolerateWrongUnreadInformation) { makeLazyServerWork(); } Map<String, String> params = new HashMap<>(); params.put(PARAM_OP, VALUE_GET_FEEDS); params.put(PARAM_CAT_ID, Data.VCAT_ALL + ""); // Hardcoded -4 fetches all feeds. See // http://tt-rss.org/redmine/wiki/tt-rss/JsonApiReference#getFeeds JsonReader reader = null; try { reader = prepareReader(params); if (reader == null) return ret; reader.beginArray(); while (reader.hasNext()) { int categoryId = -1; int id = 0; String title = null; String feedUrl = null; int unread = 0; reader.beginObject(); while (reader.hasNext()) { try { switch (reader.nextName()) { case ID: id = reader.nextInt(); break; case CAT_ID: categoryId = reader.nextInt(); break; case TITLE: title = reader.nextString(); break; case FEED_URL: feedUrl = reader.nextString(); break; case UNREAD: unread = reader.nextInt(); break; default: reader.skipValue(); break; } } catch (IllegalArgumentException e) { e.printStackTrace(); reader.skipValue(); } } reader.endObject(); if (id != -1 || categoryId == -2) // normal feed (>0) or label (-2) if (title != null) // Dont like complicated if-statements.. ret.add(new Feed(id, categoryId, title, feedUrl, unread)); } reader.endArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) try { reader.close(); } catch (IOException e1) { // Empty! } } Log.d(TAG, "getFeeds: " + (System.currentTimeMillis() - time) + "ms"); return ret; }
From source file:persistance.JSONHandler.java
private static Bee parseUnspecifiedBee(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject();/* w w w . j av a 2 s.c o m*/ while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Random random = new Random(); if (random.nextInt(50) == 0) { return new Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); } return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Male parseDrone(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject();//from w w w. j a v a 2 s . c o m while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); return new Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Female parsePrincess(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject();//from w w w . j a v a 2s .c om while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Queen parseQueen(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; Female princess = null;//from w w w. j a v a 2 s.c om Male drone = null; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "drone": drone = parseDrone(jsonReader); break; case "princess": princess = parsePrincess(jsonReader); break; case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Queen newQueen = new Queen(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); newQueen.setPrincess(princess); newQueen.setDrone(drone); return newQueen; }
From source file:persistance.JSONHandler.java
private static Colony parseColony(JsonReader jsonReader, ArrayList<Colony> colonies) throws IOException { int year = 0; Queen queen = null;/*from ww w . ja v a2 s . c o m*/ ArrayList<Bee> bees = new ArrayList<>(); Colony parentColony = null; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "year": year = jsonReader.nextInt(); break; case "queen": queen = parseQueen(jsonReader); break; case "parentColony": parentColony = parseColony(jsonReader, colonies); for (Colony colony : colonies) { if (colony.equals(parentColony)) { parentColony = colony; } } break; case "bees": jsonReader.beginArray(); while (jsonReader.hasNext()) { bees.add(parseUnspecifiedBee(jsonReader)); } jsonReader.endArray(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Colony colony = new Colony(year, queen, bees); if (parentColony != null) { colony.setParentColony(parentColony); parentColony.addChildColony(colony); } return colony; }
From source file:tools.DrawStatisticsForPubMedData.java
License:Apache License
public void parseStream(String jsonFile, String listOfJournals) throws IOException { String journalName;/*www . j ava2 s.c om*/ int count = 0; int abstract_count = 0; int duplicates = 0; try { JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile))); reader.setLenient(true); reader.beginObject(); reader.skipValue(); //System.out.println(nam); reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); this.numeOfArticles++; while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("abstractText")) { abstract_count++; reader.skipValue(); } else if (name.equals("journal")) { journalName = reader.nextString(); journalList.add(journalName); } else if (name.equals("meshMajor")) { int num_labels = readLabelsArray(reader); count += num_labels; labelDensity += (double) num_labels / 26563.0; } else if (name.equals("pmid")) { int pmid = reader.nextInt(); if (!pmids.contains(pmid)) pmids.add(pmid); else duplicates++; } else if (name.equals("title")) { reader.skipValue(); } else if (name.equals("year")) { reader.skipValue(); } else { System.out.println(name); reader.skipValue(); } } reader.endObject(); } reader.endArray(); System.out.println("Abstracts: " + abstract_count); System.out.println("Duplicates: " + duplicates); labelsPerArticle = (double) count / (double) numeOfArticles; labelDensity = labelDensity / (double) numeOfArticles; exportListOfJournals(listOfJournals); printStatistics(); } catch (Exception ex) { System.out.println("Abstracts: " + abstract_count); System.out.println("Duplicates: " + duplicates); labelsPerArticle = (double) count / (double) numeOfArticles; labelDensity = labelDensity / (double) numeOfArticles; exportListOfJournals(listOfJournals); printStatistics(); Logger.getLogger(DrawStatisticsForPubMedData.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:uk.ac.susx.tag.dependencyparser.datastructures.StringIndexer.java
License:Apache License
public static StringIndexer readJson(JsonReader reader) throws IOException { int idStart = 1; List<String> strings = new ArrayList<>(); reader.beginObject();// w w w .jav a2 s .co m while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "idStart": idStart = reader.nextInt(); break; case "strings": reader.beginArray(); while (reader.hasNext()) { strings.add(reader.nextString()); } reader.endArray(); } } reader.endObject(); return new StringIndexer(idStart, strings); }