From ad6bb6d0d2da264656c5de0e51297997374a41d6 Mon Sep 17 00:00:00 2001 From: Maurice Laveaux Date: Thu, 22 May 2014 15:41:54 +0200 Subject: Refactored JSON to GSON's implementation. * Changed JSONException to JsonParseException. --- src/io/DataWriter.java | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'src/io/DataWriter.java') 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 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); } } -- cgit v1.2.1