Skip to content

Commit 26a1928

Browse files
authored
Fix RuntimeTypeAdapterFactory depending on internal Streams class (#1959)
* Fix RuntimeTypeAdapterFactory depending on internal Streams class * Clean up gson-extras project, make it Maven module of gson-parent * Remove broken test from GraphAdapterBuilderTest
1 parent f1f9031 commit 26a1928

File tree

4 files changed

+55
-169
lines changed

4 files changed

+55
-169
lines changed

extras/pom.xml

Lines changed: 31 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
1-
<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">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
24
<modelVersion>4.0.0</modelVersion>
3-
<groupId>com.google.code.gson</groupId>
5+
<parent>
6+
<groupId>com.google.code.gson</groupId>
7+
<artifactId>gson-parent</artifactId>
8+
<version>2.8.9-SNAPSHOT</version>
9+
</parent>
10+
411
<artifactId>gson-extras</artifactId>
512
<packaging>jar</packaging>
613
<version>1.0-SNAPSHOT</version>
714
<inceptionYear>2008</inceptionYear>
815
<name>Gson Extras</name>
9-
<parent>
10-
<groupId>org.sonatype.oss</groupId>
11-
<artifactId>oss-parent</artifactId>
12-
<version>9</version>
13-
</parent>
14-
<url>http://code.google.com/p/google-gson/</url>
1516
<description>Google Gson grab bag of utilities, type adapters, etc.</description>
16-
<properties>
17-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18-
</properties>
17+
1918
<licenses>
2019
<license>
21-
<name>The Apache Software License, Version 2.0</name>
22-
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
23-
<distribution>repo</distribution>
20+
<name>Apache-2.0</name>
21+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
2422
</license>
2523
</licenses>
26-
<scm>
27-
<connection>scm:svn:http://google-gson.googlecode.com/svn/trunk/extras</connection>
28-
<developerConnection>scm:svn:https://google-gson.googlecode.com/svn/trunk/extras</developerConnection>
29-
<url>http://google-gson.codegoogle.com/svn/trunk/extras</url>
30-
</scm>
31-
<issueManagement>
32-
<system>Google Code Issue Tracking</system>
33-
<url>http://code.google.com/p/google-gson/issues/list</url>
34-
</issueManagement>
24+
3525
<organization>
3626
<name>Google, Inc.</name>
37-
<url>http://www.google.com</url>
27+
<url>https://www.google.com</url>
3828
</organization>
29+
3930
<dependencies>
4031
<dependency>
4132
<groupId>com.google.code.gson</groupId>
4233
<artifactId>gson</artifactId>
43-
<version>2.7</version>
44-
<scope>compile</scope>
34+
<version>${project.parent.version}</version>
4535
</dependency>
4636
<dependency>
4737
<groupId>javax.annotation</groupId>
@@ -51,130 +41,26 @@
5141
<dependency>
5242
<groupId>junit</groupId>
5343
<artifactId>junit</artifactId>
54-
<version>4.13.1</version>
5544
<scope>test</scope>
5645
</dependency>
5746
</dependencies>
58-
<profiles>
59-
<!-- Activate PGP signing only when performing a release -->
60-
<profile>
61-
<id>release-sign-artifacts</id>
62-
<activation>
63-
<property>
64-
<name>performRelease</name>
65-
<value>true</value>
66-
</property>
67-
</activation>
68-
<build>
69-
<plugins>
70-
<plugin>
71-
<groupId>org.apache.maven.plugins</groupId>
72-
<artifactId>maven-gpg-plugin</artifactId>
73-
<version>1.5</version>
74-
<executions>
75-
<execution>
76-
<id>sign-artifacts</id>
77-
<phase>verify</phase>
78-
<goals>
79-
<goal>sign</goal>
80-
</goals>
81-
</execution>
82-
</executions>
83-
</plugin>
84-
</plugins>
85-
</build>
86-
</profile>
87-
</profiles>
47+
8848
<build>
89-
<defaultGoal>package</defaultGoal>
90-
<plugins>
91-
<plugin>
92-
<groupId>org.apache.maven.plugins</groupId>
93-
<artifactId>maven-compiler-plugin</artifactId>
94-
<version>3.5.1</version>
95-
<configuration>
96-
<source>1.6</source>
97-
<target>1.6</target>
98-
</configuration>
99-
</plugin>
100-
<plugin>
101-
<groupId>org.apache.maven.plugins</groupId>
102-
<artifactId>maven-jar-plugin</artifactId>
103-
<version>3.0.2</version>
104-
<executions>
105-
<execution>
106-
<phase>package</phase>
107-
<goals>
108-
<goal>jar</goal>
109-
</goals>
110-
</execution>
111-
</executions>
112-
<configuration>
113-
<archive>
114-
<addMavenDescriptor>false</addMavenDescriptor>
115-
</archive>
116-
</configuration>
117-
</plugin>
118-
<plugin>
119-
<groupId>org.apache.maven.plugins</groupId>
120-
<artifactId>maven-source-plugin</artifactId>
121-
<version>3.0.1</version>
122-
<executions>
123-
<execution>
124-
<id>attach-sources</id>
125-
<phase>verify</phase>
126-
<goals>
127-
<goal>jar</goal>
128-
</goals>
129-
</execution>
130-
</executions>
131-
</plugin>
132-
<plugin>
133-
<groupId>org.apache.maven.plugins</groupId>
134-
<artifactId>maven-javadoc-plugin</artifactId>
135-
<version>2.10.4</version>
136-
<executions>
137-
<execution>
138-
<id>attach-javadocs</id>
139-
<goals>
140-
<goal>jar</goal>
141-
</goals>
142-
</execution>
143-
</executions>
144-
<configuration>
145-
<links>
146-
<link>http://download.oracle.com/javase/1.5.0/docs/api/</link>
147-
</links>
148-
<version>true</version>
149-
<show>public</show>
150-
</configuration>
151-
</plugin>
152-
<plugin>
153-
<groupId>org.apache.maven.plugins</groupId>
154-
<artifactId>maven-eclipse-plugin</artifactId>
155-
<version>2.10</version>
156-
<configuration>
157-
<downloadSources>true</downloadSources>
158-
<downloadJavadocs>true</downloadJavadocs>
159-
<workspace>
160-
../eclipse-ws/
161-
</workspace>
162-
<workspaceCodeStylesURL>
163-
file:///${basedir}/../lib/gson-formatting-styles.xml
164-
</workspaceCodeStylesURL>
165-
</configuration>
166-
</plugin>
167-
<plugin>
168-
<groupId>org.apache.maven.plugins</groupId>
169-
<artifactId>maven-release-plugin</artifactId>
170-
<!-- version>2.3.2</version -->
171-
<configuration>
172-
<arguments>-DenableCiProfile=true</arguments>
173-
<tagBase>https://google-gson.googlecode.com/svn/tags/</tagBase>
174-
</configuration>
175-
</plugin>
176-
</plugins>
49+
<pluginManagement>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-deploy-plugin</artifactId>
54+
<version>3.0.0-M1</version>
55+
<configuration>
56+
<!-- Currently not deployed -->
57+
<skip>true</skip>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</pluginManagement>
17762
</build>
63+
17864
<developers>
17965
<developer>
18066
<name>Inderjeet Singh</name>

extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.google.gson.JsonPrimitive;
2828
import com.google.gson.TypeAdapter;
2929
import com.google.gson.TypeAdapterFactory;
30-
import com.google.gson.internal.Streams;
3130
import com.google.gson.reflect.TypeToken;
3231
import com.google.gson.stream.JsonReader;
3332
import com.google.gson.stream.JsonWriter;
@@ -204,11 +203,13 @@ public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
204203
return registerSubtype(type, type.getSimpleName());
205204
}
206205

