You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by cr...@apache.org on 2019/04/24 07:08:14 UTC

[incubator-dubbo] branch master updated: [Dubbo-3914]Fix protostuff serialize java.sql.Timestamp (#3915)

This is an automated email from the ASF dual-hosted git repository.

crazyhzm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 9b11b7a  [Dubbo-3914]Fix protostuff serialize java.sql.Timestamp (#3915)
9b11b7a is described below

commit 9b11b7ac89c0d897220bc9b42b3e5e9c3eb708f2
Author: zhangfei <zh...@163.com>
AuthorDate: Wed Apr 24 15:08:04 2019 +0800

    [Dubbo-3914]Fix protostuff serialize java.sql.Timestamp (#3915)
    
    * fix: #3727
    
    * style: code tidy up
    
    * style: add apache license
    
    * fix: #3914 protostuff serialize java.sql.Timestamp
---
 .../protostuff/delegate/TimestampDelegate.java     | 57 ++++++++++++++++++++++
 .../serialize/protostuff/utils/WrapperUtils.java   |  4 ++
 .../protostuff/ProtostuffObjectOutputTest.java     | 12 +++++
 3 files changed, 73 insertions(+)

diff --git a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/delegate/TimestampDelegate.java b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/delegate/TimestampDelegate.java
new file mode 100644
index 0000000..083e69c
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/delegate/TimestampDelegate.java
@@ -0,0 +1,57 @@
+/*
+ * 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.dubbo.common.serialize.protostuff.delegate;
+
+import io.protostuff.Input;
+import io.protostuff.Output;
+import io.protostuff.Pipe;
+import io.protostuff.WireFormat.FieldType;
+import io.protostuff.runtime.Delegate;
+
+import java.io.IOException;
+import java.sql.Timestamp;
+
+/**
+ * Custom {@link Timestamp} delegate
+ */
+public class TimestampDelegate implements Delegate<Timestamp> {
+
+    @Override
+    public FieldType getFieldType() {
+        return FieldType.FIXED64;
+    }
+
+    @Override
+    public Timestamp readFrom(Input input) throws IOException {
+        return new Timestamp(input.readFixed64());
+    }
+
+    @Override
+    public void writeTo(Output output, int number, Timestamp value, boolean repeated) throws IOException {
+        output.writeFixed64(number, value.getTime(), repeated);
+    }
+
+    @Override
+    public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) throws IOException {
+        output.writeFixed64(number, input.readFixed64(), repeated);
+    }
+
+    @Override
+    public Class<?> typeClass() {
+        return Timestamp.class;
+    }
+}
diff --git a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/utils/WrapperUtils.java b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/utils/WrapperUtils.java
index 02ea4f0..9ebcc46 100644
--- a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/utils/WrapperUtils.java
+++ b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/utils/WrapperUtils.java
@@ -22,9 +22,11 @@ import org.apache.dubbo.common.serialize.protostuff.delegate.TimeDelegate;
 
 import io.protostuff.runtime.DefaultIdStrategy;
 import io.protostuff.runtime.RuntimeEnv;
+import org.apache.dubbo.common.serialize.protostuff.delegate.TimestampDelegate;
 
 import java.math.BigDecimal;
 import java.sql.Time;
+import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.BitSet;
 import java.util.Calendar;
@@ -52,6 +54,7 @@ public class WrapperUtils {
     static {
         if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) {
             ((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new TimeDelegate());
+            ((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new TimestampDelegate());
         }
 
         WRAPPER_SET.add(Map.class);
@@ -80,6 +83,7 @@ public class WrapperUtils {
         WRAPPER_SET.add(Date.class);
         WRAPPER_SET.add(Calendar.class);
         WRAPPER_SET.add(Time.class);
+        WRAPPER_SET.add(Timestamp.class);
 
         WRAPPER_SET.add(Wrapper.class);
 
diff --git a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java
index bb28386..39a621b 100644
--- a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java
+++ b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java
@@ -16,12 +16,14 @@
  */
 package org.apache.dubbo.common.serialize.protostuff;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.nullValue;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.sql.Timestamp;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -46,6 +48,16 @@ public class ProtostuffObjectOutputTest {
         assertThat(protostuffObjectInput.readObject(), nullValue());
     }
 
+    @Test
+    public void testSerializeTimestamp() throws IOException, ClassNotFoundException {
+        Timestamp originTime = new Timestamp(System.currentTimeMillis());
+        this.protostuffObjectOutput.writeObject(originTime);
+        this.flushToInput();
+
+        Timestamp serializedTime = protostuffObjectInput.readObject(Timestamp.class);
+        assertThat(serializedTime, is(originTime));
+    }
+
     private void flushToInput() throws IOException {
         this.protostuffObjectOutput.flushBuffer();
         this.byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());