diff --git a/moshi/src/main/java/com/squareup/moshi/JsonReader.java b/moshi/src/main/java/com/squareup/moshi/JsonReader.java index d84026c..b42f36f 100644 --- a/moshi/src/main/java/com/squareup/moshi/JsonReader.java +++ b/moshi/src/main/java/com/squareup/moshi/JsonReader.java @@ -899,7 +899,7 @@ public class JsonReader implements Closeable { peeked = PEEKED_BUFFERED; double result = Double.parseDouble(peekedString); // don't catch this NumberFormatException. if (!lenient && (Double.isNaN(result) || Double.isInfinite(result))) { - throw new IOException("JSON forbids NaN and infinities: " + result + throw new NumberFormatException("JSON forbids NaN and infinities: " + result + " at path " + getPath()); } peekedString = null; diff --git a/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java b/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java index d9c10a7..ad75957 100644 --- a/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java +++ b/moshi/src/main/java/com/squareup/moshi/StandardJsonAdapters.java @@ -103,18 +103,20 @@ final class StandardJsonAdapters { static final JsonAdapter FLOAT_JSON_ADAPTER = new JsonAdapter() { @Override public Float fromJson(JsonReader reader) throws IOException { float value = (float) reader.nextDouble(); + // Double check for infinity after float conversion; many doubles > Float.MAX if (!reader.isLenient() && Float.isInfinite(value)) { - throw new IOException("JSON forbids NaN and infinities: " + value + throw new NumberFormatException("JSON forbids NaN and infinities: " + value + " at path " + reader.getPath()); } return value; } @Override public void toJson(JsonWriter writer, Float value) throws IOException { - // Use the Number overload. + // Manual null pointer check for the float.class adapter. if (value == null) { throw new NullPointerException(); } + // Use the Number overload so we write out float precision instead of double precision. writer.value(value); } }; diff --git a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java index c8e2c39..77c7bd3 100644 --- a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java +++ b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java @@ -269,8 +269,7 @@ public final class MoshiTest { try { adapter.fromJson(reader); fail(); - } catch (IOException expected) { - // TODO: should this really be NumberFormatException? + } catch (NumberFormatException expected) { assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[0]"); } @@ -279,8 +278,7 @@ public final class MoshiTest { try { adapter.fromJson(reader); fail(); - } catch (IOException expected) { - // TODO: should this really be NumberFormatException? + } catch (NumberFormatException expected) { assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: -Infinity at path $[0]"); } } @@ -339,8 +337,7 @@ public final class MoshiTest { try { adapter.fromJson(reader); fail(); - } catch (IOException expected) { - // TODO: should this really be NumberFormatException? + } catch (NumberFormatException expected) { assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: Infinity at path $[1]"); } @@ -349,8 +346,7 @@ public final class MoshiTest { try { adapter.fromJson(reader); fail(); - } catch (IOException expected) { - // TODO: should this really be NumberFormatException? + } catch (NumberFormatException expected) { assertThat(expected.getMessage()).isEqualTo("JSON forbids NaN and infinities: -Infinity at path $[1]"); } }