summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-10 17:30:49 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-10 17:30:49 +0200
commitbb7e2b32ef5ef531ac3f007c09206c4a9db32ca9 (patch)
tree45db33608d033749b71cd5e6862c31face26e88a
parenta4db85de038f3d07bd379ddaf37bc7815c37b28b (diff)
downloadDatafiller-bb7e2b32ef5ef531ac3f007c09206c4a9db32ca9.tar.gz
Centralize Gson creation and registration
-rw-r--r--src/data/Tweet.java6
-rw-r--r--src/data/TwitterJsonDeserializer.java27
-rw-r--r--src/data/ValidatingJsonDeserializer.java22
-rw-r--r--src/io/TweetReader.java7
-rw-r--r--test/data/ValidatingJsonDeserializerTest.java7
5 files changed, 49 insertions, 20 deletions
diff --git a/src/data/Tweet.java b/src/data/Tweet.java
index 3caae6a..04b5f2c 100644
--- a/src/data/Tweet.java
+++ b/src/data/Tweet.java
@@ -1,7 +1,5 @@
package data;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
/**
* Represents the tweet object as returned by the twitter API.
@@ -32,8 +30,8 @@ public class Tweet {
@Override
public String toString() {
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- return gson.toJson(this);
+ return TwitterJsonDeserializer.getGsonBuilder()
+ .setPrettyPrinting().create().toJson(this);
}
public static class Place {
diff --git a/src/data/TwitterJsonDeserializer.java b/src/data/TwitterJsonDeserializer.java
new file mode 100644
index 0000000..47506d0
--- /dev/null
+++ b/src/data/TwitterJsonDeserializer.java
@@ -0,0 +1,27 @@
+package data;
+
+import com.google.gson.GsonBuilder;
+
+/**
+ * Deserializer for Twitter objects.
+ *
+ * @author Peter Wu
+ */
+public class TwitterJsonDeserializer extends ValidatingJsonDeserializer {
+
+ public TwitterJsonDeserializer() {
+ super(getBaseGsonBuilder()
+ .create());
+ }
+
+ private static GsonBuilder getBaseGsonBuilder() {
+ return new GsonBuilder();
+ }
+
+ public static GsonBuilder getGsonBuilder() {
+ TwitterJsonDeserializer tjd = new TwitterJsonDeserializer();
+ return getBaseGsonBuilder()
+ .registerTypeAdapter(Tweet.class, tjd)
+ .registerTypeAdapter(User.class, tjd);
+ }
+}
diff --git a/src/data/ValidatingJsonDeserializer.java b/src/data/ValidatingJsonDeserializer.java
index d5a8dbc..6fd9dc7 100644
--- a/src/data/ValidatingJsonDeserializer.java
+++ b/src/data/ValidatingJsonDeserializer.java
@@ -21,12 +21,22 @@ import java.lang.reflect.Type;
*/
public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> {
+ private final Gson gson;
+
+ public ValidatingJsonDeserializer() {
+ this(new Gson());
+ }
+
+ public ValidatingJsonDeserializer(Gson gson) {
+ this.gson = gson;
+ }
+
@Override
public T deserialize(JsonElement je, Type type,
JsonDeserializationContext jdc) throws JsonParseException {
T obj;
try {
- obj = new Gson().fromJson(je, type);
+ obj = gson.fromJson(je, type);
} catch (JsonParseException jpe) {
DebuggingJsonDeserializer.tryValidate(je.toString(), (Class) type);
throw new JsonParseException("Debugger could not find a bug", jpe);
@@ -35,7 +45,7 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> {
return obj;
}
- void checkObject(String path, JsonElement je, Class type)
+ protected final void checkObject(String path, JsonElement je, Class type)
throws JsonParseException {
JsonObject jsonObj = je.getAsJsonObject();
for (Field f : type.getDeclaredFields()) {
@@ -53,15 +63,14 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> {
}
}
tryValidateProperty(path, val, f);
- // TODO: validate type?
}
}
private void tryValidateProperty(String path, JsonElement je, Field f)
throws JsonParseException {
Class<?> type = f.getType();
- // assume that this annotation is only applied to objects
- Validator v = f.getAnnotation(Validator.class);
+
+ // validates arrays
ArrayValidator av = f.getAnnotation(ArrayValidator.class);
path += f.getName();
if (av != null) {
@@ -83,6 +92,9 @@ public class ValidatingJsonDeserializer<T> implements JsonDeserializer<T> {
+ maxLen + ": " + path);
}
}
+
+ // validates objects, recursively
+ Validator v = f.getAnnotation(Validator.class);
if (v != null) {
if (type.isArray()) {
// the class expects an array, so the value must have one too.
diff --git a/src/io/TweetReader.java b/src/io/TweetReader.java
index 8968b84..15ddcea 100644
--- a/src/io/TweetReader.java
+++ b/src/io/TweetReader.java
@@ -1,10 +1,9 @@
package io;
import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import data.Tweet;
-import data.ValidatingJsonDeserializer;
+import data.TwitterJsonDeserializer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -27,9 +26,7 @@ public class TweetReader implements ITweetReader {
}
this.is = is;
reader = new BufferedReader(new InputStreamReader(is));
- gson = new GsonBuilder()
- .registerTypeAdapter(Tweet.class, new ValidatingJsonDeserializer())
- .create();
+ gson = TwitterJsonDeserializer.getGsonBuilder().create();
}
@Override
diff --git a/test/data/ValidatingJsonDeserializerTest.java b/test/data/ValidatingJsonDeserializerTest.java
index b51631a..73523f4 100644
--- a/test/data/ValidatingJsonDeserializerTest.java
+++ b/test/data/ValidatingJsonDeserializerTest.java
@@ -1,7 +1,6 @@
package data;
import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
@@ -278,9 +277,7 @@ public class ValidatingJsonDeserializerTest {
+ "}";
private Object deserializeJson(String json, Class cls) {
- Gson gson = new GsonBuilder()
- .registerTypeAdapter(cls, new ValidatingJsonDeserializer<>())
- .create();
+ Gson gson = TwitterJsonDeserializer.getGsonBuilder().create();
return gson.fromJson(json, cls);
}
@@ -312,8 +309,6 @@ public class ValidatingJsonDeserializerTest {
@Test
public void testDeserializeTweet() {
Tweet tweet = (Tweet) deserializeJson(exampleTweet, Tweet.class);
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- //System.out.println(gson.toJson(tweet));
assertEquals(461441338630619136L, tweet.id);
assertNotNull(tweet.retweeted_status);
assertEquals("Wed Apr 30 09:46:02 +0000 2014", tweet.created_at);