Implemented some code review comments from r482

This commit is contained in:
Inderjeet Singh 2009-10-09 01:35:45 +00:00
parent 6d60a7db1d
commit e37add7836
9 changed files with 14 additions and 39 deletions

View File

@ -19,8 +19,8 @@ package com.google.gson;
/**
* A strategy (or policy) definition that is used to decide whether or not a field or top-level
* class should be serialized or deserialized as part of the JSON output/input. For serialization,
* if the {@link this#shouldSkipClass(Class)} method returns false then that class or field type
* will not be part of the JSON output. For deserialization, if {@link this#shouldSkipClass(Class)}
* if the {@link #shouldSkipClass(Class)} method returns false then that class or field type
* will not be part of the JSON output. For deserialization, if {@link #shouldSkipClass(Class)}
* returns false, then it will not be set as part of the Java object structure.
*
* <p>The following are a few examples that shows how you can use this exclusion mechanism.

View File

@ -90,7 +90,7 @@ final class FieldAttributes {
}
/**
* Return the {@link T} annotation object from this field if it exist; otherwise returns
* Return the T annotation object from this field if it exist; otherwise returns
* {@code null}.
*
* @param annotation the class of the annotation that will be retrieved

View File

@ -202,7 +202,7 @@ public final class Gson {
if (src == null) {
return JsonNull.createJsonNull();
}
return toJsonTree(src, src.getClass(), false);
return toJsonTree(src, src.getClass());
}
/**
@ -222,16 +222,12 @@ public final class Gson {
* @since 1.4
*/
public JsonElement toJsonTree(Object src, Type typeOfSrc) {
return toJsonTree(src, typeOfSrc, true);
}
private JsonElement toJsonTree(Object src, Type typeOfSrc, boolean preserveType) {
if (src == null) {
return JsonNull.createJsonNull();
}
JsonSerializationContextDefault context = new JsonSerializationContextDefault(
createDefaultObjectNavigatorFactory(serializationStrategy), serializeNulls, serializers);
return context.serialize(src, typeOfSrc, preserveType);
return context.serialize(src, typeOfSrc, true);
}
/**
@ -251,7 +247,7 @@ public final class Gson {
if (src == null) {
return serializeNulls ? NULL_STRING : "";
}
return toJson(src, src.getClass(), false);
return toJson(src, src.getClass());
}
/**
@ -270,12 +266,8 @@ public final class Gson {
* @return Json representation of {@code src}
*/
public String toJson(Object src, Type typeOfSrc) {
return toJson(src, typeOfSrc, true);
}
private String toJson(Object src, Type typeOfSrc, boolean preserveType) {
StringWriter writer = new StringWriter();
toJson(src, typeOfSrc, writer, preserveType);
toJson(src, typeOfSrc, writer);
return writer.toString();
}
@ -295,7 +287,7 @@ public final class Gson {
public void toJson(Object src, Appendable writer) {
try {
if (src != null) {
toJson(src, src.getClass(), writer, false);
toJson(src, src.getClass(), writer);
} else if (serializeNulls) {
writeOutNullString(writer);
}
@ -320,11 +312,7 @@ public final class Gson {
* @since 1.2
*/
public void toJson(Object src, Type typeOfSrc, Appendable writer) {
toJson(src, typeOfSrc, writer, true);
}
private void toJson(Object src, Type typeOfSrc, Appendable writer, boolean preserveType) {
JsonElement jsonElement = toJsonTree(src, typeOfSrc, preserveType);
JsonElement jsonElement = toJsonTree(src, typeOfSrc);
toJson(jsonElement, writer);
}

View File

@ -98,8 +98,4 @@ abstract class JsonDeserializationVisitor<T> implements ObjectNavigator.Visitor
// This happens primarily because of custom deserializers
return childVisitor.getTarget();
}
public ObjectTypePair getActualTypeIfMoreSpecific(ObjectTypePair objTypePair) {
return objTypePair;
}
}

View File

@ -42,7 +42,7 @@ final class JsonSerializationContextDefault implements JsonSerializationContext
if (src == null) {
return JsonNull.createJsonNull();
}
return serialize(src, src.getClass(), false);
return serialize(src, src.getClass(), true);
}
public JsonElement serialize(Object src, Type typeOfSrc) {

View File

@ -236,8 +236,4 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
public JsonElement getJsonElement() {
return root;
}
public ObjectTypePair getActualTypeIfMoreSpecific(ObjectTypePair objTypePair) {
return objTypePair.toMoreSpecificType();
}
}

View File

@ -63,8 +63,6 @@ final class ObjectNavigator {
*/
public boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent);
public ObjectTypePair getActualTypeIfMoreSpecific(ObjectTypePair objTypePair);
/**
* Retrieve the current target
*/
@ -117,7 +115,7 @@ final class ObjectNavigator {
objectToVisit = visitor.getTarget();
} else {
visitor.startVisitingObject(objectToVisit);
ObjectTypePair currObjTypePair = visitor.getActualTypeIfMoreSpecific(objTypePair);
ObjectTypePair currObjTypePair = objTypePair.toMoreSpecificType();
Class<?> topLevelClass = new TypeInfo(currObjTypePair.getType()).getRawClass();
for (Class<?> curr = topLevelClass; curr != null && !curr.equals(Object.class);
curr = curr.getSuperclass()) {

View File

@ -23,8 +23,6 @@ import java.lang.reflect.Type;
* @author Inderjeet Singh
*/
final class ObjectTypePair {
private static final int PRIME = 31;
private final Object obj;
private final Type type;
private final boolean preserveType;
@ -46,9 +44,7 @@ final class ObjectTypePair {
@SuppressWarnings("unchecked")
Pair<JsonSerializer, ObjectTypePair> getMatchingSerializer(
ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers) {
if (obj == null) {
return null;
}
Preconditions.checkNotNull(obj);
JsonSerializer serializer = null;
if (!preserveType) {
// First try looking up the serializer for the actual type
@ -95,7 +91,7 @@ final class ObjectTypePair {
public int hashCode() {
// Not using type.hashCode() since I am not sure if the subclasses of type reimplement
// hashCode() to be equal for equal types
return ((obj == null) ? PRIME : obj.hashCode());
return ((obj == null) ? 31 : obj.hashCode());
}
@Override

View File

@ -72,6 +72,7 @@ public class FieldAttributesTest extends TestCase {
}
private static class Foo {
@SuppressWarnings("unused")
public transient List<String> bar;
}
}