Skip to content

Commit bda2e3d

Browse files
authored
Improve number strategy implementation (#1987)
* Fix GsonBuilder not copying number strategies from Gson * Improve ToNumberPolicy exception messages
1 parent cd748df commit bda2e3d

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

gson/src/main/java/com/google/gson/GsonBuilder.java

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public GsonBuilder() {
130130
this.timeStyle = gson.timeStyle;
131131
this.factories.addAll(gson.builderFactories);
132132
this.hierarchyFactories.addAll(gson.builderHierarchyFactories);
133+
this.objectToNumberStrategy = gson.objectToNumberStrategy;
134+
this.numberToNumberStrategy = gson.numberToNumberStrategy;
133135
}
134136

135137
/**

gson/src/main/java/com/google/gson/ToNumberPolicy.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public enum ToNumberPolicy implements ToNumberStrategy {
7171
try {
7272
Double d = Double.valueOf(value);
7373
if ((d.isInfinite() || d.isNaN()) && !in.isLenient()) {
74-
throw new MalformedJsonException("JSON forbids NaN and infinities: " + d + in);
74+
throw new MalformedJsonException("JSON forbids NaN and infinities: " + d + "; at path " + in.getPath());
7575
}
7676
return d;
7777
} catch (NumberFormatException doubleE) {
78-
throw new JsonParseException("Cannot parse " + value, doubleE);
78+
throw new JsonParseException("Cannot parse " + value + "; at path " + in.getPath(), doubleE);
7979
}
8080
}
8181
}
@@ -91,7 +91,7 @@ public enum ToNumberPolicy implements ToNumberStrategy {
9191
try {
9292
return new BigDecimal(value);
9393
} catch (NumberFormatException e) {
94-
throw new JsonParseException("Cannot parse " + value, e);
94+
throw new JsonParseException("Cannot parse " + value + "; at path " + in.getPath(), e);
9595
}
9696
}
9797
}

gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ JsonElement nextJsonElement() throws IOException {
283283
}
284284

285285
@Override public String toString() {
286-
return getClass().getSimpleName();
286+
return getClass().getSimpleName() + locationString();
287287
}
288288

289289
public void promoteNameToValue() throws IOException {

gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public void testDouble() throws IOException {
3333
strategy.readNumber(fromString("1e400"));
3434
fail();
3535
} catch (MalformedJsonException expected) {
36+
assertEquals("JSON forbids NaN and infinities: Infinity at line 1 column 6 path $", expected.getMessage());
37+
}
38+
try {
39+
strategy.readNumber(fromString("\"not-a-number\""));
40+
fail();
41+
} catch (NumberFormatException expected) {
3642
}
3743
}
3844

@@ -52,24 +58,35 @@ public void testLongOrDouble() throws IOException {
5258
strategy.readNumber(fromString("1e400"));
5359
fail();
5460
} catch (MalformedJsonException expected) {
61+
assertEquals("JSON forbids NaN and infinities: Infinity; at path $", expected.getMessage());
62+
}
63+
try {
64+
strategy.readNumber(fromString("\"not-a-number\""));
65+
fail();
66+
} catch (JsonParseException expected) {
67+
assertEquals("Cannot parse not-a-number; at path $", expected.getMessage());
5568
}
69+
5670
assertEquals(Double.NaN, strategy.readNumber(fromStringLenient("NaN")));
5771
assertEquals(Double.POSITIVE_INFINITY, strategy.readNumber(fromStringLenient("Infinity")));
5872
assertEquals(Double.NEGATIVE_INFINITY, strategy.readNumber(fromStringLenient("-Infinity")));
5973
try {
6074
strategy.readNumber(fromString("NaN"));
6175
fail();
6276
} catch (MalformedJsonException expected) {
77+
assertEquals("Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $", expected.getMessage());
6378
}
6479
try {
6580
strategy.readNumber(fromString("Infinity"));
6681
fail();
6782
} catch (MalformedJsonException expected) {
83+
assertEquals("Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $", expected.getMessage());
6884
}
6985
try {
7086
strategy.readNumber(fromString("-Infinity"));
7187
fail();
7288
} catch (MalformedJsonException expected) {
89+
assertEquals("Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $", expected.getMessage());
7390
}
7491
}
7592

@@ -78,6 +95,13 @@ public void testBigDecimal() throws IOException {
7895
assertEquals(new BigDecimal("10.1"), strategy.readNumber(fromString("10.1")));
7996
assertEquals(new BigDecimal("3.141592653589793238462643383279"), strategy.readNumber(fromString("3.141592653589793238462643383279")));
8097
assertEquals(new BigDecimal("1e400"), strategy.readNumber(fromString("1e400")));
98+
99+
try {
100+
strategy.readNumber(fromString("\"not-a-number\""));
101+
fail();
102+
} catch (JsonParseException expected) {
103+
assertEquals("Cannot parse not-a-number; at path $", expected.getMessage());
104+
}
81105
}
82106

83107
public void testNullsAreNeverExpected() throws IOException {

0 commit comments

Comments
 (0)