More consistency on serializeNulls: we never emit the empty string for top-level objects.

This commit is contained in:
Jesse Wilson 2011-10-01 01:10:11 +00:00
parent de835d4dcd
commit 38ce53766e
8 changed files with 14 additions and 20 deletions

View File

@ -574,7 +574,7 @@ public final class Gson {
boolean oldSerializeNulls = writer.getSerializeNulls();
writer.setSerializeNulls(serializeNulls);
try {
Streams.write(jsonElement, serializeNulls, writer);
Streams.write(jsonElement, writer);
} catch (IOException e) {
throw new JsonIOException(e);
} finally {

View File

@ -98,7 +98,7 @@ final class GsonToMiniGsonTypeAdapterFactory implements TypeAdapter.Factory {
return;
}
JsonElement element = serializer.serialize(value, type, serializationContext);
Streams.write(element, serializeNulls, writer);
Streams.write(element, writer);
}
private TypeAdapter<T> delegate() {

View File

@ -333,7 +333,7 @@ public abstract class JsonElement {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setLenient(true);
Streams.write(this, true, jsonWriter);
Streams.write(this, jsonWriter);
return stringWriter.toString();
} catch (IOException e) {
throw new AssertionError(e);

View File

@ -104,13 +104,10 @@ public final class Streams {
/**
* Writes the JSON element to the writer, recursively.
*/
public static void write(JsonElement element, boolean serializeNulls, JsonWriter writer)
public static void write(JsonElement element, JsonWriter writer)
throws IOException {
if (element == null || element.isJsonNull()) {
if (serializeNulls) {
writer.nullValue();
}
writer.nullValue();
} else if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive();
if (primitive.isNumber()) {
@ -129,7 +126,7 @@ public final class Streams {
writer.nullValue();
continue;
}
write(e, serializeNulls, writer);
write(e, writer);
}
writer.endArray();
@ -137,11 +134,8 @@ public final class Streams {
writer.beginObject();
for (Map.Entry<String, JsonElement> e : element.getAsJsonObject().entrySet()) {
JsonElement value = e.getValue();
if (!serializeNulls && value.isJsonNull()) {
continue;
}
writer.name(e.getKey());
write(value, serializeNulls, writer);
write(value, writer);
}
writer.endObject();

View File

@ -134,7 +134,7 @@ public final class MapTypeAdapterFactory implements TypeAdapter.Factory {
private final TypeAdapter<V> valueTypeAdapter;
private final ObjectConstructor<? extends Map<K, V>> constructor;
public Adapter(MiniGson context, Type keyType, TypeAdapter<K> keyTypeAdapter,
public Adapter(MiniGson context, Type keyType, TypeAdapter<K> keyTypeAdapter,
Type valueType, TypeAdapter<V> valueTypeAdapter,
ObjectConstructor<? extends Map<K, V>> constructor) {
this.keyTypeAdapter =
@ -203,7 +203,7 @@ public final class MapTypeAdapterFactory implements TypeAdapter.Factory {
writer.beginArray();
for (int i = 0; i < keys.size(); i++) {
writer.beginArray(); // entry array
Streams.write(keys.get(i), true, writer);
Streams.write(keys.get(i), writer);
valueTypeAdapter.write(writer, values.get(i));
writer.endArray();
}

View File

@ -163,7 +163,7 @@ public final class MixedStreamTest extends TestCase {
StringWriter stringWriter = new StringWriter();
gson.toJson(null, new JsonWriter(stringWriter));
assertEquals("", stringWriter.toString());
assertEquals("null", stringWriter.toString());
}
public void testReadNulls() {

View File

@ -139,7 +139,7 @@ public class ObjectTest extends TestCase {
assertEquals(json, target.getExpectedJson());
}
public void testNullSerialization() throws Exception {
assertEquals("", gson.toJson(null));
assertEquals("null", gson.toJson(null));
}
public void testEmptyStringDeserialization() throws Exception {

View File

@ -62,7 +62,7 @@ public class ReadersWritersTest extends TestCase {
public void testTopLevelNullObjectSerializationWithWriter() {
StringWriter writer = new StringWriter();
gson.toJson(null, writer);
assertEquals("", writer.toString());
assertEquals("null", writer.toString());
}
public void testTopLevelNullObjectDeserializationWithReader() {
@ -84,7 +84,7 @@ public class ReadersWritersTest extends TestCase {
Integer nullIntObject = gson.fromJson(reader, Integer.class);
assertNull(nullIntObject);
}
public void testReadWriteTwoStrings() throws IOException {
Gson gson= new Gson();
CharArrayWriter writer= new CharArrayWriter();
@ -97,7 +97,7 @@ public class ReadersWritersTest extends TestCase {
String actualTwo = gson.fromJson(parser.next(), String.class);
assertEquals("two", actualTwo);
}
public void testReadWriteTwoObjects() throws IOException {
Gson gson= new Gson();
CharArrayWriter writer= new CharArrayWriter();