Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
DanySK authored Jan 8, 2022
2 parents 7aa69eb + 78a4285 commit d399bd9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public static void main(String[] args) {
collection.add(new Event("GREETINGS", "guest"));
String json = gson.toJson(collection);
System.out.println("Using Gson.toJson() on a raw collection: " + json);
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(json).getAsJsonArray();
JsonArray array = JsonParser.parseString(json).getAsJsonArray();
String message = gson.fromJson(array.get(0), String.class);
int number = gson.fromJson(array.get(1), int.class);
Event event = gson.fromJson(array.get(2), Event.class);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/google/gson/graph/GraphAdapterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public GraphAdapterBuilder() {
public GraphAdapterBuilder addType(Type type) {
final ObjectConstructor<?> objectConstructor = constructorConstructor.get(TypeToken.get(type));
InstanceCreator<Object> instanceCreator = new InstanceCreator<Object>() {
@Override
public Object createInstance(Type type) {
return objectConstructor.construct();
}
Expand Down Expand Up @@ -83,6 +84,7 @@ static class Factory implements TypeAdapterFactory, InstanceCreator {
this.instanceCreators = instanceCreators;
}

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!instanceCreators.containsKey(type.getType())) {
return null;
Expand Down Expand Up @@ -212,6 +214,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
* that is only when we've called back into Gson to deserialize a tree.
*/
@SuppressWarnings("unchecked")
@Override
public Object createInstance(Type type) {
Graph graph = graphThreadLocal.get();
if (graph == null || graph.nextCreate == null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/google/gson/interceptors/Intercept.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
* after it has been deserialized from Json.
* Here is an example of how this annotation is used:
* <p>Here is an example of how this annotation is used:
* <p><pre>
* &#64Intercept(postDeserialize=UserValidator.class)
* <pre>
* &#64;Intercept(postDeserialize=UserValidator.class)
* public class User {
* String name;
* String password;
Expand All @@ -47,7 +47,7 @@
* }
* }
* }
* </pre></p>
* </pre>
*
* @author Inderjeet Singh
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
Expand Down Expand Up @@ -204,11 +203,13 @@ public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
return registerSubtype(type, type.getSimpleName());
}

@Override
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type.getRawType() != baseType) {
return null;
}

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

return new TypeAdapter<R>() {
@Override public R read(JsonReader in) throws IOException {
JsonElement jsonElement = Streams.parse(in);
JsonElement jsonElement = jsonElementAdapter.read(in);
JsonElement labelJsonElement;
if (maintainType) {
labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
Expand Down Expand Up @@ -255,7 +256,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();

if (maintainType) {
Streams.write(jsonObject, out);
jsonElementAdapter.write(out, jsonObject);
return;
}

Expand All @@ -270,7 +271,7 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
clone.add(e.getKey(), e.getValue());
}
Streams.write(clone, out);
jsonElementAdapter.write(out, clone);
}
}.nullSafe();
}
Expand Down
23 changes: 18 additions & 5 deletions src/test/java/com/google/gson/graph/GraphAdapterBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@

package com.google.gson.graph;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
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() {
Roshambo rock = new Roshambo("ROCK");
Roshambo scissors = new Roshambo("SCISSORS");
Expand All @@ -46,6 +52,7 @@ public void testSerialization() {
gson.toJson(rock).replace('"', '\''));
}

@Test
public void testDeserialization() {
String json = "{'0x1':{'name':'ROCK','beats':'0x2'}," +
"'0x2':{'name':'SCISSORS','beats':'0x3'}," +
Expand All @@ -66,6 +73,7 @@ public void testDeserialization() {
assertSame(rock, paper.beats);
}

@Test
public void testSerializationDirectSelfReference() {
Roshambo suicide = new Roshambo("SUICIDE");
suicide.beats = suicide;
Expand All @@ -87,6 +95,7 @@ public void testSerializationDirectSelfReference() {
// gson.toJson(suicide).replace('"', '\''));
}

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

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

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

@Test
public void testDeserializeListOfLists() {
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
Expand All @@ -137,6 +148,7 @@ public void testDeserializeListOfLists() {
assertEquals(Collections.emptyList(), listOfLists.get(1));
}

@Test
public void testSerializationWithMultipleTypes() {
Company google = new Company("Google");
new Employee("Jesse", google);
Expand All @@ -155,6 +167,7 @@ public void testSerializationWithMultipleTypes() {
gson.toJson(google).replace('"', '\''));
}

@Test
public void testDeserializationWithMultipleTypes() {
GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
Expand Down

0 comments on commit d399bd9

Please sign in to comment.