diff options
author | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-22 15:41:54 +0200 |
---|---|---|
committer | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-22 15:41:54 +0200 |
commit | ad6bb6d0d2da264656c5de0e51297997374a41d6 (patch) | |
tree | 90b988b6e63a60fdf60a7ed467f2130c31c619ad /src/io/DataWriter.java | |
parent | 2b2a47a0086eab52b09e5425e75cafb488e51074 (diff) | |
download | TwitterDataAnalytics-ad6bb6d0d2da264656c5de0e51297997374a41d6.tar.gz |
Refactored JSON to GSON's implementation.
* Changed JSONException to JsonParseException.
Diffstat (limited to 'src/io/DataWriter.java')
-rw-r--r-- | src/io/DataWriter.java | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/io/DataWriter.java b/src/io/DataWriter.java index c1e1ce9..f3d4006 100644 --- a/src/io/DataWriter.java +++ b/src/io/DataWriter.java @@ -1,5 +1,8 @@ package io; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; @@ -81,7 +84,7 @@ public class DataWriter implements ResultListener, Closeable, Flushable { } @Override - public void tweetGenerated(JSONObject obj) { + public void tweetGenerated(JsonObject obj) { try { // ensure that the file is open m_tweet.open(); @@ -105,19 +108,24 @@ public class DataWriter implements ResultListener, Closeable, Flushable { try { is = store.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + JsonParser jsonParser = new JsonParser(); // parse each line into a JSONObject, read the id and add it to // the set of ids. while ((line = reader.readLine()) != null) { - JSONObject obj = new JSONObject(line); - long id = obj.getLong("id"); - idSet.add(id); - lineno++; + try { + JsonObject obj = jsonParser.parse(line).getAsJsonObject(); + long id = obj.get("id").getAsLong(); + idSet.add(id); + lineno++; + } catch (JsonParseException ex) { + getLogger().log(Level.WARNING, "Tweet found without an id: {0}", ex); + } } } catch (FileNotFoundException ex) { // ignore, file will be created if necessary. - } catch (JSONException | IOException ex) { + } catch (IOException ex) { if (line != null) { - getLogger().log(Level.INFO, "Last line: " + line); + getLogger().log(Level.INFO, "Last line: {0}", line); } getLogger().log(Level.WARNING, store.getFileName() + ": error occurred in file at line " + lineno, ex); @@ -134,21 +142,17 @@ public class DataWriter implements ResultListener, Closeable, Flushable { * @param output The stream to write objects to. * @param idSet The id set to add the obj id to. */ - private void writeObject(JSONObject obj, OutputStream output, + private void writeObject(JsonObject obj, OutputStream output, Set<Long> idSet) { - try { - long id = obj.getLong("id"); + long id = obj.get("id").getAsLong(); - if (!idSet.contains(id)) { - try { - output.write((obj.toString() + "\n").getBytes(Charsets.UTF_8)); - idSet.add(id); - } catch (IOException ex) { - getLogger().log(Level.WARNING, "Cannot write to file", ex); - } + if (!idSet.contains(id)) { + try { + output.write((obj.toString() + "\n").getBytes(Charsets.UTF_8)); + idSet.add(id); + } catch (IOException ex) { + getLogger().log(Level.WARNING, "Cannot write to file", ex); } - } catch (JSONException ex) { - getLogger().log(Level.WARNING, "ID not found?!", ex); } } |