diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 16236f9e..db4dab5a 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -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 tokenStack = new Stack<>(); + private final Stack tokenStack = new Stack<>(); private FaunaTokenType currentFaunaTokenType; private FaunaTokenType bufferedFaunaTokenType; private String taggedTokenValue; @@ -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; @@ -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; diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index b786f75c..9a25885e 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -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; @@ -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> 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> tokens) throws IOException { for (Map.Entry entry : tokens) { @@ -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;