206+
@Override
207207
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
208208
if (type.getRawType() != baseType) {
209209
return null;
210210
}
211211

212+
final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
212213
final Map<String, TypeAdapter<?>> labelToDelegate
213214
= new LinkedHashMap<String, TypeAdapter<?>>();
214215
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
@@ -221,7 +222,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
221222

222223
return new TypeAdapter<R>() {
223224
@Override public R read(JsonReader in) throws IOException {
224-
JsonElement jsonElement = Streams.parse(in);
225+
JsonElement jsonElement = jsonElementAdapter.read(in);
225226
JsonElement labelJsonElement;
226227
if (maintainType) {
227228
labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
@@ -255,7 +256,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
255256
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
256257

257258
if (maintainType) {
258-
Streams.write(jsonObject, out);
259+
jsonElementAdapter.write(out, jsonObject);
259260
return;
260261
}
261262

@@ -270,7 +271,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
270271
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
271272
clone.add(e.getKey(), e.getValue());
272273
}
273-
Streams.write(clone, out);
274+
jsonElementAdapter.write(out, clone);
274275
}
275276
}.nullSafe();
276277
}

extras/src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616

1717
package com.google.gson.graph;
1818

19-
import com.google.gson.Gson;
20-
import com.google.gson.GsonBuilder;
21-
import com.google.gson.reflect.TypeToken;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertSame;
21+
2222
import java.lang.reflect.Type;
2323
import java.util.ArrayList;
2424
import java.util.Collections;
2525
import java.util.List;
26-
import junit.framework.TestCase;
2726

