Skip to content

Commit 99b568c

Browse files
author
Capacitor+ Bot
committed
chore: sync upstream PR ionic-team#8087 from @Crylion
2 parents 4c7c126 + 29c1cb8 commit 99b568c

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.Map;
2727
import org.json.JSONArray;
2828
import org.json.JSONException;
29-
import org.json.JSONObject;
29+
import org.json.JSONTokener;
3030

3131
public class HttpRequestHandler {
3232

@@ -302,36 +302,25 @@ public static JSObject buildResponseHeaders(CapacitorHttpUrlConnection connectio
302302
/**
303303
* Returns a JSObject or a JSArray based on a string-ified input
304304
* @param input String-ified JSON that needs parsing
305-
* @return A JSObject or JSArray
306-
* @throws JSONException thrown if the JSON is malformed
305+
* @return A JSObject, JSArray or literal
307306
*/
308-
public static Object parseJSON(String input) throws JSONException {
309-
JSONObject json = new JSONObject();
307+
public static Object parseJSON(String input) {
310308
try {
311-
if ("null".equals(input.trim())) {
312-
return JSONObject.NULL;
313-
} else if ("true".equals(input.trim())) {
314-
return true;
315-
} else if ("false".equals(input.trim())) {
316-
return false;
317-
} else if (input.trim().length() <= 0) {
318-
return "";
319-
} else if (input.trim().matches("^\".*\"$")) {
320-
// a string enclosed in " " is a json value, return the string without the quotes
321-
return input.trim().substring(1, input.trim().length() - 1);
322-
} else if (input.trim().matches("^-?\\d+$")) {
323-
return Integer.parseInt(input.trim());
324-
} else if (input.trim().matches("^-?\\d+(\\.\\d+)?$")) {
325-
return Double.parseDouble(input.trim());
326-
} else {
309+
// try to parse input as an object
310+
return new JSObject(input);
311+
} catch (JSONException e) {
312+
try {
313+
// fall through to try to parse it as an array
314+
return new JSArray(input);
315+
} catch (JSONException e2) {
327316
try {
328-
return new JSObject(input);
329-
} catch (JSONException e) {
330-
return new JSArray(input);
317+
// the value is probably a literal, so we can try to have the JSONTokener parse it for us
318+
return new JSONTokener(input).nextValue();
319+
} catch (JSONException e3) {
320+
// if nothing could be parsed, return the input as is
321+
return input;
331322
}
332323
}
333-
} catch (JSONException e) {
334-
return input;
335324
}
336325
}
337326

0 commit comments

Comments
 (0)