From c13b57ca267b4d7aca11b0d93436e0d98332ca7a Mon Sep 17 00:00:00 2001 From: md-yasir Date: Thu, 23 Oct 2025 22:36:53 +0530 Subject: [PATCH 1/5] Made Cookie constructor to private. --- src/main/java/org/json/Cookie.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/json/Cookie.java b/src/main/java/org/json/Cookie.java index ab908a304..11cc97a21 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() { } /** From 1de42aa4fd2baa6f83a6bac6ef38d29fb8579999 Mon Sep 17 00:00:00 2001 From: md-yasir Date: Thu, 23 Oct 2025 22:37:00 +0530 Subject: [PATCH 2/5] Made CookieList constructor to private. --- src/main/java/org/json/CookieList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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() { } /** From 5dc1031d17d24ea3b49b3e37ce388acbd170dc2d Mon Sep 17 00:00:00 2001 From: md-yasir Date: Thu, 23 Oct 2025 22:38:01 +0530 Subject: [PATCH 3/5] Made JSONMl constructor to private and refactored ternary operations to independent statement in L243 --- src/main/java/org/json/JSONML.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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); + } } } From 2c6082a0a2828dc2083056b99696d6c4209e3869 Mon Sep 17 00:00:00 2001 From: md-yasir Date: Thu, 23 Oct 2025 22:50:12 +0530 Subject: [PATCH 4/5] Refactored stop conditions to be invariant by using while loop. --- src/main/java/org/json/Cookie.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/Cookie.java b/src/main/java/org/json/Cookie.java index 11cc97a21..630136e58 100644 --- a/src/main/java/org/json/Cookie.java +++ b/src/main/java/org/json/Cookie.java @@ -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(); } From ff897a3952061d23bbf1b519e9aeded4e8492d98 Mon Sep 17 00:00:00 2001 From: md-yasir Date: Thu, 23 Oct 2025 23:11:38 +0530 Subject: [PATCH 5/5] Restructured switch statements to if else for more readability. --- src/main/java/org/json/JSONTokener.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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); }