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
This commit is contained in:
Marcono1234 2021-09-18 02:21:37 +02:00 committed by GitHub
parent f1f90313fc
commit 26a1928277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 169 deletions

View File

@ -1,47 +1,37 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.google.code.gson</groupId> <parent>
<groupId>com.google.code.gson</groupId>
<artifactId>gson-parent</artifactId>
<version>2.8.9-SNAPSHOT</version>
</parent>
<artifactId>gson-extras</artifactId> <artifactId>gson-extras</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear> <inceptionYear>2008</inceptionYear>
<name>Gson Extras</name> <name>Gson Extras</name>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</parent>
<url>http://code.google.com/p/google-gson/</url>
<description>Google Gson grab bag of utilities, type adapters, etc.</description> <description>Google Gson grab bag of utilities, type adapters, etc.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<licenses> <licenses>
<license> <license>
<name>The Apache Software License, Version 2.0</name> <name>Apache-2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license> </license>
</licenses> </licenses>
<scm>
<connection>scm:svn:http://google-gson.googlecode.com/svn/trunk/extras</connection>
<developerConnection>scm:svn:https://google-gson.googlecode.com/svn/trunk/extras</developerConnection>
<url>http://google-gson.codegoogle.com/svn/trunk/extras</url>
</scm>
<issueManagement>
<system>Google Code Issue Tracking</system>
<url>http://code.google.com/p/google-gson/issues/list</url>
</issueManagement>
<organization> <organization>
<name>Google, Inc.</name> <name>Google, Inc.</name>
<url>http://www.google.com</url> <url>https://www.google.com</url>
</organization> </organization>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.7</version> <version>${project.parent.version}</version>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.annotation</groupId> <groupId>javax.annotation</groupId>
@ -51,130 +41,26 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<profiles>
<!-- Activate PGP signing only when performing a release -->
<profile>
<id>release-sign-artifacts</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build> <build>
<defaultGoal>package</defaultGoal> <pluginManagement>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>3.5.1</version> <version>3.0.0-M1</version>
<configuration> <configuration>
<source>1.6</source> <!-- Currently not deployed -->
<target>1.6</target> <skip>true</skip>
</configuration> </configuration>
</plugin> </plugin>
<plugin> </plugins>
<groupId>org.apache.maven.plugins</groupId> </pluginManagement>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<links>
<link>http://download.oracle.com/javase/1.5.0/docs/api/</link>
</links>
<version>true</version>
<show>public</show>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<workspace>
../eclipse-ws/
</workspace>
<workspaceCodeStylesURL>
file:///${basedir}/../lib/gson-formatting-styles.xml
</workspaceCodeStylesURL>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<!-- version>2.3.2</version -->
<configuration>
<arguments>-DenableCiProfile=true</arguments>
<tagBase>https://google-gson.googlecode.com/svn/tags/</tagBase>
</configuration>
</plugin>
</plugins>
</build> </build>
<developers> <developers>
<developer> <developer>
<name>Inderjeet Singh</name> <name>Inderjeet Singh</name>

View File

@ -27,7 +27,6 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory; import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
@ -204,11 +203,13 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
return registerSubtype(type, type.getSimpleName()); return registerSubtype(type, type.getSimpleName());
} }
@Override
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type.getRawType() != baseType) { if (type.getRawType() != baseType) {
return null; return null;
} }
final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
final Map<String, TypeAdapter<?>> labelToDelegate final Map<String, TypeAdapter<?>> labelToDelegate
= new LinkedHashMap<String, TypeAdapter<?>>(); = new LinkedHashMap<String, TypeAdapter<?>>();
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
@ -221,7 +222,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
return new TypeAdapter<R>() { return new TypeAdapter<R>() {
@Override public R read(JsonReader in) throws IOException { @Override public R read(JsonReader in) throws IOException {
JsonElement jsonElement = Streams.parse(in); JsonElement jsonElement = jsonElementAdapter.read(in);
JsonElement labelJsonElement; JsonElement labelJsonElement;
if (maintainType) { if (maintainType) {
labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
@ -255,7 +256,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
if (maintainType) { if (maintainType) {
Streams.write(jsonObject, out); jsonElementAdapter.write(out, jsonObject);
return; return;
} }
@ -270,7 +271,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
clone.add(e.getKey(), e.getValue()); clone.add(e.getKey(), e.getValue());
} }
Streams.write(clone, out); jsonElementAdapter.write(out, clone);
} }
}.nullSafe(); }.nullSafe();
} }

