Don't serialize nulls in JsonElementWriter if setSerializeNulls(false)
This commit is contained in:
parent
9b7b49bf0f
commit
ed2b25ddef
@ -71,8 +71,10 @@ public final class JsonElementWriter extends JsonWriter {
|
||||
|
||||
private void put(JsonElement value) {
|
||||
if (pendingName != null) {
|
||||
JsonObject object = (JsonObject) peek();
|
||||
object.add(pendingName, value);
|
||||
if (!value.isJsonNull() || getSerializeNulls()) {
|
||||
JsonObject object = (JsonObject) peek();
|
||||
object.add(pendingName, value);
|
||||
}
|
||||
pendingName = null;
|
||||
} else if (stack.isEmpty()) {
|
||||
product = value;
|
||||
|
@ -23,7 +23,6 @@ public final class JsonElementWriterTest extends TestCase {
|
||||
|
||||
// TODO: more tests
|
||||
// TODO: figure out what should be returned by an empty writer
|
||||
// TODO: test when serialize nulls is false
|
||||
|
||||
public void testArray() throws IOException {
|
||||
JsonElementWriter writer = new JsonElementWriter();
|
||||
@ -97,4 +96,24 @@ public final class JsonElementWriterTest extends TestCase {
|
||||
} catch (IOException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testSerializeNullsFalse() throws IOException {
|
||||
JsonElementWriter writer = new JsonElementWriter();
|
||||
writer.setSerializeNulls(false);
|
||||
writer.beginObject();
|
||||
writer.name("A");
|
||||
writer.nullValue();
|
||||
writer.endObject();
|
||||
assertEquals("{}", writer.get().toString());
|
||||
}
|
||||
|
||||
public void testSerializeNullsTrue() throws IOException {
|
||||
JsonElementWriter writer = new JsonElementWriter();
|
||||
writer.setSerializeNulls(true);
|
||||
writer.beginObject();
|
||||
writer.name("A");
|
||||
writer.nullValue();
|
||||
writer.endObject();
|
||||
assertEquals("{\"A\":null}", writer.get().toString());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user