Add comments, fix nits

This commit is contained in:
Scott Blum
2014-08-14 01:38:44 -04:00
parent 1e4f375e75
commit 9a713f80ed
3 changed files with 9 additions and 11 deletions

View File

@@ -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;

View File

@@ -103,18 +103,20 @@ final class StandardJsonAdapters {
static final JsonAdapter<Float> FLOAT_JSON_ADAPTER = new JsonAdapter<Float>() {
@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);
}
};

View File

@@ -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]");
}
}