From 439db9d16aa9c817940323f32585ff5523885220 Mon Sep 17 00:00:00 2001 From: Nathan Herring Date: Fri, 10 Jun 2022 16:25:32 -0700 Subject: [PATCH] Add explicit support for floats in JsonTreeWriter. Follow-up to comments on #2130, which introduced a new override which was not overridden by JsonTreeWriter. Also tweaks the doccomments for both float and double varients of JsonWriter.value. Supplement to the fix for #1127. --- .../gson/internal/bind/JsonTreeWriter.java | 8 +++++ .../com/google/gson/stream/JsonWriter.java | 9 ++--- .../internal/bind/JsonTreeWriterTest.java | 35 ++++++++++++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java index f1fce21c40..e28fbfeb34 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java @@ -170,6 +170,14 @@ private void put(JsonElement value) { return this; } + @Override public JsonWriter value(float value) throws IOException { + if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) { + throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); + } + put(new JsonPrimitive(value)); + return this; + } + @Override public JsonWriter value(double value) throws IOException { if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) { throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index 059915626f..061927fd66 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -493,8 +493,9 @@ public JsonWriter value(Boolean value) throws IOException { /** * Encodes {@code value}. * - * @param value a finite value. May not be {@link Float#isNaN() NaNs} or {@link Float#isInfinite() - * infinities}. + * @param value a finite value, or if {@link #setLenient(boolean) lenient}, + * also {@link Float#isNaN() NaN} or {@link Float#isInfinite() + * infinity}. * @return this writer. * @throws IllegalArgumentException if the value is NaN or Infinity and this writer is not {@link * #setLenient(boolean) lenient}. @@ -512,8 +513,8 @@ public JsonWriter value(float value) throws IOException { /** * Encodes {@code value}. * - * @param value a finite value. May not be {@link Double#isNaN() NaNs} or - * {@link Double#isInfinite() infinities}. + * @param value a finite value, or if {@link #setLenient(boolean) lenient}, + * also {@link Double#isNaN() NaN} or {@link Double#isInfinite() infinity}. * @return this writer. * @throws IllegalArgumentException if the value is NaN or Infinity and this writer is * not {@link #setLenient(boolean) lenient}. diff --git a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java index 094f04bd28..3167be1319 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java @@ -152,17 +152,35 @@ public void testLenientNansAndInfinities() throws IOException { JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(true); writer.beginArray(); + writer.value(Float.NaN); + writer.value(Float.NEGATIVE_INFINITY); + writer.value(Float.POSITIVE_INFINITY); writer.value(Double.NaN); writer.value(Double.NEGATIVE_INFINITY); writer.value(Double.POSITIVE_INFINITY); writer.endArray(); - assertEquals("[NaN,-Infinity,Infinity]", writer.get().toString()); + assertEquals("[NaN,-Infinity,Infinity,NaN,-Infinity,Infinity]", writer.get().toString()); } public void testStrictNansAndInfinities() throws IOException { JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(false); writer.beginArray(); + try { + writer.value(Float.NaN); + fail(); + } catch (IllegalArgumentException expected) { + } + try { + writer.value(Float.NEGATIVE_INFINITY); + fail(); + } catch (IllegalArgumentException expected) { + } + try { + writer.value(Float.POSITIVE_INFINITY); + fail(); + } catch (IllegalArgumentException expected) { + } try { writer.value(Double.NaN); fail(); @@ -184,6 +202,21 @@ public void testStrictBoxedNansAndInfinities() throws IOException { JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(false); writer.beginArray(); + try { + writer.value(Float.valueOf(Float.NaN)); + fail(); + } catch (IllegalArgumentException expected) { + } + try { + writer.value(Float.valueOf(Float.NEGATIVE_INFINITY)); + fail(); + } catch (IllegalArgumentException expected) { + } + try { + writer.value(Float.valueOf(Float.POSITIVE_INFINITY)); + fail(); + } catch (IllegalArgumentException expected) { + } try { writer.value(Double.valueOf(Double.NaN)); fail();