Skip to content

Commit

Permalink
Ensure "excluder" is added prior to user defined type adapters/factor…
Browse files Browse the repository at this point in the history
…ies.

- Added test expose bad behaviour.
  • Loading branch information
joel-leitch committed Dec 20, 2012
1 parent 6d90f0d commit b0531e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 7 additions & 7 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ public Gson() {
factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
factories.add(ObjectTypeAdapter.FACTORY);

// the excluder must precede all adapters that handle user-defined types
factories.add(excluder);

// user's type adapters
factories.addAll(typeAdapterFactories);

Expand Down Expand Up @@ -231,9 +234,6 @@ public Gson() {
factories.add(TypeAdapters.ENUM_FACTORY);
factories.add(TypeAdapters.CLASS_FACTORY);

// the excluder must precede all adapters that handle user-defined types
factories.add(excluder);

// type adapters for composite and user-defined types
factories.add(new CollectionTypeAdapterFactory(constructorConstructor));
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
Expand Down Expand Up @@ -888,11 +888,11 @@ public void setDelegate(TypeAdapter<T> typeAdapter) {

@Override
public String toString() {
StringBuilder sb = new StringBuilder("{")
.append("serializeNulls:").append(serializeNulls)
return new StringBuilder("{serializeNulls:")
.append(serializeNulls)
.append("factories:").append(factories)
.append(",instanceCreators:").append(constructorConstructor)
.append("}");
return sb.toString();
.append("}")
.toString();
}
}
19 changes: 19 additions & 0 deletions gson/src/test/java/com/google/gson/functional/ObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes;
import com.google.gson.common.TestTypes.ArrayOfObjects;
import com.google.gson.common.TestTypes.BagOfPrimitiveWrappers;
import com.google.gson.common.TestTypes.BagOfPrimitives;
Expand Down Expand Up @@ -284,6 +288,21 @@ public void testAnonymousLocalClassesSerialization() throws Exception {
}));
}

public void testAnonymousLocalClassesCustomSerialization() throws Exception {
gson = new GsonBuilder()
.registerTypeHierarchyAdapter(ClassWithNoFields.class,
new JsonSerializer<ClassWithNoFields>() {
public JsonElement serialize(
ClassWithNoFields src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonObject();
}
}).create();

assertEquals("null", gson.toJson(new ClassWithNoFields() {
// empty anonymous class
}));
}

public void testPrimitiveArrayFieldSerialization() {
PrimitiveArray target = new PrimitiveArray(new long[] { 1L, 2L, 3L });
assertEquals(target.getExpectedJson(), gson.toJson(target));
Expand Down

0 comments on commit b0531e1

Please sign in to comment.