Skip to content

Commit

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

[BT-4418] FaunaParser Long values
  • Loading branch information
pchitolina-fauna authored Jan 31, 2024
2 parents 7f01106 + 8169db0 commit 4f97913
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
14 changes: 13 additions & 1 deletion faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.fauna.common.enums.FaunaTokenType.END_OBJECT;
import static com.fauna.common.enums.FaunaTokenType.END_PAGE;
import static com.fauna.common.enums.FaunaTokenType.END_REF;
import static com.fauna.common.enums.FaunaTokenType.LONG;
import static com.fauna.common.enums.FaunaTokenType.NONE;
import static com.fauna.common.enums.FaunaTokenType.TIME;

Expand Down Expand Up @@ -134,8 +135,10 @@ private void handleStartObject() throws IOException {
case DOUBLE_TAG:
handleTaggedString(FaunaTokenType.DOUBLE);
break;
case DOC_TAG:
case LONG_TAG:
handleTaggedString(FaunaTokenType.LONG);
break;
case DOC_TAG:
case MOD_TAG:
case OBJECT_TAG:
case REF_TAG:
Expand Down Expand Up @@ -239,4 +242,13 @@ public Double getValueAsDouble() {
throw new SerializationException("Error getting the current token as Double", e);
}
}

public Long getValueAsLong() {
validateTaggedType(LONG);
try {
return Long.parseLong(taggedTokenValue);
} catch (NumberFormatException e) {
throw new SerializationException("Error getting the current token as Long", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,35 @@ public void testGetValueAsDoubleFail() throws IOException {
assertEquals("Error getting the current token as Double", ex.getMessage());
}

@Test
public void testGetValueAsLong() throws IOException {
String s = "{\"@long\": \"123\"}";
InputStream inputStream = new ByteArrayInputStream(s.getBytes());
FaunaParser reader = new FaunaParser(inputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.LONG, 123L)
);

assertReader(reader, expectedTokens);
}

@Test
public void testGetValueAsLongFail() throws IOException {
String invalidJson = "{\"@long\": \"abc\"}";
InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes());
FaunaParser invalidReader = new FaunaParser(invalidInputStream);

List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of(
Map.entry(FaunaTokenType.LONG, "abc")
);

Exception ex = assertThrows(SerializationException.class,
() -> assertReader(invalidReader, expectedTokens));

assertEquals("Error getting the current token as Long", ex.getMessage());
}

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 @@ -232,6 +261,9 @@ private static void assertReader(FaunaParser reader,
case DOUBLE:
assertEquals(entry.getValue(), reader.getValueAsDouble());
break;
case LONG:
assertEquals(entry.getValue(), reader.getValueAsLong());
break;
default:
assertNull(entry.getValue() == null);
break;
Expand Down

0 comments on commit 4f97913

Please sign in to comment.