From a18a751240fc1058f82eda2581298335e6e77c5f Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Thu, 18 Dec 2008 20:14:29 +0000 Subject: [PATCH] Share the same JsonNull instance with the Gson code. --- .../java/com/google/gson/DefaultTypeAdapters.java | 4 ++-- gson/src/main/java/com/google/gson/JsonNull.java | 12 ++++++++++++ gson/src/main/java/com/google/gson/JsonObject.java | 4 ++-- gson/src/main/java/com/google/gson/JsonParser.java | 2 +- .../com/google/gson/JsonSerializationVisitor.java | 8 ++++---- gson/src/main/javacc/JsonParser.jj | 2 +- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index a7000571..158469cc 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -393,7 +393,7 @@ final class DefaultTypeAdapters { public JsonElement serialize(Collection src, Type typeOfSrc, JsonSerializationContext context) { if (src == null) { - return new JsonNull(); + return JsonNull.createJsonNull(); } JsonArray array = new JsonArray(); Type childGenericType = null; @@ -457,7 +457,7 @@ final class DefaultTypeAdapters { JsonElement valueElement; if (value == null) { - valueElement = new JsonNull(); + valueElement = JsonNull.createJsonNull(); } else { Type childType = (childGenericType == null) ? childType = value.getClass() : childGenericType; diff --git a/gson/src/main/java/com/google/gson/JsonNull.java b/gson/src/main/java/com/google/gson/JsonNull.java index 107c6fb5..014fd4b5 100755 --- a/gson/src/main/java/com/google/gson/JsonNull.java +++ b/gson/src/main/java/com/google/gson/JsonNull.java @@ -24,6 +24,7 @@ package com.google.gson; * @since 1.2 */ public final class JsonNull extends JsonElement { + private static final JsonNull INSTANCE = new JsonNull(); /** * Creates a new JsonNull object. @@ -52,4 +53,15 @@ public final class JsonNull extends JsonElement { public boolean equals(Object other) { 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; + } } diff --git a/gson/src/main/java/com/google/gson/JsonObject.java b/gson/src/main/java/com/google/gson/JsonObject.java index 786d0915..7e5bc7d6 100644 --- a/gson/src/main/java/com/google/gson/JsonObject.java +++ b/gson/src/main/java/com/google/gson/JsonObject.java @@ -116,7 +116,7 @@ public final class JsonObject extends JsonElement { */ private JsonElement createJsonElement(Object value) { if (value == null) { - return new JsonNull(); + return JsonNull.createJsonNull(); } else { return new JsonPrimitive(value); } @@ -151,7 +151,7 @@ public final class JsonObject extends JsonElement { public JsonElement get(String memberName) { if (members.containsKey(memberName)) { JsonElement member = members.get(memberName); - return member == null ? new JsonNull() : member; + return member == null ? JsonNull.createJsonNull() : member; } else { return null; } diff --git a/gson/src/main/java/com/google/gson/JsonParser.java b/gson/src/main/java/com/google/gson/JsonParser.java index 8e079d8b..a9452803 100755 --- a/gson/src/main/java/com/google/gson/JsonParser.java +++ b/gson/src/main/java/com/google/gson/JsonParser.java @@ -54,7 +54,7 @@ final class JsonParser implements JsonParserConstants { Token t; t = jj_consume_token(NULL); {if (true) - return new JsonNull();} + return JsonNull.createJsonNull();} throw new Error("Missing return statement in function"); } diff --git a/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java b/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java index 9470a96b..0dfebcac 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java @@ -67,7 +67,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { public void visitArrayField(Field f, Type typeOfF, Object obj) { if (isFieldNull(f, obj)) { if (serializeNulls) { - addChildAsElement(f, new JsonNull()); + addChildAsElement(f, JsonNull.createJsonNull()); } } else { Object array = getFieldValue(f, obj); @@ -78,7 +78,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { public void visitObjectField(Field f, Type typeOfF, Object obj) { if (isFieldNull(f, obj)) { if (serializeNulls) { - addChildAsElement(f, new JsonNull()); + addChildAsElement(f, JsonNull.createJsonNull()); } } else { Object fieldValue = getFieldValue(f, obj); @@ -127,7 +127,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { JsonSerializer serializer = serializers.getHandlerFor(objType); if (serializer != null) { if (obj == null) { - assignToRoot(new JsonNull()); + assignToRoot(JsonNull.createJsonNull()); } else { assignToRoot(serializer.serialize(obj, objType, context)); } @@ -143,7 +143,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { Object obj = f.get(parent); if (obj == null) { if (serializeNulls) { - addChildAsElement(f, new JsonNull()); + addChildAsElement(f, JsonNull.createJsonNull()); } return true; } diff --git a/gson/src/main/javacc/JsonParser.jj b/gson/src/main/javacc/JsonParser.jj index 64bd3ab1..c5c349cc 100755 --- a/gson/src/main/javacc/JsonParser.jj +++ b/gson/src/main/javacc/JsonParser.jj @@ -78,7 +78,7 @@ private JsonNull JsonNull() : Token t; } { - t = { return new JsonNull(); } + t = { return JsonNull.createJsonNull(); } } private void Members(JsonObject o) :