@@ -347,8 +347,7 @@ public void beginArray() throws IOException {
347
347
pathIndices [stackSize - 1 ] = 0 ;
348
348
peeked = PEEKED_NONE ;
349
349
} else {
350
- throw new IllegalStateException ("Expected BEGIN_ARRAY but was " + peek ()
351
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
350
+ throw new IllegalStateException ("Expected BEGIN_ARRAY but was " + peek () + locationString ());
352
351
}
353
352
}
354
353
@@ -366,8 +365,7 @@ public void endArray() throws IOException {
366
365
pathIndices [stackSize - 1 ]++;
367
366
peeked = PEEKED_NONE ;
368
367
} else {
369
- throw new IllegalStateException ("Expected END_ARRAY but was " + peek ()
370
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
368
+ throw new IllegalStateException ("Expected END_ARRAY but was " + peek () + locationString ());
371
369
}
372
370
}
373
371
@@ -384,8 +382,7 @@ public void beginObject() throws IOException {
384
382
push (JsonScope .EMPTY_OBJECT );
385
383
peeked = PEEKED_NONE ;
386
384
} else {
387
- throw new IllegalStateException ("Expected BEGIN_OBJECT but was " + peek ()
388
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
385
+ throw new IllegalStateException ("Expected BEGIN_OBJECT but was " + peek () + locationString ());
389
386
}
390
387
}
391
388
@@ -404,8 +401,7 @@ public void endObject() throws IOException {
404
401
pathIndices [stackSize - 1 ]++;
405
402
peeked = PEEKED_NONE ;
406
403
} else {
407
- throw new IllegalStateException ("Expected END_OBJECT but was " + peek ()
408
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
404
+ throw new IllegalStateException ("Expected END_OBJECT but was " + peek () + locationString ());
409
405
}
410
406
}
411
407
@@ -790,8 +786,7 @@ public String nextName() throws IOException {
790
786
} else if (p == PEEKED_DOUBLE_QUOTED_NAME ) {
791
787
result = nextQuotedValue ('"' );
792
788
} else {
793
- throw new IllegalStateException ("Expected a name but was " + peek ()
794
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
789
+ throw new IllegalStateException ("Expected a name but was " + peek () + locationString ());
795
790
}
796
791
peeked = PEEKED_NONE ;
797
792
pathNames [stackSize - 1 ] = result ;
@@ -827,8 +822,7 @@ public String nextString() throws IOException {
827
822
result = new String (buffer , pos , peekedNumberLength );
828
823
pos += peekedNumberLength ;
829
824
} else {
830
- throw new IllegalStateException ("Expected a string but was " + peek ()
831
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
825
+ throw new IllegalStateException ("Expected a string but was " + peek () + locationString ());
832
826
}
833
827
peeked = PEEKED_NONE ;
834
828
pathIndices [stackSize - 1 ]++;
@@ -856,8 +850,7 @@ public boolean nextBoolean() throws IOException {
856
850
pathIndices [stackSize - 1 ]++;
857
851
return false ;
858
852
}
859
- throw new IllegalStateException ("Expected a boolean but was " + peek ()
860
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
853
+ throw new IllegalStateException ("Expected a boolean but was " + peek () + locationString ());
861
854
}
862
855
863
856
/**
@@ -876,8 +869,7 @@ public void nextNull() throws IOException {
876
869
peeked = PEEKED_NONE ;
877
870
pathIndices [stackSize - 1 ]++;
878
871
} else {
879
- throw new IllegalStateException ("Expected null but was " + peek ()
880
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
872
+ throw new IllegalStateException ("Expected null but was " + peek () + locationString ());
881
873
}
882
874
}
883
875
@@ -910,15 +902,14 @@ public double nextDouble() throws IOException {
910
902
} else if (p == PEEKED_UNQUOTED ) {
911
903
peekedString = nextUnquotedValue ();
912
904
} else if (p != PEEKED_BUFFERED ) {
913
- throw new IllegalStateException ("Expected a double but was " + peek ()
914
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
905
+ throw new IllegalStateException ("Expected a double but was " + peek () + locationString ());
915
906
}
916
907
917
908
peeked = PEEKED_BUFFERED ;
918
909
double result = Double .parseDouble (peekedString ); // don't catch this NumberFormatException.
919
910
if (!lenient && (Double .isNaN (result ) || Double .isInfinite (result ))) {
920
- throw new MalformedJsonException ("JSON forbids NaN and infinities: " + result
921
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
911
+ throw new MalformedJsonException (
912
+ "JSON forbids NaN and infinities: " + result + locationString ());
922
913
}
923
914
peekedString = null ;
924
915
peeked = PEEKED_NONE ;
@@ -966,16 +957,14 @@ public long nextLong() throws IOException {
966
957
// Fall back to parse as a double below.
967
958
}
968
959
} else {
969
- throw new IllegalStateException ("Expected a long but was " + peek ()
970
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
960
+ throw new IllegalStateException ("Expected a long but was " + peek () + locationString ());
971
961
}
972
962
973
963
peeked = PEEKED_BUFFERED ;
974
964
double asDouble = Double .parseDouble (peekedString ); // don't catch this NumberFormatException.
975
965
long result = (long ) asDouble ;
976
966
if (result != asDouble ) { // Make sure no precision was lost casting to 'long'.
977
- throw new NumberFormatException ("Expected a long but was " + peekedString
978
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
967
+ throw new NumberFormatException ("Expected a long but was " + peekedString + locationString ());
979
968
}
980
969
peekedString = null ;
981
970
peeked = PEEKED_NONE ;
@@ -1172,8 +1161,7 @@ public int nextInt() throws IOException {
1172
1161
if (p == PEEKED_LONG ) {
1173
1162
result = (int ) peekedLong ;
1174
1163
if (peekedLong != result ) { // Make sure no precision was lost casting to 'int'.
1175
- throw new NumberFormatException ("Expected an int but was " + peekedLong
1176
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
1164
+ throw new NumberFormatException ("Expected an int but was " + peekedLong + locationString ());
1177
1165
}
1178
1166
peeked = PEEKED_NONE ;
1179
1167
pathIndices [stackSize - 1 ]++;
@@ -1198,16 +1186,14 @@ public int nextInt() throws IOException {
1198
1186
// Fall back to parse as a double below.
1199
1187
}
1200
1188
} else {
1201
- throw new IllegalStateException ("Expected an int but was " + peek ()
1202
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
1189
+ throw new IllegalStateException ("Expected an int but was " + peek () + locationString ());
1203
1190
}
1204
1191
1205
1192
peeked = PEEKED_BUFFERED ;
1206
1193
double asDouble = Double .parseDouble (peekedString ); // don't catch this NumberFormatException.
1207
1194
result = (int ) asDouble ;
1208
1195
if (result != asDouble ) { // Make sure no precision was lost casting to 'int'.
1209
- throw new NumberFormatException ("Expected an int but was " + peekedString
1210
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
1196
+ throw new NumberFormatException ("Expected an int but was " + peekedString + locationString ());
1211
1197
}
1212
1198
peekedString = null ;
1213
1199
peeked = PEEKED_NONE ;
@@ -1315,14 +1301,6 @@ private boolean fillBuffer(int minimum) throws IOException {
1315
1301
return false ;
1316
1302
}
1317
1303
1318
- int getLineNumber () {
1319
- return lineNumber + 1 ;
1320
- }
1321
-
1322
- int getColumnNumber () {
1323
- return pos - lineStart + 1 ;
1324
- }
1325
-
1326
1304
/**
1327
1305
* Returns the next character in the stream that is neither whitespace nor a
1328
1306
* part of a comment. When this returns, the returned character is always at
@@ -1412,8 +1390,7 @@ private int nextNonWhitespace(boolean throwOnEof) throws IOException {
1412
1390
}
1413
1391
}
1414
1392
if (throwOnEof ) {
1415
- throw new EOFException ("End of input"
1416
- + " at line " + getLineNumber () + " column " + getColumnNumber ());
1393
+ throw new EOFException ("End of input" + locationString ());
1417
1394
} else {
1418
1395
return -1 ;
1419
1396
}
@@ -1465,8 +1442,13 @@ private boolean skipTo(String toFind) throws IOException {
1465
1442
}
1466
1443
1467
1444
@ Override public String toString () {
1468
- return getClass ().getSimpleName ()
1469
- + " at line " + getLineNumber () + " column " + getColumnNumber ();
1445
+ return getClass ().getSimpleName () + locationString ();
1446
+ }
1447
+
1448
+ private String locationString () {
1449
+ int line = lineNumber + 1 ;
1450
+ int column = pos - lineStart + 1 ;
1451
+ return " at line " + line + " column " + column + " path " + getPath ();
1470
1452
}
1471
1453
1472
1454
/**
@@ -1571,8 +1553,7 @@ private char readEscapeCharacter() throws IOException {
1571
1553
* with this reader's content.
1572
1554
*/
1573
1555
private IOException syntaxError (String message ) throws IOException {
1574
- throw new MalformedJsonException (message
1575
- + " at line " + getLineNumber () + " column " + getColumnNumber () + " path " + getPath ());
1556
+ throw new MalformedJsonException (message + locationString ());
1576
1557
}
1577
1558
1578
1559
/**
@@ -1615,9 +1596,8 @@ private void consumeNonExecutePrefix() throws IOException {
1615
1596
} else if (p == PEEKED_UNQUOTED_NAME ) {
1616
1597
reader .peeked = PEEKED_UNQUOTED ;
1617
1598
} else {
1618
- throw new IllegalStateException ("Expected a name but was " + reader .peek () + " "
1619
- + " at line " + reader .getLineNumber () + " column " + reader .getColumnNumber ()
1620
- + " path " + reader .getPath ());
1599
+ throw new IllegalStateException (
1600
+ "Expected a name but was " + reader .peek () + reader .locationString ());
1621
1601
}
1622
1602
}
1623
1603
};
0 commit comments