List of usage examples for com.google.gson.stream JsonReader hasNext
public boolean hasNext() throws IOException
From source file:org.spongepowered.plugin.meta.gson.ModMetadataCollectionAdapter.java
License:MIT License
@Override public List<PluginMetadata> read(JsonReader in) throws IOException { in.beginArray();/* w ww . j av a 2s . c om*/ ImmutableList.Builder<PluginMetadata> result = ImmutableList.builder(); while (in.hasNext()) { result.add(this.metadataAdapter.read(in)); } in.endArray(); return result.build(); }
From source file:org.sprintapi.hyperdata.gson.HyperDataTypeAdapter.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override//from ww w. j av a 2 s .co m public Object read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } Object instance = constructor.construct(); BoundField metadataField = null; if (metadataAccess != null) { metadataField = boundFields.get(metadataAccess.fieldName); } Object meta = null; Map<String, BoundField> metaBoundFields = null; try { in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if ((name != null) && (metadataField != null) && (name.startsWith(Constants.META_CHAR))) { if (meta == null) { meta = constructorConstructor.get(metadataField.type).construct(); if (!Map.class.isAssignableFrom(meta.getClass())) { metaBoundFields = reflectiveFactory.getBoundFields(gson, metadataField.type, metadataField.type.getRawType()); } } if (metaBoundFields != null) { BoundField field = metaBoundFields.get(name.substring(1)); if (field == null || !field.deserialized) { in.skipValue(); } else { field.read(in, meta); } } else { TypeAdapter<Object> ta = gson.getAdapter(Object.class); Object value = ta.read(in); ((Map) meta).put(name.substring(1), value); } } else if ((name != null) && (!name.equals(metadataAccess.fieldName))) { BoundField field = boundFields.get(name); if (field == null || !field.deserialized) { in.skipValue(); } else { field.read(in, instance); } } } if (metadataAccess.setter != null) { metadataAccess.setter.invoke(instance, meta); //TODO } } catch (IllegalStateException e) { throw new JsonSyntaxException(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (IllegalArgumentException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } in.endObject(); return instance; }
From source file:org.terasology.rendering.gui.components.UITextWrap.java
License:Apache License
public void showFromJson() throws IOException { int maxlines = getLineCount(); int screenlines = getScreenLines(); long beginpos, endpos, counter; if (screenlines > maxlines) { beginpos = -1;/*from ww w. jav a2s . co m*/ } else { if (currentpos < 0) { currentpos = 0; } if (currentpos > maxlines - screenlines) { currentpos = maxlines - screenlines; } beginpos = maxlines - (screenlines + currentpos) - 1; } endpos = beginpos + screenlines + 1; //if(endpos >maxlines){endpos = maxlines +1;} counter = 0; Gson gson = new Gson(); JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\consolelog.json")); reader.beginArray(); _text = ""; while (reader.hasNext()) { if (counter > beginpos && counter < endpos) { _text += gson.fromJson(reader, String.class); } else { gson.fromJson(reader, String.class); } counter++; } reader.endArray(); reader.close(); }
From source file:org.terasology.rendering.gui.components.UITextWrap.java
License:Apache License
public void loadHelp() throws IOException { Gson gson = new Gson(); JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\help.json")); reader.beginArray();/*w w w. j a v a 2 s . c o m*/ _text = ""; while (reader.hasNext()) { _text += gson.fromJson(reader, String.class) + newLine; } reader.endArray(); reader.close(); }
From source file:org.terasology.rendering.gui.components.UITextWrap.java
License:Apache License
public void loadError() throws IOException { Gson gson = new Gson(); JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\error.json")); reader.beginArray();/*from w w w. j a va 2s . c om*/ _text = ""; while (reader.hasNext()) { _text += gson.fromJson(reader, String.class) + newLine; } reader.endArray(); reader.close(); }
From source file:org.terasology.rendering.gui.components.UITextWrap.java
License:Apache License
public int getLineCount() throws IOException { Gson gson = new Gson(); JsonReader reader = new JsonReader(new FileReader(".\\data\\console\\consolelog.json")); int counter = 0; reader.beginArray();//www .j a va 2s. c o m while (reader.hasNext()) { counter++; gson.fromJson(reader, String.class); } reader.endArray(); reader.close(); return counter; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private String readResult(Map<String, String> params, boolean login, boolean retry) throws IOException { InputStream in = doRequest(params); if (in == null) return null; JsonReader reader = null; String ret = ""; try {//w ww. ja v a 2 s . c o m reader = new JsonReader(new InputStreamReader(in, "UTF-8")); // Check if content contains array or object, array indicates login-response or error, object is content reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (!name.equals("content")) { reader.skipValue(); continue; } JsonToken t = reader.peek(); if (!t.equals(JsonToken.BEGIN_OBJECT)) continue; JsonObject object = new JsonObject(); reader.beginObject(); while (reader.hasNext()) { object.addProperty(reader.nextName(), reader.nextString()); } reader.endObject(); if (object.get(SESSION_ID) != null) { ret = object.get(SESSION_ID).getAsString(); } if (object.get(STATUS) != null) { ret = object.get(STATUS).getAsString(); } if (this.apiLevel == -1 && object.get(API_LEVEL) != null) { this.apiLevel = object.get(API_LEVEL).getAsInt(); } if (object.get(VALUE) != null) { ret = object.get(VALUE).getAsString(); } if (object.get(ERROR) != null) { String message = object.get(ERROR).getAsString(); Context ctx = MyApplication.context(); switch (message) { case API_DISABLED: lastError = ctx.getString(R.string.Error_ApiDisabled, Controller.getInstance().username()); break; case NOT_LOGGED_IN: case LOGIN_ERROR: if (!login && retry && login()) return readResult(params, false, false); // Just do the same request again else lastError = ctx.getString(R.string.Error_LoginFailed); break; case INCORRECT_USAGE: lastError = ctx.getString(R.string.Error_ApiIncorrectUsage); break; case UNKNOWN_METHOD: lastError = ctx.getString(R.string.Error_ApiUnknownMethod); break; default: lastError = ctx.getString(R.string.Error_ApiUnknownError); break; } hasLastError = true; Log.e(TAG, message); return null; } } } finally { if (reader != null) reader.close(); } if (ret.startsWith("\"")) ret = ret.substring(1, ret.length()); if (ret.endsWith("\"")) ret = ret.substring(0, ret.length() - 1); return ret; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private JsonReader prepareReader(Map<String, String> params, boolean firstCall) throws IOException { InputStream in = doRequest(params); if (in == null) return null; JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); // Check if content contains array or object, array indicates login-response or error, object is content try {/*from w w w . j a v a 2s .c o m*/ reader.beginObject(); } catch (Exception e) { e.printStackTrace(); return null; } while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("content")) { JsonToken t = reader.peek(); if (t.equals(JsonToken.BEGIN_ARRAY)) { return reader; } else if (t.equals(JsonToken.BEGIN_OBJECT)) { JsonObject object = new JsonObject(); reader.beginObject(); String nextName = reader.nextName(); // We have a BEGIN_OBJECT here but its just the response to call "subscribeToFeed" if ("status".equals(nextName)) return reader; // Handle error while (reader.hasNext()) { if (nextName != null) { object.addProperty(nextName, reader.nextString()); nextName = null; } else { object.addProperty(reader.nextName(), reader.nextString()); } } reader.endObject(); if (object.get(ERROR) != null) { String message = object.get(ERROR).toString(); if (message.contains(NOT_LOGGED_IN)) { lastError = NOT_LOGGED_IN; if (firstCall && login() && !hasLastError) return prepareReader(params, false); // Just do the same request again else return null; } if (message.contains(API_DISABLED)) { hasLastError = true; lastError = MyApplication.context().getString(R.string.Error_ApiDisabled, Controller.getInstance().username()); return null; } // Any other error hasLastError = true; lastError = message; } } } else { reader.skipValue(); } } return null; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
private Set<String> parseAttachments(JsonReader reader) throws IOException { Set<String> ret = new HashSet<>(); reader.beginArray();/* w w w . j a v a 2s .c o m*/ while (reader.hasNext()) { String attId = null; String attUrl = null; reader.beginObject(); while (reader.hasNext()) { try { switch (reader.nextName()) { case CONTENT_URL: attUrl = reader.nextString(); break; case ID: attId = reader.nextString(); break; default: reader.skipValue(); break; } } catch (IllegalArgumentException e) { e.printStackTrace(); reader.skipValue(); } } reader.endObject(); if (attId != null && attUrl != null) ret.add(attUrl); } reader.endArray(); return ret; }
From source file:org.ttrssreader.net.JSONConnector.java
License:Open Source License
/** * parse articles from JSON-reader//from w ww . j av a2s. 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; }