Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: chunked tweet JSON parse exception #348

Merged
merged 2 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,5 @@

<url>https://github.com/Redouane59/twittered</url>

<version>2.14</version>
<version>2.15-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public boolean consumeBuffer(String data) {
}

// Check if the buffer is empty and we receive a valid json data
if (this.buffer.toString().isEmpty() && (!data.trim().startsWith("{"))) {
if (buffer.toString().isEmpty() && (!data.trim().startsWith("{"))) {
LOGGER.warn("Invalid JSON Start Character. Ignoring : " + data);
return false;
}
buffer.append(data);

// If we detect a \r\n in the buffer, then at least a tweet is complete
return (this.buffer.indexOf("\r\n") != -1);
return (buffer.indexOf("\r\n") != -1);
}

/**
Expand Down Expand Up @@ -96,11 +96,14 @@ private <T> boolean readSocket(IAPIEventListener listener, final Response respon
/**
* Returns true if the data received are not in error depending on the response.code
*/
private <T> boolean handleData(IAPIEventListener listener, final Response response, final Class<? extends T> clazz, String line)
throws JsonProcessingException {
private <T> boolean handleData(IAPIEventListener listener, final Response response, final Class<? extends T> clazz, String line) {
if (response.getCode() == 200) {
if (clazz == TweetV2.class) {
listener.onTweetStreamed((TweetV2) TwitterClient.OBJECT_MAPPER.readValue(line, clazz));
try {
listener.onTweetStreamed((TweetV2) TwitterClient.OBJECT_MAPPER.readValue(line, clazz));
} catch (JsonProcessingException e) {
listener.onUnknownDataStreamed(line);
}
} else {
listener.onUnknownDataStreamed(line);
}
Expand Down Expand Up @@ -138,10 +141,10 @@ public String[] getJsonTweets() {
String lastJSON = result.get(result.size() - 1);
boolean complete = isValidJSON(lastJSON);
// Reinit the StringBuilder...
this.buffer = new StringBuilder();
buffer = new StringBuilder();
// If not complete, reconsume the last buffer
if (!complete) {
this.consumeBuffer(lastJSON);
consumeBuffer(lastJSON);
result.remove(result.size() - 1);
}
return result.stream().toArray(String[]::new);
Expand Down