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:bind.JsonTreeReader.java
License:Apache License
@Override public void beginObject() throws IOException { expect(JsonToken.BEGIN_OBJECT); JsonObject object = ((JsonElement) peekStack()).getAsJsonObject(); stack.add(object.entrySet().iterator()); }
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public JsonToken peek() throws IOException { if (stack.isEmpty()) { return JsonToken.END_DOCUMENT; }//from w w w . j ava2 s . c o m Object o = peekStack(); if (o instanceof Iterator) { Object secondToTop = stack.get(stack.size() - 2); boolean isObject = secondToTop instanceof JsonElement && ((JsonElement) secondToTop).isJsonObject(); Iterator<?> iterator = (Iterator<?>) o; if (iterator.hasNext()) { if (isObject) { return JsonToken.NAME; } else { stack.add(iterator.next()); return peek(); } } else { return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY; } } else if (o instanceof JsonElement) { JsonElement el = (JsonElement) o; if (el.isJsonObject()) { return JsonToken.BEGIN_OBJECT; } else if (el.isJsonArray()) { return JsonToken.BEGIN_ARRAY; } else if (el.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) o; if (primitive.isString()) { return JsonToken.STRING; } else if (primitive.isBoolean()) { return JsonToken.BOOLEAN; } else if (primitive.isNumber()) { return JsonToken.NUMBER; } else { throw new AssertionError(); } } else if (el.isJsonNull()) { return JsonToken.NULL; } throw new AssertionError(); } else if (o == SENTINEL_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } else { throw new AssertionError(); } }
From source file:com.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java
License:Apache License
@Override public MailChimpObject read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();/* w w w . j a va 2 s .c o m*/ return null; } MailChimpObject result; try { result = constructor.newInstance(); } catch (Exception e) { throw new RuntimeException("Failed to invoke " + constructor + " with no args", e); } in.beginObject(); while (in.hasNext()) { final String key; if (in.peek() == JsonToken.NAME) { key = in.nextName(); } else { key = in.nextString(); } final Object value; Type valueType = result.getReflectiveMappingTypes().get(key); if (valueType != null) { value = gson.getAdapter(TypeToken.get(valueType)).read(in); } else { if (in.peek() == JsonToken.BEGIN_OBJECT) { value = gson.getAdapter(MailChimpObject.class).read(in); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { value = readList(in); } else { value = gson.getAdapter(Object.class).read(in); } } if (result.put(key, value) != null) { throw new JsonSyntaxException("duplicate key: " + key); } } in.endObject(); return result; }
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 w w w . j a v a 2 s . c om*/ 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.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 . ja va 2 s. co m*/ 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();/* ww w .j a v a 2 s. 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) { //notifyUser(context.getString(R.string.flowzr_sync_receiving) + " " + tableName + ". " + context.getString(R.string.hint_run_background), (int)(Math.round(j))); } } } reader.endArray(); return i; }
From source file:com.gd.misc.test.JsonReader.java
License:Apache License
/** * Returns the type of the next token without consuming it. *///from w w w . j a v a2 s . co m public JsonToken peek() throws IOException { int p = peeked; if (p == PEEKED_NONE) { p = doPeek(); } switch (p) { case PEEKED_BEGIN_OBJECT: return JsonToken.BEGIN_OBJECT; case PEEKED_END_OBJECT: return JsonToken.END_OBJECT; case PEEKED_BEGIN_ARRAY: return JsonToken.BEGIN_ARRAY; case PEEKED_END_ARRAY: return JsonToken.END_ARRAY; case PEEKED_SINGLE_QUOTED_NAME: case PEEKED_DOUBLE_QUOTED_NAME: case PEEKED_UNQUOTED_NAME: return JsonToken.NAME; case PEEKED_TRUE: case PEEKED_FALSE: return JsonToken.BOOLEAN; case PEEKED_NULL: return JsonToken.NULL; case PEEKED_SINGLE_QUOTED: case PEEKED_DOUBLE_QUOTED: case PEEKED_UNQUOTED: case PEEKED_BUFFERED: return JsonToken.STRING; case PEEKED_LONG: case PEEKED_NUMBER: return JsonToken.NUMBER; case PEEKED_EOF: return JsonToken.END_DOCUMENT; default: throw new AssertionError(); } }
From source file:com.gelakinetic.mtgfam.helpers.JsonParser.java
License:Open Source License
public void readCardJsonStream(InputStream in, CardProgressReporter progReport, String setName, CardDbAdapter mDbHelper, Context context) throws IOException { String dialogText = String.format(context.getString(R.string.update_parse_cards), setName); JsonReader reader = new JsonReader(new InputStreamReader(in, "ISO-8859-1")); String s, s1, s2, ptstr;//w w w . j a va 2 s.co m int numTotalElements = 0; int elementsParsed = 0; reader.beginObject(); s = reader.nextName(); ArrayList<MtgSet> setsAdded = new ArrayList<MtgSet>(); progReport.reportJsonCardProgress("determinate", ""); reader.beginObject(); while (reader.hasNext()) { s = reader.nextName(); if (s.equalsIgnoreCase("v")) { // bdd_date reader.skipValue(); } if (s.equalsIgnoreCase("u")) { // bdd_version reader.skipValue(); } if (s.equalsIgnoreCase("s")) { // sets reader.beginObject(); while (reader.hasNext()) { s1 = reader.nextName(); if (s1.equalsIgnoreCase("b")) { // set MtgSet set; JsonToken jt = reader.peek(); if (jt.equals(JsonToken.BEGIN_OBJECT)) { set = new MtgSet(); reader.beginObject(); while (reader.hasNext()) { s2 = reader.nextName(); if (s2.equalsIgnoreCase("a")) { // name set.name = reader.nextString(); } if (s2.equalsIgnoreCase("r")) { // code_magiccards set.code_magiccards = reader.nextString(); } if (s2.equalsIgnoreCase("q")) { // code set.code = reader.nextString(); } if (s2.equalsIgnoreCase("y")) { // date set.date = reader.nextLong(); } } setsAdded.add(set); reader.endObject(); } else if (jt.equals(JsonToken.BEGIN_ARRAY)) { reader.beginArray(); while (reader.hasNext()) { set = new MtgSet(); reader.beginObject(); while (reader.hasNext()) { s2 = reader.nextName(); if (s2.equalsIgnoreCase("a")) { // name set.name = reader.nextString(); } if (s2.equalsIgnoreCase("r")) { // code_magiccards set.code_magiccards = reader.nextString(); } if (s2.equalsIgnoreCase("q")) { // code set.code = reader.nextString(); } if (s2.equalsIgnoreCase("y")) { // date set.date = reader.nextLong(); } } setsAdded.add(set); reader.endObject(); } reader.endArray(); } } } reader.endObject(); } if (s.equalsIgnoreCase("p")) { // cards reader.beginObject(); while (reader.hasNext()) { s1 = reader.nextName(); if (s1.equalsIgnoreCase("o")) { // card MtgCard c; reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); c = new MtgCard(); while (reader.hasNext()) { s2 = reader.nextName(); if (s2.equalsIgnoreCase("a")) { // name c.name = reader.nextString(); } else if (s2.equalsIgnoreCase("b")) { // set c.set = reader.nextString(); } else if (s2.equalsIgnoreCase("c")) { // type c.type = reader.nextString(); } else if (s2.equalsIgnoreCase("d")) { // rarity c.rarity = reader.nextString().charAt(0); } else if (s2.equalsIgnoreCase("e")) { // manacost c.manacost = reader.nextString(); } else if (s2.equalsIgnoreCase("f")) { // converted_manacost try { c.cmc = reader.nextInt(); } catch (Exception e) { reader.skipValue(); } } else if (s2.equalsIgnoreCase("g")) { // power try { ptstr = reader.nextString(); try { c.power = Integer.parseInt(ptstr); } catch (NumberFormatException e) { if (ptstr.equals("*")) { c.power = CardDbAdapter.STAR; } else if (ptstr.equals("1+*")) { c.power = CardDbAdapter.ONEPLUSSTAR; } else if (ptstr.equals("2+*")) { c.power = CardDbAdapter.TWOPLUSSTAR; } else if (ptstr.equals("7-*")) { c.power = CardDbAdapter.SEVENMINUSSTAR; } else if (ptstr.equals("*{^2}")) { c.power = CardDbAdapter.STARSQUARED; } else if (ptstr.equals("{1/2}")) { c.power = 0.5f; } else if (ptstr.equals("1{1/2}")) { c.power = 1.5f; } else if (ptstr.equals("2{1/2}")) { c.power = 2.5f; } else if (ptstr.equals("3{1/2}")) { c.power = 3.5f; } } } catch (Exception e) { reader.skipValue(); } } else if (s2.equalsIgnoreCase("h")) { // toughness try { ptstr = reader.nextString(); try { c.toughness = Integer.parseInt(ptstr); } catch (NumberFormatException e) { if (ptstr.equals("*")) { c.toughness = CardDbAdapter.STAR; } else if (ptstr.equals("1+*")) { c.toughness = CardDbAdapter.ONEPLUSSTAR; } else if (ptstr.equals("2+*")) { c.toughness = CardDbAdapter.TWOPLUSSTAR; } else if (ptstr.equals("7-*")) { c.toughness = CardDbAdapter.SEVENMINUSSTAR; } else if (ptstr.equals("*{^2}")) { c.toughness = CardDbAdapter.STARSQUARED; } else if (ptstr.equals("{1/2}")) { c.toughness = 0.5f; } else if (ptstr.equals("1{1/2}")) { c.toughness = 1.5f; } else if (ptstr.equals("2{1/2}")) { c.toughness = 2.5f; } else if (ptstr.equals("3{1/2}")) { c.toughness = 3.5f; } } } catch (Exception e) { reader.skipValue(); } } else if (s2.equalsIgnoreCase("i")) { // loyalty try { c.loyalty = reader.nextInt(); } catch (Exception e) { reader.skipValue(); } } else if (s2.equalsIgnoreCase("j")) { // ability c.ability = reader.nextString(); } else if (s2.equalsIgnoreCase("k")) { // flavor c.flavor = reader.nextString(); } else if (s2.equalsIgnoreCase("l")) { // artist c.artist = reader.nextString(); } else if (s2.equalsIgnoreCase("m")) { // number try { c.number = reader.nextString(); } catch (Exception e) { reader.skipValue(); } } else if (s2.equalsIgnoreCase("n")) { // color c.color = reader.nextString(); } else if (s2.equalsIgnoreCase("x")) { // multiverse id c.multiverse_id = reader.nextInt(); } } mDbHelper.createCard(c); elementsParsed++; progReport.reportJsonCardProgress(new String[] { dialogText, dialogText, "" + (int) Math.round(100 * elementsParsed / (double) numTotalElements) }); reader.endObject(); } reader.endArray(); } } reader.endObject(); } if (s.equalsIgnoreCase("w")) { // num_cards numTotalElements = reader.nextInt(); } } reader.endObject(); reader.close(); // Add the set information to the database AFTER adding the cards. // This way if the update fails while parsing cards, the database won't think it has the set already, when it doesnt. for (MtgSet set : setsAdded) { mDbHelper.createSet(set); } return; }
From source file:com.gelakinetic.mtgfam.helpers.updaters.CardAndSetParser.java
License:Open Source License
/** * If a set has a patch, and doesn't exist in the database, this is called to parse an InputStream of JSON and add * it into the database.// w w w .j a va 2 s .c om * <p/> * The JSON uses single character keys, which is a silly thing I did in the name of compression. The patches are * zipped anyway, so it doesn't matter much, but we're stuck with it. * <p/> * There is some special processing for weird power and toughness too * * @param in An InputStream containing valid JSON * @param progressReporter A percentage progress is reported through this class to be shown in the notification * @param cardsToAdd An array list to place cards before adding to the database * @param setsToAdd An array list to place sets before adding to the database * @throws IOException If something goes wrong with the InputStream, this will be thrown */ public void readCardJsonStream(InputStream in, CardProgressReporter progressReporter, ArrayList<MtgCard> cardsToAdd, ArrayList<MtgSet> setsToAdd) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "ISO-8859-1")); /* Three levels of strings for parsing nested JSON */ String s, s1, s2; String pouTouStr; int numTotalElements = 0; int elementsParsed = 0; reader.beginObject(); reader.nextName(); reader.beginObject(); while (reader.hasNext()) { s = reader.nextName(); if (s.equalsIgnoreCase("v")) { /* bdd_date */ reader.skipValue(); } if (s.equalsIgnoreCase("u")) { /* bdd_version */ reader.skipValue(); } if (s.equalsIgnoreCase("s")) { /* sets */ reader.beginObject(); while (reader.hasNext()) { s1 = reader.nextName(); if (s1.equalsIgnoreCase("b")) { /* set */ MtgSet set; JsonToken jt = reader.peek(); if (jt.equals(JsonToken.BEGIN_OBJECT)) { set = new MtgSet(); reader.beginObject(); while (reader.hasNext()) { s2 = reader.nextName(); if (s2.equalsIgnoreCase("a")) { /* name */ set.name = reader.nextString(); } if (s2.equalsIgnoreCase("r")) { /* code_magicCards */ set.codeMagicCards = reader.nextString(); } if (s2.equalsIgnoreCase("q")) { /* code */ set.code = reader.nextString(); } if (s2.equalsIgnoreCase("y")) { /* date */ set.date = reader.nextLong(); } } setsToAdd.add(set); reader.endObject(); } else if (jt.equals(JsonToken.BEGIN_ARRAY)) { reader.beginArray(); while (reader.hasNext()) { set = new MtgSet(); reader.beginObject(); while (reader.hasNext()) { s2 = reader.nextName(); if (s2.equalsIgnoreCase("a")) { /* name */ set.name = reader.nextString(); } if (s2.equalsIgnoreCase("r")) { /* code_magicCards */ set.codeMagicCards = reader.nextString(); } if (s2.equalsIgnoreCase("q")) { /* code */ set.code = reader.nextString(); } if (s2.equalsIgnoreCase("y")) { /* date */ set.date = reader.nextLong(); } } setsToAdd.add(set); reader.endObject(); } reader.endArray(); } } } reader.endObject(); } if (s.equalsIgnoreCase("p")) { /* cards */ reader.beginObject(); while (reader.hasNext()) { s1 = reader.nextName(); if (s1.equalsIgnoreCase("o")) { /* card */ MtgCard c; reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); c = new MtgCard(); while (reader.hasNext()) { s2 = reader.nextName(); if (s2.equalsIgnoreCase("a")) { /* name */ c.name = reader.nextString(); } else if (s2.equalsIgnoreCase("b")) { /* set */ c.set = reader.nextString(); } else if (s2.equalsIgnoreCase("c")) { /* type */ c.type = reader.nextString(); } else if (s2.equalsIgnoreCase("d")) { /* rarity */ c.rarity = reader.nextString().charAt(0); } else if (s2.equalsIgnoreCase("e")) { /* manaCost */ c.manaCost = reader.nextString(); } else if (s2.equalsIgnoreCase("f")) { /* converted_manaCost */ try { c.cmc = reader.nextInt(); } catch (NumberFormatException e) { reader.skipValue(); } } else if (s2.equalsIgnoreCase("g")) { /* power */ pouTouStr = reader.nextString(); try { c.power = Integer.parseInt(pouTouStr); } catch (NumberFormatException e) { switch (pouTouStr) { case "*": c.power = CardDbAdapter.STAR; break; case "1+*": c.power = CardDbAdapter.ONE_PLUS_STAR; break; case "2+*": c.power = CardDbAdapter.TWO_PLUS_STAR; break; case "7-*": c.power = CardDbAdapter.SEVEN_MINUS_STAR; break; case "*{^2}": c.power = CardDbAdapter.STAR_SQUARED; break; case "{1/2}": c.power = 0.5f; break; case "1{1/2}": c.power = 1.5f; break; case "2{1/2}": c.power = 2.5f; break; case "3{1/2}": c.power = 3.5f; break; } } } else if (s2.equalsIgnoreCase("h")) { /* toughness */ pouTouStr = reader.nextString(); try { c.toughness = Integer.parseInt(pouTouStr); } catch (NumberFormatException e) { switch (pouTouStr) { case "*": c.toughness = CardDbAdapter.STAR; break; case "1+*": c.toughness = CardDbAdapter.ONE_PLUS_STAR; break; case "2+*": c.toughness = CardDbAdapter.TWO_PLUS_STAR; break; case "7-*": c.toughness = CardDbAdapter.SEVEN_MINUS_STAR; break; case "*{^2}": c.toughness = CardDbAdapter.STAR_SQUARED; break; case "{1/2}": c.toughness = 0.5f; break; case "1{1/2}": c.toughness = 1.5f; break; case "2{1/2}": c.toughness = 2.5f; break; case "3{1/2}": c.toughness = 3.5f; break; } } } else if (s2.equalsIgnoreCase("i")) { /* loyalty */ try { c.loyalty = reader.nextInt(); } catch (NumberFormatException e) { reader.skipValue(); } } else if (s2.equalsIgnoreCase("j")) { /* ability */ c.ability = reader.nextString(); } else if (s2.equalsIgnoreCase("k")) { /* flavor */ c.flavor = reader.nextString(); } else if (s2.equalsIgnoreCase("l")) { /* artist */ c.artist = reader.nextString(); } else if (s2.equalsIgnoreCase("m")) { /* number */ c.number = reader.nextString(); } else if (s2.equalsIgnoreCase("n")) { /* color */ c.color = reader.nextString(); } else if (s2.equalsIgnoreCase("x")) { /* multiverse id */ try { c.multiverseId = reader.nextInt(); } catch (NumberFormatException e) { reader.skipValue(); } } } cardsToAdd.add(c); elementsParsed++; progressReporter.reportJsonCardProgress( (int) Math.round(100 * elementsParsed / (double) numTotalElements)); reader.endObject(); } reader.endArray(); } } reader.endObject(); } if (s.equalsIgnoreCase("w")) { /* num_cards */ try { numTotalElements = reader.nextInt(); } catch (NumberFormatException e) { reader.skipValue(); } } } reader.endObject(); reader.close(); }
From source file:com.gilecode.yagson.adapters.DefaultTypeAdapterRuntimeWrapper.java
License:Apache License
@Override public T read(JsonReader in, ReadContext ctx) throws IOException { // Although similar check exists in ReadContext.doRead(), we need to duplicate it here, as wrappers hide // simple delegate adapters if (in.peek() == JsonToken.BEGIN_OBJECT && AdapterUtils.isSimpleTypeAdapter(defaultTypeAdapter)) { // if default adapter is simple and '{' is found, we expect and parse type advice here, and fail otherwise return TypeUtils.readTypeAdvisedValue(gson, in, formalType, ctx); } else {//from w w w.j av a 2 s.c o m // no type advice, or defaultTypeAdapter is able to process type advice itself, as it is non-Simple return defaultTypeAdapter.read(in, ctx); } }