View File

@ -16,16 +16,22 @@
package com.google.gson.graph; package com.google.gson.graph;
import com.google.gson.Gson; import static org.junit.Assert.assertEquals;
import com.google.gson.GsonBuilder; import static org.junit.Assert.assertSame;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import junit.framework.TestCase;
public final class GraphAdapterBuilderTest extends TestCase { import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public final class GraphAdapterBuilderTest {
@Test
public void testSerialization() { public void testSerialization() {
Roshambo rock = new Roshambo("ROCK"); Roshambo rock = new Roshambo("ROCK");
Roshambo scissors = new Roshambo("SCISSORS"); Roshambo scissors = new Roshambo("SCISSORS");
@ -46,6 +52,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
gson.toJson(rock).replace('"', '\'')); gson.toJson(rock).replace('"', '\''));
} }
@Test
public void testDeserialization() { public void testDeserialization() {
String json = "{'0x1':{'name':'ROCK','beats':'0x2'}," + String json = "{'0x1':{'name':'ROCK','beats':'0x2'}," +
"'0x2':{'name':'SCISSORS','beats':'0x3'}," + "'0x2':{'name':'SCISSORS','beats':'0x3'}," +
@ -66,20 +73,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
assertSame(rock, paper.beats); assertSame(rock, paper.beats);
} }
public void testSerializationDirectSelfReference() { @Test
Roshambo suicide = new Roshambo("SUICIDE");
suicide.beats = suicide;
GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
.addType(Roshambo.class)
.registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
assertEquals("{'0x1':{'name':'SUICIDE','beats':'0x1'}}",
gson.toJson(suicide).replace('"', '\''));
}
public void testDeserializationDirectSelfReference() { public void testDeserializationDirectSelfReference() {
String json = "{'0x1':{'name':'SUICIDE','beats':'0x1'}}"; String json = "{'0x1':{'name':'SUICIDE','beats':'0x1'}}";
@ -94,6 +88,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
assertSame(suicide, suicide.beats); assertSame(suicide, suicide.beats);
} }
@Test
public void testSerializeListOfLists() { public void testSerializeListOfLists() {
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType(); Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
Type listOfAnyType = new TypeToken<List<?>>() {}.getType(); Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
@ -113,6 +108,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\'')); assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\''));
} }
@Test
public void testDeserializeListOfLists() { public void testDeserializeListOfLists() {
Type listOfAnyType = new TypeToken<List<?>>() {}.getType(); Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType(); Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
@ -130,6 +126,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
assertEquals(Collections.emptyList(), listOfLists.get(1)); assertEquals(Collections.emptyList(), listOfLists.get(1));
} }
@Test
public void testSerializationWithMultipleTypes() { public void testSerializationWithMultipleTypes() {
Company google = new Company("Google"); Company google = new Company("Google");
new Employee("Jesse", google); new Employee("Jesse", google);
@ -148,6 +145,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
gson.toJson(google).replace('"', '\'')); gson.toJson(google).replace('"', '\''));
} }
@Test
public void testDeserializationWithMultipleTypes() { public void testDeserializationWithMultipleTypes() {
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder() new GraphAdapterBuilder()

View File

@ -20,6 +20,7 @@
<modules> <modules>
<module>gson</module> <module>gson</module>
<module>extras</module>
</modules> </modules>
<properties> <properties>
@ -41,7 +42,7 @@
<licenses> <licenses>
<license> <license>
<name>Apache 2.0</name> <name>Apache-2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url> <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license> </license>
</licenses> </licenses>