List of usage examples for com.google.gson.stream JsonReader endArray
public void endArray() throws IOException
From source file:com.battlelancer.seriesguide.dataliberation.JsonImportTask.java
License:Apache License
private int importLists(File importPath) { File backupLists = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_LISTS); if (!backupLists.exists() || !backupLists.canRead()) { // Skip lists if the file is not accessible return SUCCESS; }//from w w w.j a v a2s . c o m // Access JSON from backup folder to create new database try { InputStream in = new FileInputStream(backupLists); Gson gson = new Gson(); JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginArray(); while (reader.hasNext()) { List list = gson.fromJson(reader, List.class); addListToDatabase(list); } reader.endArray(); reader.close(); } catch (JsonParseException | IOException | IllegalStateException e) { // the given Json might not be valid or unreadable Timber.e(e, "JSON lists import failed"); return ERROR; } return SUCCESS; }
From source file:com.battlelancer.seriesguide.dataliberation.JsonImportTask.java
License:Apache License
private int importMovies(File importPath) { context.getContentResolver().delete(Movies.CONTENT_URI, null, null); File backupMovies = new File(importPath, JsonExportTask.EXPORT_JSON_FILE_MOVIES); if (!backupMovies.exists() || !backupMovies.canRead()) { // Skip movies if the file is not available return SUCCESS; }/*w w w . ja v a 2 s . c o m*/ // Access JSON from backup folder to create new database try { InputStream in = new FileInputStream(backupMovies); Gson gson = new Gson(); JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginArray(); while (reader.hasNext()) { Movie movie = gson.fromJson(reader, Movie.class); addMovieToDatabase(movie); } reader.endArray(); reader.close(); } catch (JsonParseException | IOException | IllegalStateException e) { // the given Json might not be valid or unreadable Timber.e(e, "JSON movies import failed"); return ERROR; } return SUCCESS; }
From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java
License:Open Source License
private MeetingPayload extractPayload(JsonReader in) throws IOException, NumberFormatException, IllegalStateException, JsonParseException { NginxClojureRT.log.debug(TAG + "TypeAdapter extracting Payload..."); MeetingPayload meetingPayload = new MeetingPayload(); if (in.peek() == JsonToken.NULL) { in.nextNull();/*from w ww.ja v a2 s .c o m*/ throw new JsonParseException("null Payload"); } in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "aps": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "badge": meetingPayload.getAps().setBadge(in.nextLong()); break; case "sound": meetingPayload.getAps().setSound(in.nextString()); break; case "alert": in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case "title": meetingPayload.getAps().getAlert().setTitle(in.nextString()); break; case "body": meetingPayload.getAps().getAlert().setBody(in.nextString()); break; case "action-loc-key": meetingPayload.getAps().getAlert().setActionLocKey(in.nextString()); break; } } in.endObject(); break; } } in.endObject(); break; case "dapi": meetingPayload.setDapi(in.nextString()); break; case "acme1": meetingPayload.setAcme1(in.nextString()); break; case "acme2": meetingPayload.setAcme2(in.nextLong()); break; case "acme3": meetingPayload.setAcme3(in.nextLong()); break; case "acme4": NginxClojureRT.log.info(TAG + "TypeAdapter Reader is reading acme4..."); meetingPayload.setAcme4(in.nextLong()); break; case "acme5": meetingPayload.setAcme5(in.nextLong()); break; case "acme6": meetingPayload.setAcme6(in.nextLong()); break; case "acme7": ArrayList<String> attendees = new ArrayList<>(); in.beginArray(); while (in.hasNext()) { attendees.add(in.nextString()); } in.endArray(); meetingPayload.setAcme7(attendees); break; case "acme8": meetingPayload.setAcme8(in.nextString()); break; } } in.endObject(); return meetingPayload; }
From source file:com.cinchapi.concourse.util.Convert.java
License:Apache License
/** * Convert the next JSON object in the {@code reader} to a mapping that * associates each key with the Java objects that represent the * corresponding values./* w w w . j a va2 s . co m*/ * * <p> * This method has the same rules and limitations as * {@link #jsonToJava(String)}. It simply uses a {@link JsonReader} to * handle reading an array of objects. * </p> * <p> * <strong>This method DOES NOT {@link JsonReader#close()} the * {@code reader}.</strong> * </p> * * @param reader the {@link JsonReader} that contains a stream of JSON * @return the JSON data in the form of a {@link Multimap} from keys to * values */ private static Multimap<String, Object> jsonToJava(JsonReader reader) { Multimap<String, Object> data = HashMultimap.create(); try { reader.beginObject(); JsonToken peek0; while ((peek0 = reader.peek()) != JsonToken.END_OBJECT) { String key = reader.nextName(); peek0 = reader.peek(); if (peek0 == JsonToken.BEGIN_ARRAY) { // If we have an array, add the elements individually. If // there are any duplicates in the array, they will be // filtered out by virtue of the fact that a HashMultimap // does not store dupes. reader.beginArray(); JsonToken peek = reader.peek(); do { Object value; if (peek == JsonToken.BOOLEAN) { value = reader.nextBoolean(); } else if (peek == JsonToken.NUMBER) { value = stringToJava(reader.nextString()); } else if (peek == JsonToken.STRING) { String orig = reader.nextString(); value = stringToJava(orig); if (orig.isEmpty()) { value = orig; } // If the token looks like a string, it MUST be // converted to a Java string unless it is a // masquerading double or an instance of Thrift // translatable class that has a special string // representation (i.e. Tag, Link) else if (orig.charAt(orig.length() - 1) != 'D' && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) { value = value.toString(); } } else if (peek == JsonToken.NULL) { reader.skipValue(); continue; } else { throw new JsonParseException("Cannot parse nested object or array within an array"); } data.put(key, value); } while ((peek = reader.peek()) != JsonToken.END_ARRAY); reader.endArray(); } else { Object value; if (peek0 == JsonToken.BOOLEAN) { value = reader.nextBoolean(); } else if (peek0 == JsonToken.NUMBER) { value = stringToJava(reader.nextString()); } else if (peek0 == JsonToken.STRING) { String orig = reader.nextString(); value = stringToJava(orig); if (orig.isEmpty()) { value = orig; } // If the token looks like a string, it MUST be // converted to a Java string unless it is a // masquerading double or an instance of Thrift // translatable class that has a special string // representation (i.e. Tag, Link) else if (orig.charAt(orig.length() - 1) != 'D' && !CLASSES_WITH_ENCODED_STRING_REPR.contains(value.getClass())) { value = value.toString(); } } else if (peek0 == JsonToken.NULL) { reader.skipValue(); continue; } else { throw new JsonParseException("Cannot parse nested object to value"); } data.put(key, value); } } reader.endObject(); return data; } catch (IOException | IllegalStateException e) { throw new JsonParseException(e.getMessage()); } }
From source file:com.confighub.core.utils.Utils.java
License:Open Source License
private static void skipToken(final JsonReader reader) throws IOException { final JsonToken token = reader.peek(); switch (token) { case BEGIN_ARRAY: reader.beginArray();//from w ww . j a v a 2s .c o m break; case END_ARRAY: reader.endArray(); break; case BEGIN_OBJECT: reader.beginObject(); break; case END_OBJECT: reader.endObject(); break; case NAME: reader.nextName(); break; case STRING: case NUMBER: case BOOLEAN: case NULL: reader.skipValue(); break; case END_DOCUMENT: default: throw new AssertionError(token); } }
From source file:com.dabay6.libraries.androidshared.ui.dialogs.changelog.util.ChangeLogDialogUtils.java
License:Open Source License
/** * Builds the html using the change log json asset file. * * @param context {@link Context} used to retrieve resources. * @param assetName Name of the asset file containing the change log json. * @param style The css style to be applied to the html. * * @return A {@link String} containing html. *///w ww .ja v a 2 s.c o m private static String getHtmlChangeLog(final Context context, final String assetName, final String style) throws IOException { final JsonReader reader; final Gson gson = new Gson(); final List<Release> releases = ListUtils.newList(); String html = null; try { reader = new JsonReader(new InputStreamReader(AssetUtils.open(context, assetName), "UTF-8")); reader.beginArray(); while (reader.hasNext()) { final Release release = gson.fromJson(reader, Release.class); releases.add(release); } reader.endArray(); reader.close(); if (releases.size() > 0) { html = String.format("<html><head>%s</head><body>", style); html += parseReleases(context, releases); html += "</body></html>"; } } catch (final Exception ex) { html = null; Logger.error(TAG, ex.getMessage(), ex); } return html; }
From source file:com.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java
License:Apache License
private List<?> readList(JsonReader in) throws IOException { List<Object> result = new ArrayList<Object>(); in.beginArray();//from www .j av a 2 s . c o m while (in.peek() != JsonToken.END_ARRAY) { final Object element; if (in.peek() == JsonToken.BEGIN_OBJECT) { element = gson.getAdapter(MailChimpObject.class).read(in); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { element = readList(in); } else { element = gson.getAdapter(Object.class).read(in); } result.add(element); } in.endArray(); return result; }
From source file:com.facebook.buck.worker.WorkerProcessProtocolZero.java
License:Apache License
private static void receiveHandshake(JsonReader reader, int messageId, Optional<Path> stdErr) throws IOException { int id = -1;/*ww w . ja v a 2s .c o m*/ String type = ""; String protocolVersion = ""; try { reader.beginArray(); reader.beginObject(); while (reader.hasNext()) { String property = reader.nextName(); if (property.equals("id")) { id = reader.nextInt(); } else if (property.equals("type")) { type = reader.nextString(); } else if (property.equals("protocol_version")) { protocolVersion = reader.nextString(); } else if (property.equals("capabilities")) { try { reader.beginArray(); reader.endArray(); } catch (IllegalStateException e) { throw new HumanReadableException( "Expected handshake response's \"capabilities\" to " + "be an empty array."); } } else { reader.skipValue(); } } reader.endObject(); } catch (IOException e) { throw new HumanReadableException(e, "Error receiving handshake response from external process.\n" + "Stderr from external process:\n%s", getStdErrorOutput(stdErr)); } if (id != messageId) { throw new HumanReadableException(String.format( "Expected handshake response's \"id\" value " + "to be \"%d\", got \"%d\" instead.", messageId, id)); } if (!type.equals(TYPE_HANDSHAKE)) { throw new HumanReadableException( String.format("Expected handshake response's \"type\" " + "to be \"%s\", got \"%s\" instead.", TYPE_HANDSHAKE, type)); } if (!protocolVersion.equals(PROTOCOL_VERSION)) { throw new HumanReadableException(String.format( "Expected handshake response's " + "\"protocol_version\" to be \"%s\", got \"%s\" instead.", PROTOCOL_VERSION, protocolVersion)); } }
From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public static <T> int readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts) throws IOException, JSONException, Exception { String n = null;/*from w w w . j ava2s . 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(); } else if (peek == JsonToken.STRING) { reader.skipValue(); } else { reader.skipValue(); } } return i; }
From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public static <T> int readJsnArr(JsonReader reader, String tableName, Class<T> clazz) throws IOException, JSONException, Exception { JSONObject o = new JSONObject(); JsonToken peek = reader.peek();// w ww. j av a 2s.co 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) { //notifyUser(context.getString(R.string.flowzr_sync_receiving) + " " + tableName + ". " + context.getString(R.string.hint_run_background), (int)(Math.round(j))); } } } reader.endArray(); return i; }