1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Fixes position reports on errors

This commit is contained in:
John J. Aylward 2017-06-23 13:40:41 -04:00
parent 1add1247fa
commit e7e6ed9205
3 changed files with 67 additions and 48 deletions

View file

@ -70,9 +70,12 @@ public class CDL {
c = x.next();
if (c == q) {
//Handle escaped double-quote
if(x.next() != '\"')
{
char nextC = x.next();
if(nextC != '\"') {
// if our quote was the end of the file, don't step
if(nextC > 0) {
x.back();
}
break;
}
}

View file

@ -39,7 +39,7 @@ SOFTWARE.
* @version 2014-05-03
*/
public class JSONTokener {
/** current read character. */
/** current read character position on the current line. */
private long character;
/** flag to indicate if the end of the input has been found. */
private boolean eof;
@ -47,7 +47,7 @@ public class JSONTokener {
private long index;
/** current line of the input. */
private long line;
/** previous index of the input. */
/** previous character read from the input. */
private char previous;
/** Reader for the input. */
private final Reader reader;
@ -103,8 +103,8 @@ public class JSONTokener {
if (this.usePrevious || this.index <= 0) {
throw new JSONException("Stepping back two steps is not supported");
}
this.index -= 1;
this.character -= 1;
this.index--;
this.character--;
this.usePrevious = true;
this.eof = false;
}
@ -145,11 +145,23 @@ public class JSONTokener {
* or backward while checking for more data.
*/
public boolean more() throws JSONException {
this.next();
if (this.end()) {
if(this.usePrevious) {
return true;
}
try {
this.reader.mark(1);
} catch (IOException e) {
throw new JSONException("Unable to preserve stream position", e);
}
try {
if(this.reader.read()<0) {
this.eof = true;
return false;
}
this.back();
this.reader.reset();
} catch (IOException e) {
throw new JSONException("Unable to read the next character from the stream", e);
}
return true;
}
@ -174,7 +186,7 @@ public class JSONTokener {
if (c <= 0) { // End of stream
this.eof = true;
c = 0;
return 0;
}
}
this.index += 1;
@ -202,9 +214,12 @@ public class JSONTokener {
public char next(char c) throws JSONException {
char n = this.next();
if (n != c) {
if(n > 0) {
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
n + "'");
}
throw this.syntaxError("Expected '" + c + "' and instead saw ''");
}
return n;
}

View file

@ -64,11 +64,8 @@ public class XMLTokener extends JSONTokener {
char c;
int i;
StringBuilder sb = new StringBuilder();
for (;;) {
while (more()) {
c = next();
if (end()) {
throw syntaxError("Unclosed CDATA");
}
sb.append(c);
i = sb.length() - 3;
if (i >= 0 && sb.charAt(i) == ']' &&
@ -77,6 +74,7 @@ public class XMLTokener extends JSONTokener {
return sb.toString();
}
}
throw syntaxError("Unclosed CDATA");
}
@ -103,7 +101,10 @@ public class XMLTokener extends JSONTokener {
}
sb = new StringBuilder();
for (;;) {
if (c == '<' || c == 0) {
if (c == 0) {
return sb.toString().trim();
}
if (c == '<') {
back();
return sb.toString().trim();
}