mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Updates for supporting the Android API
This commit is contained in:
parent
5024f2d210
commit
e8b1b66888
9 changed files with 198 additions and 101 deletions
77
XML.java
77
XML.java
|
@ -25,7 +25,6 @@ SOFTWARE.
|
|||
*/
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* This provides static methods to convert an XML text into a JSONObject, and to
|
||||
|
@ -430,11 +429,49 @@ public class XML {
|
|||
* @return JSON value of this string or the string
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
Object ret = JSONObject.stringToValue(string);
|
||||
if(ret instanceof String){
|
||||
return unescape((String)ret);
|
||||
if (string.equals("")) {
|
||||
return string;
|
||||
}
|
||||
return ret;
|
||||
if (string.equalsIgnoreCase("true")) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("false")) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if (string.equalsIgnoreCase("null")) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* If it might be a number, try converting it. If a number cannot be
|
||||
* produced, then the value will just be a string.
|
||||
*/
|
||||
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
// if we want full Big Number support this block can be replaced with:
|
||||
// return stringToNumber(string);
|
||||
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|
||||
|| string.indexOf('E') > -1 || "-0".equals(string)) {
|
||||
Double d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
Long myLong = Long.valueOf(string);
|
||||
if (string.equals(myLong.toString())) {
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return Integer.valueOf(myLong.intValue());
|
||||
}
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
return unescape(string);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -482,8 +519,11 @@ public class XML {
|
|||
public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
XMLTokener x = new XMLTokener(string);
|
||||
while (x.more() && x.skipPast("<")) {
|
||||
parse(x, jo, null, keepStrings);
|
||||
while (x.more()) {
|
||||
x.skipPast("<");
|
||||
if(x.more()) {
|
||||
parse(x, jo, null, keepStrings);
|
||||
}
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
@ -526,10 +566,10 @@ public class XML {
|
|||
}
|
||||
|
||||
// Loop thru the keys.
|
||||
// don't use the new entrySet accessor to maintain Android Support
|
||||
jo = (JSONObject) object;
|
||||
for (final Entry<String, ?> entry : jo.entrySet()) {
|
||||
final String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
for (final String key : jo.keySet()) {
|
||||
Object value = jo.opt(key);
|
||||
if (value == null) {
|
||||
value = "";
|
||||
} else if (value.getClass().isArray()) {
|
||||
|
@ -540,13 +580,14 @@ public class XML {
|
|||
if ("content".equals(key)) {
|
||||
if (value instanceof JSONArray) {
|
||||
ja = (JSONArray) value;
|
||||
int i = 0;
|
||||
for (Object val : ja) {
|
||||
int jaLength = ja.length();
|
||||
// don't use the new iterator API to maintain support for Android
|
||||
for (int i = 0; i < jaLength; i++) {
|
||||
if (i > 0) {
|
||||
sb.append('\n');
|
||||
}
|
||||
Object val = ja.opt(i);
|
||||
sb.append(escape(val.toString()));
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
sb.append(escape(value.toString()));
|
||||
|
@ -556,7 +597,10 @@ public class XML {
|
|||
|
||||
} else if (value instanceof JSONArray) {
|
||||
ja = (JSONArray) value;
|
||||
for (Object val : ja) {
|
||||
int jaLength = ja.length();
|
||||
// don't use the new iterator API to maintain support for Android
|
||||
for (int i = 0; i < jaLength; i++) {
|
||||
Object val = ja.opt(i);
|
||||
if (val instanceof JSONArray) {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
|
@ -597,7 +641,10 @@ public class XML {
|
|||
} else {
|
||||
ja = (JSONArray) object;
|
||||
}
|
||||
for (Object val : ja) {
|
||||
int jaLength = ja.length();
|
||||
// don't use the new iterator API to maintain support for Android
|
||||
for (int i = 0; i < jaLength; i++) {
|
||||
Object val = ja.opt(i);
|
||||
// XML does not have good support for arrays. If an array
|
||||
// appears in a place where XML is lacking, synthesize an
|
||||
// <array> element.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue