Skip to content

Commit

Permalink
Fix bugs in getPath() with arrays of objects and arrays of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse committed Nov 11, 2014
1 parent f2591b6 commit d6c8c1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public void endArray() throws IOException {
}
if (p == PEEKED_END_ARRAY) {
stackSize--;
pathIndices[stackSize - 1]++;
peeked = PEEKED_NONE;
} else {
throw new IllegalStateException("Expected END_ARRAY but was " + peek()
Expand Down Expand Up @@ -400,6 +401,7 @@ public void endObject() throws IOException {
if (p == PEEKED_END_OBJECT) {
stackSize--;
pathNames[stackSize] = null; // Free the last path name so that it can be garbage collected!
pathIndices[stackSize - 1]++;
peeked = PEEKED_NONE;
} else {
throw new IllegalStateException("Expected END_OBJECT but was " + peek()
Expand Down
48 changes: 44 additions & 4 deletions gson/src/test/java/com/google/gson/stream/JsonReaderPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public void testPath() throws IOException {
reader.nextString();
assertEquals("$.a[5].c", reader.getPath());
reader.endObject();
assertEquals("$.a[5]", reader.getPath());
assertEquals("$.a[6]", reader.getPath());
reader.beginArray();
assertEquals("$.a[5][0]", reader.getPath());
assertEquals("$.a[6][0]", reader.getPath());
reader.nextInt();
assertEquals("$.a[5][1]", reader.getPath());
assertEquals("$.a[6][1]", reader.getPath());
reader.endArray();
assertEquals("$.a[5]", reader.getPath());
assertEquals("$.a[7]", reader.getPath());
reader.endArray();
assertEquals("$.a", reader.getPath());
reader.endObject();
Expand Down Expand Up @@ -173,4 +173,44 @@ public void testSkipNestedStructures() throws IOException {
reader.skipValue();
assertEquals("$[1]", reader.getPath());
}

public void testArrayOfObjects() throws IOException {
JsonReader reader = new JsonReader(new StringReader("[{},{},{}]"));
reader.beginArray();
assertEquals("$[0]", reader.getPath());
reader.beginObject();
assertEquals("$[0].", reader.getPath());
reader.endObject();
assertEquals("$[1]", reader.getPath());
reader.beginObject();
assertEquals("$[1].", reader.getPath());
reader.endObject();
assertEquals("$[2]", reader.getPath());
reader.beginObject();
assertEquals("$[2].", reader.getPath());
reader.endObject();
assertEquals("$[3]", reader.getPath());
reader.endArray();
assertEquals("$", reader.getPath());
}

public void testArrayOfArrays() throws IOException {
JsonReader reader = new JsonReader(new StringReader("[[],[],[]]"));
reader.beginArray();
assertEquals("$[0]", reader.getPath());
reader.beginArray();
assertEquals("$[0][0]", reader.getPath());
reader.endArray();
assertEquals("$[1]", reader.getPath());
reader.beginArray();
assertEquals("$[1][0]", reader.getPath());
reader.endArray();
assertEquals("$[2]", reader.getPath());
reader.beginArray();
assertEquals("$[2][0]", reader.getPath());
reader.endArray();
assertEquals("$[3]", reader.getPath());
reader.endArray();
assertEquals("$", reader.getPath());
}
}

0 comments on commit d6c8c1e

Please sign in to comment.