mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 16:00:51 -07:00
?:
This commit is contained in:
parent
9bb970c6bc
commit
2a39f47197
3 changed files with 308 additions and 302 deletions
|
@ -32,7 +32,7 @@ import java.util.Iterator;
|
||||||
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
|
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
|
||||||
* the JsonML transform.
|
* the JsonML transform.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2011-10-05
|
* @version 2011-11-24
|
||||||
*/
|
*/
|
||||||
public class JSONML {
|
public class JSONML {
|
||||||
|
|
||||||
|
@ -224,8 +224,9 @@ public class JSONML {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(token instanceof String ?
|
ja.put(token instanceof String
|
||||||
XML.stringToValue((String)token) : token);
|
? XML.stringToValue((String)token)
|
||||||
|
: token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ SOFTWARE.
|
||||||
* it. It is used by the JSONObject and JSONArray constructors to parse
|
* it. It is used by the JSONObject and JSONArray constructors to parse
|
||||||
* JSON source strings.
|
* JSON source strings.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2010-12-24
|
* @version 2011-11-24
|
||||||
*/
|
*/
|
||||||
public class JSONTokener {
|
public class JSONTokener {
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class JSONTokener {
|
||||||
private int index;
|
private int index;
|
||||||
private int line;
|
private int line;
|
||||||
private char previous;
|
private char previous;
|
||||||
private Reader reader;
|
private final Reader reader;
|
||||||
private boolean usePrevious;
|
private boolean usePrevious;
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,8 +55,9 @@ public class JSONTokener {
|
||||||
* @param reader A reader.
|
* @param reader A reader.
|
||||||
*/
|
*/
|
||||||
public JSONTokener(Reader reader) {
|
public JSONTokener(Reader reader) {
|
||||||
this.reader = reader.markSupported() ?
|
this.reader = reader.markSupported()
|
||||||
reader : new BufferedReader(reader);
|
? reader
|
||||||
|
: new BufferedReader(reader);
|
||||||
this.eof = false;
|
this.eof = false;
|
||||||
this.usePrevious = false;
|
this.usePrevious = false;
|
||||||
this.previous = 0;
|
this.previous = 0;
|
||||||
|
@ -90,7 +91,7 @@ public class JSONTokener {
|
||||||
* the next number or identifier.
|
* the next number or identifier.
|
||||||
*/
|
*/
|
||||||
public void back() throws JSONException {
|
public void back() throws JSONException {
|
||||||
if (usePrevious || 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 -= 1;
|
||||||
|
@ -120,7 +121,7 @@ public class JSONTokener {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean end() {
|
public boolean end() {
|
||||||
return eof && !usePrevious;
|
return this.eof && !this.usePrevious;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -130,11 +131,11 @@ public class JSONTokener {
|
||||||
* @return true if not yet at the end of the source.
|
* @return true if not yet at the end of the source.
|
||||||
*/
|
*/
|
||||||
public boolean more() throws JSONException {
|
public boolean more() throws JSONException {
|
||||||
next();
|
this.next();
|
||||||
if (end()) {
|
if (this.end()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
back();
|
this.back();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,9 +185,9 @@ public class JSONTokener {
|
||||||
* @throws JSONException if the character does not match.
|
* @throws JSONException if the character does not match.
|
||||||
*/
|
*/
|
||||||
public char next(char c) throws JSONException {
|
public char next(char c) throws JSONException {
|
||||||
char n = next();
|
char n = this.next();
|
||||||
if (n != c) {
|
if (n != c) {
|
||||||
throw syntaxError("Expected '" + c + "' and instead saw '" +
|
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
||||||
n + "'");
|
n + "'");
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
|
@ -211,9 +212,9 @@ public class JSONTokener {
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
|
||||||
while (pos < n) {
|
while (pos < n) {
|
||||||
chars[pos] = next();
|
chars[pos] = this.next();
|
||||||
if (end()) {
|
if (this.end()) {
|
||||||
throw syntaxError("Substring bounds error");
|
throw this.syntaxError("Substring bounds error");
|
||||||
}
|
}
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
@ -228,7 +229,7 @@ public class JSONTokener {
|
||||||
*/
|
*/
|
||||||
public char nextClean() throws JSONException {
|
public char nextClean() throws JSONException {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char c = next();
|
char c = this.next();
|
||||||
if (c == 0 || c > ' ') {
|
if (c == 0 || c > ' ') {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
@ -251,14 +252,14 @@ public class JSONTokener {
|
||||||
char c;
|
char c;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = next();
|
c = this.next();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 0:
|
case 0:
|
||||||
case '\n':
|
case '\n':
|
||||||
case '\r':
|
case '\r':
|
||||||
throw syntaxError("Unterminated string");
|
throw this.syntaxError("Unterminated string");
|
||||||
case '\\':
|
case '\\':
|
||||||
c = next();
|
c = this.next();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'b':
|
case 'b':
|
||||||
sb.append('\b');
|
sb.append('\b');
|
||||||
|
@ -276,7 +277,7 @@ public class JSONTokener {
|
||||||
sb.append('\r');
|
sb.append('\r');
|
||||||
break;
|
break;
|
||||||
case 'u':
|
case 'u':
|
||||||
sb.append((char)Integer.parseInt(next(4), 16));
|
sb.append((char)Integer.parseInt(this.next(4), 16));
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
|
@ -285,7 +286,7 @@ public class JSONTokener {
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw syntaxError("Illegal escape.");
|
throw this.syntaxError("Illegal escape.");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -307,10 +308,10 @@ public class JSONTokener {
|
||||||
public String nextTo(char delimiter) throws JSONException {
|
public String nextTo(char delimiter) throws JSONException {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char c = next();
|
char c = this.next();
|
||||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||||
if (c != 0) {
|
if (c != 0) {
|
||||||
back();
|
this.back();
|
||||||
}
|
}
|
||||||
return sb.toString().trim();
|
return sb.toString().trim();
|
||||||
}
|
}
|
||||||
|
@ -329,11 +330,11 @@ public class JSONTokener {
|
||||||
char c;
|
char c;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = next();
|
c = this.next();
|
||||||
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
||||||
c == '\n' || c == '\r') {
|
c == '\n' || c == '\r') {
|
||||||
if (c != 0) {
|
if (c != 0) {
|
||||||
back();
|
this.back();
|
||||||
}
|
}
|
||||||
return sb.toString().trim();
|
return sb.toString().trim();
|
||||||
}
|
}
|
||||||
|
@ -350,18 +351,18 @@ public class JSONTokener {
|
||||||
* @return An object.
|
* @return An object.
|
||||||
*/
|
*/
|
||||||
public Object nextValue() throws JSONException {
|
public Object nextValue() throws JSONException {
|
||||||
char c = nextClean();
|
char c = this.nextClean();
|
||||||
String string;
|
String string;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
return nextString(c);
|
return this.nextString(c);
|
||||||
case '{':
|
case '{':
|
||||||
back();
|
this.back();
|
||||||
return new JSONObject(this);
|
return new JSONObject(this);
|
||||||
case '[':
|
case '[':
|
||||||
back();
|
this.back();
|
||||||
return new JSONArray(this);
|
return new JSONArray(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -377,13 +378,13 @@ public class JSONTokener {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
c = next();
|
c = this.next();
|
||||||
}
|
}
|
||||||
back();
|
this.back();
|
||||||
|
|
||||||
string = sb.toString().trim();
|
string = sb.toString().trim();
|
||||||
if (string.equals("")) {
|
if (string.equals("")) {
|
||||||
throw syntaxError("Missing value");
|
throw this.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
return JSONObject.stringToValue(string);
|
return JSONObject.stringToValue(string);
|
||||||
}
|
}
|
||||||
|
@ -402,11 +403,11 @@ public class JSONTokener {
|
||||||
int startIndex = this.index;
|
int startIndex = this.index;
|
||||||
int startCharacter = this.character;
|
int startCharacter = this.character;
|
||||||
int startLine = this.line;
|
int startLine = this.line;
|
||||||
reader.mark(Integer.MAX_VALUE);
|
this.reader.mark(Integer.MAX_VALUE);
|
||||||
do {
|
do {
|
||||||
c = next();
|
c = this.next();
|
||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
reader.reset();
|
this.reader.reset();
|
||||||
this.index = startIndex;
|
this.index = startIndex;
|
||||||
this.character = startCharacter;
|
this.character = startCharacter;
|
||||||
this.line = startLine;
|
this.line = startLine;
|
||||||
|
@ -417,7 +418,7 @@ public class JSONTokener {
|
||||||
throw new JSONException(exc);
|
throw new JSONException(exc);
|
||||||
}
|
}
|
||||||
|
|
||||||
back();
|
this.back();
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -429,7 +430,7 @@ public class JSONTokener {
|
||||||
* @return A JSONException object, suitable for throwing
|
* @return A JSONException object, suitable for throwing
|
||||||
*/
|
*/
|
||||||
public JSONException syntaxError(String message) {
|
public JSONException syntaxError(String message) {
|
||||||
return new JSONException(message + toString());
|
return new JSONException(message + this.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -439,7 +440,7 @@ public class JSONTokener {
|
||||||
* @return " at {index} [character {character} line {line}]"
|
* @return " at {index} [character {character} line {line}]"
|
||||||
*/
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return " at " + index + " [character " + this.character + " line " +
|
return " at " + this.index + " [character " + this.character + " line " +
|
||||||
this.line + "]";
|
this.line + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -54,7 +54,7 @@ SOFTWARE.
|
||||||
* <p>
|
* <p>
|
||||||
* This can sometimes be easier than using a JSONObject to build a string.
|
* This can sometimes be easier than using a JSONObject to build a string.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2011-11-14
|
* @version 2011-11-24
|
||||||
*/
|
*/
|
||||||
public class JSONWriter {
|
public class JSONWriter {
|
||||||
private static final int maxdepth = 200;
|
private static final int maxdepth = 200;
|
||||||
|
@ -157,8 +157,9 @@ public class JSONWriter {
|
||||||
*/
|
*/
|
||||||
private JSONWriter end(char mode, char c) throws JSONException {
|
private JSONWriter end(char mode, char c) throws JSONException {
|
||||||
if (this.mode != mode) {
|
if (this.mode != mode) {
|
||||||
throw new JSONException(mode == 'a' ? "Misplaced endArray." :
|
throw new JSONException(mode == 'a'
|
||||||
"Misplaced endObject.");
|
? "Misplaced endArray."
|
||||||
|
: "Misplaced endObject.");
|
||||||
}
|
}
|
||||||
this.pop(mode);
|
this.pop(mode);
|
||||||
try {
|
try {
|
||||||
|
@ -259,8 +260,11 @@ public class JSONWriter {
|
||||||
throw new JSONException("Nesting error.");
|
throw new JSONException("Nesting error.");
|
||||||
}
|
}
|
||||||
this.top -= 1;
|
this.top -= 1;
|
||||||
this.mode = this.top == 0 ?
|
this.mode = this.top == 0
|
||||||
'd' : this.stack[this.top - 1] == null ? 'a' : 'k';
|
? 'd'
|
||||||
|
: this.stack[this.top - 1] == null
|
||||||
|
? 'a'
|
||||||
|
: 'k';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue