You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/08/30 02:03:55 UTC

[GitHub] liubao68 closed pull request #732: SCB-333 Update to support the date time with JSR-310

liubao68 closed pull request #732: SCB-333 Update to support the date time with JSR-310
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/732
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
index 6dc1bbb01..927c872f4 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
@@ -26,6 +26,7 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.databind.type.TypeFactory;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 
 public final class RestObjectMapper extends ObjectMapper {
   public static final RestObjectMapper INSTANCE = new RestObjectMapper();
@@ -52,6 +53,7 @@ public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fie
     getFactory().disable(Feature.AUTO_CLOSE_SOURCE);
     disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
     disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
+    registerModule(new JavaTimeModule());
   }
 
   public String convertToString(Object value) throws Exception {
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java
index 1dd335e71..59199aa47 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestObjectMapper.java
@@ -17,11 +17,14 @@
 
 package org.apache.servicecomb.common.rest.codec;
 
+import java.time.LocalDateTime;
+
 import org.junit.Assert;
 import org.junit.Test;
 
 import com.fasterxml.jackson.core.JsonParser.Feature;
 import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.type.TypeFactory;
 
 public class TestRestObjectMapper {
 
@@ -34,4 +37,10 @@ public void testAutoCloseSource() {
   public void testDeserializationFeature() {
     Assert.assertFalse(RestObjectMapper.INSTANCE.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
   }
+
+  @Test
+  public void testJSR310Module() {
+    Assert.assertTrue(RestObjectMapper.INSTANCE.canDeserialize(TypeFactory.defaultInstance().constructType(LocalDateTime.class)));
+    Assert.assertTrue(RestObjectMapper.INSTANCE.canSerialize(LocalDateTime.class));
+  }
 }
diff --git a/foundations/foundation-common/pom.xml b/foundations/foundation-common/pom.xml
index d1a369f8b..4a320ba09 100644
--- a/foundations/foundation-common/pom.xml
+++ b/foundations/foundation-common/pom.xml
@@ -39,6 +39,10 @@
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-annotations</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.datatype</groupId>
+      <artifactId>jackson-datatype-jsr310</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java
index 75c7b36ef..79545c2cf 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java
@@ -29,6 +29,7 @@
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 
 public final class JsonUtils {
   public static final ObjectMapper OBJ_MAPPER;
@@ -37,6 +38,7 @@
     OBJ_MAPPER = new ObjectMapper();
     OBJ_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
     OBJ_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
+    OBJ_MAPPER.registerModule(new JavaTimeModule());
   }
 
   private JsonUtils() {
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJsonUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJsonUtils.java
new file mode 100644
index 000000000..fca332c91
--- /dev/null
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJsonUtils.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.foundation.common.utils;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.time.Duration;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.Period;
+import java.time.ZoneOffset;
+
+public class TestJsonUtils {
+  @Test
+  public void testJSR310() throws Exception {
+    String period = "\"P1Y1M2D\"";
+    String duration = "\"P1DT1H2M3S\"";
+    String offsetDateTime = "\"2018-05-28T11:00:00+08:00\"";
+    String localDate = "\"2018-05-28\"";
+    String localDateTime = "\"2018-05-28T11:00:00\"";
+
+    Assert.assertEquals(
+        Period.of(1, 1, 2),
+        JsonUtils.OBJ_MAPPER.readValue(period, Period.class)
+    );
+    Assert.assertEquals(
+        Duration.ofDays(1).plusHours(1).plusMinutes(2).plusSeconds(3),
+        JsonUtils.OBJ_MAPPER.readValue(duration, Duration.class)
+    );
+    Assert.assertEquals(
+        OffsetDateTime.of(2018, 5, 28, 3, 0, 0, 0, ZoneOffset.ofHours(0)),
+        JsonUtils.OBJ_MAPPER.readValue(offsetDateTime, OffsetDateTime.class)
+    );
+    Assert.assertEquals(
+        LocalDate.of(2018, 5, 28),
+        JsonUtils.OBJ_MAPPER.readValue(localDate, LocalDate.class)
+    );
+    Assert.assertEquals(
+        LocalDateTime.of(2018, 5, 28, 11, 0, 0),
+        JsonUtils.OBJ_MAPPER.readValue(localDateTime, LocalDateTime.class)
+    );
+  }
+}
diff --git a/java-chassis-dependencies/pom.xml b/java-chassis-dependencies/pom.xml
index e303cce34..124b8fa81 100644
--- a/java-chassis-dependencies/pom.xml
+++ b/java-chassis-dependencies/pom.xml
@@ -800,6 +800,11 @@
         <artifactId>jackson-datatype-joda</artifactId>
         <version>${jackson.version}</version>
       </dependency>
+      <dependency>
+        <groupId>com.fasterxml.jackson.datatype</groupId>
+        <artifactId>jackson-datatype-jsr310</artifactId>
+        <version>${jackson.version}</version>
+      </dependency>
       <dependency>
         <groupId>io.protostuff</groupId>
         <artifactId>protostuff-core</artifactId>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services