From cbcc5a5e4c1edd4689c756a2548a5087201ee74e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 22 Jan 2012 23:01:24 +0700 Subject: toJSON() --- src/com/juick/json/Message.java | 54 ++++++++++++++++++++++++++++++++++++++--- src/com/juick/json/Place.java | 28 ++++++++++++++++++--- src/com/juick/json/User.java | 25 ++++++++++++++++--- 3 files changed, 98 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/com/juick/json/Message.java b/src/com/juick/json/Message.java index 83a8624f..da817a01 100644 --- a/src/com/juick/json/Message.java +++ b/src/com/juick/json/Message.java @@ -29,10 +29,10 @@ import org.json.JSONObject; * * @author Ugnich Anton */ -public class Message extends com.juick.Message { +public class Message { - public static Message parseJSON(JSONObject json) throws JSONException { - Message jmsg = new Message(); + public static com.juick.Message parseJSON(JSONObject json) throws JSONException { + com.juick.Message jmsg = new com.juick.Message(); jmsg.MID = json.getInt("mid"); if (json.has("rid")) { jmsg.RID = json.getInt("rid"); @@ -67,4 +67,52 @@ public class Message extends com.juick.Message { return jmsg; } + + public static JSONObject toJSON(com.juick.Message msg) { + JSONObject json = new JSONObject(); + + try { + if (msg.MID > 0) { + json.put("mid", msg.MID); + } + if (msg.RID > 0) { + json.put("rid", msg.RID); + } + if (msg.Text != null) { + json.put("body", msg.Text); + } + if (msg.Timestamp != null) { + json.put("timestamp", msg.Timestamp); + } + if (msg.User != null) { + json.put("user", com.juick.json.User.toJSON(msg.User)); + } + if (msg.tags != null && msg.tags.size() > 0) { + json.put("tags", new JSONArray(msg.tags)); + } + if (msg.replies > 0) { + json.put("replies", msg.replies); + } + if (msg.place != null) { + json.put("place", com.juick.json.Place.toJSON(msg.place)); + } + if (msg.AttachmentType != null) { + String fname = msg.MID + (msg.RID > 0 ? "-" + msg.RID : "") + "." + msg.AttachmentType; + if (msg.AttachmentType.equals("jpg")) { + JSONObject photo = new JSONObject(); + photo.put("thumbnail", "http://i.juick.com/ps/" + fname); + photo.put("small", "http://i.juick.com/photo-512/" + fname); + photo.put("medium", "http://i.juick.com/photo-1024/" + fname); + json.put("photo", photo); + } else { + JSONObject video = new JSONObject(); + video.put("mp4", "http://i.juick.com/video/" + fname); + json.put("video", video); + } + } + } catch (JSONException e) { + } + + return json; + } } diff --git a/src/com/juick/json/Place.java b/src/com/juick/json/Place.java index 63a5929c..4e3e5579 100644 --- a/src/com/juick/json/Place.java +++ b/src/com/juick/json/Place.java @@ -25,10 +25,10 @@ import org.json.JSONObject; * * @author Ugnich Anton */ -public class Place extends com.juick.Place { +public class Place { - public static Place parseJSON(JSONObject json) throws JSONException { - Place jplace = new Place(); + public static com.juick.Place parseJSON(JSONObject json) throws JSONException { + com.juick.Place jplace = new com.juick.Place(); jplace.pid = json.getInt("pid"); jplace.lat = json.getDouble("lat"); @@ -55,4 +55,26 @@ public class Place extends com.juick.Place { return jplace; } + + public static JSONObject toJSON(com.juick.Place place) { + JSONObject json = new JSONObject(); + + try { + if (place.pid > 0) { + json.put("pid", place.pid); + } + if (place.lat >= -90 && place.lat <= 90) { + json.put("lat", place.lat); + } + if (place.lon >= -180 && place.lon <= 180) { + json.put("lon", place.lon); + } + if (place.name != null) { + json.put("name", place.name); + } + } catch (JSONException e) { + } + + return json; + } } diff --git a/src/com/juick/json/User.java b/src/com/juick/json/User.java index b4e0e7e8..43ee4803 100644 --- a/src/com/juick/json/User.java +++ b/src/com/juick/json/User.java @@ -24,10 +24,10 @@ import org.json.JSONObject; * * @author Ugnich Anton */ -public class User extends com.juick.User { +public class User { - public static User parseJSON(JSONObject json) throws JSONException { - User juser = new User(); + public static com.juick.User parseJSON(JSONObject json) throws JSONException { + com.juick.User juser = new com.juick.User(); juser.UID = json.getInt("uid"); juser.UName = json.getString("uname"); if (json.has("fullname")) { @@ -35,4 +35,23 @@ public class User extends com.juick.User { } return juser; } + + public static JSONObject toJSON(com.juick.User user) { + JSONObject json = new JSONObject(); + + try { + if (user.UID > 0) { + json.put("uid", user.UID); + } + if (user.UName != null) { + json.put("uname", user.UName); + } + if (user.FullName != null) { + json.put("fullname", user.FullName); + } + } catch (JSONException e) { + } + + return json; + } } -- cgit v1.2.3