fix for bug 56 where Gson wasn't serialization null fields of type string,

collection and arrays properly even when serializeNulls was set.
This commit is contained in:
Inderjeet Singh 2008-10-10 22:33:46 +00:00
parent d61e754fb6
commit cdd5163458

View File

@ -80,14 +80,22 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
}
public void visitArrayField(Field f, Type typeOfF, Object obj) {
if (!isFieldNull(f, obj)) {
if (isFieldNull(f, obj)) {
if (serializeNulls) {
addChildAsElement(f, new JsonNull());
}
} else {
Object array = getFieldValue(f, obj);
addAsChildOfObject(f, typeOfF, array);
}
}
public void visitCollectionField(Field f, Type typeOfF, Object obj) {
if (!isFieldNull(f, obj)) {
if (isFieldNull(f, obj)) {
if (serializeNulls) {
addChildAsElement(f, new JsonNull());
}
} else {
if (typeOfF == null) {
throw new RuntimeException("Can not handle non-generic collections");
}
@ -151,8 +159,12 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
return childVisitor.getJsonElement();
}
public void visitPrimitiveField(Field f, Type typeOfF, Object obj) {
if (!isFieldNull(f, obj)) {
public void visitPrimitiveField(Field f, Type typeOfF, Object obj) {
if (isFieldNull(f, obj)) {
if (serializeNulls) {
addChildAsElement(f, new JsonNull());
}
} else {
TypeInfo typeInfo = new TypeInfo(typeOfF);
if (typeInfo.isPrimitiveOrStringAndNotAnArray()) {
Object fieldValue = getFieldValue(f, obj);