Skip to content

Commit 201795b

Browse files
tswstarplanetralf0131
authored andcommitted
[Dubbo-3707] Merge dubbo-serialization-gson into incubator-dubbo (#3714)
1 parent 1706807 commit 201795b

File tree

14 files changed

+673
-0
lines changed

14 files changed

+673
-0
lines changed

dubbo-all/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,13 @@
352352
<scope>compile</scope>
353353
<optional>true</optional>
354354
</dependency>
355+
<dependency>
356+
<groupId>org.apache.dubbo</groupId>
357+
<artifactId>dubbo-serialization-gson</artifactId>
358+
<version>${project.version}</version>
359+
<scope>compile</scope>
360+
<optional>true</optional>
361+
</dependency>
355362
<dependency>
356363
<groupId>org.apache.dubbo</groupId>
357364
<artifactId>dubbo-configcenter-api</artifactId>
@@ -540,6 +547,7 @@
540547
<include>org.apache.dubbo:dubbo-serialization-avro</include>
541548
<include>org.apache.dubbo:dubbo-serialization-jdk</include>
542549
<include>org.apache.dubbo:dubbo-serialization-protostuff</include>
550+
<include>org.apache.dubbo:dubbo-serialization-gson</include>
543551
<include>org.apache.dubbo:dubbo-configcenter-api</include>
544552
<include>org.apache.dubbo:dubbo-configcenter-definition</include>
545553
<include>org.apache.dubbo:dubbo-configcenter-apollo</include>

dubbo-bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@
322322
<artifactId>dubbo-serialization-avro</artifactId>
323323
<version>${project.version}</version>
324324
</dependency>
325+
<dependency>
326+
<groupId>org.apache.dubbo</groupId>
327+
<artifactId>dubbo-serialization-gson</artifactId>
328+
<version>${project.version}</version>
329+
</dependency>
325330
<dependency>
326331
<groupId>org.apache.dubbo</groupId>
327332
<artifactId>dubbo-compatible</artifactId>

dubbo-distribution/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@
250250
<artifactId>dubbo-serialization-avro</artifactId>
251251
<version>${project.version}</version>
252252
</dependency>
253+
<dependency>
254+
<groupId>org.apache.dubbo</groupId>
255+
<artifactId>dubbo-serialization-gson</artifactId>
256+
<version>${project.version}</version>
257+
</dependency>
253258
<dependency>
254259
<groupId>org.apache.dubbo</groupId>
255260
<artifactId>dubbo</artifactId>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
<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">
18+
<modelVersion>4.0.0</modelVersion>
19+
<parent>
20+
<artifactId>dubbo-serialization</artifactId>
21+
<groupId>org.apache.dubbo</groupId>
22+
<version>2.7.2-SNAPSHOT</version>
23+
</parent>
24+
<artifactId>dubbo-serialization-gson</artifactId>
25+
<packaging>jar</packaging>
26+
<name>${project.artifactId}</name>
27+
<description>The GSON serialization implement for dubbo</description>
28+
<properties>
29+
<skip_maven_deploy>false</skip_maven_deploy>
30+
</properties>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.apache.dubbo</groupId>
34+
<artifactId>dubbo-serialization-api</artifactId>
35+
<version>${project.parent.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.google.code.gson</groupId>
39+
<artifactId>gson</artifactId>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
18+
package org.apache.dubbo.common.serialize.gson;
19+
20+
import com.google.gson.Gson;
21+
import org.apache.dubbo.common.serialize.ObjectInput;
22+
import org.apache.dubbo.common.utils.PojoUtils;
23+
24+
import java.io.BufferedReader;
25+
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.lang.reflect.Type;
28+
import java.io.Reader;
29+
import java.io.IOException;
30+
import java.io.EOFException;
31+
32+
public class GsonJsonObjectInput implements ObjectInput {
33+
private final BufferedReader reader;
34+
private Gson gson;
35+
36+
public GsonJsonObjectInput(InputStream in) {
37+
this(new InputStreamReader(in));
38+
}
39+
40+
public GsonJsonObjectInput(Reader reader) {
41+
this.reader = new BufferedReader(reader);
42+
gson = new Gson();
43+
}
44+
45+
@Override
46+
public boolean readBool() throws IOException {
47+
return read(boolean.class);
48+
}
49+
50+
@Override
51+
public byte readByte() throws IOException {
52+
return read(byte.class);
53+
}
54+
55+
@Override
56+
public short readShort() throws IOException {
57+
return read(short.class);
58+
}
59+
60+
@Override
61+
public int readInt() throws IOException {
62+
return read(int.class);
63+
}
64+
65+
@Override
66+
public long readLong() throws IOException {
67+
return read(long.class);
68+
}
69+
70+
@Override
71+
public float readFloat() throws IOException {
72+
return read(float.class);
73+
}
74+
75+
@Override
76+
public double readDouble() throws IOException {
77+
return read(double.class);
78+
}
79+
80+
@Override
81+
public String readUTF() throws IOException {
82+
return read(String.class);
83+
}
84+
85+
@Override
86+
public byte[] readBytes() throws IOException {
87+
return readLine().getBytes();
88+
}
89+
90+
@Override
91+
public Object readObject() throws IOException, ClassNotFoundException {
92+
String json = readLine();
93+
return gson.fromJson(json, String.class);
94+
}
95+
96+
@Override
97+
public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException {
98+
return read(cls);
99+
}
100+
101+
@Override
102+
@SuppressWarnings("unchecked")
103+
public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
104+
Object value = readObject(cls);
105+
return (T) PojoUtils.realize(value, cls, type);
106+
}
107+
108+
private String readLine() throws IOException {
109+
String line = reader.readLine();
110+
if (line == null || line.trim().length() == 0) throw new EOFException();
111+
return line;
112+
}
113+
114+
private <T> T read(Class<T> cls) throws IOException {
115+
String json = readLine();
116+
return gson.fromJson(json, cls);
117+
}
118+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
18+
package org.apache.dubbo.common.serialize.gson;
19+
20+
import com.google.gson.Gson;
21+
import org.apache.dubbo.common.serialize.ObjectOutput;
22+
23+
import java.io.OutputStream;
24+
import java.io.OutputStreamWriter;
25+
import java.io.PrintWriter;
26+
import java.io.Writer;
27+
import java.io.IOException;
28+
29+
30+
public class GsonJsonObjectOutput implements ObjectOutput {
31+
32+
private final PrintWriter writer;
33+
private Gson gson = null;
34+
35+
public GsonJsonObjectOutput(OutputStream out) {
36+
this(new OutputStreamWriter(out));
37+
}
38+
39+
public GsonJsonObjectOutput(Writer writer) {
40+
this.gson = new Gson();
41+
this.writer = new PrintWriter(writer);
42+
}
43+
44+
@Override
45+
public void writeBool(boolean v) throws IOException {
46+
writeObject(v);
47+
}
48+
49+
@Override
50+
public void writeByte(byte v) throws IOException {
51+
writeObject(v);
52+
}
53+
54+
@Override
55+
public void writeShort(short v) throws IOException {
56+
writeObject(v);
57+
}
58+
59+
@Override
60+
public void writeInt(int v) throws IOException {
61+
writeObject(v);
62+
}
63+
64+
@Override
65+
public void writeLong(long v) throws IOException {
66+
writeObject(v);
67+
}
68+
69+
@Override
70+
public void writeFloat(float v) throws IOException {
71+
writeObject(v);
72+
}
73+
74+
@Override
75+
public void writeDouble(double v) throws IOException {
76+
writeObject(v);
77+
}
78+
79+
@Override
80+
public void writeUTF(String v) throws IOException {
81+
writeObject(v);
82+
}
83+
84+
@Override
85+
public void writeBytes(byte[] b) throws IOException {
86+
writer.println(new String(b));
87+
}
88+
89+
@Override
90+
public void writeBytes(byte[] b, int off, int len) throws IOException {
91+
writer.println(new String(b, off, len));
92+
}
93+
94+
@Override
95+
public void writeObject(Object obj) throws IOException {
96+
char[] json = gson.toJson(obj).toCharArray();
97+
writer.write(json, 0, json.length);
98+
writer.println();
99+
writer.flush();
100+
json = null;
101+
}
102+
103+
@Override
104+
public void flushBuffer() throws IOException {
105+
writer.flush();
106+
}
107+
108+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
18+
package org.apache.dubbo.common.serialize.gson;
19+
20+
import org.apache.dubbo.common.URL;
21+
import org.apache.dubbo.common.serialize.ObjectInput;
22+
import org.apache.dubbo.common.serialize.ObjectOutput;
23+
import org.apache.dubbo.common.serialize.Serialization;
24+
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
28+
29+
public class GsonSerialization implements Serialization {
30+
31+
32+
@Override
33+
public byte getContentTypeId() {
34+
return 16;
35+
}
36+
37+
@Override
38+
public String getContentType() {
39+
return "text/json";
40+
}
41+
42+
@Override
43+
public ObjectOutput serialize(URL url, OutputStream output) throws IOException {
44+
return new GsonJsonObjectOutput(output);
45+
}
46+
47+
@Override
48+
public ObjectInput deserialize(URL url, InputStream input) throws IOException {
49+
return new GsonJsonObjectInput(input);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gson=org.apache.dubbo.common.serialize.gson.GsonSerialization

0 commit comments

Comments
 (0)