Skip to content

Commit

Permalink
Merge pull request #23 from fauna/feature/BT-4418-faunaparser_read_ob…
Browse files Browse the repository at this point in the history
…ject

[BT-4418] FaunaParser Object values
  • Loading branch information
pchitolina-fauna authored Jan 31, 2024
2 parents 792e110 + 275ac11 commit af0ac35
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public enum FaunaTokenType {
NONE,

START_OBJECT,
START_ESCAPED_OBJECT,
END_OBJECT,

START_ARRAY,
Expand Down
57 changes: 31 additions & 26 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<FaunaTokenType> tokenStack = new Stack<>();
private final Stack<Object> 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);
Expand Down Expand Up @@ -114,6 +118,9 @@ public boolean read() throws IOException {
case VALUE_FALSE:
currentFaunaTokenType = FaunaTokenType.FALSE;
break;
case FIELD_NAME:
currentFaunaTokenType = FaunaTokenType.FIELD_NAME;
break;
default:
throw new SerializationException(
"Unhandled JSON token type " + currentToken + ".");
Expand Down Expand Up @@ -149,8 +156,12 @@ private void handleStartObject() throws IOException {
case MOD_TAG:
handleTaggedString(FaunaTokenType.MODULE);
break;
case DOC_TAG:
case OBJECT_TAG:
advanceTrue();
currentFaunaTokenType = FaunaTokenType.START_OBJECT;
tokenStack.push(InternalTokenType.START_ESCAPED_OBJECT);
break;
case DOC_TAG:
case REF_TAG:
case SET_TAG:
throw new SerializationException(
Expand All @@ -174,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.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,37 @@ public void readArrayWithEmptyObject() throws IOException {
assertReader(reader, expectedTokens);
}

@Test
public void testReadEscapedObject() throws IOException {
String s = "{\n" +
" \"@object\": {\n" +
" \"@int\": \"notanint\",\n" +
" \"anInt\": { \"@int\": \"123\" },\n" +
" \"@object\": \"notanobject\",\n" +
" \"anEscapedObject\": { \"@object\": { \"@long\": \"notalong\" } }\n" +
" }\n" +
"}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(s.getBytes()));

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
Map.entry(FaunaTokenType.FIELD_NAME, "@int"),
Map.entry(FaunaTokenType.STRING, "notanint"),
Map.entry(FaunaTokenType.FIELD_NAME, "anInt"),
Map.entry(FaunaTokenType.INT, 123),
Map.entry(FaunaTokenType.FIELD_NAME, "@object"),
Map.entry(FaunaTokenType.STRING, "notanobject"),
Map.entry(FaunaTokenType.FIELD_NAME, "anEscapedObject"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null),
Map.entry(FaunaTokenType.FIELD_NAME, "@long"),
Map.entry(FaunaTokenType.STRING, "notalong"),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null),
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, 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 @@ -298,14 +329,8 @@ 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);
assertNull(entry.getValue());
break;
}
}
Expand Down

0 comments on commit af0ac35

Please sign in to comment.