You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by hu...@apache.org on 2019/04/16 02:47:01 UTC

[incubator-dubbo] branch master updated: [Dubbo-3707] Merge dubbo-serialization-gson into incubator-dubbo (#3714)

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

huxing 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 201795b  [Dubbo-3707] Merge dubbo-serialization-gson into incubator-dubbo (#3714)
201795b is described below

commit 201795bd5c4dcc5e711c4c129029cb6339fb56b7
Author: Taosheng Wei <ts...@apache.org>
AuthorDate: Tue Apr 16 10:46:39 2019 +0800

    [Dubbo-3707] Merge dubbo-serialization-gson into incubator-dubbo (#3714)
---
 dubbo-all/pom.xml                                  |   8 ++
 dubbo-bom/pom.xml                                  |   5 +
 dubbo-distribution/pom.xml                         |   5 +
 .../{ => dubbo-serialization-gson}/pom.xml         |  38 +++---
 .../common/serialize/gson/GsonJsonObjectInput.java | 118 +++++++++++++++++
 .../serialize/gson/GsonJsonObjectOutput.java       | 108 ++++++++++++++++
 .../common/serialize/gson/GsonSerialization.java   |  51 ++++++++
 ...org.apache.dubbo.common.serialize.Serialization |   1 +
 .../serialize/gson/GsonJsonObjectOutputTest.java   | 141 +++++++++++++++++++++
 .../serialize/gson/GsonJsonSerializationTest.java  |  64 ++++++++++
 .../apache/dubbo/common/serialize/gson/Image.java  | 120 ++++++++++++++++++
 .../dubbo-serialization-test/pom.xml               |   5 +
 dubbo-serialization/pom.xml                        |   1 +
 dubbo-test/pom.xml                                 |   4 +
 14 files changed, 650 insertions(+), 19 deletions(-)

diff --git a/dubbo-all/pom.xml b/dubbo-all/pom.xml
index 31b175a..e0e2366 100644
--- a/dubbo-all/pom.xml
+++ b/dubbo-all/pom.xml
@@ -354,6 +354,13 @@
         </dependency>
         <dependency>
             <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-serialization-gson</artifactId>
+            <version>${project.version}</version>
+            <scope>compile</scope>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
             <artifactId>dubbo-configcenter-api</artifactId>
             <version>${project.version}</version>
             <scope>compile</scope>
@@ -540,6 +547,7 @@
                                     <include>org.apache.dubbo:dubbo-serialization-avro</include>
                                     <include>org.apache.dubbo:dubbo-serialization-jdk</include>
                                     <include>org.apache.dubbo:dubbo-serialization-protostuff</include>
+                                    <include>org.apache.dubbo:dubbo-serialization-gson</include>
                                     <include>org.apache.dubbo:dubbo-configcenter-api</include>
                                     <include>org.apache.dubbo:dubbo-configcenter-definition</include>
                                     <include>org.apache.dubbo:dubbo-configcenter-apollo</include>
diff --git a/dubbo-bom/pom.xml b/dubbo-bom/pom.xml
index 2359319..e894f8b 100644
--- a/dubbo-bom/pom.xml
+++ b/dubbo-bom/pom.xml
@@ -324,6 +324,11 @@
             </dependency>
             <dependency>
                 <groupId>org.apache.dubbo</groupId>
+                <artifactId>dubbo-serialization-gson</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.dubbo</groupId>
                 <artifactId>dubbo-compatible</artifactId>
                 <version>${project.version}</version>
             </dependency>
diff --git a/dubbo-distribution/pom.xml b/dubbo-distribution/pom.xml
index 51f486e..50fd855 100644
--- a/dubbo-distribution/pom.xml
+++ b/dubbo-distribution/pom.xml
@@ -252,6 +252,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-serialization-gson</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
             <artifactId>dubbo</artifactId>
             <version>${project.version}</version>
         </dependency>
diff --git a/dubbo-serialization/pom.xml b/dubbo-serialization/dubbo-serialization-gson/pom.xml
similarity index 55%
copy from dubbo-serialization/pom.xml
copy to dubbo-serialization/dubbo-serialization-gson/pom.xml
index 1c6de9d..58677c7 100644
--- a/dubbo-serialization/pom.xml
+++ b/dubbo-serialization/dubbo-serialization-gson/pom.xml
@@ -1,5 +1,5 @@
 <!--
-  Licensed to the Apache Software Foundation (ASF) under one or more
+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
@@ -14,29 +14,29 @@
   See the License for the specific language governing permissions and
   limitations under the License.
   -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
+        <artifactId>dubbo-serialization</artifactId>
         <groupId>org.apache.dubbo</groupId>
-        <artifactId>dubbo-parent</artifactId>
-        <version>${revision}</version>
+        <version>2.7.2-SNAPSHOT</version>
     </parent>
-    <artifactId>dubbo-serialization</artifactId>
-    <packaging>pom</packaging>
+    <artifactId>dubbo-serialization-gson</artifactId>
+    <packaging>jar</packaging>
     <name>${project.artifactId}</name>
-    <description>The serialization module of dubbo project</description>
+    <description>The GSON serialization implement for dubbo</description>
     <properties>
         <skip_maven_deploy>false</skip_maven_deploy>
     </properties>
-    <modules>
-        <module>dubbo-serialization-api</module>
-        <module>dubbo-serialization-hessian2</module>
-        <module>dubbo-serialization-fastjson</module>
-        <module>dubbo-serialization-kryo</module>
-        <module>dubbo-serialization-fst</module>
-        <module>dubbo-serialization-jdk</module>
-        <module>dubbo-serialization-protostuff</module>
-        <module>dubbo-serialization-avro</module>
-        <module>dubbo-serialization-test</module>
-    </modules>
-</project>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-serialization-api</artifactId>
+            <version>${project.parent.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectInput.java b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectInput.java
new file mode 100644
index 0000000..94014f2
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectInput.java
@@ -0,0 +1,118 @@
+/*
+ * 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.gson;
+
+import com.google.gson.Gson;
+import org.apache.dubbo.common.serialize.ObjectInput;
+import org.apache.dubbo.common.utils.PojoUtils;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Type;
+import java.io.Reader;
+import java.io.IOException;
+import java.io.EOFException;
+
+public class GsonJsonObjectInput implements ObjectInput {
+    private final BufferedReader reader;
+    private Gson gson;
+
+    public GsonJsonObjectInput(InputStream in) {
+        this(new InputStreamReader(in));
+    }
+
+    public GsonJsonObjectInput(Reader reader) {
+        this.reader = new BufferedReader(reader);
+        gson = new Gson();
+    }
+
+    @Override
+    public boolean readBool() throws IOException {
+        return read(boolean.class);
+    }
+
+    @Override
+    public byte readByte() throws IOException {
+        return read(byte.class);
+    }
+
+    @Override
+    public short readShort() throws IOException {
+        return read(short.class);
+    }
+
+    @Override
+    public int readInt() throws IOException {
+        return read(int.class);
+    }
+
+    @Override
+    public long readLong() throws IOException {
+        return read(long.class);
+    }
+
+    @Override
+    public float readFloat() throws IOException {
+        return read(float.class);
+    }
+
+    @Override
+    public double readDouble() throws IOException {
+        return read(double.class);
+    }
+
+    @Override
+    public String readUTF() throws IOException {
+        return read(String.class);
+    }
+
+    @Override
+    public byte[] readBytes() throws IOException {
+        return readLine().getBytes();
+    }
+
+    @Override
+    public Object readObject() throws IOException, ClassNotFoundException {
+        String json = readLine();
+        return gson.fromJson(json, String.class);
+    }
+
+    @Override
+    public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException {
+        return read(cls);
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
+        Object value = readObject(cls);
+        return (T) PojoUtils.realize(value, cls, type);
+    }
+
+    private String readLine() throws IOException {
+        String line = reader.readLine();
+        if (line == null || line.trim().length() == 0) throw new EOFException();
+        return line;
+    }
+
+    private <T> T read(Class<T> cls) throws IOException {
+        String json = readLine();
+        return gson.fromJson(json, cls);
+    }
+}
diff --git a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutput.java b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutput.java
new file mode 100644
index 0000000..41155da
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutput.java
@@ -0,0 +1,108 @@
+/*
+ * 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.gson;
+
+import com.google.gson.Gson;
+import org.apache.dubbo.common.serialize.ObjectOutput;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.io.IOException;
+
+
+public class GsonJsonObjectOutput implements ObjectOutput {
+
+    private final PrintWriter writer;
+    private Gson gson = null;
+
+    public GsonJsonObjectOutput(OutputStream out) {
+        this(new OutputStreamWriter(out));
+    }
+
+    public GsonJsonObjectOutput(Writer writer) {
+        this.gson = new Gson();
+        this.writer = new PrintWriter(writer);
+    }
+
+    @Override
+    public void writeBool(boolean v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeByte(byte v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeShort(short v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeInt(int v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeLong(long v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeFloat(float v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeDouble(double v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeUTF(String v) throws IOException {
+        writeObject(v);
+    }
+
+    @Override
+    public void writeBytes(byte[] b) throws IOException {
+        writer.println(new String(b));
+    }
+
+    @Override
+    public void writeBytes(byte[] b, int off, int len) throws IOException {
+        writer.println(new String(b, off, len));
+    }
+
+    @Override
+    public void writeObject(Object obj) throws IOException {
+        char[] json = gson.toJson(obj).toCharArray();
+        writer.write(json, 0, json.length);
+        writer.println();
+        writer.flush();
+        json = null;
+    }
+
+    @Override
+    public void flushBuffer() throws IOException {
+        writer.flush();
+    }
+
+}
diff --git a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonSerialization.java b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonSerialization.java
new file mode 100644
index 0000000..eaa8f37
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonSerialization.java
@@ -0,0 +1,51 @@
+/*
+ * 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.gson;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.serialize.ObjectInput;
+import org.apache.dubbo.common.serialize.ObjectOutput;
+import org.apache.dubbo.common.serialize.Serialization;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class GsonSerialization implements Serialization {
+
+
+    @Override
+    public byte getContentTypeId() {
+        return 16;
+    }
+
+    @Override
+    public String getContentType() {
+        return "text/json";
+    }
+
+    @Override
+    public ObjectOutput serialize(URL url, OutputStream output) throws IOException {
+        return new GsonJsonObjectOutput(output);
+    }
+
+    @Override
+    public ObjectInput deserialize(URL url, InputStream input) throws IOException {
+        return new GsonJsonObjectInput(input);
+    }
+}
diff --git a/dubbo-serialization/dubbo-serialization-gson/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization b/dubbo-serialization/dubbo-serialization-gson/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization
new file mode 100644
index 0000000..f0cd8df
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-gson/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization
@@ -0,0 +1 @@
+gson=org.apache.dubbo.common.serialize.gson.GsonSerialization
diff --git a/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutputTest.java b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutputTest.java
new file mode 100644
index 0000000..25fb639
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutputTest.java
@@ -0,0 +1,141 @@
+/*
+ * 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.gson;
+
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class GsonJsonObjectOutputTest {
+    private GsonJsonObjectOutput gsonJsonObjectOutput;
+    private GsonJsonObjectInput gsonJsonObjectInput;
+    private ByteArrayOutputStream byteArrayOutputStream;
+    private ByteArrayInputStream byteArrayInputStream;
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        this.byteArrayOutputStream = new ByteArrayOutputStream();
+        this.gsonJsonObjectOutput = new GsonJsonObjectOutput(byteArrayOutputStream);
+    }
+
+    @Test
+    public void testWriteBool() throws IOException {
+        this.gsonJsonObjectOutput.writeBool(true);
+        this.flushToInput();
+
+        assertThat(gsonJsonObjectInput.readBool(), is(true));
+    }
+
+    @Test
+    public void testWriteShort() throws IOException {
+        this.gsonJsonObjectOutput.writeShort((short) 2);
+        this.flushToInput();
+
+        assertThat(gsonJsonObjectInput.readShort(), is((short) 2));
+    }
+
+    @Test
+    public void testWriteInt() throws IOException {
+        this.gsonJsonObjectOutput.writeInt(1);
+        this.flushToInput();
+
+        assertThat(gsonJsonObjectInput.readInt(), is(1));
+    }
+
+    @Test
+    public void testWriteLong() throws IOException {
+        this.gsonJsonObjectOutput.writeLong(1000L);
+        this.flushToInput();
+
+        assertThat(gsonJsonObjectInput.readLong(), is(1000L));
+    }
+
+    @Test
+    public void testWriteUTF() throws IOException {
+        this.gsonJsonObjectOutput.writeUTF("Pace Hasîtî 和平 Мир");
+        this.flushToInput();
+
+        assertThat(gsonJsonObjectInput.readUTF(), is("Pace Hasîtî 和平 Мир"));
+    }
+
+
+    @Test
+    public void testWriteFloat() throws IOException {
+        this.gsonJsonObjectOutput.writeFloat(1.88f);
+        this.flushToInput();
+
+        assertThat(this.gsonJsonObjectInput.readFloat(), is(1.88f));
+    }
+
+    @Test
+    public void testWriteDouble() throws IOException {
+        this.gsonJsonObjectOutput.writeDouble(1.66d);
+        this.flushToInput();
+
+        assertThat(this.gsonJsonObjectInput.readDouble(), is(1.66d));
+    }
+
+    @Test
+    public void testWriteBytes() throws IOException {
+        this.gsonJsonObjectOutput.writeBytes("hello".getBytes());
+        this.flushToInput();
+
+        assertThat(this.gsonJsonObjectInput.readBytes(), is("hello".getBytes()));
+    }
+
+    @Test
+    public void testWriteBytesWithSubLength() throws IOException {
+        this.gsonJsonObjectOutput.writeBytes("hello".getBytes(), 2, 2);
+        this.flushToInput();
+
+        assertThat(this.gsonJsonObjectInput.readBytes(), is("ll".getBytes()));
+    }
+
+    @Test
+    public void testWriteByte() throws IOException {
+        this.gsonJsonObjectOutput.writeByte((byte) 123);
+        this.flushToInput();
+
+        assertThat(this.gsonJsonObjectInput.readByte(), is((byte) 123));
+    }
+
+    @Test
+    public void testWriteObject() throws IOException, ClassNotFoundException {
+        Image image = new Image("http://dubbo.io/logo.png", "logo", 300, 480, Image.Size.SMALL);
+        this.gsonJsonObjectOutput.writeObject(image);
+        this.flushToInput();
+
+        Image readObjectForImage = gsonJsonObjectInput.readObject(Image.class);
+        assertThat(readObjectForImage, not(nullValue()));
+        assertThat(readObjectForImage, is(image));
+    }
+
+    private void flushToInput() throws IOException {
+        this.gsonJsonObjectOutput.flushBuffer();
+        this.byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
+        this.gsonJsonObjectInput = new GsonJsonObjectInput(byteArrayInputStream);
+    }
+}
diff --git a/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonSerializationTest.java b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonSerializationTest.java
new file mode 100644
index 0000000..51fca3d
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonSerializationTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.gson;
+
+import org.apache.dubbo.common.serialize.ObjectInput;
+import org.apache.dubbo.common.serialize.ObjectOutput;
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+
+public class GsonJsonSerializationTest {
+    private GsonSerialization gsonJsonSerialization;
+
+    @BeforeEach
+    public void setUp() {
+        this.gsonJsonSerialization = new GsonSerialization();
+    }
+
+    @Test
+    public void testContentType() {
+        assertThat(gsonJsonSerialization.getContentType(), is("text/json"));
+    }
+
+    @Test
+    public void testContentTypeId() {
+        assertThat(gsonJsonSerialization.getContentTypeId(), is((byte) 16));
+    }
+
+    @Test
+    public void testObjectOutput() throws IOException {
+        ObjectOutput objectOutput = gsonJsonSerialization.serialize(null, mock(OutputStream.class));
+        assertThat(objectOutput, Matchers.<ObjectOutput>instanceOf(GsonJsonObjectOutput.class));
+    }
+
+    @Test
+    public void testObjectInput() throws IOException {
+        ObjectInput objectInput = gsonJsonSerialization.deserialize(null, mock(InputStream.class));
+        assertThat(objectInput, Matchers.<ObjectInput>instanceOf(GsonJsonObjectInput.class));
+    }
+}
diff --git a/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/Image.java b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/Image.java
new file mode 100644
index 0000000..d1316d3
--- /dev/null
+++ b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/Image.java
@@ -0,0 +1,120 @@
+/*
+ * 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.gson;
+
+
+public class Image implements java.io.Serializable {
+    private static final long serialVersionUID = 1L;
+    public String uri;
+    public String title;  // Can be null
+    public int width;
+    public int height;
+    public Size size;
+
+    public Image() {
+    }
+
+    public Image(String uri, String title, int width, int height, Size size) {
+        this.height = height;
+        this.title = title;
+        this.uri = uri;
+        this.width = width;
+        this.size = size;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Image image = (Image) o;
+
+        if (height != image.height) return false;
+        if (width != image.width) return false;
+        if (size != image.size) return false;
+        if (title != null ? !title.equals(image.title) : image.title != null) return false;
+        if (uri != null ? !uri.equals(image.uri) : image.uri != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = uri != null ? uri.hashCode() : 0;
+        result = 31 * result + (title != null ? title.hashCode() : 0);
+        result = 31 * result + width;
+        result = 31 * result + height;
+        result = 31 * result + (size != null ? size.hashCode() : 0);
+        return result;
+    }
+
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append("[Image ");
+        sb.append("uri=").append(uri);
+        sb.append(", title=").append(title);
+        sb.append(", width=").append(width);
+        sb.append(", height=").append(height);
+        sb.append(", size=").append(size);
+        sb.append("]");
+        return sb.toString();
+    }
+
+    public String getUri() {
+        return uri;
+    }
+
+    public void setUri(String uri) {
+        this.uri = uri;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public int getWidth() {
+        return width;
+    }
+
+    public void setWidth(int width) {
+        this.width = width;
+    }
+
+    public int getHeight() {
+        return height;
+    }
+
+    public void setHeight(int height) {
+        this.height = height;
+    }
+
+    public Size getSize() {
+        return size;
+    }
+
+    public void setSize(Size size) {
+        this.size = size;
+    }
+
+    public enum Size {
+        SMALL, LARGE
+    }
+}
diff --git a/dubbo-serialization/dubbo-serialization-test/pom.xml b/dubbo-serialization/dubbo-serialization-test/pom.xml
index 2c44dbc..868b45b 100644
--- a/dubbo-serialization/dubbo-serialization-test/pom.xml
+++ b/dubbo-serialization/dubbo-serialization-test/pom.xml
@@ -64,6 +64,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-serialization-gson</artifactId>
+            <version>${project.parent.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
             <artifactId>dubbo-serialization-api</artifactId>
             <version>${project.parent.version}</version>
         </dependency>
diff --git a/dubbo-serialization/pom.xml b/dubbo-serialization/pom.xml
index 1c6de9d..97b986d 100644
--- a/dubbo-serialization/pom.xml
+++ b/dubbo-serialization/pom.xml
@@ -38,5 +38,6 @@
         <module>dubbo-serialization-protostuff</module>
         <module>dubbo-serialization-avro</module>
         <module>dubbo-serialization-test</module>
+        <module>dubbo-serialization-gson</module>
     </modules>
 </project>
diff --git a/dubbo-test/pom.xml b/dubbo-test/pom.xml
index 5c950ad..4c8e19d 100644
--- a/dubbo-test/pom.xml
+++ b/dubbo-test/pom.xml
@@ -200,6 +200,10 @@
         </dependency>
         <dependency>
             <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-serialization-gson</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
             <artifactId>dubbo-serialization-kryo</artifactId>
         </dependency>
         <dependency>