Share the same JsonNull instance with the Gson code.
This commit is contained in:
parent
17256b8ce5
commit
a18a751240
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ private JsonNull JsonNull() :
|
||||
Token t;
|
||||
}
|
||||
{
|
||||
t = <NULL> { return new JsonNull(); }
|
||||
t = <NULL> { return JsonNull.createJsonNull(); }
|
||||
}
|
||||
|
||||
private void Members(JsonObject o) :
|
||||
|
Loading…
Reference in New Issue
Block a user