Skip to content

Commit

Permalink
Merge pull request #28 from fauna/feature/BT-4418-faunaparser_add_skip
Browse files Browse the repository at this point in the history
[BT-4418] FaunaParser add skip()
  • Loading branch information
pchitolina-fauna authored Jan 31, 2024
2 parents 4e78324 + 14fb5a0 commit 4c71899
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
23 changes: 22 additions & 1 deletion faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,28 @@ public FaunaTokenType getCurrentTokenType() {
END_REF,
END_ARRAY
));


public void skip() throws IOException {
switch (getCurrentTokenType()) {
case START_OBJECT:
case START_ARRAY:
case START_PAGE:
case START_REF:
case START_DOCUMENT:
skipInternal();
break;
}
}

private void skipInternal() throws IOException {
int startCount = tokenStack.size();
while (read()) {
if (tokenStack.size() < startCount) {
break;
}
}
}

public boolean read() throws IOException {
taggedTokenValue = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,76 @@ public void throwsOnMalformedJson() {
}, "Failed to advance underlying JSON reader.");
}

@Test
public void skipValues() throws IOException {
List<String> tests = List.of(
"{\"k1\": {}, \"k2\": {}}",
"[\"k1\",[],{}]",
"{\"@ref\": {}}",
"{\"@doc\": {}}",
"{\"@set\": {}}",
"{\"@object\":{}}"
);

for (String test : tests) {
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read();
reader.skip();
assertFalse(reader.read());
}
}

@Test
public void skipNestedEscapedObject() throws IOException {
String test = "{\"@object\": {\"inner\": {\"@object\": {\"foo\": \"bar\"}}, \"k2\": {}}}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read(); // {"@object":{
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.read(); // inner
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
reader.read(); // {"@object":{
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.skip(); // "foo": "bar"}}
assertEquals(FaunaTokenType.END_OBJECT, reader.getCurrentTokenType());
reader.read();
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
assertEquals("k2", reader.getValueAsString());
}

@Test
public void skipNestedObject() throws IOException {
String test = "{\"k\":{\"inner\":{}},\"k2\":{}}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read(); // {
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.read(); // k
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
reader.read(); // {
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.skip(); // "inner":{}}
assertEquals(FaunaTokenType.END_OBJECT, reader.getCurrentTokenType());
reader.read();
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
assertEquals("k2", reader.getValueAsString());
}

@Test
public void skipNestedArrays() throws IOException {
String test = "{\"k\":[\"1\",\"2\"],\"k2\":{}}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read(); // {
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.read(); // k
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
reader.read(); // [
assertEquals(FaunaTokenType.START_ARRAY, reader.getCurrentTokenType());
reader.skip(); // "1","2"]
assertEquals(FaunaTokenType.END_ARRAY, reader.getCurrentTokenType());
reader.read();
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
assertEquals("k2", reader.getValueAsString());
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand Down

0 comments on commit 4c71899

Please sign in to comment.