Consolidate location and path rendering for exceptions.
This commit is contained in:
parent
03337640c2
commit
ecaa57114f
@ -347,8 +347,7 @@ public class JsonReader implements Closeable {
|
|||||||
pathIndices[stackSize - 1] = 0;
|
pathIndices[stackSize - 1] = 0;
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected BEGIN_ARRAY but was " + peek()
|
throw new IllegalStateException("Expected BEGIN_ARRAY but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -366,8 +365,7 @@ public class JsonReader implements Closeable {
|
|||||||
pathIndices[stackSize - 1]++;
|
pathIndices[stackSize - 1]++;
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected END_ARRAY but was " + peek()
|
throw new IllegalStateException("Expected END_ARRAY but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,8 +382,7 @@ public class JsonReader implements Closeable {
|
|||||||
push(JsonScope.EMPTY_OBJECT);
|
push(JsonScope.EMPTY_OBJECT);
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected BEGIN_OBJECT but was " + peek()
|
throw new IllegalStateException("Expected BEGIN_OBJECT but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,8 +401,7 @@ public class JsonReader implements Closeable {
|
|||||||
pathIndices[stackSize - 1]++;
|
pathIndices[stackSize - 1]++;
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected END_OBJECT but was " + peek()
|
throw new IllegalStateException("Expected END_OBJECT but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -790,8 +786,7 @@ public class JsonReader implements Closeable {
|
|||||||
} else if (p == PEEKED_DOUBLE_QUOTED_NAME) {
|
} else if (p == PEEKED_DOUBLE_QUOTED_NAME) {
|
||||||
result = nextQuotedValue('"');
|
result = nextQuotedValue('"');
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected a name but was " + peek()
|
throw new IllegalStateException("Expected a name but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
pathNames[stackSize - 1] = result;
|
pathNames[stackSize - 1] = result;
|
||||||
@ -827,8 +822,7 @@ public class JsonReader implements Closeable {
|
|||||||
result = new String(buffer, pos, peekedNumberLength);
|
result = new String(buffer, pos, peekedNumberLength);
|
||||||
pos += peekedNumberLength;
|
pos += peekedNumberLength;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected a string but was " + peek()
|
throw new IllegalStateException("Expected a string but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
pathIndices[stackSize - 1]++;
|
pathIndices[stackSize - 1]++;
|
||||||
@ -856,8 +850,7 @@ public class JsonReader implements Closeable {
|
|||||||
pathIndices[stackSize - 1]++;
|
pathIndices[stackSize - 1]++;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("Expected a boolean but was " + peek()
|
throw new IllegalStateException("Expected a boolean but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -876,8 +869,7 @@ public class JsonReader implements Closeable {
|
|||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
pathIndices[stackSize - 1]++;
|
pathIndices[stackSize - 1]++;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected null but was " + peek()
|
throw new IllegalStateException("Expected null but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -910,15 +902,14 @@ public class JsonReader implements Closeable {
|
|||||||
} else if (p == PEEKED_UNQUOTED) {
|
} else if (p == PEEKED_UNQUOTED) {
|
||||||
peekedString = nextUnquotedValue();
|
peekedString = nextUnquotedValue();
|
||||||
} else if (p != PEEKED_BUFFERED) {
|
} else if (p != PEEKED_BUFFERED) {
|
||||||
throw new IllegalStateException("Expected a double but was " + peek()
|
throw new IllegalStateException("Expected a double but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
peeked = PEEKED_BUFFERED;
|
peeked = PEEKED_BUFFERED;
|
||||||
double result = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
|
double result = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
|
||||||
if (!lenient && (Double.isNaN(result) || Double.isInfinite(result))) {
|
if (!lenient && (Double.isNaN(result) || Double.isInfinite(result))) {
|
||||||
throw new MalformedJsonException("JSON forbids NaN and infinities: " + result
|
throw new MalformedJsonException(
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
"JSON forbids NaN and infinities: " + result + locationString());
|
||||||
}
|
}
|
||||||
peekedString = null;
|
peekedString = null;
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
@ -966,16 +957,14 @@ public class JsonReader implements Closeable {
|
|||||||
// Fall back to parse as a double below.
|
// Fall back to parse as a double below.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected a long but was " + peek()
|
throw new IllegalStateException("Expected a long but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
peeked = PEEKED_BUFFERED;
|
peeked = PEEKED_BUFFERED;
|
||||||
double asDouble = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
|
double asDouble = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
|
||||||
long result = (long) asDouble;
|
long result = (long) asDouble;
|
||||||
if (result != asDouble) { // Make sure no precision was lost casting to 'long'.
|
if (result != asDouble) { // Make sure no precision was lost casting to 'long'.
|
||||||
throw new NumberFormatException("Expected a long but was " + peekedString
|
throw new NumberFormatException("Expected a long but was " + peekedString + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
peekedString = null;
|
peekedString = null;
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
@ -1172,8 +1161,7 @@ public class JsonReader implements Closeable {
|
|||||||
if (p == PEEKED_LONG) {
|
if (p == PEEKED_LONG) {
|
||||||
result = (int) peekedLong;
|
result = (int) peekedLong;
|
||||||
if (peekedLong != result) { // Make sure no precision was lost casting to 'int'.
|
if (peekedLong != result) { // Make sure no precision was lost casting to 'int'.
|
||||||
throw new NumberFormatException("Expected an int but was " + peekedLong
|
throw new NumberFormatException("Expected an int but was " + peekedLong + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
pathIndices[stackSize - 1]++;
|
pathIndices[stackSize - 1]++;
|
||||||
@ -1198,16 +1186,14 @@ public class JsonReader implements Closeable {
|
|||||||
// Fall back to parse as a double below.
|
// Fall back to parse as a double below.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected an int but was " + peek()
|
throw new IllegalStateException("Expected an int but was " + peek() + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
peeked = PEEKED_BUFFERED;
|
peeked = PEEKED_BUFFERED;
|
||||||
double asDouble = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
|
double asDouble = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
|
||||||
result = (int) asDouble;
|
result = (int) asDouble;
|
||||||
if (result != asDouble) { // Make sure no precision was lost casting to 'int'.
|
if (result != asDouble) { // Make sure no precision was lost casting to 'int'.
|
||||||
throw new NumberFormatException("Expected an int but was " + peekedString
|
throw new NumberFormatException("Expected an int but was " + peekedString + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
peekedString = null;
|
peekedString = null;
|
||||||
peeked = PEEKED_NONE;
|
peeked = PEEKED_NONE;
|
||||||
@ -1315,14 +1301,6 @@ public class JsonReader implements Closeable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getLineNumber() {
|
|
||||||
return lineNumber + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getColumnNumber() {
|
|
||||||
return pos - lineStart + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next character in the stream that is neither whitespace nor a
|
* Returns the next character in the stream that is neither whitespace nor a
|
||||||
* part of a comment. When this returns, the returned character is always at
|
* part of a comment. When this returns, the returned character is always at
|
||||||
@ -1412,8 +1390,7 @@ public class JsonReader implements Closeable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (throwOnEof) {
|
if (throwOnEof) {
|
||||||
throw new EOFException("End of input"
|
throw new EOFException("End of input" + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber());
|
|
||||||
} else {
|
} else {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -1465,8 +1442,13 @@ public class JsonReader implements Closeable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
return getClass().getSimpleName()
|
return getClass().getSimpleName() + locationString();
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber();
|
}
|
||||||
|
|
||||||
|
private String locationString() {
|
||||||
|
int line = lineNumber + 1;
|
||||||
|
int column = pos - lineStart + 1;
|
||||||
|
return " at line " + line + " column " + column + " path " + getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1571,8 +1553,7 @@ public class JsonReader implements Closeable {
|
|||||||
* with this reader's content.
|
* with this reader's content.
|
||||||
*/
|
*/
|
||||||
private IOException syntaxError(String message) throws IOException {
|
private IOException syntaxError(String message) throws IOException {
|
||||||
throw new MalformedJsonException(message
|
throw new MalformedJsonException(message + locationString());
|
||||||
+ " at line " + getLineNumber() + " column " + getColumnNumber() + " path " + getPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1615,9 +1596,8 @@ public class JsonReader implements Closeable {
|
|||||||
} else if (p == PEEKED_UNQUOTED_NAME) {
|
} else if (p == PEEKED_UNQUOTED_NAME) {
|
||||||
reader.peeked = PEEKED_UNQUOTED;
|
reader.peeked = PEEKED_UNQUOTED;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Expected a name but was " + reader.peek() + " "
|
throw new IllegalStateException(
|
||||||
+ " at line " + reader.getLineNumber() + " column " + reader.getColumnNumber()
|
"Expected a name but was " + reader.peek() + reader.locationString());
|
||||||
+ " path " + reader.getPath());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user