Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbda2e3d

Browse files
authored
Improve number strategy implementation (#1987)
* Fix GsonBuilder not copying number strategies from Gson* Improve ToNumberPolicy exception messages
1 parentcd748df commitbda2e3d

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‎

Lines changed: 2 additions & 0 deletions
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‎

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ JsonElement nextJsonElement() throws IOException {
283283
}
284284

285285
@OverridepublicStringtoString() {
286-
returngetClass().getSimpleName();
286+
returngetClass().getSimpleName() +locationString();
287287
}
288288

289289
publicvoidpromoteNameToValue()throwsIOException {

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

Lines changed: 24 additions & 0 deletions
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 (MalformedJsonExceptionexpected) {
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 (NumberFormatExceptionexpected) {
3642
}
3743
}
3844

@@ -52,24 +58,35 @@ public void testLongOrDouble() throws IOException {
5258
strategy.readNumber(fromString("1e400"));
5359
fail();
5460
}catch (MalformedJsonExceptionexpected) {
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 (JsonParseExceptionexpected) {
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 (MalformedJsonExceptionexpected) {
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 (MalformedJsonExceptionexpected) {
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 (MalformedJsonExceptionexpected) {
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(newBigDecimal("10.1"),strategy.readNumber(fromString("10.1")));
7996
assertEquals(newBigDecimal("3.141592653589793238462643383279"),strategy.readNumber(fromString("3.141592653589793238462643383279")));
8097
assertEquals(newBigDecimal("1e400"),strategy.readNumber(fromString("1e400")));
98+
99+
try {
100+
strategy.readNumber(fromString("\"not-a-number\""));
101+
fail();
102+
}catch (JsonParseExceptionexpected) {
103+
assertEquals("Cannot parse not-a-number; at path $",expected.getMessage());
104+
}
81105
}
82106

83107
publicvoidtestNullsAreNeverExpected()throwsIOException {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp