Skip to content

Commit 78a4285

Browse files
committed
feat: merge branch 'master' of https://github.com/google/gson into google-master
2 parents 32f8637 + 4ec67c0 commit 78a4285

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

src/main/java/com/google/gson/extras/examples/rawcollections/RawCollectionsExample.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public static void main(String[] args) {
4545
collection.add(new Event("GREETINGS", "guest"));
4646
String json = gson.toJson(collection);
4747
System.out.println("Using Gson.toJson() on a raw collection: " + json);
48-
JsonParser parser = new JsonParser();
49-
JsonArray array = parser.parse(json).getAsJsonArray();
48+
JsonArray array = JsonParser.parseString(json).getAsJsonArray();
5049
String message = gson.fromJson(array.get(0), String.class);
5150
int number = gson.fromJson(array.get(1), int.class);
5251
Event event = gson.fromJson(array.get(2), Event.class);

src/main/java/com/google/gson/graph/GraphAdapterBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public GraphAdapterBuilder() {
5252
public GraphAdapterBuilder addType(Type type) {
5353
final ObjectConstructor<?> objectConstructor = constructorConstructor.get(TypeToken.get(type));
5454
InstanceCreator<Object> instanceCreator = new InstanceCreator<Object>() {
55+
@Override
5556
public Object createInstance(Type type) {
5657
return objectConstructor.construct();
5758
}
@@ -83,6 +84,7 @@ static class Factory implements TypeAdapterFactory, InstanceCreator {
8384
this.instanceCreators = instanceCreators;
8485
}
8586

87+
@Override
8688
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
8789
if (!instanceCreators.containsKey(type.getType())) {
8890
return null;
@@ -212,6 +214,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
212214
* that is only when we've called back into Gson to deserialize a tree.
213215
*/
214216
@SuppressWarnings("unchecked")
217+
@Override
215218
public Object createInstance(Type type) {
216219
Graph graph = graphThreadLocal.get();
217220
if (graph == null || graph.nextCreate == null) {

src/main/java/com/google/gson/interceptors/Intercept.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
* after it has been deserialized from Json.
2929
* Here is an example of how this annotation is used:
3030
* <p>Here is an example of how this annotation is used:
31-
* <p><pre>
32-
* &#64Intercept(postDeserialize=UserValidator.class)
31+
* <pre>
32+
* &#64;Intercept(postDeserialize=UserValidator.class)
3333
* public class User {
3434
* String name;
3535
* String password;
@@ -47,7 +47,7 @@
4747
* }
4848
* }
4949
* }
50-
* </pre></p>
50+
* </pre>
5151
*
5252
* @author Inderjeet Singh
5353
*/

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

+5-4
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
}

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

+18-5
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,6 +73,7 @@ public void testDeserialization() {
6673
assertSame(rock, paper.beats);
6774
}
6875

76+
@Test
6977
public void testSerializationDirectSelfReference() {
7078
Roshambo suicide = new Roshambo("SUICIDE");
7179
suicide.beats = suicide;
@@ -87,6 +95,7 @@ public void testSerializationDirectSelfReference() {
8795
// gson.toJson(suicide).replace('"', '\''));
8896
}
8997

98+
@Test
9099
public void testDeserializationDirectSelfReference() {
91100
String json = "{'0x1':{'name':'SUICIDE','beats':'0x1'}}";
92101

@@ -101,6 +110,7 @@ public void testDeserializationDirectSelfReference() {
101110
assertSame(suicide, suicide.beats);
102111
}
103112

113+
@Test
104114
public void testSerializeListOfLists() {
105115
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
106116
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
@@ -120,6 +130,7 @@ public void testSerializeListOfLists() {
120130
assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\''));
121131
}
122132

133+
@Test
123134
public void testDeserializeListOfLists() {
124135
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
125136
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
@@ -137,6 +148,7 @@ public void testDeserializeListOfLists() {
137148
assertEquals(Collections.emptyList(), listOfLists.get(1));
138149
}
139150

151+
@Test
140152
public void testSerializationWithMultipleTypes() {
141153
Company google = new Company("Google");
142154
new Employee("Jesse", google);
@@ -155,6 +167,7 @@ public void testSerializationWithMultipleTypes() {
155167
gson.toJson(google).replace('"', '\''));
156168
}
157169

170+
@Test
158171
public void testDeserializationWithMultipleTypes() {
159172
GsonBuilder gsonBuilder = new GsonBuilder();
160173
new GraphAdapterBuilder()

0 commit comments

Comments
 (0)