From 03dd3f96c7ee75b6fff2b7c40d0c9a58fb04fce5 Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Tue, 22 Aug 2023 08:51:08 -0700 Subject: [PATCH] fix(http): return numbers and booleans as-is when application/json is the content type --- .../getcapacitor/plugin/util/HttpRequestHandler.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java index 5077e9b85..9d8e0a44a 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java @@ -302,14 +302,18 @@ public static Object parseJSON(String input) throws JSONException { if ("null".equals(input.trim())) { return JSONObject.NULL; } else if ("true".equals(input.trim())) { - return new JSONObject().put("flag", "true"); + return true; } else if ("false".equals(input.trim())) { - return new JSONObject().put("flag", "false"); + return false; } else if (input.trim().length() <= 0) { return ""; } else if (input.trim().matches("^\".*\"$")) { // a string enclosed in " " is a json value, return the string without the quotes return input.trim().substring(1, input.trim().length() - 1); + } else if (input.trim().matches("^-?\\d+$")) { + return Integer.parseInt(input.trim()); + } else if (input.trim().matches("^-?\\d+(\\.\\d+)?$")) { + return Double.parseDouble(input.trim()); } else { try { return new JSObject(input); @@ -318,7 +322,7 @@ public static Object parseJSON(String input) throws JSONException { } } } catch (JSONException e) { - return new JSArray(input); + return input; } }