For a non-lenient JsonWriter the methods value(double) and value(Number) write the name of the current JSON property before checking if the value is valid and throwing an exception.
This is problematic for JsonWriters which are supposed to not serialize properties with null values (setSerializeNulls(false)):
public void testSerializeNulls() throws IOException {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setSerializeNulls(false);
jsonWriter.beginObject();
// Make sure that methods throwing exceptions do not write
// name
jsonWriter.name("test");
try {
// value(double)
jsonWriter.value(Double.NaN);
} catch (IllegalArgumentException expected) {
}
jsonWriter.nullValue();
jsonWriter.endObject();
// Fails because `test:null` has been written
assertEquals("{}", stringWriter.toString());
}
For a non-lenient JsonWriter the methods
value(double)andvalue(Number)write the name of the current JSON property before checking if the value is valid and throwing an exception.This is problematic for JsonWriters which are supposed to not serialize properties with
nullvalues (setSerializeNulls(false)):