Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BT-4418] FaunaParser add skip() #28

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -71,7 +71,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
Loading