diff --git a/src/main/java/org/json/Cookie.java b/src/main/java/org/json/Cookie.java index ab908a304..630136e58 100644 --- a/src/main/java/org/json/Cookie.java +++ b/src/main/java/org/json/Cookie.java @@ -18,7 +18,7 @@ public class Cookie { /** * Constructs a new Cookie object. */ - public Cookie() { + private Cookie() { } /** @@ -189,21 +189,30 @@ public static String toString(JSONObject jo) throws JSONException { * @return The unescaped string. */ public static String unescape(String string) { + int i = 0; int length = string.length(); StringBuilder sb = new StringBuilder(length); - for (int i = 0; i < length; ++i) { + + while (i < length) { char c = string.charAt(i); if (c == '+') { - c = ' '; + sb.append(' '); + i++; } else if (c == '%' && i + 2 < length) { int d = JSONTokener.dehexchar(string.charAt(i + 1)); int e = JSONTokener.dehexchar(string.charAt(i + 2)); + if (d >= 0 && e >= 0) { - c = (char)(d * 16 + e); - i += 2; + sb.append((char)(d * 16 + e)); + i += 3; + } else { + sb.append(c); + i++; } + } else { + sb.append(c); + i++; } - sb.append(c); } return sb.toString(); } diff --git a/src/main/java/org/json/CookieList.java b/src/main/java/org/json/CookieList.java index d1064db52..e9dd4e652 100644 --- a/src/main/java/org/json/CookieList.java +++ b/src/main/java/org/json/CookieList.java @@ -14,7 +14,7 @@ public class CookieList { /** * Constructs a new CookieList object. */ - public CookieList() { + private CookieList() { } /** diff --git a/src/main/java/org/json/JSONML.java b/src/main/java/org/json/JSONML.java index 6e98c8267..6b702080f 100644 --- a/src/main/java/org/json/JSONML.java +++ b/src/main/java/org/json/JSONML.java @@ -17,9 +17,10 @@ public class JSONML { /** * Constructs a new JSONML object. */ - public JSONML() { + private JSONML() { } + /** * Parse XML values and store them in a JSONArray. * @param x The XMLTokener containing the source string. @@ -239,9 +240,21 @@ private static Object parse( } } else { if (ja != null) { - ja.put(token instanceof String - ? (config.isKeepStrings() ? XML.unescape((String)token) : XML.stringToValue((String)token)) - : token); + Object value; + + if (token instanceof String) { + String strToken = (String) token; + if (config.isKeepStrings()) { + value = XML.unescape(strToken); + } else { + value = XML.stringToValue(strToken); + } + } else { + value = token; + } + + ja.put(value); + } } } diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 07ff18c99..118b05e57 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -454,15 +454,16 @@ public String nextTo(String delimiters) throws JSONException { */ public Object nextValue() throws JSONException { char c = this.nextClean(); - switch (c) { - case '{': + + if (c == '{') { this.back(); try { return new JSONObject(this, jsonParserConfiguration); } catch (StackOverflowError e) { throw new JSONException("JSON Array or Object depth too large to process.", e); } - case '[': + + } else if (c == '[') { this.back(); try { return new JSONArray(this, jsonParserConfiguration); @@ -470,9 +471,11 @@ public Object nextValue() throws JSONException { throw new JSONException("JSON Array or Object depth too large to process.", e); } } + return nextSimpleValue(c); } + Object nextSimpleValue(char c) { String string; @@ -482,9 +485,8 @@ Object nextSimpleValue(char c) { c == '\'') { throw this.syntaxError("Strict mode error: Single quoted strings are not allowed"); } - switch (c) { - case '"': - case '\'': + + if (c == '"' || c == '\'') { return this.nextString(c); }