List of usage examples for com.google.gson.stream JsonToken BEGIN_OBJECT
JsonToken BEGIN_OBJECT
To view the source code for com.google.gson.stream JsonToken BEGIN_OBJECT.
Click Source Link
From source file:org.lanternpowered.server.data.persistence.json.JsonDataFormat.java
License:MIT License
private static void readView(JsonReader reader, DataView view) throws IOException { reader.beginObject();//from www .java 2 s.c o m while (reader.hasNext()) { DataQuery key = DataQuery.of(reader.nextName()); if (reader.peek() == JsonToken.BEGIN_OBJECT) { // Check this early so we don't need to copy the view readView(reader, view.createView(key)); } else { view.set(key, read0(reader)); } } reader.endObject(); }
From source file:org.mythdroid.services.GuideService.java
License:Open Source License
/** * Get ProgramGuide data/*from www . jav a2 s. c o m*/ * @param start Date for start of data * @param end Date for end of data * @return ArrayList of Channels */ public ArrayList<Channel> GetProgramGuide(Date start, Date end) throws IOException { final Params params = new Params(); params.put("StartTime", Globals.utcFormat(start)); //$NON-NLS-1$ params.put("EndTime", Globals.utcFormat(end)); //$NON-NLS-1$ params.put("StartChanId", 0); //$NON-NLS-1$ params.put("NumChannels", -1); //$NON-NLS-1$ params.put("Details", "true"); //$NON-NLS-1$ //$NON-NLS-2$ InputStream is = jc.GetStream("GetProgramGuide", params); //$NON-NLS-1$ JsonReader jreader = new JsonReader(new BufferedReader(new InputStreamReader(is, "UTF-8")) //$NON-NLS-1$ ); ArrayList<Channel> channels = new ArrayList<Channel>(); jreader.beginObject(); skipTo(jreader, JsonToken.BEGIN_OBJECT); jreader.beginObject(); skipTo(jreader, JsonToken.NAME); while (jreader.hasNext()) { String name = jreader.nextName(); if (name.equals("NumOfChannels")) { //$NON-NLS-1$ channels.ensureCapacity(jreader.nextInt()); break; } jreader.skipValue(); } skipTo(jreader, JsonToken.BEGIN_ARRAY); jreader.beginArray(); while (jreader.hasNext()) { jreader.beginObject(); channels.add((Channel) gson.fromJson(jreader, Channel.class)); jreader.endObject(); } jreader.endArray(); jreader.endObject(); jreader.endObject(); jreader.close(); jc.endStream(); return channels; }
From source file:org.mythdroid.services.VideoService.java
License:Open Source License
/** * Get a list of videos / directories in a given subdirectory * @param subdir the desired subdirectory or "ROOT" for the top-level * @return an ArrayList of Videos/* w w w . j a v a 2 s .co m*/ */ public ArrayList<Video> getVideos(String subdir) throws IOException { ArrayList<Video> videos = new ArrayList<Video>(128); InputStream is = jc.GetStream("GetVideoList", null); //$NON-NLS-1$ if (is == null) return null; JsonReader jreader = new JsonReader(new BufferedReader(new InputStreamReader(is, "UTF-8")) //$NON-NLS-1$ ); Video vid; final ArrayList<String> subdirs = new ArrayList<String>(16); jreader.beginObject(); skipTo(jreader, JsonToken.BEGIN_OBJECT); jreader.beginObject(); skipTo(jreader, JsonToken.BEGIN_ARRAY); jreader.beginArray(); while (jreader.hasNext()) { jreader.beginObject(); vid = gson.fromJson(jreader, Video.class); jreader.endObject(); if (!subdir.equals("ROOT") && !vid.filename.startsWith(subdir)) //$NON-NLS-1$ continue; String name = vid.filename; if (!subdir.equals("ROOT")) //$NON-NLS-1$ name = vid.filename.substring(subdir.length() + 1); int slash; if ((slash = name.indexOf('/')) > 0) { String dir = name.substring(0, slash); if (!subdirs.contains(dir)) subdirs.add(dir); } else videos.add(vid); } jreader.endArray(); jreader.endObject(); jreader.endObject(); jreader.close(); jc.endStream(); for (String name : subdirs) { try { videos.add(new Video("-1 DIRECTORY " + name)); //$NON-NLS-1$ } catch (IllegalArgumentException e) { ErrUtil.logWarn(e); } } videos.trimToSize(); return videos; }
From source file:org.translator.java.AppInventorProperties.java
License:Apache License
protected AppInventorProperties(JsonReader reader) throws IOException { reader.beginObject();// ww w. ja v a 2 s .c om while (reader.peek() == JsonToken.NAME) { String name = reader.nextName(); if (name.equals("$Components")) { reader.beginArray(); while (reader.peek() == JsonToken.BEGIN_OBJECT) components.add(new AppInventorProperties(reader)); reader.endArray(); } else { String n = reader.nextString(); if (n.matches("True|False")) n = n.toLowerCase(); properties.put(name, n); } } reader.endObject(); }
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;/*from ww w . ja v a 2s . c om*/ String ret = ""; try { 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 {/* w w w . jav a2 s .c om*/ 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.wso2.carbon.governance.rest.api.internal.JSONMessageBodyReader.java
License:Open Source License
/** * Traverses through a json object and maps the keys and values to a {@link Map} * * @param reader {@link JsonReader} * @param map map that the values to be added. * @param token {@link JsonToken} * @param isArray whether the object is inside a json array * @throws IOException If unable to parse the json object *//*ww w.j a va2s . com*/ protected void handleObject(JsonReader reader, Map<String, Object> map, JsonToken token, boolean isArray) throws IOException { String key = null; while (true) { if (token == null) { token = reader.peek(); } if (JsonToken.BEGIN_OBJECT.equals(token)) { reader.beginObject(); } else if (JsonToken.END_OBJECT.equals(token)) { reader.endObject(); } else if (JsonToken.NAME.equals(token)) { key = reader.nextName(); } else if (JsonToken.STRING.equals(token)) { String value = reader.nextString(); handleValue(key, value, map, isArray); key = null; } else if (JsonToken.NUMBER.equals(token)) { Double value = reader.nextDouble(); handleValue(key, value, map, isArray); key = null; } else if (token.equals(JsonToken.BEGIN_ARRAY)) { Map<String, Object> values = handleArray(reader); if (key != null) { map.put(key, values); } } else { reader.skipValue(); } if (reader.hasNext()) { token = reader.peek(); } else { break; } } }
From source file:pl.softech.gw.json.TaskGsonTypeAdapter.java
License:Apache License
@Override public ITask read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();/* w w w. j a va 2 s. c om*/ return null; } reader.beginObject(); TaskInstanceCreator instanceCreator = new TaskInstanceCreator(); while (reader.peek() != JsonToken.END_OBJECT) { String name = reader.nextName(); Object value; if (reader.peek() == JsonToken.BEGIN_OBJECT && !name.equals("module")) { value = read(reader); } else { if (name.equals("module")) { value = gson.getAdapter(ProjectModule.class).read(reader); } else { value = reader.nextString(); } } if (name.equals("class")) { instanceCreator.className = value.toString(); } else { instanceCreator.params.put(name, new Param(name, value)); } } reader.endObject(); return instanceCreator.create(); }
From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public <T> int readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts) throws IOException, JSONException, Exception { String n = null;// w ww . j ava 2 s .c om int i = 0; while (reader.hasNext()) { JsonToken peek = reader.peek(); String v = null; if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (peek == JsonToken.NAME) { n = reader.nextName(); } else if (peek == JsonToken.BEGIN_ARRAY) { if (n.equals(tableName)) { i = readJsnArr(reader, tableName, clazz); } else { if (n.equals("params")) { reader.beginArray(); if (reader.hasNext()) { reader.beginObject(); if (reader.hasNext()) { n = reader.nextName(); v = reader.nextString(); } reader.endObject(); } reader.endArray(); } else { reader.skipValue(); } } } else if (peek == JsonToken.END_OBJECT) { reader.endObject(); } else if (peek == JsonToken.END_ARRAY) { reader.endArray(); } } return i; }
From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public <T> int readJsnArr(JsonReader reader, String tableName, Class<T> clazz) throws IOException, JSONException, Exception { JSONObject o = new JSONObject(); JsonToken peek = reader.peek();/*from ww w.j ava2s . c o m*/ String n = null; reader.beginArray(); int j = 0; int i = 0; while (reader.hasNext()) { peek = reader.peek(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (reader.peek() == JsonToken.END_OBJECT) { reader.endObject(); } o = new JSONObject(); while (reader.hasNext()) { peek = reader.peek(); if (peek == JsonToken.NAME) { n = reader.nextName(); } else if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (peek == JsonToken.END_OBJECT) { reader.endObject(); } else if (peek == JsonToken.BOOLEAN) { try { o.put(n, reader.nextBoolean()); } catch (JSONException e) { e.printStackTrace(); } } else if (peek == JsonToken.STRING) { try { o.put(n, reader.nextString()); } catch (JSONException e) { e.printStackTrace(); } } else if (peek == JsonToken.NUMBER) { try { o.put(n, reader.nextDouble()); } catch (JSONException e) { e.printStackTrace(); } } } reader.endObject(); if (o.has("key")) { i = i + 1; j = j + 1; if (j % 100 == 0) { j = 2; } saveEntityFromJson(o, tableName, clazz, i); if (i % 10 == 0) { flowzrSyncActivity.notifyUser( flowzrSyncActivity.getString(R.string.flowzr_sync_receiving) + " " + tableName + ". " + flowzrSyncActivity.getString(R.string.hint_run_background), (int) (Math.round(j))); } } } reader.endArray(); return i; }