aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-01-16 18:25:11 +0300
committerGravatar Vitaly Takmazov2016-01-16 18:25:11 +0300
commit9a50ad8dea5e9ebc5eb1c5efb19aa7a784283e14 (patch)
tree4edd3e1296c7369603fe0364f248a673de14cf5f
parentee0e13e7243abecf242236c224551774195524a3 (diff)
date and tests
-rw-r--r--build.gradle1
-rw-r--r--src/main/java/com/juick/json/MessageSerializer.java16
-rw-r--r--src/test/java/com/juick/JsonTests.java30
3 files changed, 42 insertions, 5 deletions
diff --git a/build.gradle b/build.gradle
index 2e438790..456e86c8 100644
--- a/build.gradle
+++ b/build.gradle
@@ -6,4 +6,5 @@ repositories {
dependencies {
compile 'org.json:json:20151123'
+ testCompile 'junit:junit:4.12'
}
diff --git a/src/main/java/com/juick/json/MessageSerializer.java b/src/main/java/com/juick/json/MessageSerializer.java
index dccf1b3a..da87d4be 100644
--- a/src/main/java/com/juick/json/MessageSerializer.java
+++ b/src/main/java/com/juick/json/MessageSerializer.java
@@ -22,6 +22,9 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -31,10 +34,14 @@ import org.json.JSONObject;
* @author Ugnich Anton
*/
public class MessageSerializer extends JSONSerializer<Message> {
+
+ private final static Logger LOGGER = Logger.getLogger(MessageSerializer.class.getName());
UserSerializer userSerializer = new UserSerializer();
PlaceSerializer placeSerializer = new PlaceSerializer();
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
@Override
public Message deserialize(JSONObject json) throws JSONException {
com.juick.Message jmsg = new com.juick.Message();
@@ -55,7 +62,7 @@ public class MessageSerializer extends JSONSerializer<Message> {
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
- jmsg.Timestamp = df.parse(json.getString("timestamp"));
+ jmsg.setDate(df.parse(json.getString("timestamp")));
} catch (ParseException e) {
}
@@ -100,10 +107,8 @@ public class MessageSerializer extends JSONSerializer<Message> {
if (msg.getText() != null) {
json.put("body", msg.getText());
}
- if (msg.Timestamp != null) {
- json.put("timestamp", msg.Timestamp);
- } else if (msg.TimestampString != null) {
- json.put("timestamp", msg.TimestampString);
+ if (msg.getDate() != null) {
+ json.put("timestamp", df.format(msg.getDate()));
}
if (msg.getUser() != null) {
json.put("user", userSerializer.serialize(msg.getUser()));
@@ -126,6 +131,7 @@ public class MessageSerializer extends JSONSerializer<Message> {
json.put("photo", photo);
}
} catch (JSONException e) {
+ LOGGER.log(Level.SEVERE, "JSON Exception", e);
}
return json;
diff --git a/src/test/java/com/juick/JsonTests.java b/src/test/java/com/juick/JsonTests.java
new file mode 100644
index 00000000..5d78a2d6
--- /dev/null
+++ b/src/test/java/com/juick/JsonTests.java
@@ -0,0 +1,30 @@
+package com.juick;
+
+import static org.junit.Assert.assertEquals;
+
+import com.juick.json.MessageSerializer;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.juick.Message;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+
+public class JsonTests {
+ @Test
+ public void DateTest() {
+ Message msg = new Message();
+ SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ try {
+ msg.setDate(df.parse("2015-01-01 00:00:00"));
+ MessageSerializer serializer = new MessageSerializer();
+ String json = serializer.serialize(msg).toString();
+ assertEquals("{\"timestamp\":\"2015-01-01 00:00:00\"}", json);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+
+ }
+} \ No newline at end of file