Skip to content

Non-lenient JsonWriter.value for NaN or Infinity writes name before throwing exception #1736

Description

@Marcono1234

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());
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions