Merge pull request #870 from google/jw/reader-location

Consolidate location and path rendering for exceptions.
This commit is contained in:
inder123 2016-06-09 15:26:10 -07:00 committed by GitHub
commit ebad966efd

View File

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