aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2015-11-04 15:24:53 +0300
committerGravatar Vitaly Takmazov2015-11-04 15:25:35 +0300
commit655b821e274ca4be078389475a8bb33139c55a4f (patch)
tree6319c4ba8c32bff6e1f4116665ac90b75468d0ad /src/main/java/com/juick
parent5f4602b34ccc3f899edd45e5e541cbb7307c9585 (diff)
refactoring
Diffstat (limited to 'src/main/java/com/juick')
-rw-r--r--src/main/java/com/juick/json/JSONSerializer.java53
-rw-r--r--src/main/java/com/juick/json/MessageSerializer.java (renamed from src/main/java/com/juick/json/Message.java)18
-rw-r--r--src/main/java/com/juick/json/Messages.java27
-rw-r--r--src/main/java/com/juick/json/PlaceSerializer.java (renamed from src/main/java/com/juick/json/Place.java)12
-rw-r--r--src/main/java/com/juick/json/UserSerializer.java (renamed from src/main/java/com/juick/json/User.java)11
-rw-r--r--src/main/java/com/juick/json/Users.java31
6 files changed, 79 insertions, 73 deletions
diff --git a/src/main/java/com/juick/json/JSONSerializer.java b/src/main/java/com/juick/json/JSONSerializer.java
new file mode 100644
index 00000000..8ac72cb0
--- /dev/null
+++ b/src/main/java/com/juick/json/JSONSerializer.java
@@ -0,0 +1,53 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.juick.json;
+
+import java.util.Iterator;
+import java.util.List;
+import org.json.JSONObject;
+
+/**
+ *
+ * @author vt
+ * @param <T>
+ */
+public abstract class JSONSerializer<T> {
+
+ /**
+ *
+ * @param json
+ * @return
+ */
+ public abstract T deserialize(JSONObject json);
+
+ /**
+ *
+ * @param obj
+ * @return
+ */
+ public abstract JSONObject serialize(T obj);
+
+ /**
+ *
+ * @param objs
+ * @return
+ */
+ public String serializeList(List<T> objs) {
+ String json = "[";
+
+ Iterator<T> i = objs.iterator();
+ while (i.hasNext()) {
+ T m = i.next();
+ if (json.length() > 1) {
+ json += ",";
+ }
+ json += serialize(m).toString();
+ }
+
+ json += "]";
+ return json;
+ }
+}
diff --git a/src/main/java/com/juick/json/Message.java b/src/main/java/com/juick/json/MessageSerializer.java
index 4ab1901a..0879f13a 100644
--- a/src/main/java/com/juick/json/Message.java
+++ b/src/main/java/com/juick/json/MessageSerializer.java
@@ -17,6 +17,7 @@
*/
package com.juick.json;
+import com.juick.Message;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -29,9 +30,13 @@ import org.json.JSONObject;
*
* @author Ugnich Anton
*/
-public class Message {
+public class MessageSerializer extends JSONSerializer<Message> {
+
+ UserSerializer userSerializer = new UserSerializer();
+ PlaceSerializer placeSerializer = new PlaceSerializer();
- public static com.juick.Message parseJSON(JSONObject json) throws JSONException {
+ @Override
+ public Message deserialize(JSONObject json) throws JSONException {
com.juick.Message jmsg = new com.juick.Message();
jmsg.MID = json.getInt("mid");
if (json.has("rid")) {
@@ -45,7 +50,7 @@ public class Message {
jmsg.ReadOnly = json.has("readonly");
jmsg.Text = json.getString("body").replace("&quot;", "\"");
- jmsg.User = com.juick.json.User.parseJSON(json.getJSONObject("user"));
+ jmsg.User = userSerializer.deserialize(json.getJSONObject("user"));
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -72,7 +77,8 @@ public class Message {
return jmsg;
}
- public static JSONObject toJSON(com.juick.Message msg) {
+ @Override
+ public JSONObject serialize(Message msg) {
JSONObject json = new JSONObject();
try {
@@ -100,7 +106,7 @@ public class Message {
json.put("timestamp", msg.TimestampString);
}
if (msg.User != null) {
- json.put("user", com.juick.json.User.toJSON(msg.User));
+ json.put("user", userSerializer.serialize(msg.User));
}
if (msg.Tags != null && msg.Tags.size() > 0) {
json.put("tags", new JSONArray(msg.Tags));
@@ -109,7 +115,7 @@ public class Message {
json.put("replies", msg.Replies);
}
if (msg.Place != null) {
- json.put("place", com.juick.json.Place.toJSON(msg.Place));
+ json.put("place", placeSerializer.serialize(msg.Place));
}
if (msg.AttachmentType != null) {
String fname = msg.MID + (msg.RID > 0 ? "-" + msg.RID : "") + "." + msg.AttachmentType;
diff --git a/src/main/java/com/juick/json/Messages.java b/src/main/java/com/juick/json/Messages.java
deleted file mode 100644
index a1879b09..00000000
--- a/src/main/java/com/juick/json/Messages.java
+++ /dev/null
@@ -1,27 +0,0 @@
-package com.juick.json;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-
-/**
- *
- * @author ugnich
- */
-public class Messages {
-
- public static String arrayToString(ArrayList<com.juick.Message> messages) {
- String json = "[";
-
- Iterator<com.juick.Message> i = messages.iterator();
- while (i.hasNext()) {
- com.juick.Message m = i.next();
- if (json.length() > 1) {
- json += ",";
- }
- json += Message.toJSON(m).toString();
- }
-
- json += "]";
- return json;
- }
-}
diff --git a/src/main/java/com/juick/json/Place.java b/src/main/java/com/juick/json/PlaceSerializer.java
index f195e7d7..f433f7f0 100644
--- a/src/main/java/com/juick/json/Place.java
+++ b/src/main/java/com/juick/json/PlaceSerializer.java
@@ -17,7 +17,7 @@
*/
package com.juick.json;
-import org.json.JSONArray;
+import com.juick.Place;
import org.json.JSONException;
import org.json.JSONObject;
@@ -25,10 +25,11 @@ import org.json.JSONObject;
*
* @author Ugnich Anton
*/
-public class Place {
+public class PlaceSerializer extends JSONSerializer<Place> {
- public static com.juick.Place parseJSON(JSONObject json) throws JSONException {
- com.juick.Place jplace = new com.juick.Place();
+ @Override
+ public Place deserialize(JSONObject json) throws JSONException {
+ Place jplace = new Place();
jplace.pid = json.getInt("pid");
jplace.lat = json.getDouble("lat");
@@ -47,7 +48,8 @@ public class Place {
return jplace;
}
- public static JSONObject toJSON(com.juick.Place place) {
+ @Override
+ public JSONObject serialize(Place place) {
JSONObject json = new JSONObject();
try {
diff --git a/src/main/java/com/juick/json/User.java b/src/main/java/com/juick/json/UserSerializer.java
index dd3143c6..07f87616 100644
--- a/src/main/java/com/juick/json/User.java
+++ b/src/main/java/com/juick/json/UserSerializer.java
@@ -17,6 +17,7 @@
*/
package com.juick.json;
+import com.juick.User;
import org.json.JSONException;
import org.json.JSONObject;
@@ -24,10 +25,11 @@ import org.json.JSONObject;
*
* @author Ugnich Anton
*/
-public class User {
+public class UserSerializer extends JSONSerializer<User> {
- public static com.juick.User parseJSON(JSONObject json) throws JSONException {
- com.juick.User juser = new com.juick.User();
+ @Override
+ public User deserialize(JSONObject json) throws JSONException {
+ User juser = new User();
juser.UID = json.getInt("uid");
juser.UName = json.getString("uname");
if (json.has("fullname")) {
@@ -36,7 +38,8 @@ public class User {
return juser;
}
- public static JSONObject toJSON(com.juick.User user) {
+ @Override
+ public JSONObject serialize(User user) {
JSONObject json = new JSONObject();
try {
diff --git a/src/main/java/com/juick/json/Users.java b/src/main/java/com/juick/json/Users.java
deleted file mode 100644
index 0e486f74..00000000
--- a/src/main/java/com/juick/json/Users.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-package com.juick.json;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-
-/**
- *
- * @author ugnich
- */
-public class Users {
-
- public static String arrayToString(ArrayList<com.juick.User> users) {
- String json = "[";
-
- Iterator<com.juick.User> i = users.iterator();
- while (i.hasNext()) {
- com.juick.User u = i.next();
- if (json.length() > 1) {
- json += ",";
- }
- json += User.toJSON(u).toString();
- }
-
- json += "]";
- return json;
- }
-}