List of usage examples for com.google.gson.stream JsonReader endArray
public void endArray() throws IOException
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * parse articles from JSON-reader/*from w w w . j a va 2 s. co m*/ * * @param articles container, where parsed articles will be stored * @param reader JSON-reader, containing articles (received from server) * @param skipNames set of names (article properties), which should not be processed (may be {@code null}) * @param filter filter for articles, defining which articles should be omitted while parsing (may be {@code * null}) * @return amount of processed articles */ private int parseArticleArray(final Set<Article> articles, JsonReader reader, Set<Article.ArticleField> skipNames, IArticleOmitter filter) { long time = System.currentTimeMillis(); int count = 0; try { reader.beginArray(); while (reader.hasNext()) { Article article = new Article(); boolean skipObject = false; reader.beginObject(); skipObject = parseArticle(article, reader, skipNames, filter); reader.endObject(); if (!skipObject && article.id != -1 && article.title != null) articles.add(article); count++; } reader.endArray(); } catch (OutOfMemoryError e) { Controller.getInstance().lowMemory(true); // Low memory detected } catch (Exception e) { Log.e(TAG, "Input data could not be read: " + e.getMessage() + " (" + e.getCause() + ")", e); } Log.d(TAG, String.format("parseArticleArray: parsing %s articles took %s ms", count, (System.currentTimeMillis() - time))); return count; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private Set<Label> parseLabels(final JsonReader reader) throws IOException { Set<Label> ret = new HashSet<>(); if (reader.peek().equals(JsonToken.BEGIN_ARRAY)) { reader.beginArray();//from w w w . ja v a 2 s .c o m } else { reader.skipValue(); return ret; } try { while (reader.hasNext()) { Label label = new Label(); reader.beginArray(); try { label.id = Integer.parseInt(reader.nextString()); label.caption = reader.nextString(); label.foregroundColor = reader.nextString(); label.backgroundColor = reader.nextString(); label.checked = true; } catch (IllegalArgumentException e) { e.printStackTrace(); reader.skipValue(); continue; } ret.add(label); reader.endArray(); } reader.endArray(); } catch (Exception e) { // Ignore exceptions here try { if (reader.peek().equals(JsonToken.END_ARRAY)) reader.endArray(); } catch (Exception ee) { // Empty! } } return ret; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * Retrieves all categories./*from w ww . jav a 2 s.c o 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/* w w w.j a v a2s . 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:org.wso2.carbon.governance.rest.api.internal.JSONMessageBodyReader.java
License:Open Source License
/** * Traverse through a json array and maps the array elements to a {@link Map} * * @param reader {@link JsonReader} * @return map with json array values mapped to key value pairs. * @throws IOException If unable to parse the json array. *//* w w w . j ava 2 s.co m*/ private Map<String, Object> handleArray(JsonReader reader) throws IOException { Map<String, Object> values = new HashMap<>(); reader.beginArray(); JsonToken token = reader.peek(); while (token != null) { if (token.equals(JsonToken.END_ARRAY)) { reader.endArray(); return values; } else { handleObject(reader, values, token, true); } token = reader.peek(); } return values; }
From source file:persistance.JSONHandler.java
public static ArrayList<Colony> loadColonies(String filePath) throws IOException { ArrayList<Colony> colonies = new ArrayList<>(); try (Reader reader = new InputStreamReader(new FileInputStream(filePath))) { Gson gson = new GsonBuilder().create(); JsonReader jsonReader = gson.newJsonReader(reader); jsonReader.beginArray();// ww w . j a v a 2 s . c om while (jsonReader.hasNext()) { colonies.add(parseColony(jsonReader, colonies)); } jsonReader.endArray(); return colonies; } }
From source file:persistance.JSONHandler.java
private static Colony parseColony(JsonReader jsonReader, ArrayList<Colony> colonies) throws IOException { int year = 0; Queen queen = null;// w ww . j a v a2s .c om 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:pl.nask.hsn2.task.SignatureProcessor.java
License:Open Source License
public final void process(InputStream stream) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(stream)); reader.beginObject();/*w ww . j a v a2s. co m*/ while (reader.hasNext()) { String name = reader.nextName(); if ("signatures".equals(name)) { reader.beginArray(); while (reader.hasNext()) { Signature<Map<String, Object>> signature = new Gson().fromJson(reader, Signature.class); extractData(signature); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); }
From source file:project.latex.balloon.BalloonController.java
static List<String> loadTransmittedDataKeys(String filePath) throws IOException { if (filePath == null) { throw new IllegalArgumentException("Cannot load keys from null file"); }//from w w w . jav a 2 s . c om JsonReader reader = null; try { List<String> dataKeys = new ArrayList<>(); reader = new JsonReader(new FileReader(filePath)); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); reader.beginArray(); while (reader.hasNext()) { dataKeys.add(reader.nextString()); } reader.endArray(); } reader.endObject(); reader.close(); return dataKeys; } finally { if (reader != null) { reader.close(); } } }
From source file:project.latex.balloon.TransmittedDataKeysResource.java
final void loadTransmittedDataKeys(String filePath) throws IOException { if (filePath == null) { throw new IllegalArgumentException("Cannot load keys from null file"); }//w w w. ja v a2 s . com JsonReader reader = null; try { List<String> dataKeys = new ArrayList<>(); reader = new JsonReader(new FileReader(filePath)); reader.beginObject(); while (reader.hasNext()) { reader.nextName(); reader.beginArray(); while (reader.hasNext()) { dataKeys.add(reader.nextString()); } reader.endArray(); } reader.endObject(); reader.close(); this.transmittedDataKeys = dataKeys; } finally { if (reader != null) { reader.close(); } } }