diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-06 21:45:25 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-06 21:45:25 +0000 |
commit | 9eef67cf0a37bf882cf3268b0c288f04773cc15a (patch) | |
tree | e62ec505372386437c0e97d3d72b88029a1387d8 /sca-java-2.x/trunk/modules/databinding-json/src | |
parent | 90ee764596f898d35dd50a1c1f525bd04f8d971f (diff) |
Fix the Date format for json data binding
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1165872 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/databinding-json/src')
2 files changed, 116 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java index 895c9e70ce..2307150370 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/main/java/org/apache/tuscany/sca/databinding/json/jackson/JacksonHelper.java @@ -43,6 +43,7 @@ import org.codehaus.jackson.map.deser.CustomDeserializerFactory; import org.codehaus.jackson.map.deser.StdDeserializerProvider; import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector; import org.codehaus.jackson.map.ser.CustomSerializerFactory; +import org.codehaus.jackson.map.util.StdDateFormat; import org.codehaus.jackson.xc.JaxbAnnotationIntrospector; import org.codehaus.jackson.xc.XmlAdapterJsonDeserializer; import org.codehaus.jackson.xc.XmlAdapterJsonSerializer; @@ -99,8 +100,10 @@ public class JacksonHelper { AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); mapper.getDeserializationConfig().setAnnotationIntrospector(pair); mapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE); + mapper.getDeserializationConfig().setDateFormat(StdDateFormat.getBlueprintISO8601Format()); mapper.getSerializationConfig().setAnnotationIntrospector(pair); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); + mapper.getSerializationConfig().setDateFormat(StdDateFormat.getBlueprintISO8601Format()); return mapper; } diff --git a/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java b/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java index b401e7012d..4476f4cf53 100644 --- a/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding-json/src/test/java/org/apache/tuscany/sca/databinding/json/jackson/Object2JSONTestCase.java @@ -20,6 +20,8 @@ package org.apache.tuscany.sca.databinding.json.jackson; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; import java.util.List; import junit.framework.Assert; @@ -41,6 +43,7 @@ public class Object2JSONTestCase { private String friends[]; private List<String> books; private YourBean you; + private Date date; public String getName() { return name; @@ -90,6 +93,80 @@ public class Object2JSONTestCase { this.you = you; } + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + age; + result = prime * result + ((books == null) ? 0 : books.hashCode()); + result = prime * result + ((date == null) ? 0 : date.hashCode()); + result = prime * result + Arrays.hashCode(friends); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + (vip ? 1231 : 1237); + result = prime * result + ((you == null) ? 0 : you.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + MyBean other = (MyBean)obj; + if (age != other.age) { + return false; + } + if (books == null) { + if (other.books != null) { + return false; + } + } else if (!books.equals(other.books)) { + return false; + } + if (date == null) { + if (other.date != null) { + return false; + } + } else if (!date.equals(other.date)) { + return false; + } + if (!Arrays.equals(friends, other.friends)) { + return false; + } + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + if (vip != other.vip) { + return false; + } + if (you == null) { + if (other.you != null) { + return false; + } + } else if (!you.equals(other.you)) { + return false; + } + return true; + } + } public static class YourBean { @@ -111,6 +188,40 @@ public class Object2JSONTestCase { public void setName(String name) { this.name = name; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + id; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + YourBean other = (YourBean)obj; + if (id != other.id) { + return false; + } + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + return true; + } } @Test @@ -121,6 +232,7 @@ public class Object2JSONTestCase { me.setFriends(new String[] {"John", "Mike"}); me.setVip(true); me.setName("Me"); + me.setDate(new Date()); YourBean you = new YourBean(); you.setId(123); you.setName(null); @@ -136,6 +248,7 @@ public class Object2JSONTestCase { // String json = // "{\"age\":30,\"books\":[],\"friends\":[\"John\",\"Mike\"],\"name\":\"Me\",\"vip\":true,\"you\":{\"id\":123,\"name\":null}}"; // Assert.assertEquals(json, result.toString()); + Assert.assertEquals(v, me); } @Test |