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) {
|
private void put(JsonElement value) {
|
||||||
if (pendingName != null) {
|
if (pendingName != null) {
|
||||||
JsonObject object = (JsonObject) peek();
|
if (!value.isJsonNull() || getSerializeNulls()) {
|
||||||
object.add(pendingName, value);
|
JsonObject object = (JsonObject) peek();
|
||||||
|
object.add(pendingName, value);
|
||||||
|
}
|
||||||
pendingName = null;
|
pendingName = null;
|
||||||
} else if (stack.isEmpty()) {
|
} else if (stack.isEmpty()) {
|
||||||
product = value;
|
product = value;
|
||||||
|
|
|
@ -23,7 +23,6 @@ public final class JsonElementWriterTest extends TestCase {
|
||||||
|
|
||||||
// TODO: more tests
|
// TODO: more tests
|
||||||
// TODO: figure out what should be returned by an empty writer
|
// TODO: figure out what should be returned by an empty writer
|
||||||
// TODO: test when serialize nulls is false
|
|
||||||
|
|
||||||
public void testArray() throws IOException {
|
public void testArray() throws IOException {
|
||||||
JsonElementWriter writer = new JsonElementWriter();
|
JsonElementWriter writer = new JsonElementWriter();
|
||||||
|
@ -97,4 +96,24 @@ public final class JsonElementWriterTest extends TestCase {
|
||||||
} catch (IOException expected) {
|
} 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