diff options
author | Vitaly Takmazov | 2016-07-12 17:51:27 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-12 17:51:27 +0300 |
commit | f04b1c21e42525377d01b5d102006ae4efe5d4b1 (patch) | |
tree | 1d9f9d00765049adff2319bdb8f2f3fa7ac2a0ba | |
parent | bdbf65e0a50758bfcc88e1cc9c501eeed4e7e45d (diff) |
jackson-databind serialization with tests
-rw-r--r-- | juick-core/src/main/java/com/juick/Message.java | 8 | ||||
-rw-r--r-- | src/test/java/com/juick/tests/JsonTests.java | 16 |
2 files changed, 23 insertions, 1 deletions
diff --git a/juick-core/src/main/java/com/juick/Message.java b/juick-core/src/main/java/com/juick/Message.java index 2b66e667..cb5fe92e 100644 --- a/juick-core/src/main/java/com/juick/Message.java +++ b/juick-core/src/main/java/com/juick/Message.java @@ -17,6 +17,8 @@ */ package com.juick; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.*; @@ -36,6 +38,7 @@ public class Message implements Comparable { public int ReplyTo = 0; private String Text = null; private User User = null; + @JsonProperty("tags") public List<Tag> Tags = new ArrayList<>(); private Date date; public int TimeAgo = 0; @@ -157,7 +160,7 @@ public class Message implements Comparable { return null; } } - + @JsonIgnore public String getTagsString() { String ret = ""; if (!Tags.isEmpty()) { @@ -209,6 +212,7 @@ public class Message implements Comparable { User = user; } + @JsonProperty("body") public String getText() { return Text; } @@ -217,6 +221,8 @@ public class Message implements Comparable { Text = text; } + @JsonProperty("timestamp") + @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone="UTC") public Date getDate() { return date; } diff --git a/src/test/java/com/juick/tests/JsonTests.java b/src/test/java/com/juick/tests/JsonTests.java index ab0d6283..8378d7f1 100644 --- a/src/test/java/com/juick/tests/JsonTests.java +++ b/src/test/java/com/juick/tests/JsonTests.java @@ -3,6 +3,7 @@ package com.juick.tests; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; +import com.juick.Tag; import com.juick.User; import com.juick.json.MessageSerializer; import com.juick.json.UserSerializer; @@ -12,6 +13,7 @@ import org.junit.Test; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Date; import java.util.TimeZone; import static org.junit.Assert.assertEquals; @@ -48,5 +50,19 @@ public class JsonTests { String jacksonString = mapper.writeValueAsString(user); JSONObject jacksonJson = new JSONObject(jacksonString); assertEquals("jackson should serialize as ugnich", handmadeJson.length(), jacksonJson.length()); + Message msg = new Message(); + msg.setMID(1); + msg.setUser(user); + msg.setText("yo"); + msg.setDate(new Date()); + msg.Tags.add(new Tag("test")); + msg.Tags.add(new Tag("json")); + MessageSerializer messageSerializer = new MessageSerializer(); + JSONObject handmadeJsonMessage = messageSerializer.serialize(msg); + Message jacksonMessage = mapper.readValue(handmadeJsonMessage.toString(), Message.class); + assertEquals("jackson should deserialize ugnich json", msg, jacksonMessage); + String jacksonStringMessage = mapper.writeValueAsString(jacksonMessage); + JSONObject jacksonJsonMessage = new JSONObject(jacksonStringMessage); + assertEquals("jackson should serialize as ugnich", handmadeJsonMessage.length(), jacksonJsonMessage.length()); } }
\ No newline at end of file |