Skip to content

Commit 9b11b7a

Browse files
felixz7CrazyHZM
authored andcommitted
[Dubbo-3914]Fix protostuff serialize java.sql.Timestamp (apache#3915)
* fix: apache#3727 * style: code tidy up * style: add apache license * fix: apache#3914 protostuff serialize java.sql.Timestamp
1 parent ddcdb29 commit 9b11b7a

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.common.serialize.protostuff.delegate;
18+
19+
import io.protostuff.Input;
20+
import io.protostuff.Output;
21+
import io.protostuff.Pipe;
22+
import io.protostuff.WireFormat.FieldType;
23+
import io.protostuff.runtime.Delegate;
24+
25+
import java.io.IOException;
26+
import java.sql.Timestamp;
27+
28+
/**
29+
* Custom {@link Timestamp} delegate
30+
*/
31+
public class TimestampDelegate implements Delegate<Timestamp> {
32+
33+
@Override
34+
public FieldType getFieldType() {
35+
return FieldType.FIXED64;
36+
}
37+
38+
@Override
39+
public Timestamp readFrom(Input input) throws IOException {
40+
return new Timestamp(input.readFixed64());
41+
}
42+
43+
@Override
44+
public void writeTo(Output output, int number, Timestamp value, boolean repeated) throws IOException {
45+
output.writeFixed64(number, value.getTime(), repeated);
46+
}
47+
48+
@Override
49+
public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) throws IOException {
50+
output.writeFixed64(number, input.readFixed64(), repeated);
51+
}
52+
53+
@Override
54+
public Class<?> typeClass() {
55+
return Timestamp.class;
56+
}
57+
}

dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/utils/WrapperUtils.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222

2323
import io.protostuff.runtime.DefaultIdStrategy;
2424
import io.protostuff.runtime.RuntimeEnv;
25+
import org.apache.dubbo.common.serialize.protostuff.delegate.TimestampDelegate;
2526

2627
import java.math.BigDecimal;
2728
import java.sql.Time;
29+
import java.sql.Timestamp;
2830
import java.util.ArrayList;
2931
import java.util.BitSet;
3032
import java.util.Calendar;
@@ -52,6 +54,7 @@ public class WrapperUtils {
5254
static {
5355
if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) {
5456
((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new TimeDelegate());
57+
((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new TimestampDelegate());
5558
}
5659

5760
WRAPPER_SET.add(Map.class);
@@ -80,6 +83,7 @@ public class WrapperUtils {
8083
WRAPPER_SET.add(Date.class);
8184
WRAPPER_SET.add(Calendar.class);
8285
WRAPPER_SET.add(Time.class);
86+
WRAPPER_SET.add(Timestamp.class);
8387

8488
WRAPPER_SET.add(Wrapper.class);
8589

dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
*/
1717
package org.apache.dubbo.common.serialize.protostuff;
1818

19+
import static org.hamcrest.CoreMatchers.is;
1920
import static org.hamcrest.MatcherAssert.assertThat;
2021
import static org.hamcrest.Matchers.nullValue;
2122

2223
import java.io.ByteArrayInputStream;
2324
import java.io.ByteArrayOutputStream;
2425
import java.io.IOException;
26+
import java.sql.Timestamp;
2527
import org.junit.jupiter.api.BeforeEach;
2628
import org.junit.jupiter.api.Test;
2729

@@ -46,6 +48,16 @@ public void testWriteObjectNull() throws IOException, ClassNotFoundException {
4648
assertThat(protostuffObjectInput.readObject(), nullValue());
4749
}
4850

51+
@Test
52+
public void testSerializeTimestamp() throws IOException, ClassNotFoundException {
53+
Timestamp originTime = new Timestamp(System.currentTimeMillis());
54+
this.protostuffObjectOutput.writeObject(originTime);
55+
this.flushToInput();
56+
57+
Timestamp serializedTime = protostuffObjectInput.readObject(Timestamp.class);
58+
assertThat(serializedTime, is(originTime));
59+
}
60+
4961
private void flushToInput() throws IOException {
5062
this.protostuffObjectOutput.flushBuffer();
5163
this.byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

0 commit comments

Comments
 (0)