Share the same JsonNull instance with the Gson code.

This commit is contained in:
Joel Leitch 2008-12-18 20:14:29 +00:00
parent 17256b8ce5
commit a18a751240
6 changed files with 22 additions and 10 deletions

View File

@ -393,7 +393,7 @@ final class DefaultTypeAdapters {
public JsonElement serialize(Collection src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(Collection src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) { if (src == null) {
return new JsonNull(); return JsonNull.createJsonNull();
} }
JsonArray array = new JsonArray(); JsonArray array = new JsonArray();
Type childGenericType = null; Type childGenericType = null;
@ -457,7 +457,7 @@ final class DefaultTypeAdapters {
JsonElement valueElement; JsonElement valueElement;
if (value == null) { if (value == null) {
valueElement = new JsonNull(); valueElement = JsonNull.createJsonNull();
} else { } else {
Type childType = (childGenericType == null) ? Type childType = (childGenericType == null) ?
childType = value.getClass() : childGenericType; childType = value.getClass() : childGenericType;

View File

@ -24,6 +24,7 @@ package com.google.gson;
* @since 1.2 * @since 1.2
*/ */
public final class JsonNull extends JsonElement { public final class JsonNull extends JsonElement {
private static final JsonNull INSTANCE = new JsonNull();
/** /**
* Creates a new JsonNull object. * Creates a new JsonNull object.
@ -52,4 +53,15 @@ public final class JsonNull extends JsonElement {
public boolean equals(Object other) { public boolean equals(Object other) {
return other instanceof JsonNull; return other instanceof JsonNull;
} }
/**
* Creation method used to return an instance of a {@link JsonNull}. To reduce the memory
* footprint, a single object has been created for this class; therefore the same instance is
* being returned for each invocation of this method.
*
* @return a instance of a {@link JsonNull}
*/
static JsonNull createJsonNull() {
return INSTANCE;
}
} }

View File

@ -116,7 +116,7 @@ public final class JsonObject extends JsonElement {
*/ */
private JsonElement createJsonElement(Object value) { private JsonElement createJsonElement(Object value) {
if (value == null) { if (value == null) {
return new JsonNull(); return JsonNull.createJsonNull();
} else { } else {
return new JsonPrimitive(value); return new JsonPrimitive(value);
} }
@ -151,7 +151,7 @@ public final class JsonObject extends JsonElement {
public JsonElement get(String memberName) { public JsonElement get(String memberName) {
if (members.containsKey(memberName)) { if (members.containsKey(memberName)) {
JsonElement member = members.get(memberName); JsonElement member = members.get(memberName);
return member == null ? new JsonNull() : member; return member == null ? JsonNull.createJsonNull() : member;
} else { } else {
return null; return null;
} }

View File

@ -54,7 +54,7 @@ final class JsonParser implements JsonParserConstants {
Token t; Token t;
t = jj_consume_token(NULL); t = jj_consume_token(NULL);
{if (true) {if (true)
return new JsonNull();} return JsonNull.createJsonNull();}
throw new Error("Missing return statement in function"); throw new Error("Missing return statement in function");
} }

View File

@ -67,7 +67,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
public void visitArrayField(Field f, Type typeOfF, Object obj) { public void visitArrayField(Field f, Type typeOfF, Object obj) {
if (isFieldNull(f, obj)) { if (isFieldNull(f, obj)) {
if (serializeNulls) { if (serializeNulls) {
addChildAsElement(f, new JsonNull()); addChildAsElement(f, JsonNull.createJsonNull());
} }
} else { } else {
Object array = getFieldValue(f, obj); Object array = getFieldValue(f, obj);
@ -78,7 +78,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
public void visitObjectField(Field f, Type typeOfF, Object obj) { public void visitObjectField(Field f, Type typeOfF, Object obj) {
if (isFieldNull(f, obj)) { if (isFieldNull(f, obj)) {
if (serializeNulls) { if (serializeNulls) {
addChildAsElement(f, new JsonNull()); addChildAsElement(f, JsonNull.createJsonNull());
} }
} else { } else {
Object fieldValue = getFieldValue(f, obj); Object fieldValue = getFieldValue(f, obj);
@ -127,7 +127,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
JsonSerializer serializer = serializers.getHandlerFor(objType); JsonSerializer serializer = serializers.getHandlerFor(objType);
if (serializer != null) { if (serializer != null) {
if (obj == null) { if (obj == null) {
assignToRoot(new JsonNull()); assignToRoot(JsonNull.createJsonNull());
} else { } else {
assignToRoot(serializer.serialize(obj, objType, context)); assignToRoot(serializer.serialize(obj, objType, context));
} }
@ -143,7 +143,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
Object obj = f.get(parent); Object obj = f.get(parent);
if (obj == null) { if (obj == null) {
if (serializeNulls) { if (serializeNulls) {
addChildAsElement(f, new JsonNull()); addChildAsElement(f, JsonNull.createJsonNull());
} }
return true; return true;
} }

View File

@ -78,7 +78,7 @@ private JsonNull JsonNull() :
Token t; Token t;
} }
{ {
t = <NULL> { return new JsonNull(); } t = <NULL> { return JsonNull.createJsonNull(); }
} }
private void Members(JsonObject o) : private void Members(JsonObject o) :