Skip to content

Commit d2aee65

Browse files
authored
Add explicit support for floats in JsonTreeWriter. (#2132)
Follow-up to comments on #2130, which introduced a new override which was not overridden by `JsonTreeWriter`. Also tweaks the doccomments for `float`, `double` and `Number` variants of `JsonWriter.value`. Supplement to the fix for #1127.
1 parent 08d4572 commit d2aee65

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ private void put(JsonElement value) {
170170
return this;
171171
}
172172

173+
@Override public JsonWriter value(float value) throws IOException {
174+
if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) {
175+
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
176+
}
177+
put(new JsonPrimitive(value));
178+
return this;
179+
}
180+
173181
@Override public JsonWriter value(double value) throws IOException {
174182
if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
175183
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);

gson/src/main/java/com/google/gson/stream/JsonWriter.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,9 @@ public JsonWriter value(Boolean value) throws IOException {
493493
/**
494494
* Encodes {@code value}.
495495
*
496-
* @param value a finite value. May not be {@link Float#isNaN() NaNs} or {@link Float#isInfinite()
497-
* infinities}.
496+
* @param value a finite value, or if {@link #setLenient(boolean) lenient},
497+
* also {@link Float#isNaN() NaN} or {@link Float#isInfinite()
498+
* infinity}.
498499
* @return this writer.
499500
* @throws IllegalArgumentException if the value is NaN or Infinity and this writer is not {@link
500501
* #setLenient(boolean) lenient}.
@@ -512,8 +513,8 @@ public JsonWriter value(float value) throws IOException {
512513
/**
513514
* Encodes {@code value}.
514515
*
515-
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
516-
* {@link Double#isInfinite() infinities}.
516+
* @param value a finite value, or if {@link #setLenient(boolean) lenient},
517+
* also {@link Double#isNaN() NaN} or {@link Double#isInfinite() infinity}.
517518
* @return this writer.
518519
* @throws IllegalArgumentException if the value is NaN or Infinity and this writer is
519520
* not {@link #setLenient(boolean) lenient}.
@@ -555,8 +556,8 @@ private static boolean isTrustedNumberType(Class<? extends Number> c) {
555556
* Encodes {@code value}. The value is written by directly writing the {@link Number#toString()}
556557
* result to JSON. Implementations must make sure that the result represents a valid JSON number.
557558
*
558-
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
559-
* {@link Double#isInfinite() infinities}.
559+
* @param value a finite value, or if {@link #setLenient(boolean) lenient},
560+
* also {@link Double#isNaN() NaN} or {@link Double#isInfinite() infinity}.
560561
* @return this writer.
561562
* @throws IllegalArgumentException if the value is NaN or Infinity and this writer is
562563
* not {@link #setLenient(boolean) lenient}; or if the {@code toString()} result is not a

gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,35 @@ public void testLenientNansAndInfinities() throws IOException {
152152
JsonTreeWriter writer = new JsonTreeWriter();
153153
writer.setLenient(true);
154154
writer.beginArray();
155+
writer.value(Float.NaN);
156+
writer.value(Float.NEGATIVE_INFINITY);
157+
writer.value(Float.POSITIVE_INFINITY);
155158
writer.value(Double.NaN);
156159
writer.value(Double.NEGATIVE_INFINITY);
157160
writer.value(Double.POSITIVE_INFINITY);
158161
writer.endArray();
159-
assertEquals("[NaN,-Infinity,Infinity]", writer.get().toString());
162+
assertEquals("[NaN,-Infinity,Infinity,NaN,-Infinity,Infinity]", writer.get().toString());
160163
}
161164

162165
public void testStrictNansAndInfinities() throws IOException {
163166
JsonTreeWriter writer = new JsonTreeWriter();
164167
writer.setLenient(false);
165168
writer.beginArray();
169+
try {
170+
writer.value(Float.NaN);
171+
fail();
172+
} catch (IllegalArgumentException expected) {
173+
}
174+
try {
175+
writer.value(Float.NEGATIVE_INFINITY);
176+
fail();
177+
} catch (IllegalArgumentException expected) {
178+
}
179+
try {
180+
writer.value(Float.POSITIVE_INFINITY);
181+
fail();
182+
} catch (IllegalArgumentException expected) {
183+
}
166184
try {
167185
writer.value(Double.NaN);
168186
fail();
@@ -184,6 +202,21 @@ public void testStrictBoxedNansAndInfinities() throws IOException {
184202
JsonTreeWriter writer = new JsonTreeWriter();
185203
writer.setLenient(false);
186204
writer.beginArray();
205+
try {
206+
writer.value(Float.valueOf(Float.NaN));
207+
fail();
208+
} catch (IllegalArgumentException expected) {
209+
}
210+
try {
211+
writer.value(Float.valueOf(Float.NEGATIVE_INFINITY));
212+
fail();
213+
} catch (IllegalArgumentException expected) {
214+
}
215+
try {
216+
writer.value(Float.valueOf(Float.POSITIVE_INFINITY));
217+
fail();
218+
} catch (IllegalArgumentException expected) {
219+
}
187220
try {
188221
writer.value(Double.valueOf(Double.NaN));
189222
fail();

0 commit comments

Comments
 (0)