spacing :D
This commit is contained in:
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
/**
|
||||
* This provides static methods to convert comma delimited text into a
|
||||
|
||||
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
/**
|
||||
* Convert a web browser cookie specification to a JSONObject and back.
|
||||
|
||||
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
+145
-145
@@ -34,159 +34,159 @@ import java.util.Iterator;
|
||||
*/
|
||||
public class HTTP {
|
||||
|
||||
/** Carriage return/line feed. */
|
||||
public static final String CRLF = "\r\n";
|
||||
/** Carriage return/line feed. */
|
||||
public static final String CRLF = "\r\n";
|
||||
|
||||
/**
|
||||
* Convert an HTTP header string into a JSONObject. It can be a request
|
||||
* header or a response header. A request header will contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* A response header will contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* In addition, the other parameters in the header will be captured, using
|
||||
* the HTTP field names as JSON names, so that
|
||||
*
|
||||
* <pre>
|
||||
* Date: Sun, 26 May 2002 18:06:04 GMT
|
||||
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
|
||||
* Cache-Control: no-cache
|
||||
* </pre>
|
||||
*
|
||||
* become
|
||||
*
|
||||
* <pre>
|
||||
* {...
|
||||
* Date: "Sun, 26 May 2002 18:06:04 GMT",
|
||||
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
|
||||
* "Cache-Control": "no-cache",
|
||||
* ...}
|
||||
* </pre>
|
||||
*
|
||||
* It does no further checking or conversion. It does not parse dates. It
|
||||
* does not do '%' transforms on URLs.
|
||||
*
|
||||
* @param string
|
||||
* An HTTP header string.
|
||||
* @return A JSONObject containing the elements and attributes of the XML
|
||||
* string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
HTTPTokener x = new HTTPTokener(string);
|
||||
String token;
|
||||
/**
|
||||
* Convert an HTTP header string into a JSONObject. It can be a request
|
||||
* header or a response header. A request header will contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* A response header will contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* In addition, the other parameters in the header will be captured, using
|
||||
* the HTTP field names as JSON names, so that
|
||||
*
|
||||
* <pre>
|
||||
* Date: Sun, 26 May 2002 18:06:04 GMT
|
||||
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
|
||||
* Cache-Control: no-cache
|
||||
* </pre>
|
||||
*
|
||||
* become
|
||||
*
|
||||
* <pre>
|
||||
* {...
|
||||
* Date: "Sun, 26 May 2002 18:06:04 GMT",
|
||||
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
|
||||
* "Cache-Control": "no-cache",
|
||||
* ...}
|
||||
* </pre>
|
||||
*
|
||||
* It does no further checking or conversion. It does not parse dates. It
|
||||
* does not do '%' transforms on URLs.
|
||||
*
|
||||
* @param string
|
||||
* An HTTP header string.
|
||||
* @return A JSONObject containing the elements and attributes of the XML
|
||||
* string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
HTTPTokener x = new HTTPTokener(string);
|
||||
String token;
|
||||
|
||||
token = x.nextToken();
|
||||
if (token.toUpperCase().startsWith("HTTP")) {
|
||||
token = x.nextToken();
|
||||
if (token.toUpperCase().startsWith("HTTP")) {
|
||||
|
||||
// Response
|
||||
// Response
|
||||
|
||||
jo.put("HTTP-Version", token);
|
||||
jo.put("Status-Code", x.nextToken());
|
||||
jo.put("Reason-Phrase", x.nextTo('\0'));
|
||||
x.next();
|
||||
jo.put("HTTP-Version", token);
|
||||
jo.put("Status-Code", x.nextToken());
|
||||
jo.put("Reason-Phrase", x.nextTo('\0'));
|
||||
x.next();
|
||||
|
||||
} else {
|
||||
} else {
|
||||
|
||||
// Request
|
||||
// Request
|
||||
|
||||
jo.put("Method", token);
|
||||
jo.put("Request-URI", x.nextToken());
|
||||
jo.put("HTTP-Version", x.nextToken());
|
||||
}
|
||||
jo.put("Method", token);
|
||||
jo.put("Request-URI", x.nextToken());
|
||||
jo.put("HTTP-Version", x.nextToken());
|
||||
}
|
||||
|
||||
// Fields
|
||||
// Fields
|
||||
|
||||
while (x.more()) {
|
||||
String name = x.nextTo(':');
|
||||
x.next(':');
|
||||
jo.put(name, x.nextTo('\0'));
|
||||
x.next();
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
while (x.more()) {
|
||||
String name = x.nextTo(':');
|
||||
x.next(':');
|
||||
jo.put(name, x.nextTo('\0'));
|
||||
x.next();
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into an HTTP header. A request header must contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* A response header must contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* Any other members of the JSONObject will be output as HTTP fields. The
|
||||
* result will end with two CRLF pairs.
|
||||
*
|
||||
* @param jo
|
||||
* A JSONObject
|
||||
* @return An HTTP header string.
|
||||
* @throws JSONException
|
||||
* if the object does not contain enough information.
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
Iterator<?> keys = jo.keys();
|
||||
String string;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Status-Code"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Reason-Phrase"));
|
||||
} else if (jo.has("Method") && jo.has("Request-URI")) {
|
||||
sb.append(jo.getString("Method"));
|
||||
sb.append(' ');
|
||||
sb.append('"');
|
||||
sb.append(jo.getString("Request-URI"));
|
||||
sb.append('"');
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
} else {
|
||||
throw new JSONException("Not enough material for an HTTP header.");
|
||||
}
|
||||
sb.append(CRLF);
|
||||
while (keys.hasNext()) {
|
||||
string = keys.next().toString();
|
||||
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string)
|
||||
&& !"Reason-Phrase".equals(string)
|
||||
&& !"Method".equals(string)
|
||||
&& !"Request-URI".equals(string) && !jo.isNull(string)) {
|
||||
sb.append(string);
|
||||
sb.append(": ");
|
||||
sb.append(jo.getString(string));
|
||||
sb.append(CRLF);
|
||||
}
|
||||
}
|
||||
sb.append(CRLF);
|
||||
return sb.toString();
|
||||
}
|
||||
/**
|
||||
* Convert a JSONObject into an HTTP header. A request header must contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* Method: "POST" (for example),
|
||||
* "Request-URI": "/" (for example),
|
||||
* "HTTP-Version": "HTTP/1.1" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* A response header must contain
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "HTTP-Version": "HTTP/1.1" (for example),
|
||||
* "Status-Code": "200" (for example),
|
||||
* "Reason-Phrase": "OK" (for example)
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* Any other members of the JSONObject will be output as HTTP fields. The
|
||||
* result will end with two CRLF pairs.
|
||||
*
|
||||
* @param jo
|
||||
* A JSONObject
|
||||
* @return An HTTP header string.
|
||||
* @throws JSONException
|
||||
* if the object does not contain enough information.
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
Iterator<?> keys = jo.keys();
|
||||
String string;
|
||||
StringBuffer sb = new StringBuffer();
|
||||
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Status-Code"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Reason-Phrase"));
|
||||
} else if (jo.has("Method") && jo.has("Request-URI")) {
|
||||
sb.append(jo.getString("Method"));
|
||||
sb.append(' ');
|
||||
sb.append('"');
|
||||
sb.append(jo.getString("Request-URI"));
|
||||
sb.append('"');
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
} else {
|
||||
throw new JSONException("Not enough material for an HTTP header.");
|
||||
}
|
||||
sb.append(CRLF);
|
||||
while (keys.hasNext()) {
|
||||
string = keys.next().toString();
|
||||
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string)
|
||||
&& !"Reason-Phrase".equals(string)
|
||||
&& !"Method".equals(string)
|
||||
&& !"Request-URI".equals(string) && !jo.isNull(string)) {
|
||||
sb.append(string);
|
||||
sb.append(": ");
|
||||
sb.append(jo.getString(string));
|
||||
sb.append(CRLF);
|
||||
}
|
||||
}
|
||||
sb.append(CRLF);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
/**
|
||||
* The HTTPTokener extends the JSONTokener to provide additional methods
|
||||
|
||||
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
@@ -173,7 +173,7 @@ public class JSONArray {
|
||||
}
|
||||
} else {
|
||||
throw new JSONException(
|
||||
"JSONArray initial value should be a string or collection or array.");
|
||||
"JSONArray initial value should be a string or collection or array.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,11 +207,11 @@ public class JSONArray {
|
||||
Object object = this.get(index);
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
|
||||
@@ -230,11 +230,11 @@ public class JSONArray {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).doubleValue()
|
||||
: Double.parseDouble((String)object);
|
||||
? ((Number)object).doubleValue()
|
||||
: Double.parseDouble((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a number.");
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,11 +250,11 @@ public class JSONArray {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a number.");
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,7 +289,7 @@ public class JSONArray {
|
||||
return (JSONObject)object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a JSONObject.");
|
||||
"] is not a JSONObject.");
|
||||
}
|
||||
|
||||
|
||||
@@ -305,11 +305,11 @@ public class JSONArray {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index +
|
||||
"] is not a number.");
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,8 +379,8 @@ public class JSONArray {
|
||||
*/
|
||||
public Object opt(int index) {
|
||||
return (index < 0 || index >= this.length())
|
||||
? null
|
||||
: this.myArrayList.get(index);
|
||||
? null
|
||||
: this.myArrayList.get(index);
|
||||
}
|
||||
|
||||
|
||||
@@ -556,8 +556,8 @@ public class JSONArray {
|
||||
public String optString(int index, String defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
return JSONObject.NULL.equals(object)
|
||||
? defaultValue
|
||||
: object.toString();
|
||||
? defaultValue
|
||||
: object.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -900,7 +900,7 @@ public class JSONArray {
|
||||
writer.write(']');
|
||||
return writer;
|
||||
} catch (IOException e) {
|
||||
throw new JSONException(e);
|
||||
throw new JSONException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
@@ -97,22 +97,22 @@ public class JSONObject {
|
||||
/**
|
||||
* The maximum number of keys in the key pool.
|
||||
*/
|
||||
private static final int keyPoolSize = 100;
|
||||
private static final int keyPoolSize = 100;
|
||||
|
||||
/**
|
||||
/**
|
||||
* Key pooling is like string interning, but without permanently tying up
|
||||
* memory. To help conserve memory, storage of duplicated key strings in
|
||||
* JSONObjects will be avoided by using a key pool to manage unique key
|
||||
* string objects. This is used by JSONObject.put(string, object).
|
||||
*/
|
||||
private static HashMap keyPool = new HashMap(keyPoolSize);
|
||||
private static HashMap keyPool = new HashMap(keyPoolSize);
|
||||
|
||||
/**
|
||||
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
|
||||
* whilst Java's null is equivalent to the value that JavaScript calls
|
||||
* undefined.
|
||||
*/
|
||||
private static final class Null {
|
||||
private static final class Null {
|
||||
|
||||
/**
|
||||
* There is only intended to be a single instance of the NULL object,
|
||||
@@ -212,7 +212,7 @@ public class JSONObject {
|
||||
key = x.nextValue().toString();
|
||||
}
|
||||
|
||||
// The key is followed by ':'. We will also tolerate '=' or '=>'.
|
||||
// The key is followed by ':'. We will also tolerate '=' or '=>'.
|
||||
|
||||
c = x.nextClean();
|
||||
if (c == '=') {
|
||||
@@ -224,7 +224,7 @@ public class JSONObject {
|
||||
}
|
||||
this.putOnce(key, x.nextValue());
|
||||
|
||||
// Pairs are separated by ','. We will also tolerate ';'.
|
||||
// Pairs are separated by ','. We will also tolerate ';'.
|
||||
|
||||
switch (x.nextClean()) {
|
||||
case ';':
|
||||
@@ -339,16 +339,16 @@ public class JSONObject {
|
||||
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
|
||||
Thread.currentThread().getContextClassLoader());
|
||||
|
||||
// Iterate through the keys in the bundle.
|
||||
// Iterate through the keys in the bundle.
|
||||
|
||||
Enumeration keys = bundle.getKeys();
|
||||
while (keys.hasMoreElements()) {
|
||||
Object key = keys.nextElement();
|
||||
if (key instanceof String) {
|
||||
|
||||
// Go through the path, ensuring that there is a nested JSONObject for each
|
||||
// segment except the last. Add the value using the last segment's name into
|
||||
// the deepest nested JSONObject.
|
||||
// Go through the path, ensuring that there is a nested JSONObject for each
|
||||
// segment except the last. Add the value using the last segment's name into
|
||||
// the deepest nested JSONObject.
|
||||
|
||||
String[] path = ((String)key).split("\\.");
|
||||
int last = path.length - 1;
|
||||
@@ -385,15 +385,15 @@ public class JSONObject {
|
||||
* or if the key is null.
|
||||
*/
|
||||
public JSONObject accumulate(
|
||||
String key,
|
||||
Object value
|
||||
) throws JSONException {
|
||||
String key,
|
||||
Object value
|
||||
) throws JSONException {
|
||||
testValidity(value);
|
||||
Object object = this.opt(key);
|
||||
if (object == null) {
|
||||
this.put(key, value instanceof JSONArray
|
||||
? new JSONArray().put(value)
|
||||
: value);
|
||||
: value);
|
||||
} else if (object instanceof JSONArray) {
|
||||
((JSONArray)object).put(value);
|
||||
} else {
|
||||
@@ -440,7 +440,7 @@ public class JSONObject {
|
||||
return "null";
|
||||
}
|
||||
|
||||
// Shave off trailing zeros and decimal point, if possible.
|
||||
// Shave off trailing zeros and decimal point, if possible.
|
||||
|
||||
String string = Double.toString(d);
|
||||
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
@@ -488,11 +488,11 @@ public class JSONObject {
|
||||
Object object = this.get(key);
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
@@ -511,11 +511,11 @@ public class JSONObject {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).doubleValue()
|
||||
: Double.parseDouble((String)object);
|
||||
? ((Number)object).doubleValue()
|
||||
: Double.parseDouble((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not a number.");
|
||||
"] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,11 +532,11 @@ public class JSONObject {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not an int.");
|
||||
"] is not an int.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -589,11 +589,11 @@ public class JSONObject {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] is not a long.");
|
||||
"] is not a long.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -655,7 +655,7 @@ public class JSONObject {
|
||||
return (String)object;
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key) +
|
||||
"] not a string.");
|
||||
"] not a string.");
|
||||
}
|
||||
|
||||
|
||||
@@ -767,7 +767,7 @@ public class JSONObject {
|
||||
}
|
||||
testValidity(number);
|
||||
|
||||
// Shave off trailing zeros and decimal point, if possible.
|
||||
// Shave off trailing zeros and decimal point, if possible.
|
||||
|
||||
String string = number.toString();
|
||||
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
|
||||
@@ -981,48 +981,48 @@ public class JSONObject {
|
||||
private void populateMap(Object bean) {
|
||||
Class klass = bean.getClass();
|
||||
|
||||
// If klass is a System class then set includeSuperClass to false.
|
||||
// If klass is a System class then set includeSuperClass to false.
|
||||
|
||||
boolean includeSuperClass = klass.getClassLoader() != null;
|
||||
|
||||
Method[] methods = includeSuperClass
|
||||
? klass.getMethods()
|
||||
: klass.getDeclaredMethods();
|
||||
for (int i = 0; i < methods.length; i += 1) {
|
||||
try {
|
||||
Method method = methods[i];
|
||||
if (Modifier.isPublic(method.getModifiers())) {
|
||||
String name = method.getName();
|
||||
String key = "";
|
||||
if (name.startsWith("get")) {
|
||||
if ("getClass".equals(name) ||
|
||||
"getDeclaringClass".equals(name)) {
|
||||
key = "";
|
||||
} else {
|
||||
key = name.substring(3);
|
||||
}
|
||||
} else if (name.startsWith("is")) {
|
||||
key = name.substring(2);
|
||||
}
|
||||
if (key.length() > 0 &&
|
||||
Character.isUpperCase(key.charAt(0)) &&
|
||||
method.getParameterTypes().length == 0) {
|
||||
if (key.length() == 1) {
|
||||
key = key.toLowerCase();
|
||||
} else if (!Character.isUpperCase(key.charAt(1))) {
|
||||
key = key.substring(0, 1).toLowerCase() +
|
||||
key.substring(1);
|
||||
}
|
||||
: klass.getDeclaredMethods();
|
||||
for (int i = 0; i < methods.length; i += 1) {
|
||||
try {
|
||||
Method method = methods[i];
|
||||
if (Modifier.isPublic(method.getModifiers())) {
|
||||
String name = method.getName();
|
||||
String key = "";
|
||||
if (name.startsWith("get")) {
|
||||
if ("getClass".equals(name) ||
|
||||
"getDeclaringClass".equals(name)) {
|
||||
key = "";
|
||||
} else {
|
||||
key = name.substring(3);
|
||||
}
|
||||
} else if (name.startsWith("is")) {
|
||||
key = name.substring(2);
|
||||
}
|
||||
if (key.length() > 0 &&
|
||||
Character.isUpperCase(key.charAt(0)) &&
|
||||
method.getParameterTypes().length == 0) {
|
||||
if (key.length() == 1) {
|
||||
key = key.toLowerCase();
|
||||
} else if (!Character.isUpperCase(key.charAt(1))) {
|
||||
key = key.substring(0, 1).toLowerCase() +
|
||||
key.substring(1);
|
||||
}
|
||||
|
||||
Object result = method.invoke(bean, (Object[])null);
|
||||
if (result != null) {
|
||||
this.map.put(key, wrap(result));
|
||||
Object result = method.invoke(bean, (Object[])null);
|
||||
if (result != null) {
|
||||
this.map.put(key, wrap(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1335,12 +1335,12 @@ public class JSONObject {
|
||||
if (o instanceof Double) {
|
||||
if (((Double)o).isInfinite() || ((Double)o).isNaN()) {
|
||||
throw new JSONException(
|
||||
"JSON does not allow non-finite numbers.");
|
||||
"JSON does not allow non-finite numbers.");
|
||||
}
|
||||
} else if (o instanceof Float) {
|
||||
if (((Float)o).isInfinite() || ((Float)o).isNaN()) {
|
||||
throw new JSONException(
|
||||
"JSON does not allow non-finite numbers.");
|
||||
"JSON does not allow non-finite numbers.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1462,70 +1462,70 @@ public class JSONObject {
|
||||
return quote(value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap an object, if necessary. If the object is null, return the NULL
|
||||
* object. If it is an array or collection, wrap it in a JSONArray. If
|
||||
* it is a map, wrap it in a JSONObject. If it is a standard property
|
||||
* (Double, String, et al) then it is already wrapped. Otherwise, if it
|
||||
* comes from one of the java packages, turn it into a string. And if
|
||||
* it doesn't, try to wrap it in a JSONObject. If the wrapping fails,
|
||||
* then null is returned.
|
||||
*
|
||||
* @param object The object to wrap
|
||||
* @return The wrapped value
|
||||
*/
|
||||
public static Object wrap(Object object) {
|
||||
try {
|
||||
if (object == null) {
|
||||
return NULL;
|
||||
}
|
||||
if (object instanceof JSONObject || object instanceof JSONArray ||
|
||||
NULL.equals(object) || object instanceof JSONString ||
|
||||
object instanceof Byte || object instanceof Character ||
|
||||
object instanceof Short || object instanceof Integer ||
|
||||
object instanceof Long || object instanceof Boolean ||
|
||||
object instanceof Float || object instanceof Double ||
|
||||
object instanceof String) {
|
||||
return object;
|
||||
}
|
||||
/**
|
||||
* Wrap an object, if necessary. If the object is null, return the NULL
|
||||
* object. If it is an array or collection, wrap it in a JSONArray. If
|
||||
* it is a map, wrap it in a JSONObject. If it is a standard property
|
||||
* (Double, String, et al) then it is already wrapped. Otherwise, if it
|
||||
* comes from one of the java packages, turn it into a string. And if
|
||||
* it doesn't, try to wrap it in a JSONObject. If the wrapping fails,
|
||||
* then null is returned.
|
||||
*
|
||||
* @param object The object to wrap
|
||||
* @return The wrapped value
|
||||
*/
|
||||
public static Object wrap(Object object) {
|
||||
try {
|
||||
if (object == null) {
|
||||
return NULL;
|
||||
}
|
||||
if (object instanceof JSONObject || object instanceof JSONArray ||
|
||||
NULL.equals(object) || object instanceof JSONString ||
|
||||
object instanceof Byte || object instanceof Character ||
|
||||
object instanceof Short || object instanceof Integer ||
|
||||
object instanceof Long || object instanceof Boolean ||
|
||||
object instanceof Float || object instanceof Double ||
|
||||
object instanceof String) {
|
||||
return object;
|
||||
}
|
||||
|
||||
if (object instanceof Collection) {
|
||||
return new JSONArray((Collection)object);
|
||||
}
|
||||
if (object.getClass().isArray()) {
|
||||
return new JSONArray(object);
|
||||
}
|
||||
if (object instanceof Map) {
|
||||
return new JSONObject((Map)object);
|
||||
}
|
||||
Package objectPackage = object.getClass().getPackage();
|
||||
String objectPackageName = objectPackage != null
|
||||
? objectPackage.getName()
|
||||
: "";
|
||||
if (
|
||||
objectPackageName.startsWith("java.") ||
|
||||
objectPackageName.startsWith("javax.") ||
|
||||
object.getClass().getClassLoader() == null
|
||||
) {
|
||||
return object.toString();
|
||||
}
|
||||
return new JSONObject(object);
|
||||
} catch(Exception exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (object instanceof Collection) {
|
||||
return new JSONArray((Collection)object);
|
||||
}
|
||||
if (object.getClass().isArray()) {
|
||||
return new JSONArray(object);
|
||||
}
|
||||
if (object instanceof Map) {
|
||||
return new JSONObject((Map)object);
|
||||
}
|
||||
Package objectPackage = object.getClass().getPackage();
|
||||
String objectPackageName = objectPackage != null
|
||||
? objectPackage.getName()
|
||||
: "";
|
||||
if (
|
||||
objectPackageName.startsWith("java.") ||
|
||||
objectPackageName.startsWith("javax.") ||
|
||||
object.getClass().getClassLoader() == null
|
||||
) {
|
||||
return object.toString();
|
||||
}
|
||||
return new JSONObject(object);
|
||||
} catch(Exception exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write the contents of the JSONObject as JSON text to a writer.
|
||||
* For compactness, no whitespace is added.
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @return The writer.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
/**
|
||||
* Write the contents of the JSONObject as JSON text to a writer.
|
||||
* For compactness, no whitespace is added.
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @return The writer.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
}
|
||||
|
||||
@@ -1624,5 +1624,5 @@ public class JSONObject {
|
||||
} catch (IOException exception) {
|
||||
throw new JSONException(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
|
||||
@@ -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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
/**
|
||||
* A JSONTokener takes a source string and extracts characters and tokens from
|
||||
@@ -56,8 +56,8 @@ public class JSONTokener {
|
||||
*/
|
||||
public JSONTokener(Reader reader) {
|
||||
this.reader = reader.markSupported()
|
||||
? reader
|
||||
: new BufferedReader(reader);
|
||||
? reader
|
||||
: new BufferedReader(reader);
|
||||
this.eof = false;
|
||||
this.usePrevious = false;
|
||||
this.previous = 0;
|
||||
@@ -203,23 +203,23 @@ public class JSONTokener {
|
||||
* Substring bounds error if there are not
|
||||
* n characters remaining in the source string.
|
||||
*/
|
||||
public String next(int n) throws JSONException {
|
||||
if (n == 0) {
|
||||
return "";
|
||||
}
|
||||
public String next(int n) throws JSONException {
|
||||
if (n == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
char[] chars = new char[n];
|
||||
int pos = 0;
|
||||
char[] chars = new char[n];
|
||||
int pos = 0;
|
||||
|
||||
while (pos < n) {
|
||||
chars[pos] = this.next();
|
||||
if (this.end()) {
|
||||
throw this.syntaxError("Substring bounds error");
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
while (pos < n) {
|
||||
chars[pos] = this.next();
|
||||
if (this.end()) {
|
||||
throw this.syntaxError("Substring bounds error");
|
||||
}
|
||||
pos += 1;
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -355,15 +355,15 @@ public class JSONTokener {
|
||||
String string;
|
||||
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
return this.nextString(c);
|
||||
case '{':
|
||||
this.back();
|
||||
return new JSONObject(this);
|
||||
case '[':
|
||||
this.back();
|
||||
return new JSONArray(this);
|
||||
case '"':
|
||||
case '\'':
|
||||
return this.nextString(c);
|
||||
case '{':
|
||||
this.back();
|
||||
return new JSONObject(this);
|
||||
case '[':
|
||||
this.back();
|
||||
return new JSONArray(this);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -441,6 +441,6 @@ public class JSONTokener {
|
||||
*/
|
||||
public String toString() {
|
||||
return " at " + this.index + " [character " + this.character + " line " +
|
||||
this.line + "]";
|
||||
this.line + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,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,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
*/
|
||||
|
||||
/**
|
||||
* JSONWriter provides a quick and convenient way of producing JSON text.
|
||||
@@ -158,8 +158,8 @@ public class JSONWriter {
|
||||
private JSONWriter end(char mode, char c) throws JSONException {
|
||||
if (this.mode != mode) {
|
||||
throw new JSONException(mode == 'a'
|
||||
? "Misplaced endArray."
|
||||
: "Misplaced endObject.");
|
||||
? "Misplaced endArray."
|
||||
: "Misplaced endObject.");
|
||||
}
|
||||
this.pop(mode);
|
||||
try {
|
||||
@@ -261,10 +261,10 @@ public class JSONWriter {
|
||||
}
|
||||
this.top -= 1;
|
||||
this.mode = this.top == 0
|
||||
? 'd'
|
||||
: this.stack[this.top - 1] == null
|
||||
? 'a'
|
||||
: 'k';
|
||||
? 'd'
|
||||
: this.stack[this.top - 1] == null
|
||||
? 'a'
|
||||
: 'k';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user