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 JSON structure values #22

Merged
merged 10 commits into from
Jan 31, 2024
47 changes: 44 additions & 3 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class FaunaParser {
private static final String SET_TAG = "@set";
private static final String OBJECT_TAG = "@object";//TODO Understand Module
private final JsonParser jsonParser;
private final Stack<Object> tokenStack = new Stack<>();
private final Stack<FaunaTokenType> tokenStack = new Stack<>();
private FaunaTokenType currentFaunaTokenType;
private FaunaTokenType bufferedFaunaTokenType;
private String taggedTokenValue;
Expand Down Expand Up @@ -97,6 +97,17 @@ public boolean read() throws IOException {
case START_OBJECT:
handleStartObject();
break;
case START_ARRAY:
tokenStack.push(FaunaTokenType.START_ARRAY);
currentFaunaTokenType = FaunaTokenType.START_ARRAY;
break;
case END_OBJECT:
handleEndObject();
break;
case END_ARRAY:
tokenStack.pop();
currentFaunaTokenType = FaunaTokenType.END_ARRAY;
break;
case VALUE_TRUE:
currentFaunaTokenType = FaunaTokenType.TRUE;
break;
Expand Down Expand Up @@ -152,14 +163,44 @@ private void handleStartObject() throws IOException {
}
break;
case END_OBJECT:
throw new SerializationException(
"Token not implemented: " + jsonParser.currentToken());
bufferedFaunaTokenType = FaunaTokenType.END_OBJECT;
tokenStack.push(FaunaTokenType.START_OBJECT);
currentFaunaTokenType = FaunaTokenType.START_OBJECT;
break;
default:
throw new SerializationException(
"Unexpected token following StartObject: " + jsonParser.currentToken());
}
}

private void handleEndObject() {
FaunaTokenType startToken = tokenStack.pop();
switch (startToken) {
case START_DOCUMENT:
currentFaunaTokenType = END_DOCUMENT;
advanceTrue();
break;
case START_PAGE:
currentFaunaTokenType = END_PAGE;
advanceTrue();
break;
case START_REF:
currentFaunaTokenType = END_REF;
advanceTrue();
break;
case START_ESCAPED_OBJECT:
currentFaunaTokenType = END_OBJECT;
advanceTrue();
break;
case START_OBJECT:
currentFaunaTokenType = END_OBJECT;
break;
default:
throw new SerializationException(
"Unexpected token " + startToken + ". This might be a bug.");
}
}

private void handleTaggedString(FaunaTokenType token) throws IOException {
advanceTrue();
currentFaunaTokenType = token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.InputStream;
import java.time.Instant;
import java.time.LocalDate;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -246,6 +247,22 @@ public void testGetValueAsModule() throws IOException {
assertReader(reader, expectedTokens);
}

@Test
public void readArrayWithEmptyObject() throws IOException {
String s = "[{}]";
InputStream inputStream = new ByteArrayInputStream(s.getBytes());
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_ARRAY, null),
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_ARRAY, null)
);

assertReader(reader, expectedTokens);
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand Down Expand Up @@ -281,6 +298,12 @@ private static void assertReader(FaunaParser reader,
case MODULE:
assertEquals(entry.getValue(), reader.getValueAsModule());
break;
case START_ARRAY:
case START_OBJECT:
case END_ARRAY:
case END_OBJECT:
assertNull(entry.getValue());
break;
default:
assertNull(entry.getValue() == null);
break;
Expand Down
Loading