28-
public final class GraphAdapterBuilderTest extends TestCase {
27+
import org.junit.Test;
28+
29+
import com.google.gson.Gson;
30+
import com.google.gson.GsonBuilder;
31+
import com.google.gson.reflect.TypeToken;
32+
33+
public final class GraphAdapterBuilderTest {
34+
@Test
2935
public void testSerialization() {
3036
Roshambo rock = new Roshambo("ROCK");
3137
Roshambo scissors = new Roshambo("SCISSORS");
@@ -46,6 +52,7 @@ public void testSerialization() {
4652
gson.toJson(rock).replace('"', '\''));
4753
}
4854

55+
@Test
4956
public void testDeserialization() {
5057
String json = "{'0x1':{'name':'ROCK','beats':'0x2'}," +
5158
"'0x2':{'name':'SCISSORS','beats':'0x3'}," +
@@ -66,20 +73,7 @@ public void testDeserialization() {
6673
assertSame(rock, paper.beats);
6774
}
6875

69-
public void testSerializationDirectSelfReference() {
70-
Roshambo suicide = new Roshambo("SUICIDE");
71-
suicide.beats = suicide;
72-
73-
GsonBuilder gsonBuilder = new GsonBuilder();
74-
new GraphAdapterBuilder()
75-
.addType(Roshambo.class)
76-
.registerOn(gsonBuilder);
77-
Gson gson = gsonBuilder.create();
78-
79-
assertEquals("{'0x1':{'name':'SUICIDE','beats':'0x1'}}",
80-
gson.toJson(suicide).replace('"', '\''));
81-
}
82-
76+
@Test
8377
public void testDeserializationDirectSelfReference() {
8478
String json = "{'0x1':{'name':'SUICIDE','beats':'0x1'}}";
8579

@@ -94,6 +88,7 @@ public void testDeserializationDirectSelfReference() {
9488
assertSame(suicide, suicide.beats);
9589
}
9690

91+
@Test
9792
public void testSerializeListOfLists() {
9893
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
9994
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
@@ -113,6 +108,7 @@ public void testSerializeListOfLists() {
113108
assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\''));
114109
}
115110

111+
@Test
116112
public void testDeserializeListOfLists() {
117113
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
118114
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
@@ -130,6 +126,7 @@ public void testDeserializeListOfLists() {
130126
assertEquals(Collections.emptyList(), listOfLists.get(1));
131127
}
132128

129+
@Test
133130
public void testSerializationWithMultipleTypes() {
134131
Company google = new Company("Google");
135132
new Employee("Jesse", google);
@@ -148,6 +145,7 @@ public void testSerializationWithMultipleTypes() {
148145
gson.toJson(google).replace('"', '\''));
149146
}
150147

148+
@Test
151149
public void testDeserializationWithMultipleTypes() {
152150
GsonBuilder gsonBuilder = new GsonBuilder();
153151
new GraphAdapterBuilder()

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
<modules>
2222
<module>gson</module>
23+
<module>extras</module>
2324
</modules>
2425

2526
<properties>
@@ -41,7 +42,7 @@
4142

4243
<licenses>
4344
<license>
44-
<name>Apache 2.0</name>
45+
<name>Apache-2.0</name>
4546
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
4647
</license>
4748
</licenses>

0 commit comments

Comments
 (0)