From 7aa1d0f6aa144d04389eab2faab603c853b23909 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 24 Oct 2008 20:35:22 +0000 Subject: [PATCH] Reusing the JsonNull instance everywhere instead of recreating it. This is a minor optimization, which is possible because JsonNull is a stateless object. --- gson/src/main/java/com/google/gson/JsonNull.java | 2 ++ gson/src/main/java/com/google/gson/JsonParser.java | 2 +- .../java/com/google/gson/JsonSerializationVisitor.java | 8 ++++---- gson/src/main/javacc/JsonParser.jj | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonNull.java b/gson/src/main/java/com/google/gson/JsonNull.java index 2b6909c0..92f197e6 100755 --- a/gson/src/main/java/com/google/gson/JsonNull.java +++ b/gson/src/main/java/com/google/gson/JsonNull.java @@ -25,6 +25,8 @@ package com.google.gson; */ public final class JsonNull extends JsonElement { + static final JsonNull INSTANCE = new JsonNull(); + @Override protected void toString(StringBuilder sb) { sb.append("null"); diff --git a/gson/src/main/java/com/google/gson/JsonParser.java b/gson/src/main/java/com/google/gson/JsonParser.java index 8c79ec17..036c924e 100755 --- a/gson/src/main/java/com/google/gson/JsonParser.java +++ b/gson/src/main/java/com/google/gson/JsonParser.java @@ -51,7 +51,7 @@ final class JsonParser implements JsonParserConstants { } final private JsonNull JsonNull() throws ParseException { - JsonNull json = new JsonNull(); + JsonNull json = JsonNull.INSTANCE; jj_consume_token(22); {if (true) return json;} 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 e55bcc09..3a5fa043 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java @@ -82,7 +82,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.INSTANCE); } } else { Object array = getFieldValue(f, obj); @@ -93,7 +93,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { public void visitCollectionField(Field f, Type typeOfF, Object obj) { if (isFieldNull(f, obj)) { if (serializeNulls) { - addChildAsElement(f, new JsonNull()); + addChildAsElement(f, JsonNull.INSTANCE); } } else { if (typeOfF == null) { @@ -120,7 +120,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.INSTANCE); } } else { Object fieldValue = getFieldValue(f, obj); @@ -167,7 +167,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { public void visitPrimitiveField(Field f, Type typeOfF, Object obj) { if (isFieldNull(f, obj)) { if (serializeNulls) { - addChildAsElement(f, new JsonNull()); + addChildAsElement(f, JsonNull.INSTANCE); } } else { TypeInfo typeInfo = new TypeInfo(typeOfF); diff --git a/gson/src/main/javacc/JsonParser.jj b/gson/src/main/javacc/JsonParser.jj index d6a97a65..3b10257d 100755 --- a/gson/src/main/javacc/JsonParser.jj +++ b/gson/src/main/javacc/JsonParser.jj @@ -72,7 +72,7 @@ private JsonObject JsonObject() : private JsonNull JsonNull() : { - JsonNull json = new JsonNull(); + JsonNull json = JsonNull.INSTANCE; } { "null"