Ensure "excluder" is added prior to user defined type adapters/factories.

- Added test expose bad behaviour.
This commit is contained in:
Joel Leitch 2012-12-20 19:41:33 +00:00
parent 6d90f0d894
commit b0531e1649
2 changed files with 26 additions and 7 deletions

View File

@ -195,6 +195,9 @@ public final class 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);
@ -231,9 +234,6 @@ public final class 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));
@ -888,11 +888,11 @@ public final class Gson {
@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();
}
}

View File

@ -19,8 +19,12 @@ package com.google.gson.functional;
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;
@ -284,6 +288,21 @@ public class ObjectTest extends TestCase {
}));
}
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));