diff --git a/src/main/java/com/arangodb/velocypack/ArrayIterator.java b/src/main/java/com/arangodb/velocypack/ArrayIterator.java index 04712af..ba09102 100644 --- a/src/main/java/com/arangodb/velocypack/ArrayIterator.java +++ b/src/main/java/com/arangodb/velocypack/ArrayIterator.java @@ -35,17 +35,21 @@ public ArrayIterator(final VPackSlice slice) throws VPackValueTypeException { if (!slice.isArray()) { throw new VPackValueTypeException(ValueType.ARRAY); } + if (size > 0) { + current = slice.getNth(0).getStart(); + } } @Override public VPackSlice next() { - final VPackSlice next; if (hasNext()) { - next = slice.get((int) position++); + final VPackSlice next = getCurrent(); + position++; + current += next.getByteSize(); + return next; } else { throw new NoSuchElementException(); } - return next; } @Override diff --git a/src/main/java/com/arangodb/velocypack/VPack.java b/src/main/java/com/arangodb/velocypack/VPack.java index 9472f80..0e46fac 100644 --- a/src/main/java/com/arangodb/velocypack/VPack.java +++ b/src/main/java/com/arangodb/velocypack/VPack.java @@ -667,8 +667,9 @@ private Object deserializeArray(final VPackSlice parent, final VPackSlice vp final int length = vpack.getLength(); final Class componentType = ((Class) type).getComponentType(); final Object value = Array.newInstance(componentType, length); - for (int i = 0; i < length; i++) { - Array.set(value, i, getValue(parent, vpack.get(i), componentType, null)); + int i = 0; + for (final Iterator iterator = vpack.arrayIterator(); iterator.hasNext(); ) { + Array.set(value, i++, getValue(parent, iterator.next(), componentType, null)); } return value; } diff --git a/src/main/java/com/arangodb/velocypack/VPackParser.java b/src/main/java/com/arangodb/velocypack/VPackParser.java index e5c0383..af3710c 100644 --- a/src/main/java/com/arangodb/velocypack/VPackParser.java +++ b/src/main/java/com/arangodb/velocypack/VPackParser.java @@ -324,8 +324,8 @@ private void parseArray(final VPackSlice value, final StringBuilder json, final throws VPackException { json.append(ARRAY_OPEN); int added = 0; - for (int i = 0; i < value.getLength(); i++) { - final VPackSlice valueAt = value.get(i); + for (final Iterator iterator = value.arrayIterator(); iterator.hasNext();) { + final VPackSlice valueAt = iterator.next(); if (!valueAt.isNull() || includeNullValues) { if (added++ > 0) { json.append(SEPARATOR); diff --git a/src/main/java/com/arangodb/velocypack/VPackSlice.java b/src/main/java/com/arangodb/velocypack/VPackSlice.java index 3731bad..1999bf5 100644 --- a/src/main/java/com/arangodb/velocypack/VPackSlice.java +++ b/src/main/java/com/arangodb/velocypack/VPackSlice.java @@ -783,7 +783,7 @@ private VPackSlice getNthKey(final int index) { return new VPackSlice(vpack, start + getNthOffset(index)); } - private VPackSlice getNth(final int index) { + public VPackSlice getNth(final int index) { return new VPackSlice(vpack, start + getNthOffset(index)); } diff --git a/src/test/java/com/arangodb/velocypack/VPackSliceTest.java b/src/test/java/com/arangodb/velocypack/VPackSliceTest.java index 4439b46..8555061 100644 --- a/src/test/java/com/arangodb/velocypack/VPackSliceTest.java +++ b/src/test/java/com/arangodb/velocypack/VPackSliceTest.java @@ -873,7 +873,7 @@ private void checkArray(final long[] expected, final byte[] vpack) throws VPackE } @Test - public void arrayIterator() throws VPackException { + public void arrayIteratorOnCompactArray() throws VPackException { final Collection expected = Arrays.asList("a", "b", "c", "d", "e", "f"); final VPackSlice slice = new VPackSlice(new byte[] { 0x13, 0x0f, 0x41, 0x61, 0x41, 0x62, 0x41, 0x63, 0x41, 0x64, 0x41, 0x65, 0x41, 0x66, 0x06 }); @@ -885,6 +885,18 @@ public void arrayIterator() throws VPackException { } } + @Test + public void arrayIteratorOnArrayWithIndexTable() throws VPackException { + final Collection expected = Arrays.asList(1, 42, 3); + final VPackSlice slice = new VPackSlice(new byte[] { 0x06, 0x0a, 0x03, 0x31, 0x20, 0x2a, 0x33, 0x03, 0x04, 0x06 }); + final Iterator iteratorSlice = slice.arrayIterator(); + for (final Integer i : expected) { + final VPackSlice next = iteratorSlice.next(); + assertThat(next.isInteger(), is(true)); + assertThat(next.getAsInt(), is(i)); + } + } + @Test public void objectIterator() throws VPackException { // {"a":"test","b":"test","c":"test"}