Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gson/src/main/java/com/google/gson/stream/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,10 @@ public JsonWriter value(Boolean value) throws IOException {
*/
@CanIgnoreReturnValue
public JsonWriter value(float value) throws IOException {
writeDeferredName();
if (strictness != Strictness.LENIENT && (Float.isNaN(value) || Float.isInfinite(value))) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
writeDeferredName();
beforeValue();
out.append(Float.toString(value));
return this;
Expand All @@ -596,10 +596,10 @@ public JsonWriter value(float value) throws IOException {
*/
@CanIgnoreReturnValue
public JsonWriter value(double value) throws IOException {
writeDeferredName();
if (strictness != Strictness.LENIENT && (Double.isNaN(value) || Double.isInfinite(value))) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
writeDeferredName();
beforeValue();
out.append(Double.toString(value));
return this;
Expand Down Expand Up @@ -635,7 +635,6 @@ public JsonWriter value(Number value) throws IOException {
return nullValue();
}

writeDeferredName();
String string = value.toString();
Class<? extends Number> numberClass = value.getClass();

Expand All @@ -653,6 +652,7 @@ public JsonWriter value(Number value) throws IOException {
}
}

writeDeferredName();
beforeValue();
out.append(string);
return this;
Expand Down
45 changes: 45 additions & 0 deletions gson/src/test/java/com/google/gson/stream/JsonWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,51 @@ public void testNonFiniteNumbersWhenLenient() throws IOException {
assertThat(stringWriter.toString()).isEqualTo("[NaN,-Infinity,Infinity,Infinity]");
}

/**
* Regression test for https://github.com/google/gson/issues/1736
*
* <p>When a non-finite value is rejected, the deferred name should not have been written yet, so
* the writer state remains consistent.
*/
@Test
public void testNonFiniteValueDoesNotWriteDeferredName() throws IOException {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
jsonWriter.setSerializeNulls(false);
jsonWriter.beginObject();

// value(float) with NaN should not write the name
jsonWriter.name("a");
assertThrows(IllegalArgumentException.class, () -> jsonWriter.value(Float.NaN));
jsonWriter.nullValue(); // should be suppressed since serializeNulls=false

// value(double) with NaN should not write the name
jsonWriter.name("b");
assertThrows(IllegalArgumentException.class, () -> jsonWriter.value(Double.NaN));
jsonWriter.nullValue();

// value(double) with Infinity should not write the name
jsonWriter.name("c");
assertThrows(IllegalArgumentException.class, () -> jsonWriter.value(Double.POSITIVE_INFINITY));
jsonWriter.nullValue();

// value(Number) with NaN should not write the name
jsonWriter.name("d");
assertThrows(
IllegalArgumentException.class, () -> jsonWriter.value(Double.valueOf(Double.NaN)));
jsonWriter.nullValue();

// value(Number) with invalid number string should not write the name
jsonWriter.name("e");
assertThrows(
IllegalArgumentException.class,
() -> jsonWriter.value(new LazilyParsedNumber("not-a-num")));
jsonWriter.nullValue();

jsonWriter.endObject();
assertThat(stringWriter.toString()).isEqualTo("{}");
}

@Test
public void testFloats() throws IOException {
StringWriter stringWriter = new StringWriter();
Expand Down
Loading