diff --git a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java index 9a085d90..a1a94550 100644 --- a/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java +++ b/faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java @@ -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; @@ -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: @@ -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); + } + } } \ No newline at end of file diff --git a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java index ba1568a6..8030cb9b 100644 --- a/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java +++ b/faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java @@ -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> 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> 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> tokens) throws IOException { for (Map.Entry entry : tokens) { @@ -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;