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:
parent
1add1247fa
commit
e7e6ed9205
3 changed files with 67 additions and 48 deletions
11
CDL.java
11
CDL.java
|
@ -22,7 +22,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This provides static methods to convert comma delimited text into a
|
* This provides static methods to convert comma delimited text into a
|
||||||
|
@ -70,9 +70,12 @@ public class CDL {
|
||||||
c = x.next();
|
c = x.next();
|
||||||
if (c == q) {
|
if (c == q) {
|
||||||
//Handle escaped double-quote
|
//Handle escaped double-quote
|
||||||
if(x.next() != '\"')
|
char nextC = x.next();
|
||||||
{
|
if(nextC != '\"') {
|
||||||
x.back();
|
// if our quote was the end of the file, don't step
|
||||||
|
if(nextC > 0) {
|
||||||
|
x.back();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A JSONTokener takes a source string and extracts characters and tokens from
|
* A JSONTokener takes a source string and extracts characters and tokens from
|
||||||
|
@ -39,7 +39,7 @@ SOFTWARE.
|
||||||
* @version 2014-05-03
|
* @version 2014-05-03
|
||||||
*/
|
*/
|
||||||
public class JSONTokener {
|
public class JSONTokener {
|
||||||
/** current read character. */
|
/** current read character position on the current line. */
|
||||||
private long character;
|
private long character;
|
||||||
/** flag to indicate if the end of the input has been found. */
|
/** flag to indicate if the end of the input has been found. */
|
||||||
private boolean eof;
|
private boolean eof;
|
||||||
|
@ -47,7 +47,7 @@ public class JSONTokener {
|
||||||
private long index;
|
private long index;
|
||||||
/** current line of the input. */
|
/** current line of the input. */
|
||||||
private long line;
|
private long line;
|
||||||
/** previous index of the input. */
|
/** previous character read from the input. */
|
||||||
private char previous;
|
private char previous;
|
||||||
/** Reader for the input. */
|
/** Reader for the input. */
|
||||||
private final Reader reader;
|
private final Reader reader;
|
||||||
|
@ -62,8 +62,8 @@ public class JSONTokener {
|
||||||
*/
|
*/
|
||||||
public JSONTokener(Reader reader) {
|
public JSONTokener(Reader reader) {
|
||||||
this.reader = reader.markSupported()
|
this.reader = reader.markSupported()
|
||||||
? reader
|
? reader
|
||||||
: new BufferedReader(reader);
|
: new BufferedReader(reader);
|
||||||
this.eof = false;
|
this.eof = false;
|
||||||
this.usePrevious = false;
|
this.usePrevious = false;
|
||||||
this.previous = 0;
|
this.previous = 0;
|
||||||
|
@ -103,8 +103,8 @@ public class JSONTokener {
|
||||||
if (this.usePrevious || this.index <= 0) {
|
if (this.usePrevious || this.index <= 0) {
|
||||||
throw new JSONException("Stepping back two steps is not supported");
|
throw new JSONException("Stepping back two steps is not supported");
|
||||||
}
|
}
|
||||||
this.index -= 1;
|
this.index--;
|
||||||
this.character -= 1;
|
this.character--;
|
||||||
this.usePrevious = true;
|
this.usePrevious = true;
|
||||||
this.eof = false;
|
this.eof = false;
|
||||||
}
|
}
|
||||||
|
@ -145,11 +145,23 @@ public class JSONTokener {
|
||||||
* or backward while checking for more data.
|
* or backward while checking for more data.
|
||||||
*/
|
*/
|
||||||
public boolean more() throws JSONException {
|
public boolean more() throws JSONException {
|
||||||
this.next();
|
if(this.usePrevious) {
|
||||||
if (this.end()) {
|
return true;
|
||||||
return false;
|
}
|
||||||
|
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.reader.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new JSONException("Unable to read the next character from the stream", e);
|
||||||
}
|
}
|
||||||
this.back();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +186,7 @@ public class JSONTokener {
|
||||||
|
|
||||||
if (c <= 0) { // End of stream
|
if (c <= 0) { // End of stream
|
||||||
this.eof = true;
|
this.eof = true;
|
||||||
c = 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.index += 1;
|
this.index += 1;
|
||||||
|
@ -202,8 +214,11 @@ public class JSONTokener {
|
||||||
public char next(char c) throws JSONException {
|
public char next(char c) throws JSONException {
|
||||||
char n = this.next();
|
char n = this.next();
|
||||||
if (n != c) {
|
if (n != c) {
|
||||||
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
if(n > 0) {
|
||||||
n + "'");
|
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
||||||
|
n + "'");
|
||||||
|
}
|
||||||
|
throw this.syntaxError("Expected '" + c + "' and instead saw ''");
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
@ -218,23 +233,23 @@ public class JSONTokener {
|
||||||
* Substring bounds error if there are not
|
* Substring bounds error if there are not
|
||||||
* n characters remaining in the source string.
|
* n characters remaining in the source string.
|
||||||
*/
|
*/
|
||||||
public String next(int n) throws JSONException {
|
public String next(int n) throws JSONException {
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
char[] chars = new char[n];
|
char[] chars = new char[n];
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
|
||||||
while (pos < n) {
|
while (pos < n) {
|
||||||
chars[pos] = this.next();
|
chars[pos] = this.next();
|
||||||
if (this.end()) {
|
if (this.end()) {
|
||||||
throw this.syntaxError("Substring bounds error");
|
throw this.syntaxError("Substring bounds error");
|
||||||
}
|
}
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
return new String(chars);
|
return new String(chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -378,15 +393,15 @@ public class JSONTokener {
|
||||||
String string;
|
String string;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
return this.nextString(c);
|
return this.nextString(c);
|
||||||
case '{':
|
case '{':
|
||||||
this.back();
|
this.back();
|
||||||
return new JSONObject(this);
|
return new JSONObject(this);
|
||||||
case '[':
|
case '[':
|
||||||
this.back();
|
this.back();
|
||||||
return new JSONArray(this);
|
return new JSONArray(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -476,6 +491,6 @@ public class JSONTokener {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return " at " + this.index + " [character " + this.character + " line " +
|
return " at " + this.index + " [character " + this.character + " line " +
|
||||||
this.line + "]";
|
this.line + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,11 +64,8 @@ public class XMLTokener extends JSONTokener {
|
||||||
char c;
|
char c;
|
||||||
int i;
|
int i;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (;;) {
|
while (more()) {
|
||||||
c = next();
|
c = next();
|
||||||
if (end()) {
|
|
||||||
throw syntaxError("Unclosed CDATA");
|
|
||||||
}
|
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
i = sb.length() - 3;
|
i = sb.length() - 3;
|
||||||
if (i >= 0 && sb.charAt(i) == ']' &&
|
if (i >= 0 && sb.charAt(i) == ']' &&
|
||||||
|
@ -77,6 +74,7 @@ public class XMLTokener extends JSONTokener {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw syntaxError("Unclosed CDATA");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -103,7 +101,10 @@ public class XMLTokener extends JSONTokener {
|
||||||
}
|
}
|
||||||
sb = new StringBuilder();
|
sb = new StringBuilder();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (c == '<' || c == 0) {
|
if (c == 0) {
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
if (c == '<') {
|
||||||
back();
|
back();
|
||||||
return sb.toString().trim();
|
return sb.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue