-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from fauna/feature/BT-4418-utf8faunareader_rea…
…d_string [BT-4418] FaunaParser String values
- Loading branch information
Showing
5 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
faunaJava/src/main/java/com/fauna/exception/SerializationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.fauna.exception; | ||
|
||
public class SerializationException extends RuntimeException { | ||
|
||
public SerializationException(String message) { | ||
super(message); | ||
} | ||
|
||
public SerializationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.fauna.serialization; | ||
|
||
import static com.fauna.common.enums.FaunaTokenType.END_ARRAY; | ||
import static com.fauna.common.enums.FaunaTokenType.END_DOCUMENT; | ||
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.NONE; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fauna.common.enums.FaunaTokenType; | ||
import com.fauna.exception.SerializationException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.Stack; | ||
|
||
/** | ||
* Represents a reader that provides fast, non-cached, forward-only access to serialized data. | ||
*/ | ||
public class FaunaParser { | ||
|
||
private static final String INT_TAG = "@int"; | ||
private static final String LONG_TAG = "@long"; | ||
private static final String DOUBLE_TAG = "@double"; | ||
private static final String TIME_TAG = "@time"; | ||
private static final String DATE_TAG = "@date"; | ||
private static final String REF_TAG = "@ref"; | ||
private static final String DOC_TAG = "@doc"; | ||
private static final String MOD_TAG = "@mod"; | ||
private static final String SET_TAG = "@set"; | ||
private static final String OBJECT_TAG = "@object";//TODO Understand Module | ||
private final JsonParser jsonParser; | ||
private final Stack<Object> tokenStack = new Stack<>(); | ||
private FaunaTokenType currentFaunaTokenType; | ||
private FaunaTokenType bufferedFaunaTokenType; | ||
private String taggedTokenValue; | ||
|
||
public FaunaParser(InputStream body) throws IOException { | ||
JsonFactory factory = new JsonFactory(); | ||
this.jsonParser = factory.createParser(body); | ||
currentFaunaTokenType = NONE; | ||
} | ||
|
||
public FaunaParser(JsonParser jsonParser) { | ||
this.jsonParser = jsonParser; | ||
currentFaunaTokenType = NONE; | ||
} | ||
|
||
public JsonToken getCurrentTokenType() { | ||
return jsonParser.currentToken(); | ||
} | ||
|
||
private final Set<FaunaTokenType> closers = new HashSet<>(Arrays.asList( | ||
END_OBJECT, | ||
END_PAGE, | ||
END_DOCUMENT, | ||
END_REF, | ||
END_ARRAY | ||
)); | ||
|
||
public boolean read() { | ||
taggedTokenValue = null; | ||
|
||
if (bufferedFaunaTokenType != null) { | ||
currentFaunaTokenType = bufferedFaunaTokenType; | ||
bufferedFaunaTokenType = null; | ||
if (closers.contains(currentFaunaTokenType)) { | ||
tokenStack.pop(); | ||
} | ||
return true; | ||
} | ||
|
||
if (!advance()) { | ||
return false; | ||
} | ||
|
||
JsonToken currentToken = jsonParser.currentToken(); | ||
if (currentToken != null) { | ||
switch (currentToken) { | ||
case VALUE_STRING: | ||
currentFaunaTokenType = FaunaTokenType.STRING; | ||
break; | ||
default: | ||
throw new SerializationException( | ||
"Unhandled JSON token type " + currentToken + "."); | ||
} | ||
} else { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private boolean advance() { | ||
try { | ||
return Objects.nonNull(jsonParser.nextToken()); | ||
} catch (IOException e) { | ||
throw new SerializationException("Failed to advance underlying JSON reader.", e); | ||
} | ||
} | ||
|
||
public String getValueAsString() { | ||
try { | ||
return jsonParser.getValueAsString(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error reading current token as String", e); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
faunaJava/src/test/java/com/fauna/serialization/FaunaParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.fauna.serialization; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import com.fauna.common.enums.FaunaTokenType; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
class FaunaParserTest { | ||
|
||
@Test | ||
public void testGetValueAsString() throws IOException { | ||
InputStream inputStream = new ByteArrayInputStream("\"hello\"".getBytes()); | ||
FaunaParser reader = new FaunaParser(inputStream); | ||
|
||
List<Map.Entry<FaunaTokenType, Object>> expectedTokens = List.of( | ||
Map.entry(FaunaTokenType.STRING, "hello") | ||
); | ||
|
||
assertReader(reader, expectedTokens); | ||
} | ||
|
||
private static void assertReader(FaunaParser reader, | ||
List<Map.Entry<FaunaTokenType, Object>> tokens) { | ||
for (Map.Entry<FaunaTokenType, Object> entry : tokens) { | ||
reader.read(); | ||
assertNotNull(entry.getKey()); | ||
assertNotNull(reader.getCurrentTokenType()); | ||
assertEquals(entry.getKey(), FaunaTokenType.STRING); | ||
|
||
switch (entry.getKey()) { | ||
case FIELD_NAME: | ||
case STRING: | ||
assertEquals(entry.getValue(), reader.getValueAsString()); | ||
break; | ||
default: | ||
assertNull(entry.getValue() == null); | ||
break; | ||
} | ||
} | ||
|
||
assertFalse(reader.read()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
version=0.1.0 | ||
|
||
junitVersion=5.10.0 | ||
mockitoVersion=5.6.0 | ||
mockitoVersion=5.6.0 | ||
jacksonVersion=2.15.3 |