mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
AjayKemparaj: Tabs to spaces and minor java code fixups
This commit is contained in:
parent
6394d92279
commit
9420bb7e8b
9 changed files with 435 additions and 435 deletions
90
CDL.java
90
CDL.java
|
@ -64,18 +64,18 @@ public class CDL {
|
||||||
return null;
|
return null;
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
q = c;
|
q = c;
|
||||||
sb = new StringBuffer();
|
sb = new StringBuffer();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = x.next();
|
c = x.next();
|
||||||
if (c == q) {
|
if (c == q) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (c == 0 || c == '\n' || c == '\r') {
|
if (c == 0 || c == '\n' || c == '\r') {
|
||||||
throw x.syntaxError("Missing close quote '" + q + "'.");
|
throw x.syntaxError("Missing close quote '" + q + "'.");
|
||||||
}
|
}
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
case ',':
|
case ',':
|
||||||
x.back();
|
x.back();
|
||||||
|
@ -98,7 +98,7 @@ public class CDL {
|
||||||
String value = getValue(x);
|
String value = getValue(x);
|
||||||
char c = x.next();
|
char c = x.next();
|
||||||
if (value == null ||
|
if (value == null ||
|
||||||
(ja.length() == 0 && value.length() == 0 && c != ',')) {
|
(ja.length() == 0 && value.length() == 0 && c != ',')) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
ja.put(value);
|
ja.put(value);
|
||||||
|
@ -135,43 +135,43 @@ public class CDL {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce a comma delimited text row from a JSONArray. Values containing
|
* Produce a comma delimited text row from a JSONArray. Values containing
|
||||||
* the comma character will be quoted. Troublesome characters may be
|
* the comma character will be quoted. Troublesome characters may be
|
||||||
* removed.
|
* removed.
|
||||||
* @param ja A JSONArray of strings.
|
* @param ja A JSONArray of strings.
|
||||||
* @return A string ending in NEWLINE.
|
* @return A string ending in NEWLINE.
|
||||||
*/
|
*/
|
||||||
public static String rowToString(JSONArray ja) {
|
public static String rowToString(JSONArray ja) {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (int i = 0; i < ja.length(); i += 1) {
|
for (int i = 0; i < ja.length(); i += 1) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
sb.append(',');
|
sb.append(',');
|
||||||
}
|
}
|
||||||
Object object = ja.opt(i);
|
Object object = ja.opt(i);
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
String string = object.toString();
|
String string = object.toString();
|
||||||
if (string.length() > 0 && (string.indexOf(',') >= 0 ||
|
if (string.length() > 0 && (string.indexOf(',') >= 0 ||
|
||||||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
||||||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
|
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
int length = string.length();
|
int length = string.length();
|
||||||
for (int j = 0; j < length; j += 1) {
|
for (int j = 0; j < length; j += 1) {
|
||||||
char c = string.charAt(j);
|
char c = string.charAt(j);
|
||||||
if (c >= ' ' && c != '"') {
|
if (c >= ' ' && c != '"') {
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
} else {
|
} else {
|
||||||
sb.append(string);
|
sb.append(string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append('\n');
|
sb.append('\n');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produce a JSONArray of JSONObjects from a comma delimited text string,
|
* Produce a JSONArray of JSONObjects from a comma delimited text string,
|
||||||
* using the first row as a source of names.
|
* using the first row as a source of names.
|
||||||
* @param string The comma delimited text.
|
* @param string The comma delimited text.
|
||||||
|
|
|
@ -148,9 +148,9 @@ public class HTTP {
|
||||||
sb.append(CRLF);
|
sb.append(CRLF);
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
string = keys.next().toString();
|
string = keys.next().toString();
|
||||||
if (!string.equals("HTTP-Version") && !string.equals("Status-Code") &&
|
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) &&
|
||||||
!string.equals("Reason-Phrase") && !string.equals("Method") &&
|
!"Reason-Phrase".equals(string) && !"Method".equals(string) &&
|
||||||
!string.equals("Request-URI") && !jo.isNull(string)) {
|
!"Request-URI".equals(string) && !jo.isNull(string)) {
|
||||||
sb.append(string);
|
sb.append(string);
|
||||||
sb.append(": ");
|
sb.append(": ");
|
||||||
sb.append(jo.getString(string));
|
sb.append(jo.getString(string));
|
||||||
|
|
|
@ -6,8 +6,8 @@ package org.json;
|
||||||
* @version 2010-12-24
|
* @version 2010-12-24
|
||||||
*/
|
*/
|
||||||
public class JSONException extends Exception {
|
public class JSONException extends Exception {
|
||||||
private static final long serialVersionUID = 0;
|
private static final long serialVersionUID = 0;
|
||||||
private Throwable cause;
|
private Throwable cause;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a JSONException with an explanatory message.
|
* Constructs a JSONException with an explanatory message.
|
||||||
|
|
506
JSONML.java
506
JSONML.java
|
@ -48,16 +48,16 @@ public class JSONML {
|
||||||
private static Object parse(
|
private static Object parse(
|
||||||
XMLTokener x,
|
XMLTokener x,
|
||||||
boolean arrayForm,
|
boolean arrayForm,
|
||||||
JSONArray ja
|
JSONArray ja
|
||||||
) throws JSONException {
|
) throws JSONException {
|
||||||
String attribute;
|
String attribute;
|
||||||
char c;
|
char c;
|
||||||
String closeTag = null;
|
String closeTag = null;
|
||||||
int i;
|
int i;
|
||||||
JSONArray newja = null;
|
JSONArray newja = null;
|
||||||
JSONObject newjo = null;
|
JSONObject newjo = null;
|
||||||
Object token;
|
Object token;
|
||||||
String tagName = null;
|
String tagName = null;
|
||||||
|
|
||||||
// Test for and skip past these forms:
|
// Test for and skip past these forms:
|
||||||
// <!-- ... -->
|
// <!-- ... -->
|
||||||
|
@ -69,166 +69,166 @@ public class JSONML {
|
||||||
if (!x.more()) {
|
if (!x.more()) {
|
||||||
throw x.syntaxError("Bad XML");
|
throw x.syntaxError("Bad XML");
|
||||||
}
|
}
|
||||||
token = x.nextContent();
|
token = x.nextContent();
|
||||||
if (token == XML.LT) {
|
if (token == XML.LT) {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (token instanceof Character) {
|
if (token instanceof Character) {
|
||||||
if (token == XML.SLASH) {
|
if (token == XML.SLASH) {
|
||||||
|
|
||||||
// Close tag </
|
// Close tag </
|
||||||
|
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
throw new JSONException(
|
throw new JSONException(
|
||||||
"Expected a closing name instead of '" +
|
"Expected a closing name instead of '" +
|
||||||
token + "'.");
|
token + "'.");
|
||||||
}
|
}
|
||||||
if (x.nextToken() != XML.GT) {
|
if (x.nextToken() != XML.GT) {
|
||||||
throw x.syntaxError("Misshaped close tag");
|
throw x.syntaxError("Misshaped close tag");
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
} else if (token == XML.BANG) {
|
} else if (token == XML.BANG) {
|
||||||
|
|
||||||
// <!
|
// <!
|
||||||
|
|
||||||
c = x.next();
|
c = x.next();
|
||||||
if (c == '-') {
|
if (c == '-') {
|
||||||
if (x.next() == '-') {
|
if (x.next() == '-') {
|
||||||
x.skipPast("-->");
|
x.skipPast("-->");
|
||||||
}
|
}
|
||||||
x.back();
|
x.back();
|
||||||
} else if (c == '[') {
|
} else if (c == '[') {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (token.equals("CDATA") && x.next() == '[') {
|
if (token.equals("CDATA") && x.next() == '[') {
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(x.nextCDATA());
|
ja.put(x.nextCDATA());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw x.syntaxError("Expected 'CDATA['");
|
throw x.syntaxError("Expected 'CDATA['");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = 1;
|
i = 1;
|
||||||
do {
|
do {
|
||||||
token = x.nextMeta();
|
token = x.nextMeta();
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
throw x.syntaxError("Missing '>' after '<!'.");
|
throw x.syntaxError("Missing '>' after '<!'.");
|
||||||
} else if (token == XML.LT) {
|
} else if (token == XML.LT) {
|
||||||
i += 1;
|
i += 1;
|
||||||
} else if (token == XML.GT) {
|
} else if (token == XML.GT) {
|
||||||
i -= 1;
|
i -= 1;
|
||||||
}
|
}
|
||||||
} while (i > 0);
|
} while (i > 0);
|
||||||
}
|
}
|
||||||
} else if (token == XML.QUEST) {
|
} else if (token == XML.QUEST) {
|
||||||
|
|
||||||
// <?
|
// <?
|
||||||
|
|
||||||
x.skipPast("?>");
|
x.skipPast("?>");
|
||||||
} else {
|
} else {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open tag <
|
// Open tag <
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
throw x.syntaxError("Bad tagName '" + token + "'.");
|
throw x.syntaxError("Bad tagName '" + token + "'.");
|
||||||
}
|
}
|
||||||
tagName = (String)token;
|
tagName = (String)token;
|
||||||
newja = new JSONArray();
|
newja = new JSONArray();
|
||||||
newjo = new JSONObject();
|
newjo = new JSONObject();
|
||||||
if (arrayForm) {
|
if (arrayForm) {
|
||||||
newja.put(tagName);
|
newja.put(tagName);
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(newja);
|
ja.put(newja);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
newjo.put("tagName", tagName);
|
newjo.put("tagName", tagName);
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(newjo);
|
ja.put(newjo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
token = null;
|
token = null;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
}
|
}
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attribute = value
|
// attribute = value
|
||||||
|
|
||||||
attribute = (String)token;
|
attribute = (String)token;
|
||||||
if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) {
|
if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) {
|
||||||
throw x.syntaxError("Reserved attribute.");
|
throw x.syntaxError("Reserved attribute.");
|
||||||
}
|
}
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (token == XML.EQ) {
|
if (token == XML.EQ) {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
throw x.syntaxError("Missing value");
|
throw x.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
newjo.accumulate(attribute, XML.stringToValue((String)token));
|
newjo.accumulate(attribute, XML.stringToValue((String)token));
|
||||||
token = null;
|
token = null;
|
||||||
} else {
|
} else {
|
||||||
newjo.accumulate(attribute, "");
|
newjo.accumulate(attribute, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (arrayForm && newjo.length() > 0) {
|
if (arrayForm && newjo.length() > 0) {
|
||||||
newja.put(newjo);
|
newja.put(newjo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty tag <.../>
|
// Empty tag <.../>
|
||||||
|
|
||||||
if (token == XML.SLASH) {
|
if (token == XML.SLASH) {
|
||||||
if (x.nextToken() != XML.GT) {
|
if (x.nextToken() != XML.GT) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
if (arrayForm) {
|
if (arrayForm) {
|
||||||
return newja;
|
return newja;
|
||||||
} else {
|
} else {
|
||||||
return newjo;
|
return newjo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Content, between <...> and </...>
|
// Content, between <...> and </...>
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (token != XML.GT) {
|
if (token != XML.GT) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
closeTag = (String)parse(x, arrayForm, newja);
|
closeTag = (String)parse(x, arrayForm, newja);
|
||||||
if (closeTag != null) {
|
if (closeTag != null) {
|
||||||
if (!closeTag.equals(tagName)) {
|
if (!closeTag.equals(tagName)) {
|
||||||
throw x.syntaxError("Mismatched '" + tagName +
|
throw x.syntaxError("Mismatched '" + tagName +
|
||||||
"' and '" + closeTag + "'");
|
"' and '" + closeTag + "'");
|
||||||
}
|
}
|
||||||
tagName = null;
|
tagName = null;
|
||||||
if (!arrayForm && newja.length() > 0) {
|
if (!arrayForm && newja.length() > 0) {
|
||||||
newjo.put("childNodes", newja);
|
newjo.put("childNodes", newja);
|
||||||
}
|
}
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
if (arrayForm) {
|
if (arrayForm) {
|
||||||
return newja;
|
return newja;
|
||||||
} else {
|
} else {
|
||||||
return newjo;
|
return newjo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(token instanceof String
|
ja.put(token instanceof String
|
||||||
? XML.stringToValue((String)token)
|
? XML.stringToValue((String)token)
|
||||||
: token);
|
: token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||||
return toJSONArray(new XMLTokener(string));
|
return toJSONArray(new XMLTokener(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -263,7 +263,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
||||||
return (JSONArray)parse(x, true, null);
|
return (JSONArray)parse(x, true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -281,7 +281,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
||||||
return (JSONObject)parse(x, false, null);
|
return (JSONObject)parse(x, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -299,7 +299,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||||
return toJSONObject(new XMLTokener(string));
|
return toJSONObject(new XMLTokener(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -310,75 +310,75 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static String toString(JSONArray ja) throws JSONException {
|
public static String toString(JSONArray ja) throws JSONException {
|
||||||
int i;
|
int i;
|
||||||
JSONObject jo;
|
JSONObject jo;
|
||||||
String key;
|
String key;
|
||||||
Iterator keys;
|
Iterator keys;
|
||||||
int length;
|
int length;
|
||||||
Object object;
|
Object object;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
String tagName;
|
String tagName;
|
||||||
String value;
|
String value;
|
||||||
|
|
||||||
// Emit <tagName
|
// Emit <tagName
|
||||||
|
|
||||||
tagName = ja.getString(0);
|
tagName = ja.getString(0);
|
||||||
XML.noSpace(tagName);
|
XML.noSpace(tagName);
|
||||||
tagName = XML.escape(tagName);
|
tagName = XML.escape(tagName);
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
|
|
||||||
object = ja.opt(1);
|
object = ja.opt(1);
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
i = 2;
|
i = 2;
|
||||||
jo = (JSONObject)object;
|
jo = (JSONObject)object;
|
||||||
|
|
||||||
// Emit the attributes
|
// Emit the attributes
|
||||||
|
|
||||||
keys = jo.keys();
|
keys = jo.keys();
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
key = keys.next().toString();
|
key = keys.next().toString();
|
||||||
XML.noSpace(key);
|
XML.noSpace(key);
|
||||||
value = jo.optString(key);
|
value = jo.optString(key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(XML.escape(key));
|
sb.append(XML.escape(key));
|
||||||
sb.append('=');
|
sb.append('=');
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
sb.append(XML.escape(value));
|
sb.append(XML.escape(value));
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = 1;
|
i = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Emit content in body
|
//Emit content in body
|
||||||
|
|
||||||
length = ja.length();
|
length = ja.length();
|
||||||
if (i >= length) {
|
if (i >= length) {
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
} else {
|
} else {
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
do {
|
do {
|
||||||
object = ja.get(i);
|
object = ja.get(i);
|
||||||
i += 1;
|
i += 1;
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
if (object instanceof String) {
|
if (object instanceof String) {
|
||||||
sb.append(XML.escape(object.toString()));
|
sb.append(XML.escape(object.toString()));
|
||||||
} else if (object instanceof JSONObject) {
|
} else if (object instanceof JSONObject) {
|
||||||
sb.append(toString((JSONObject)object));
|
sb.append(toString((JSONObject)object));
|
||||||
} else if (object instanceof JSONArray) {
|
} else if (object instanceof JSONArray) {
|
||||||
sb.append(toString((JSONArray)object));
|
sb.append(toString((JSONArray)object));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (i < length);
|
} while (i < length);
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,75 +391,75 @@ public class JSONML {
|
||||||
* @return An XML string.
|
* @return An XML string.
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static String toString(JSONObject jo) throws JSONException {
|
public static String toString(JSONObject jo) throws JSONException {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
int i;
|
int i;
|
||||||
JSONArray ja;
|
JSONArray ja;
|
||||||
String key;
|
String key;
|
||||||
Iterator keys;
|
Iterator keys;
|
||||||
int length;
|
int length;
|
||||||
Object object;
|
Object object;
|
||||||
String tagName;
|
String tagName;
|
||||||
String value;
|
String value;
|
||||||
|
|
||||||
//Emit <tagName
|
//Emit <tagName
|
||||||
|
|
||||||
tagName = jo.optString("tagName");
|
tagName = jo.optString("tagName");
|
||||||
if (tagName == null) {
|
if (tagName == null) {
|
||||||
return XML.escape(jo.toString());
|
return XML.escape(jo.toString());
|
||||||
}
|
}
|
||||||
XML.noSpace(tagName);
|
XML.noSpace(tagName);
|
||||||
tagName = XML.escape(tagName);
|
tagName = XML.escape(tagName);
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
|
|
||||||
//Emit the attributes
|
//Emit the attributes
|
||||||
|
|
||||||
keys = jo.keys();
|
keys = jo.keys();
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
key = keys.next().toString();
|
key = keys.next().toString();
|
||||||
if (!key.equals("tagName") && !key.equals("childNodes")) {
|
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
|
||||||
XML.noSpace(key);
|
XML.noSpace(key);
|
||||||
value = jo.optString(key);
|
value = jo.optString(key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(XML.escape(key));
|
sb.append(XML.escape(key));
|
||||||
sb.append('=');
|
sb.append('=');
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
sb.append(XML.escape(value));
|
sb.append(XML.escape(value));
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Emit content in body
|
//Emit content in body
|
||||||
|
|
||||||
ja = jo.optJSONArray("childNodes");
|
ja = jo.optJSONArray("childNodes");
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
} else {
|
} else {
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
length = ja.length();
|
length = ja.length();
|
||||||
for (i = 0; i < length; i += 1) {
|
for (i = 0; i < length; i += 1) {
|
||||||
object = ja.get(i);
|
object = ja.get(i);
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
if (object instanceof String) {
|
if (object instanceof String) {
|
||||||
sb.append(XML.escape(object.toString()));
|
sb.append(XML.escape(object.toString()));
|
||||||
} else if (object instanceof JSONObject) {
|
} else if (object instanceof JSONObject) {
|
||||||
sb.append(toString((JSONObject)object));
|
sb.append(toString((JSONObject)object));
|
||||||
} else if (object instanceof JSONArray) {
|
} else if (object instanceof JSONArray) {
|
||||||
sb.append(toString((JSONArray)object));
|
sb.append(toString((JSONArray)object));
|
||||||
} else {
|
} else {
|
||||||
sb.append(object.toString());
|
sb.append(object.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -967,8 +967,8 @@ public class JSONObject {
|
||||||
String name = method.getName();
|
String name = method.getName();
|
||||||
String key = "";
|
String key = "";
|
||||||
if (name.startsWith("get")) {
|
if (name.startsWith("get")) {
|
||||||
if (name.equals("getClass") ||
|
if ("getClass".equals(name) ||
|
||||||
name.equals("getDeclaringClass")) {
|
"getDeclaringClass".equals(name)) {
|
||||||
key = "";
|
key = "";
|
||||||
} else {
|
} else {
|
||||||
key = name.substring(3);
|
key = name.substring(3);
|
||||||
|
|
|
@ -8,11 +8,11 @@ package org.json;
|
||||||
* of using the Object's <code>toString()</code> method and quoting the result.
|
* of using the Object's <code>toString()</code> method and quoting the result.
|
||||||
*/
|
*/
|
||||||
public interface JSONString {
|
public interface JSONString {
|
||||||
/**
|
/**
|
||||||
* The <code>toJSONString</code> method allows a class to produce its own JSON
|
* The <code>toJSONString</code> method allows a class to produce its own JSON
|
||||||
* serialization.
|
* serialization.
|
||||||
*
|
*
|
||||||
* @return A strictly syntactically correct JSON text.
|
* @return A strictly syntactically correct JSON text.
|
||||||
*/
|
*/
|
||||||
public String toJSONString();
|
public String toJSONString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,12 +40,12 @@ SOFTWARE.
|
||||||
*/
|
*/
|
||||||
public class JSONTokener {
|
public class JSONTokener {
|
||||||
|
|
||||||
private int character;
|
private int character;
|
||||||
private boolean eof;
|
private boolean eof;
|
||||||
private int index;
|
private int index;
|
||||||
private int line;
|
private int line;
|
||||||
private char previous;
|
private char previous;
|
||||||
private final Reader reader;
|
private final Reader reader;
|
||||||
private boolean usePrevious;
|
private boolean usePrevious;
|
||||||
|
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ public class JSONTokener {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean end() {
|
public boolean end() {
|
||||||
return this.eof && !this.usePrevious;
|
return this.eof && !this.usePrevious;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -148,31 +148,31 @@ public class JSONTokener {
|
||||||
public char next() throws JSONException {
|
public char next() throws JSONException {
|
||||||
int c;
|
int c;
|
||||||
if (this.usePrevious) {
|
if (this.usePrevious) {
|
||||||
this.usePrevious = false;
|
this.usePrevious = false;
|
||||||
c = this.previous;
|
c = this.previous;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
c = this.reader.read();
|
c = this.reader.read();
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
throw new JSONException(exception);
|
throw new JSONException(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c <= 0) { // End of stream
|
if (c <= 0) { // End of stream
|
||||||
this.eof = true;
|
this.eof = true;
|
||||||
c = 0;
|
c = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.index += 1;
|
this.index += 1;
|
||||||
if (this.previous == '\r') {
|
if (this.previous == '\r') {
|
||||||
this.line += 1;
|
this.line += 1;
|
||||||
this.character = c == '\n' ? 0 : 1;
|
this.character = c == '\n' ? 0 : 1;
|
||||||
} else if (c == '\n') {
|
} else if (c == '\n') {
|
||||||
this.line += 1;
|
this.line += 1;
|
||||||
this.character = 0;
|
this.character = 0;
|
||||||
} else {
|
} else {
|
||||||
this.character += 1;
|
this.character += 1;
|
||||||
}
|
}
|
||||||
this.previous = (char) c;
|
this.previous = (char) c;
|
||||||
return this.previous;
|
return this.previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,8 +283,8 @@ public class JSONTokener {
|
||||||
case '\'':
|
case '\'':
|
||||||
case '\\':
|
case '\\':
|
||||||
case '/':
|
case '/':
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw this.syntaxError("Illegal escape.");
|
throw this.syntaxError("Illegal escape.");
|
||||||
}
|
}
|
||||||
|
@ -383,7 +383,7 @@ public class JSONTokener {
|
||||||
this.back();
|
this.back();
|
||||||
|
|
||||||
string = sb.toString().trim();
|
string = sb.toString().trim();
|
||||||
if (string.equals("")) {
|
if ("".equals(string)) {
|
||||||
throw this.syntaxError("Missing value");
|
throw this.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
return JSONObject.stringToValue(string);
|
return JSONObject.stringToValue(string);
|
||||||
|
@ -441,6 +441,6 @@ public class JSONTokener {
|
||||||
*/
|
*/
|
||||||
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 + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
70
XML.java
70
XML.java
|
@ -107,16 +107,16 @@ public class XML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static void noSpace(String string) throws JSONException {
|
public static void noSpace(String string) throws JSONException {
|
||||||
int i, length = string.length();
|
int i, length = string.length();
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
throw new JSONException("Empty string.");
|
throw new JSONException("Empty string.");
|
||||||
}
|
}
|
||||||
for (i = 0; i < length; i += 1) {
|
for (i = 0; i < length; i += 1) {
|
||||||
if (Character.isWhitespace(string.charAt(i))) {
|
if (Character.isWhitespace(string.charAt(i))) {
|
||||||
throw new JSONException("'" + string +
|
throw new JSONException("'" + string +
|
||||||
"' contains a space character.");
|
"' contains a space character.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -160,7 +160,7 @@ public class XML {
|
||||||
x.back();
|
x.back();
|
||||||
} else if (c == '[') {
|
} else if (c == '[') {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (token.equals("CDATA")) {
|
if ("CDATA".equals(token)) {
|
||||||
if (x.next() == '[') {
|
if (x.next() == '[') {
|
||||||
string = x.nextCDATA();
|
string = x.nextCDATA();
|
||||||
if (string.length() > 0) {
|
if (string.length() > 0) {
|
||||||
|
@ -193,7 +193,7 @@ public class XML {
|
||||||
|
|
||||||
// Close tag </
|
// Close tag </
|
||||||
|
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
throw x.syntaxError("Mismatched close tag " + token);
|
throw x.syntaxError("Mismatched close tag " + token);
|
||||||
}
|
}
|
||||||
|
@ -230,7 +230,7 @@ public class XML {
|
||||||
throw x.syntaxError("Missing value");
|
throw x.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
jsonobject.accumulate(string,
|
jsonobject.accumulate(string,
|
||||||
XML.stringToValue((String)token));
|
XML.stringToValue((String)token));
|
||||||
token = null;
|
token = null;
|
||||||
} else {
|
} else {
|
||||||
jsonobject.accumulate(string, "");
|
jsonobject.accumulate(string, "");
|
||||||
|
@ -245,7 +245,7 @@ public class XML {
|
||||||
if (jsonobject.length() > 0) {
|
if (jsonobject.length() > 0) {
|
||||||
context.accumulate(tagName, jsonobject);
|
context.accumulate(tagName, jsonobject);
|
||||||
} else {
|
} else {
|
||||||
context.accumulate(tagName, "");
|
context.accumulate(tagName, "");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -263,7 +263,7 @@ public class XML {
|
||||||
string = (String)token;
|
string = (String)token;
|
||||||
if (string.length() > 0) {
|
if (string.length() > 0) {
|
||||||
jsonobject.accumulate("content",
|
jsonobject.accumulate("content",
|
||||||
XML.stringToValue(string));
|
XML.stringToValue(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nested element
|
// Nested element
|
||||||
|
@ -275,7 +275,7 @@ public class XML {
|
||||||
} else if (jsonobject.length() == 1 &&
|
} else if (jsonobject.length() == 1 &&
|
||||||
jsonobject.opt("content") != null) {
|
jsonobject.opt("content") != null) {
|
||||||
context.accumulate(tagName,
|
context.accumulate(tagName,
|
||||||
jsonobject.opt("content"));
|
jsonobject.opt("content"));
|
||||||
} else {
|
} else {
|
||||||
context.accumulate(tagName, jsonobject);
|
context.accumulate(tagName, jsonobject);
|
||||||
}
|
}
|
||||||
|
@ -301,19 +301,19 @@ public class XML {
|
||||||
* @return A simple JSON value.
|
* @return A simple JSON value.
|
||||||
*/
|
*/
|
||||||
public static Object stringToValue(String string) {
|
public static Object stringToValue(String string) {
|
||||||
if (string.equals("")) {
|
if ("".equals(string)) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (string.equalsIgnoreCase("true")) {
|
if ("true".equalsIgnoreCase(string)) {
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
}
|
}
|
||||||
if (string.equalsIgnoreCase("false")) {
|
if ("false".equalsIgnoreCase(string)) {
|
||||||
return Boolean.FALSE;
|
return Boolean.FALSE;
|
||||||
}
|
}
|
||||||
if (string.equalsIgnoreCase("null")) {
|
if ("null".equalsIgnoreCase(string)) {
|
||||||
return JSONObject.NULL;
|
return JSONObject.NULL;
|
||||||
}
|
}
|
||||||
if (string.equals("0")) {
|
if ("0".equals(string)) {
|
||||||
return new Integer(0);
|
return new Integer(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,16 +321,16 @@ public class XML {
|
||||||
// return the string.
|
// return the string.
|
||||||
|
|
||||||
try {
|
try {
|
||||||
char initial = string.charAt(0);
|
char initial = string.charAt(0);
|
||||||
boolean negative = false;
|
boolean negative = false;
|
||||||
if (initial == '-') {
|
if (initial == '-') {
|
||||||
initial = string.charAt(1);
|
initial = string.charAt(1);
|
||||||
negative = true;
|
negative = true;
|
||||||
}
|
}
|
||||||
if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {
|
if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if ((initial >= '0' && initial <= '9')) {
|
if ((initial >= '0' && initial <= '9')) {
|
||||||
if (string.indexOf('.') >= 0) {
|
if (string.indexOf('.') >= 0) {
|
||||||
return Double.valueOf(string);
|
return Double.valueOf(string);
|
||||||
} else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) {
|
} else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) {
|
||||||
|
@ -341,7 +341,7 @@ public class XML {
|
||||||
return myLong;
|
return myLong;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
|
@ -419,7 +419,7 @@ public class XML {
|
||||||
key = keys.next().toString();
|
key = keys.next().toString();
|
||||||
value = jo.opt(key);
|
value = jo.opt(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
value = "";
|
value = "";
|
||||||
}
|
}
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
string = (String)value;
|
string = (String)value;
|
||||||
|
@ -429,7 +429,7 @@ public class XML {
|
||||||
|
|
||||||
// Emit content in body
|
// Emit content in body
|
||||||
|
|
||||||
if (key.equals("content")) {
|
if ("content".equals(key)) {
|
||||||
if (value instanceof JSONArray) {
|
if (value instanceof JSONArray) {
|
||||||
ja = (JSONArray)value;
|
ja = (JSONArray)value;
|
||||||
length = ja.length();
|
length = ja.length();
|
||||||
|
@ -462,7 +462,7 @@ public class XML {
|
||||||
sb.append(toString(value, key));
|
sb.append(toString(value, key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (value.equals("")) {
|
} else if ("".equals(value)) {
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append(key);
|
sb.append(key);
|
||||||
sb.append("/>");
|
sb.append("/>");
|
||||||
|
|
112
XMLTokener.java
112
XMLTokener.java
|
@ -272,7 +272,7 @@ public class XMLTokener extends JSONTokener {
|
||||||
}
|
}
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 0:
|
case 0:
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
case '>':
|
case '>':
|
||||||
case '/':
|
case '/':
|
||||||
case '=':
|
case '=':
|
||||||
|
@ -299,12 +299,12 @@ public class XMLTokener extends JSONTokener {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public boolean skipPast(String to) throws JSONException {
|
public boolean skipPast(String to) throws JSONException {
|
||||||
boolean b;
|
boolean b;
|
||||||
char c;
|
char c;
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
int length = to.length();
|
int length = to.length();
|
||||||
char[] circle = new char[length];
|
char[] circle = new char[length];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -312,54 +312,54 @@ public class XMLTokener extends JSONTokener {
|
||||||
* to string. If we reach an early end, bail.
|
* to string. If we reach an early end, bail.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for (i = 0; i < length; i += 1) {
|
for (i = 0; i < length; i += 1) {
|
||||||
c = next();
|
c = next();
|
||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
circle[i] = c;
|
circle[i] = c;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* We will loop, possibly for all of the remaining characters.
|
* We will loop, possibly for all of the remaining characters.
|
||||||
*/
|
*/
|
||||||
for (;;) {
|
for (;;) {
|
||||||
j = offset;
|
j = offset;
|
||||||
b = true;
|
b = true;
|
||||||
/*
|
/*
|
||||||
* Compare the circle buffer with the to string.
|
* Compare the circle buffer with the to string.
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < length; i += 1) {
|
for (i = 0; i < length; i += 1) {
|
||||||
if (circle[j] != to.charAt(i)) {
|
if (circle[j] != to.charAt(i)) {
|
||||||
b = false;
|
b = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
j += 1;
|
j += 1;
|
||||||
if (j >= length) {
|
if (j >= length) {
|
||||||
j -= length;
|
j -= length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* If we exit the loop with b intact, then victory is ours.
|
* If we exit the loop with b intact, then victory is ours.
|
||||||
*/
|
*/
|
||||||
if (b) {
|
if (b) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Get the next character. If there isn't one, then defeat is ours.
|
* Get the next character. If there isn't one, then defeat is ours.
|
||||||
*/
|
*/
|
||||||
c = next();
|
c = next();
|
||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Shove the character in the circle buffer and advance the
|
* Shove the character in the circle buffer and advance the
|
||||||
* circle offset. The offset is mod n.
|
* circle offset. The offset is mod n.
|
||||||
*/
|
*/
|
||||||
circle[offset] = c;
|
circle[offset] = c;
|
||||||
offset += 1;
|
offset += 1;
|
||||||
if (offset >= length) {
|
if (offset >= length) {
|
||||||
offset -= length;
|
offset -= length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue