From 6532173c0fa6bb5b812cbd0e306904e7d7a7b051 Mon Sep 17 00:00:00 2001 From: devpatel43543 Date: Sat, 23 Nov 2024 14:33:48 -0400 Subject: [PATCH 1/3] refactor valueToString method in JSONWriter --- src/main/java/org/json/JSONWriter.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONWriter.java b/src/main/java/org/json/JSONWriter.java index 11f4a5c7e..f5cf967ad 100644 --- a/src/main/java/org/json/JSONWriter.java +++ b/src/main/java/org/json/JSONWriter.java @@ -328,8 +328,7 @@ public static String valueToString(Object value) throws JSONException { // Instead we will quote it as a string return JSONObject.quote(numberAsString); } - if (value instanceof Boolean || value instanceof JSONObject - || value instanceof JSONArray) { + if (isDirectlyConvertibleToJSON(value)) { return value.toString(); } if (value instanceof Map) { @@ -349,6 +348,10 @@ public static String valueToString(Object value) throws JSONException { return JSONObject.quote(value.toString()); } + private static boolean isDirectlyConvertibleToJSON(Object value) { + return value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray; + } + /** * Append either the value true or the value * false. From 89b8fe8e4815e736ae2c7bee7b2ff1f32065b87f Mon Sep 17 00:00:00 2001 From: devpatel43543 Date: Sat, 23 Nov 2024 17:52:49 -0400 Subject: [PATCH 2/3] refactored extracted method from toString() --- src/main/java/org/json/HTTP.java | 66 +++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/json/HTTP.java b/src/main/java/org/json/HTTP.java index 44ab3a6d3..85a435327 100644 --- a/src/main/java/org/json/HTTP.java +++ b/src/main/java/org/json/HTTP.java @@ -113,36 +113,66 @@ public static JSONObject toJSONObject(String string) throws JSONException { public static String toString(JSONObject jo) throws JSONException { StringBuilder sb = new StringBuilder(); 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")); + appendResponseHeader(sb, jo); } 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")); + appendRequestHeader(sb, jo); } else { throw new JSONException("Not enough material for an HTTP header."); } sb.append(CRLF); - // Don't use the new entrySet API to maintain Android support + + // Append other HTTP fields + appendAdditionalHeaders(sb, jo); + + sb.append(CRLF); + return sb.toString(); + } + + /** + * Appends the HTTP response header to the StringBuilder. + * @param sb The StringBuilder to append to. + * @param jo The JSONObject representing the HTTP response. + * @throws JSONException if required fields are missing. + */ + private static void appendResponseHeader(StringBuilder sb, JSONObject jo) throws JSONException { + sb.append(jo.getString("HTTP-Version")); + sb.append(' '); + sb.append(jo.getString("Status-Code")); + sb.append(' '); + sb.append(jo.getString("Reason-Phrase")); + } + + /** + * Appends the HTTP request header to the StringBuilder. + * @param sb The StringBuilder to append to. + * @param jo The JSONObject representing the HTTP request. + * @throws JSONException if required fields are missing. + */ + private static void appendRequestHeader(StringBuilder sb, JSONObject jo) throws JSONException { + 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")); + } + /** + * Appends additional HTTP headers to the StringBuilder. + * @param sb The StringBuilder to append to. + * @param jo The JSONObject containing the additional headers. + */ + private static void appendAdditionalHeaders(StringBuilder sb, JSONObject jo) { for (final String key : jo.keySet()) { String value = jo.optString(key); - if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) && + if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) && !"Reason-Phrase".equals(key) && !"Method".equals(key) && - !"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) { + !"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) { sb.append(key); sb.append(": "); - sb.append(jo.optString(key)); + sb.append(value); sb.append(CRLF); } } - sb.append(CRLF); - return sb.toString(); } } From f0cc0c6592a4bfec8c7a1cdec0b1c97fe94cef00 Mon Sep 17 00:00:00 2001 From: devpatel43543 Date: Sat, 23 Nov 2024 18:18:57 -0400 Subject: [PATCH 3/3] refactored rename variable in JSONObject for clarity --- src/main/java/org/json/JSONObject.java | 40 +++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 26a68c6dc..70b0976e8 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -201,7 +201,7 @@ public JSONObject(JSONTokener x) throws JSONException { /** * Construct a JSONObject from a JSONTokener with custom json parse configurations. * - * @param x + * @param jsonTokener * A JSONTokener object containing the source string. * @param jsonParserConfiguration * Variable to pass parser custom configuration for json parsing. @@ -209,30 +209,30 @@ public JSONObject(JSONTokener x) throws JSONException { * If there is a syntax error in the source string or a * duplicated key. */ - public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException { + public JSONObject(JSONTokener jsonTokener, JSONParserConfiguration jsonParserConfiguration) throws JSONException { this(); - char c; + char currentChar; String key; - if (x.nextClean() != '{') { - throw x.syntaxError("A JSONObject text must begin with '{'"); + if (jsonTokener.nextClean() != '{') { + throw jsonTokener.syntaxError("A JSONObject text must begin with '{'"); } for (;;) { - c = x.nextClean(); - switch (c) { + currentChar = jsonTokener.nextClean(); + switch (currentChar) { case 0: - throw x.syntaxError("A JSONObject text must end with '}'"); + throw jsonTokener.syntaxError("A JSONObject text must end with '}'"); case '}': return; default: - key = x.nextSimpleValue(c).toString(); + key = jsonTokener.nextSimpleValue(currentChar).toString(); } // The key is followed by ':'. - c = x.nextClean(); - if (c != ':') { - throw x.syntaxError("Expected a ':' after a key"); + currentChar = jsonTokener.nextClean(); + if (currentChar != ':') { + throw jsonTokener.syntaxError("Expected a ':' after a key"); } // Use syntaxError(..) to include error location @@ -241,10 +241,10 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration // Check if key exists boolean keyExists = this.opt(key) != null; if (keyExists && !jsonParserConfiguration.isOverwriteDuplicateKey()) { - throw x.syntaxError("Duplicate key \"" + key + "\""); + throw jsonTokener.syntaxError("Duplicate key \"" + key + "\""); } - Object value = x.nextValue(); + Object value = jsonTokener.nextValue(); // Only add value if non-null if (value != null) { this.put(key, value); @@ -253,21 +253,21 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration // Pairs are separated by ','. - switch (x.nextClean()) { + switch (jsonTokener.nextClean()) { case ';': case ',': - if (x.nextClean() == '}') { + if (jsonTokener.nextClean() == '}') { return; } - if (x.end()) { - throw x.syntaxError("A JSONObject text must end with '}'"); + if (jsonTokener.end()) { + throw jsonTokener.syntaxError("A JSONObject text must end with '}'"); } - x.back(); + jsonTokener.back(); break; case '}': return; default: - throw x.syntaxError("Expected a ',' or '}'"); + throw jsonTokener.syntaxError("Expected a ',' or '}'"); } } }