From 275ac11ac87e6c37366ea01bd94164a46fafdb4b Mon Sep 17 00:00:00 2001 From: Pablo Chitolina Date: Wed, 31 Jan 2024 12:44:30 -0300 Subject: [PATCH] Add START_ESCAPED_OBJECT as internal token --- .../fauna/common/enums/FaunaTokenType.java | 1 - .../com/fauna/serialization/FaunaParser.java | 52 +++++++++---------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/common/src/main/java/com/fauna/common/enums/FaunaTokenType.java b/common/src/main/java/com/fauna/common/enums/FaunaTokenType.java index fa9c99de..d5c64a6c 100644 --- a/common/src/main/java/com/fauna/common/enums/FaunaTokenType.java +++ b/common/src/main/java/com/fauna/common/enums/FaunaTokenType.java @@ -7,7 +7,6 @@ public enum FaunaTokenType { NONE, START_OBJECT, - START_ESCAPED_OBJECT, END_OBJECT, START_ARRAY, diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 17ac1347..43399024 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -44,11 +44,15 @@ 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; + private enum InternalTokenType { + START_ESCAPED_OBJECT + } + public FaunaParser(InputStream body) throws IOException { JsonFactory factory = new JsonFactory(); this.jsonParser = factory.createParser(body); @@ -91,8 +95,6 @@ public boolean read() throws IOException { JsonToken currentToken = jsonParser.currentToken(); if (currentToken != null) { switch (currentToken) { - case NOT_AVAILABLE: - break; case VALUE_STRING: currentFaunaTokenType = FaunaTokenType.STRING; break; @@ -157,7 +159,7 @@ private void handleStartObject() throws IOException { case OBJECT_TAG: advanceTrue(); currentFaunaTokenType = FaunaTokenType.START_OBJECT; - tokenStack.push(FaunaTokenType.START_ESCAPED_OBJECT); + tokenStack.push(InternalTokenType.START_ESCAPED_OBJECT); break; case DOC_TAG: case REF_TAG: @@ -183,30 +185,24 @@ private void handleStartObject() throws IOException { } 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."); + Object startToken = tokenStack.pop(); + if (startToken.equals(FaunaTokenType.START_DOCUMENT)) { + currentFaunaTokenType = END_DOCUMENT; + advanceTrue(); + } else if (startToken.equals(FaunaTokenType.START_PAGE)) { + currentFaunaTokenType = END_PAGE; + advanceTrue(); + } else if (startToken.equals(FaunaTokenType.START_REF)) { + currentFaunaTokenType = END_REF; + advanceTrue(); + } else if (startToken.equals(InternalTokenType.START_ESCAPED_OBJECT)) { + currentFaunaTokenType = END_OBJECT; + advanceTrue(); + } else if (startToken.equals(FaunaTokenType.START_OBJECT)) { + currentFaunaTokenType = END_OBJECT; + } else { + throw new SerializationException( + "Unexpected token " + startToken + ". This might be a bug."); } }