mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Add comments, fix nits
This commit is contained in:
@@ -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;
|
||||
|
@@ -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);
|
||||
}
|
||||
};
|
||||
|
@@ -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]");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user