From 77e9bf17e4a2a5be635d92913e9b5af51233142f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Go=C5=82=C4=99biewski?= Date: Tue, 10 Nov 2026 02:46:53 +0100 Subject: [PATCH 1/2] fix(android): parse large integers, exponents and escaped strings in CapacitorHttp JSON responses --- .../plugin/util/HttpRequestHandler.java | 52 +++++++++++-- .../plugin/util/HttpRequestHandlerTest.java | 76 +++++++++++++++++++ 2 files changed, 123 insertions(+), 5 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 d92eb9bb4..fa5541b5f 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 @@ -27,6 +27,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONTokener; public class HttpRequestHandler { @@ -317,12 +318,11 @@ public static Object parseJSON(String input) throws JSONException { } 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); + return parseString(input.trim()); } else if (input.trim().matches("^-?\\d+$")) { - return Integer.parseInt(input.trim()); - } else if (input.trim().matches("^-?\\d+(\\.\\d+)?$")) { - return Double.parseDouble(input.trim()); + return parseInteger(input.trim()); + } else if (input.trim().matches("^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$")) { + return parseDouble(input.trim()); } else { try { return new JSObject(input); @@ -335,6 +335,48 @@ public static Object parseJSON(String input) throws JSONException { } } + /** + * Parses a JSON string value, resolving escape sequences such as \" or \n + * @param value A string enclosed in " " + * @return The unescaped string, or the content between the quotes when the value is not a single valid JSON string + */ + private static String parseString(String value) { + try { + JSONTokener tokener = new JSONTokener(value); + Object parsed = tokener.nextValue(); + if (parsed instanceof String && !tokener.more()) { + return (String) parsed; + } + } catch (JSONException ignored) {} + return value.substring(1, value.length() - 1); + } + + /** + * Parses an integer JSON value, falling back to a wider type when it does not fit, + * so values outside the int range are returned instead of failing the request + * @param value An integer without a fraction or exponent + * @return An Integer, a Long, a Double, or the input when it exceeds the Double range + */ + private static Object parseInteger(String value) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException ignored) {} + try { + return Long.parseLong(value); + } catch (NumberFormatException ignored) {} + return parseDouble(value); + } + + /** + * Parses a JSON number as a Double + * @param value A number, optionally with a fraction and an exponent + * @return A Double, or the input when it exceeds the Double range, as JSON cannot represent infinity + */ + private static Object parseDouble(String value) { + double number = Double.parseDouble(value); + return Double.isInfinite(number) ? value : number; + } + /** * Returns a string based on a base64 InputStream * @param in The base64 InputStream to convert to a String diff --git a/android/capacitor/src/test/java/com/getcapacitor/plugin/util/HttpRequestHandlerTest.java b/android/capacitor/src/test/java/com/getcapacitor/plugin/util/HttpRequestHandlerTest.java index 100672123..dccde7334 100644 --- a/android/capacitor/src/test/java/com/getcapacitor/plugin/util/HttpRequestHandlerTest.java +++ b/android/capacitor/src/test/java/com/getcapacitor/plugin/util/HttpRequestHandlerTest.java @@ -4,7 +4,11 @@ import com.getcapacitor.JSObject; import com.getcapacitor.plugin.util.HttpRequestHandler.HttpURLConnectionBuilder; +import com.getcapacitor.plugin.util.HttpRequestHandler.ResponseType; +import java.io.ByteArrayInputStream; +import java.io.InputStream; import java.net.URL; +import java.nio.charset.StandardCharsets; import org.junit.Test; public class HttpRequestHandlerTest { @@ -35,4 +39,76 @@ public void testHttpURLConnectionBuilderSetUrlParamsNotEncoded() throws Exceptio .url.toString(); assertEquals(expectedUrl, actualUrl); } + + @Test + public void testParseJSONKeepsIntegerWithinIntRange() throws Exception { + assertEquals(42, HttpRequestHandler.parseJSON("42")); + assertEquals(Integer.MAX_VALUE, HttpRequestHandler.parseJSON("2147483647")); + assertEquals(Integer.MIN_VALUE, HttpRequestHandler.parseJSON("-2147483648")); + } + + @Test + public void testParseJSONReturnsLongBeyondIntRange() throws Exception { + assertEquals(8000010570L, HttpRequestHandler.parseJSON("8000010570")); + assertEquals(-8000010570L, HttpRequestHandler.parseJSON("-8000010570")); + assertEquals(Long.MAX_VALUE, HttpRequestHandler.parseJSON(" 9223372036854775807\n")); + } + + @Test + public void testParseJSONReturnsDoubleBeyondLongRange() throws Exception { + assertEquals(1.2345678901234567e19, HttpRequestHandler.parseJSON("12345678901234567890")); + } + + @Test + public void testParseJSONParsesExponent() throws Exception { + assertEquals(100000.0, HttpRequestHandler.parseJSON("1e5")); + assertEquals(1500.0, HttpRequestHandler.parseJSON("1.5E+3")); + assertEquals(-0.0025, HttpRequestHandler.parseJSON("-2.5e-3")); + } + + @Test + public void testParseJSONReturnsInputBeyondDoubleRange() throws Exception { + assertEquals("1e400", HttpRequestHandler.parseJSON("1e400")); + } + + @Test + public void testParseJSONKeepsExistingNumberHandling() throws Exception { + assertEquals(1.5, HttpRequestHandler.parseJSON("1.5")); + assertEquals(123, HttpRequestHandler.parseJSON("0123")); + } + + @Test + public void testParseJSONUnescapesString() throws Exception { + assertEquals("plain", HttpRequestHandler.parseJSON("\"plain\"")); + assertEquals("a\"b", HttpRequestHandler.parseJSON("\"a\\\"b\"")); + assertEquals("line\nbreak", HttpRequestHandler.parseJSON("\"line\\nbreak\"")); + assertEquals("A/B", HttpRequestHandler.parseJSON("\"\\u0041\\/B\"")); + } + + @Test + public void testParseJSONKeepsContentBetweenQuotesWhenNotSingleString() throws Exception { + assertEquals("a\" \"b", HttpRequestHandler.parseJSON("\"a\" \"b\"")); + } + + @Test + public void testReadDataReturnsJsonNumberBeyondIntRange() throws Exception { + ICapacitorHttpUrlConnection connection = new ICapacitorHttpUrlConnection() { + @Override + public InputStream getErrorStream() { + return null; + } + + @Override + public String getHeaderField(String name) { + return "Content-Type".equals(name) ? "application/json" : null; + } + + @Override + public InputStream getInputStream() { + return new ByteArrayInputStream("8000010570".getBytes(StandardCharsets.UTF_8)); + } + }; + + assertEquals(8000010570L, HttpRequestHandler.readData(connection, ResponseType.DEFAULT)); + } } From 09bac72396f66f50e0467f734aa7fc764b376d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Go=C5=82=C4=99biewski?= Date: Tue, 10 Nov 2026 02:55:23 +0100 Subject: [PATCH 2/2] refactor(android): parse JSON numbers in a single method --- .../plugin/util/HttpRequestHandler.java | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 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 fa5541b5f..69156a539 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 @@ -319,10 +319,8 @@ public static Object parseJSON(String input) throws JSONException { return ""; } else if (input.trim().matches("^\".*\"$")) { return parseString(input.trim()); - } else if (input.trim().matches("^-?\\d+$")) { - return parseInteger(input.trim()); } else if (input.trim().matches("^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$")) { - return parseDouble(input.trim()); + return parseNumber(input.trim()); } else { try { return new JSObject(input); @@ -352,27 +350,23 @@ private static String parseString(String value) { } /** - * Parses an integer JSON value, falling back to a wider type when it does not fit, - * so values outside the int range are returned instead of failing the request - * @param value An integer without a fraction or exponent - * @return An Integer, a Long, a Double, or the input when it exceeds the Double range - */ - private static Object parseInteger(String value) { - try { - return Integer.parseInt(value); - } catch (NumberFormatException ignored) {} - try { - return Long.parseLong(value); - } catch (NumberFormatException ignored) {} - return parseDouble(value); - } - - /** - * Parses a JSON number as a Double + * Parses a JSON number into the Java type that holds it: Integer or Long for integers in their range, + * otherwise Double, like JavaScript does * @param value A number, optionally with a fraction and an exponent - * @return A Double, or the input when it exceeds the Double range, as JSON cannot represent infinity + * @return An Integer, a Long, a Double, or the input when it exceeds the Double range, as JSON cannot represent infinity */ - private static Object parseDouble(String value) { + private static Object parseNumber(String value) { + if (value.matches("^-?\\d+$")) { + try { + long number = Long.parseLong(value); + if (number >= Integer.MIN_VALUE && number <= Integer.MAX_VALUE) { + return (int) number; + } + return number; + } catch (NumberFormatException ignored) { + // beyond the Long range, parsed as a Double below + } + } double number = Double.parseDouble(value); return Double.isInfinite(number) ? value : number; }