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 * 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, * 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 * 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 this#shouldSkipClass(Class)} * 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. * 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. * <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}. * {@code null}.
* *
* @param annotation the class of the annotation that will be retrieved * @param annotation the class of the annotation that will be retrieved

View File

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

View File

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

View File

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

View File

@ -236,8 +236,4 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
public JsonElement getJsonElement() { public JsonElement getJsonElement() {
return root; 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 boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent);
public ObjectTypePair getActualTypeIfMoreSpecific(ObjectTypePair objTypePair);
/** /**
* Retrieve the current target * Retrieve the current target
*/ */
@ -117,7 +115,7 @@ final class ObjectNavigator {
objectToVisit = visitor.getTarget(); objectToVisit = visitor.getTarget();
} else { } else {
visitor.startVisitingObject(objectToVisit); visitor.startVisitingObject(objectToVisit);
ObjectTypePair currObjTypePair = visitor.getActualTypeIfMoreSpecific(objTypePair); ObjectTypePair currObjTypePair = objTypePair.toMoreSpecificType();
Class<?> topLevelClass = new TypeInfo(currObjTypePair.getType()).getRawClass(); Class<?> topLevelClass = new TypeInfo(currObjTypePair.getType()).getRawClass();
for (Class<?> curr = topLevelClass; curr != null && !curr.equals(Object.class); for (Class<?> curr = topLevelClass; curr != null && !curr.equals(Object.class);
curr = curr.getSuperclass()) { curr = curr.getSuperclass()) {

View File

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

View File

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