List of usage examples for com.google.gson.stream JsonToken BEGIN_ARRAY
JsonToken BEGIN_ARRAY
To view the source code for com.google.gson.stream JsonToken BEGIN_ARRAY.
Click Source Link
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;/*from ww w.j a va 2s . c om*/ 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.//from w ww.ja v a2 s .co m * <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.github.filosganga.geogson.gson.PositionsAdapter.java
License:Apache License
@Override public Positions read(JsonReader in) throws IOException { Positions parsed;//from ww w.j a v a 2 s. c om JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); parsed = null; } else if (peek == JsonToken.BEGIN_ARRAY) { parsed = parsePositions(in); } else { throw new IllegalArgumentException("The json must be an array or null: " + in.peek()); } return parsed; }
From source file:com.github.filosganga.geogson.gson.PositionsAdapter.java
License:Apache License
private Positions parsePositions(JsonReader in) throws IOException { Optional<Positions> parsed = Optional.absent(); if (in.peek() != JsonToken.BEGIN_ARRAY) { throw new IllegalArgumentException("The given json is not a valid positions"); }//from ww w . j av a2 s .c o m in.beginArray(); if (in.peek() == JsonToken.NUMBER) { parsed = Optional.of(parseSinglePosition(in)); } else if (in.peek() == JsonToken.BEGIN_ARRAY) { while (in.hasNext()) { Positions thisPositions = parsePositions(in); // fix bug #30: according to the recursion (i.e. the array structure; // recognize that we came from a recursion because no parsed has no // value yet): convert the already parsed Positions to the // LinearPositions/AreaPositions matching the recursion level if (parsed.equals(Optional.absent()) && thisPositions instanceof LinearPositions) { AreaPositions areaPositions = new AreaPositions( ImmutableList.of((LinearPositions) thisPositions)); parsed = Optional.of((Positions) areaPositions); } else if (parsed.equals(Optional.absent()) && thisPositions instanceof AreaPositions) { MultiDimensionalPositions multiPositions = new MultiDimensionalPositions( ImmutableList.of((AreaPositions) thisPositions)); parsed = Optional.of((Positions) multiPositions); } else { // mergeFn() does all the rest, if parsed has a value parsed = parsed.transform(mergeFn(thisPositions)).or(Optional.of(thisPositions)); } } } in.endArray(); return parsed.orNull(); }
From source file:com.github.gv2011.hprops.Converter.java
License:Open Source License
private void dispatchTop(final JsonReader json, final Consumer<Property> target, final JsonToken t, final String key) { if (t.equals(JsonToken.BEGIN_OBJECT)) doObject(json, target, key);/* www .ja v a2 s . co m*/ else if (t.equals(JsonToken.BEGIN_ARRAY)) doList(json, target, key); else throw new IllegalArgumentException(t.toString()); }
From source file:com.github.gv2011.hprops.Converter.java
License:Open Source License
private void dispatch(final JsonReader json, final Consumer<Property> target, final JsonToken t, final String key) { if (t.equals(JsonToken.BOOLEAN)) write(target, key, getBoolean(json)); else if (t.equals(JsonToken.STRING)) write(target, key, getString(json)); else if (t.equals(JsonToken.NUMBER)) write(target, key, getNumber(json)); else if (t.equals(JsonToken.BEGIN_OBJECT)) doObject(json, target, key);/*w w w . j a va 2 s . c o m*/ else if (t.equals(JsonToken.BEGIN_ARRAY)) doList(json, target, key); else if (t.equals(JsonToken.NULL)) getNull(json); else throw new IllegalArgumentException(t.toString()); }
From source file:com.goide.dlv.JsonReaderEx.java
License:Apache License
/** * Returns the type of the next token without consuming it. */// w w w .jav a 2 s. c o m @NotNull public JsonToken peek() { 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.gs.reladomo.serial.gson.GsonWrappedListTypedAdapter.java
License:Apache License
@Override public SerializedList<U, T> read(JsonReader jsonReader) throws IOException { ReladomoDeserializer deserializer;//w w w.j a v a 2 s . c om if (this.typeClass == null) { deserializer = new ReladomoDeserializer(); } else { deserializer = new ReladomoDeserializer(typeClass); } deserializer.setIgnoreUnknown(); JsonDeserializerState state = JsonDeserializerState.ListStartState.INSTANCE; while (true) { JsonToken nextToken = jsonReader.peek(); //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT; if (JsonToken.BEGIN_OBJECT == nextToken) { jsonReader.beginObject(); state = state.startObject(deserializer); } else if (JsonToken.END_OBJECT == nextToken) { jsonReader.endObject(); state = state.endObject(deserializer); } else if (JsonToken.BEGIN_ARRAY == nextToken) { jsonReader.beginArray(); state = state.startArray(deserializer); } else if (JsonToken.END_ARRAY == nextToken) { jsonReader.endArray(); state = state.endArray(deserializer); } else if (JsonToken.BOOLEAN == nextToken) { if (jsonReader.nextBoolean()) { state = state.valueTrue(deserializer); } else { state = state.valueFalse(deserializer); } } else if (JsonToken.NAME == nextToken) { String name = jsonReader.nextName(); state = state.fieldName(name, deserializer); } else if (JsonToken.NUMBER == nextToken) { String value = jsonReader.nextString(); state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss } else if (JsonToken.STRING == nextToken) { String value = jsonReader.nextString(); Attribute attribute = deserializer.getCurrentAttribute(); if (attribute instanceof TimestampAttribute) { Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value); state = state.valueTimestamp(timestamp, deserializer); } else { state = state.valueString(value, deserializer); } } else if (JsonToken.NULL == nextToken) { jsonReader.nextNull(); state = state.valueNull(deserializer); } else if (JsonToken.END_DOCUMENT == nextToken) { break; } } return deserializer.getDeserializedResultAsList(); }
From source file:com.gs.reladomo.serial.gson.GsonWrappedTypedAdapter.java
License:Apache License
@Override public Serialized<T> read(JsonReader jsonReader) throws IOException { ReladomoDeserializer deserializer;/*w w w . j av a2s . c o m*/ if (this.typeClass == null) { deserializer = new ReladomoDeserializer(); } else { deserializer = new ReladomoDeserializer(typeClass); } deserializer.setIgnoreUnknown(); JsonDeserializerState state = JsonDeserializerState.NormalParserState.INSTANCE; while (true) { JsonToken nextToken = jsonReader.peek(); //BEGIN_ARRAY, END_ARRAY, BEGIN_OBJECT, END_OBJECT, NAME, STRING, NUMBER, BOOLEAN, NULL, END_DOCUMENT; if (JsonToken.BEGIN_OBJECT == nextToken) { jsonReader.beginObject(); state = state.startObject(deserializer); } else if (JsonToken.END_OBJECT == nextToken) { jsonReader.endObject(); state = state.endObject(deserializer); } else if (JsonToken.BEGIN_ARRAY == nextToken) { jsonReader.beginArray(); state = state.startArray(deserializer); } else if (JsonToken.END_ARRAY == nextToken) { jsonReader.endArray(); state = state.endArray(deserializer); } else if (JsonToken.BOOLEAN == nextToken) { if (jsonReader.nextBoolean()) { state = state.valueTrue(deserializer); } else { state = state.valueFalse(deserializer); } } else if (JsonToken.NAME == nextToken) { String name = jsonReader.nextName(); state = state.fieldName(name, deserializer); } else if (JsonToken.NUMBER == nextToken) { String value = jsonReader.nextString(); state = state.valueString(value, deserializer); // we do the parsing to avoid precision loss } else if (JsonToken.STRING == nextToken) { String value = jsonReader.nextString(); Attribute attribute = deserializer.getCurrentAttribute(); if (attribute instanceof TimestampAttribute) { Timestamp timestamp = GsonReladomoTypeAdapterSerialWriter.jsonToTimestamp(value); state = state.valueTimestamp(timestamp, deserializer); } else { state = state.valueString(value, deserializer); } } else if (JsonToken.NULL == nextToken) { jsonReader.nextNull(); state = state.valueNull(deserializer); } else if (JsonToken.END_DOCUMENT == nextToken) { break; } } return deserializer.getDeserializedResult(); }
From source file:com.ibm.g11n.pipeline.resfilter.impl.JsonResource.java
License:Open Source License
@Override public void write(OutputStream outStream, LanguageBundle languageBundle, FilterOptions options) throws IOException, ResourceFilterException { // extracts key value pairs in original sequence order List<ResourceString> resStrings = languageBundle.getSortedResourceStrings(); JsonObject output = new JsonObject(); JsonObject top_level;//from w w w. ja v a 2s . c om if (this instanceof GlobalizeJsResource) { String resLanguageCode = languageBundle.getEmbeddedLanguageCode(); if (resLanguageCode == null || resLanguageCode.isEmpty()) { throw new ResourceFilterException( "Missing resource language code in the specified language bundle."); } top_level = new JsonObject(); top_level.add(resLanguageCode, output); } else { top_level = output; } for (ResourceString res : resStrings) { String key = res.getKey(); List<KeyPiece> keyPieces = splitKeyPieces(key); JsonElement current = output; for (int i = 0; i < keyPieces.size(); i++) { if (i + 1 < keyPieces.size()) { // There is structure under this // key piece if (current.isJsonObject()) { JsonObject currentObject = current.getAsJsonObject(); if (!currentObject.has(keyPieces.get(i).keyValue)) { if (keyPieces.get(i + 1).keyType == JsonToken.BEGIN_ARRAY) { currentObject.add(keyPieces.get(i).keyValue, new JsonArray()); } else { currentObject.add(keyPieces.get(i).keyValue, new JsonObject()); } } current = currentObject.get(keyPieces.get(i).keyValue); } else { JsonArray currentArray = current.getAsJsonArray(); Integer idx = Integer.valueOf(keyPieces.get(i).keyValue); for (int arrayIndex = currentArray.size(); arrayIndex <= idx; arrayIndex++) { currentArray.add(JsonNull.INSTANCE); } if (currentArray.get(idx).isJsonNull()) { if (keyPieces.get(i + 1).keyType == JsonToken.BEGIN_ARRAY) { currentArray.set(idx, new JsonArray()); } else { currentArray.set(idx, new JsonObject()); } } current = currentArray.get(idx); } } else { // This is the leaf node if (keyPieces.get(i).keyType == JsonToken.BEGIN_ARRAY) { JsonArray currentArray = current.getAsJsonArray(); Integer idx = Integer.valueOf(keyPieces.get(i).keyValue); JsonPrimitive e = new JsonPrimitive(res.getValue()); for (int arrayIndex = currentArray.size(); arrayIndex <= idx; arrayIndex++) { currentArray.add(JsonNull.INSTANCE); } current.getAsJsonArray().set(idx, e); } else { current.getAsJsonObject().addProperty(keyPieces.get(i).keyValue, res.getValue()); } } } } try (OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(outStream), StandardCharsets.UTF_8)) { new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(top_level, writer); } }