Reusing the JsonNull instance everywhere instead of recreating it.

This is a minor optimization, which is possible because JsonNull is a stateless object.
This commit is contained in:
Inderjeet Singh 2008-10-24 20:35:22 +00:00
parent 3cd665b199
commit 7aa1d0f6aa
4 changed files with 8 additions and 6 deletions

View File

@ -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");

View File

@ -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");

View File

@ -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);

View File

@ -72,7 +72,7 @@ private JsonObject JsonObject() :
private JsonNull JsonNull() :
{
JsonNull json = new JsonNull();
JsonNull json = JsonNull.INSTANCE;
}
{
"null"