Ensure "excluder" is added prior to user defined type adapters/factories.
- Added test expose bad behaviour.
This commit is contained in:
parent
6d90f0d894
commit
b0531e1649
|
@ -195,6 +195,9 @@ public final class Gson {
|
||||||
factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
|
factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
|
||||||
factories.add(ObjectTypeAdapter.FACTORY);
|
factories.add(ObjectTypeAdapter.FACTORY);
|
||||||
|
|
||||||
|
// the excluder must precede all adapters that handle user-defined types
|
||||||
|
factories.add(excluder);
|
||||||
|
|
||||||
// user's type adapters
|
// user's type adapters
|
||||||
factories.addAll(typeAdapterFactories);
|
factories.addAll(typeAdapterFactories);
|
||||||
|
|
||||||
|
@ -231,9 +234,6 @@ public final class Gson {
|
||||||
factories.add(TypeAdapters.ENUM_FACTORY);
|
factories.add(TypeAdapters.ENUM_FACTORY);
|
||||||
factories.add(TypeAdapters.CLASS_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
|
// type adapters for composite and user-defined types
|
||||||
factories.add(new CollectionTypeAdapterFactory(constructorConstructor));
|
factories.add(new CollectionTypeAdapterFactory(constructorConstructor));
|
||||||
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
|
factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
|
||||||
|
@ -888,11 +888,11 @@ public final class Gson {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder("{")
|
return new StringBuilder("{serializeNulls:")
|
||||||
.append("serializeNulls:").append(serializeNulls)
|
.append(serializeNulls)
|
||||||
.append("factories:").append(factories)
|
.append("factories:").append(factories)
|
||||||
.append(",instanceCreators:").append(constructorConstructor)
|
.append(",instanceCreators:").append(constructorConstructor)
|
||||||
.append("}");
|
.append("}")
|
||||||
return sb.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,12 @@ package com.google.gson.functional;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.InstanceCreator;
|
import com.google.gson.InstanceCreator;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParseException;
|
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.ArrayOfObjects;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitiveWrappers;
|
import com.google.gson.common.TestTypes.BagOfPrimitiveWrappers;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
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() {
|
public void testPrimitiveArrayFieldSerialization() {
|
||||||
PrimitiveArray target = new PrimitiveArray(new long[] { 1L, 2L, 3L });
|
PrimitiveArray target = new PrimitiveArray(new long[] { 1L, 2L, 3L });
|
||||||
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
assertEquals(target.getExpectedJson(), gson.toJson(target));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user