Minor fixes.

This commit is contained in:
Joel Leitch 2009-10-09 15:43:50 +00:00
parent 60ef777efc
commit de6af4411b
2 changed files with 24 additions and 25 deletions

View File

@ -22,7 +22,7 @@ import java.lang.reflect.Type;
/** /**
* A visitor that adds JSON elements corresponding to each field of an object * A visitor that adds JSON elements corresponding to each field of an object
* *
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch * @author Joel Leitch
*/ */
@ -184,8 +184,8 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
objTypePair = pair.getSecond(); objTypePair = pair.getSecond();
start(objTypePair); start(objTypePair);
try { try {
JsonElement element = JsonElement element =
serializer.serialize(objTypePair.getObject(), objTypePair.getType(), context); serializer.serialize(objTypePair.getObject(), objTypePair.getType(), context);
return element == null ? JsonNull.createJsonNull() : element; return element == null ? JsonNull.createJsonNull() : element;
} finally { } finally {
end(objTypePair); end(objTypePair);

View File

@ -16,10 +16,6 @@
package com.google.gson.functional; package com.google.gson.functional;
import java.lang.reflect.Type;
import junit.framework.TestCase;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
@ -34,19 +30,23 @@ import com.google.gson.common.TestTypes.ClassWithBaseField;
import com.google.gson.common.TestTypes.Sub; import com.google.gson.common.TestTypes.Sub;
import com.google.gson.common.TestTypes.SubSerializer; import com.google.gson.common.TestTypes.SubSerializer;
import junit.framework.TestCase;
import java.lang.reflect.Type;
/** /**
* Functional Test exercising custom serialization only. When test applies to both * Functional Test exercising custom serialization only. When test applies to both
* serialization and deserialization then add it to CustomTypeAdapterTest. * serialization and deserialization then add it to CustomTypeAdapterTest.
* *
* @author Inderjeet Singh * @author Inderjeet Singh
*/ */
public class CustomSerializerTest extends TestCase { public class CustomSerializerTest extends TestCase {
public void testBaseClassSerializerInvokedForBaseClassFields() { public void testBaseClassSerializerInvokedForBaseClassFields() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new BaseSerializer()) .registerTypeAdapter(Base.class, new BaseSerializer())
.registerTypeAdapter(Sub.class, new SubSerializer()) .registerTypeAdapter(Sub.class, new SubSerializer())
.create(); .create();
ClassWithBaseField target = new ClassWithBaseField(new Base()); ClassWithBaseField target = new ClassWithBaseField(new Base());
JsonObject json = (JsonObject) gson.toJsonTree(target); JsonObject json = (JsonObject) gson.toJsonTree(target);
JsonObject base = json.get("base").getAsJsonObject(); JsonObject base = json.get("base").getAsJsonObject();
@ -55,9 +55,9 @@ public class CustomSerializerTest extends TestCase {
public void testSubClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() { public void testSubClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new BaseSerializer()) .registerTypeAdapter(Base.class, new BaseSerializer())
.registerTypeAdapter(Sub.class, new SubSerializer()) .registerTypeAdapter(Sub.class, new SubSerializer())
.create(); .create();
ClassWithBaseField target = new ClassWithBaseField(new Sub()); ClassWithBaseField target = new ClassWithBaseField(new Sub());
JsonObject json = (JsonObject) gson.toJsonTree(target); JsonObject json = (JsonObject) gson.toJsonTree(target);
JsonObject base = json.get("base").getAsJsonObject(); JsonObject base = json.get("base").getAsJsonObject();
@ -66,9 +66,9 @@ public class CustomSerializerTest extends TestCase {
public void testSubClassSerializerInvokedForBaseClassFieldsHoldingArrayOfSubClassInstances() { public void testSubClassSerializerInvokedForBaseClassFieldsHoldingArrayOfSubClassInstances() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new BaseSerializer()) .registerTypeAdapter(Base.class, new BaseSerializer())
.registerTypeAdapter(Sub.class, new SubSerializer()) .registerTypeAdapter(Sub.class, new SubSerializer())
.create(); .create();
ClassWithBaseArrayField target = new ClassWithBaseArrayField(new Base[] {new Sub(), new Sub()}); ClassWithBaseArrayField target = new ClassWithBaseArrayField(new Base[] {new Sub(), new Sub()});
JsonObject json = (JsonObject) gson.toJsonTree(target); JsonObject json = (JsonObject) gson.toJsonTree(target);
JsonArray array = json.get("base").getAsJsonArray(); JsonArray array = json.get("base").getAsJsonArray();
@ -77,27 +77,26 @@ public class CustomSerializerTest extends TestCase {
assertEquals(SubSerializer.NAME, serializerKey.getAsString()); assertEquals(SubSerializer.NAME, serializerKey.getAsString());
} }
} }
public void testBaseClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() { public void testBaseClassSerializerInvokedForBaseClassFieldsHoldingSubClassInstances() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new BaseSerializer()) .registerTypeAdapter(Base.class, new BaseSerializer())
.create(); .create();
ClassWithBaseField target = new ClassWithBaseField(new Sub()); ClassWithBaseField target = new ClassWithBaseField(new Sub());
JsonObject json = (JsonObject) gson.toJsonTree(target); JsonObject json = (JsonObject) gson.toJsonTree(target);
JsonObject base = json.get("base").getAsJsonObject(); JsonObject base = json.get("base").getAsJsonObject();
assertEquals(BaseSerializer.NAME, base.get(Base.SERIALIZER_KEY).getAsString()); assertEquals(BaseSerializer.NAME, base.get(Base.SERIALIZER_KEY).getAsString());
} }
public void testSerializerReturnsNull() { public void testSerializerReturnsNull() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new JsonSerializer<Base>() { .registerTypeAdapter(Base.class, new JsonSerializer<Base>() {
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
return null; return null;
} }
}) })
.create(); .create();
JsonElement json = gson.toJsonTree(new Base()); JsonElement json = gson.toJsonTree(new Base());
System.out.println(json);
assertTrue(json.isJsonNull()); assertTrue(json.isJsonNull());
} }
} }