Merge pull request #17 from square/jw/path-bugs

Fix bugs in getPath() with arrays of objects and arrays of arrays.
This commit is contained in:
Jesse Wilson
2014-12-22 16:10:02 -05:00
2 changed files with 46 additions and 4 deletions

View File

@@ -353,6 +353,7 @@ public class JsonReader implements Closeable {
}
if (p == PEEKED_END_ARRAY) {
stackSize--;
pathIndices[stackSize - 1]++;
peeked = PEEKED_NONE;
} else {
throw new IllegalStateException("Expected END_ARRAY but was " + peek()
@@ -390,6 +391,7 @@ public class JsonReader implements Closeable {
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()

View File

@@ -47,19 +47,59 @@ public class JsonReaderPathTest {
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();
assertEquals("$", reader.getPath());
}
@Test public void arrayOfObjects() throws IOException {
JsonReader reader = new JsonReader("[{},{},{}]");
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());
}
@Test public void arrayOfArrays() throws IOException {
JsonReader reader = new JsonReader("[[],[],[]]");
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());
}
@Test public void objectPath() throws IOException {
JsonReader reader = new JsonReader("{\"a\":1,\"b\":2}");
assertEquals("$", reader.getPath());