Skip to content

Commit

Permalink
Merge pull request #17 from fauna/feature/BT-4418-utf8faunareader_rea…
Browse files Browse the repository at this point in the history
…d_boolean

[BT-4418] FaunaParser Boolean values
  • Loading branch information
pnwpedro authored Jan 30, 2024
2 parents c300f11 + 7efe46d commit 545a041
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public boolean read() throws IOException {
case START_OBJECT:
handleStartObject();
break;
case VALUE_TRUE:
currentFaunaTokenType = FaunaTokenType.TRUE;
break;
case VALUE_FALSE:
currentFaunaTokenType = FaunaTokenType.FALSE;
break;
default:
throw new SerializationException(
"Unhandled JSON token type " + currentToken + ".");
Expand Down Expand Up @@ -186,4 +192,12 @@ public Integer getValueAsInt() {
throw new RuntimeException("Error getting the current token as Integer", e);
}
}

public Boolean getValueAsBoolean() {
try {
return jsonParser.getValueAsBoolean();
} catch (IOException e) {
throw new SerializationException("Error getting the current token as Boolean", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ public void testUnexpectedEndDuringAdvance() throws IOException {
assertThrows(SerializationException.class, reader::read);
}

@Test
public void testGetValueAsBooleanTrue() throws IOException {
InputStream inputStream = new ByteArrayInputStream("true".getBytes());
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.TRUE, true)
);

assertReader(reader, expectedTokens);
}

@Test
public void testGetValueAsBooleanFalse() throws IOException {
InputStream inputStream = new ByteArrayInputStream("false".getBytes());
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.FALSE, false)
);

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 All @@ -75,6 +99,10 @@ private static void assertReader(FaunaParser reader,
case INT:
assertEquals(entry.getValue(), reader.getValueAsInt());
break;
case TRUE:
case FALSE:
assertEquals(entry.getValue(), reader.getValueAsBoolean());
break;
default:
assertNull(entry.getValue() == null);
break;
Expand Down

0 comments on commit 545a041

Please sign in to comment.