diff --git a/gson/src/main/java/com/google/gson/ToNumberPolicy.java b/gson/src/main/java/com/google/gson/ToNumberPolicy.java index 6380e5d986..7e9f85d0ca 100644 --- a/gson/src/main/java/com/google/gson/ToNumberPolicy.java +++ b/gson/src/main/java/com/google/gson/ToNumberPolicy.java @@ -68,22 +68,30 @@ public Number readNumber(JsonReader in) throws IOException { @Override public Number readNumber(JsonReader in) throws IOException, JsonParseException { String value = in.nextString(); - try { - return Long.parseLong(value); - } catch (NumberFormatException longE) { + if (value.indexOf('.') >= 0) { + return parseAsDouble(value, in); + } else { try { - Double d = Double.valueOf(value); - if ((d.isInfinite() || d.isNaN()) && !in.isLenient()) { - throw new MalformedJsonException( - "JSON forbids NaN and infinities: " + d + "; at path " + in.getPreviousPath()); - } - return d; - } catch (NumberFormatException doubleE) { - throw new JsonParseException( - "Cannot parse " + value + "; at path " + in.getPreviousPath(), doubleE); + return Long.parseLong(value); + } catch (NumberFormatException e) { + return parseAsDouble(value, in); } } } + + private Number parseAsDouble(String value, JsonReader in) throws IOException { + try { + Double d = Double.valueOf(value); + if ((d.isInfinite() || d.isNaN()) && !in.isLenient()) { + throw new MalformedJsonException( + "JSON forbids NaN and infinities: " + d + "; at path " + in.getPreviousPath()); + } + return d; + } catch (NumberFormatException e) { + throw new JsonParseException( + "Cannot parse " + value + "; at path " + in.getPreviousPath(), e); + } + } }, /**