From 133c0cc75fa55fae24c9b528f40a31179fe40a41 Mon Sep 17 00:00:00 2001 From: Michael Osipov Date: Wed, 24 May 2023 11:45:25 +0200 Subject: [PATCH 001/233] JSONTokener(InputStream) violates rfc8259#section-8.1 (#739) Always use UTF-8 when an InputStream is passed. This fixes #739. --- src/main/java/org/json/JSONTokener.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index c18058013..5dc8ae85a 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -1,6 +1,7 @@ package org.json; import java.io.*; +import java.nio.charset.Charset; /* Public Domain. @@ -56,7 +57,7 @@ public JSONTokener(Reader reader) { * @param inputStream The source. */ public JSONTokener(InputStream inputStream) { - this(new InputStreamReader(inputStream)); + this(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); } @@ -120,7 +121,7 @@ public static int dehexchar(char c) { /** * Checks if the end of the input has been reached. - * + * * @return true if at the end of the file and we didn't step back */ public boolean end() { @@ -184,7 +185,7 @@ public char next() throws JSONException { this.previous = (char) c; return this.previous; } - + /** * Get the last character read from the input or '\0' if nothing has been read yet. * @return the last character read from the input. From 084b24cbe7030cc5057effa9e6d269749ddf0beb Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Jun 2023 12:16:14 -0500 Subject: [PATCH 002/233] Update README.md for 20230618 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ecc12b73..e999230ba 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ JSON in Java [package org.json] [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230227/json-20230227.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230618/json-20230618.jar)** # Overview From f6e5bfa2db9c91f7a183c1e96d41e1328ee1b638 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Jun 2023 12:17:56 -0500 Subject: [PATCH 003/233] Update RELEASES.md for 20230618 --- docs/RELEASES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 166c43a4a..ae439831c 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -5,6 +5,8 @@ and artifactId "json". For example: [https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav) ~~~ +20230618 Final release with Java 1.6 compatibility. Future releases will require Java 1.8 or greater. + 20230227 Fix for CVE-2022-45688 and recent commits 20220924 New License - public domain, and some minor updates From c048b36516a1e35a52498f3350c6d11c10fb6f20 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Jun 2023 12:18:36 -0500 Subject: [PATCH 004/233] Update pom.xml for 20230618 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f17e0abfe..ba3edbd5c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.json json - 20230227 + 20230618 bundle JSON in Java From a963115ac2527dee688f54f7c1150d6f25b887c1 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Jun 2023 12:58:32 -0500 Subject: [PATCH 005/233] Update pom.xml for maven deploy Deploy failed on the mac pro with: gpg: signing failed: Inappropriate ioctl for device Somehow I had a different gpg version installed. This change fixed it. --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index ba3edbd5c..b9e0e608d 100644 --- a/pom.xml +++ b/pom.xml @@ -139,6 +139,12 @@ sign + + + --pinentry-mode + loopback + + From 3d524349a11f8af5142b00f6fc67b5294682be66 Mon Sep 17 00:00:00 2001 From: dburbrid Date: Mon, 26 Jun 2023 09:33:03 +0100 Subject: [PATCH 006/233] Correction of bug when compiling/testing on Windows: Issue537 file must be read as UTF-8 (Issue 745) --- src/test/java/org/json/junit/XMLTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index aefaa49da..e940032e0 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -18,6 +18,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; +import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; @@ -915,7 +916,7 @@ public void testIssue537CaseSensitiveHexEscapeFullFile(){ InputStream xmlStream = null; try { xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue537.xml"); - Reader xmlReader = new InputStreamReader(xmlStream); + Reader xmlReader = new InputStreamReader(xmlStream, Charset.forName("UTF-8")); JSONObject actual = XML.toJSONObject(xmlReader, true); InputStream jsonStream = null; try { From 4951ec48c8d4879ca1d4f22a1a3d5c489976a52c Mon Sep 17 00:00:00 2001 From: dburbrid Date: Thu, 29 Jun 2023 09:39:34 +0100 Subject: [PATCH 007/233] Renamed object methods from ...Obj to ...Object. Added object method for optDoubleObject (returns Double vice double). Added similar methods in JSONArray. Added test methods. --- src/main/java/org/json/JSONArray.java | 168 +++++++++++++++++ src/main/java/org/json/JSONObject.java | 176 +++++++++++++++++- .../java/org/json/junit/JSONArrayTest.java | 40 ++++ .../org/json/junit/JSONObjectNumberTest.java | 20 ++ .../java/org/json/junit/JSONObjectTest.java | 65 +++++++ 5 files changed, 467 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 3be3e1451..375d03888 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -599,6 +599,38 @@ public boolean optBoolean(int index, boolean defaultValue) { } } + /** + * Get the optional Boolean object associated with an index. It returns false + * if there is no value at that index, or if the value is not Boolean.TRUE + * or the String "true". + * + * @param index + * The index must be between 0 and length() - 1. + * @return The truth. + */ + public Boolean optBooleanObject(int index) { + return this.optBooleanObject(index, false); + } + + /** + * Get the optional Boolean object associated with an index. It returns the + * defaultValue if there is no value at that index or if it is not a Boolean + * or the String "true" or "false" (case insensitive). + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * A boolean default. + * @return The truth. + */ + public Boolean optBooleanObject(int index, Boolean defaultValue) { + try { + return this.getBoolean(index); + } catch (Exception e) { + return defaultValue; + } + } + /** * Get the optional double value associated with an index. NaN is returned * if there is no value for the index, or if the value is not a number and @@ -635,6 +667,42 @@ public double optDouble(int index, double defaultValue) { return doubleValue; } + /** + * Get the optional Double object associated with an index. NaN is returned + * if there is no value for the index, or if the value is not a number and + * cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @return The object. + */ + public Double optDoubleObject(int index) { + return this.optDoubleObject(index, Double.NaN); + } + + /** + * Get the optional double value associated with an index. The defaultValue + * is returned if there is no value for the index, or if the value is not a + * number and cannot be converted to a number. + * + * @param index + * subscript + * @param defaultValue + * The default object. + * @return The object. + */ + public Double optDoubleObject(int index, Double defaultValue) { + final Number val = this.optNumber(index, null); + if (val == null) { + return defaultValue; + } + final Double doubleValue = val.doubleValue(); + // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { + // return defaultValue; + // } + return doubleValue; + } + /** * Get the optional float value associated with an index. NaN is returned * if there is no value for the index, or if the value is not a number and @@ -671,6 +739,42 @@ public float optFloat(int index, float defaultValue) { return floatValue; } + /** + * Get the optional Float object associated with an index. NaN is returned + * if there is no value for the index, or if the value is not a number and + * cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @return The object. + */ + public Float optFloatObject(int index) { + return this.optFloatObject(index, Float.NaN); + } + + /** + * Get the optional Float object associated with an index. The defaultValue + * is returned if there is no value for the index, or if the value is not a + * number and cannot be converted to a number. + * + * @param index + * subscript + * @param defaultValue + * The default object. + * @return The object. + */ + public Float optFloatObject(int index, Float defaultValue) { + final Number val = this.optNumber(index, null); + if (val == null) { + return defaultValue; + } + final Float floatValue = val.floatValue(); + // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { + // return floatValue; + // } + return floatValue; + } + /** * Get the optional int value associated with an index. Zero is returned if * there is no value for the index, or if the value is not a number and @@ -703,6 +807,38 @@ public int optInt(int index, int defaultValue) { return val.intValue(); } + /** + * Get the optional Integer object associated with an index. Zero is returned if + * there is no value for the index, or if the value is not a number and + * cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @return The object. + */ + public Integer optIntegerObject(int index) { + return this.optIntegerObject(index, 0); + } + + /** + * Get the optional Integer object associated with an index. The defaultValue is + * returned if there is no value for the index, or if the value is not a + * number and cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default object. + * @return The object. + */ + public Integer optIntegerObject(int index, Integer defaultValue) { + final Number val = this.optNumber(index, null); + if (val == null) { + return defaultValue; + } + return val.intValue(); + } + /** * Get the enum value associated with a key. * @@ -846,6 +982,38 @@ public long optLong(int index, long defaultValue) { return val.longValue(); } + /** + * Get the optional Long object associated with an index. Zero is returned if + * there is no value for the index, or if the value is not a number and + * cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @return The object. + */ + public Long optLongObject(int index) { + return this.optLongObject(index, 0L); + } + + /** + * Get the optional Long object associated with an index. The defaultValue is + * returned if there is no value for the index, or if the value is not a + * number and cannot be converted to a number. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default object. + * @return The object. + */ + public Long optLongObject(int index, Long defaultValue) { + final Number val = this.optNumber(index, null); + if (val == null) { + return defaultValue; + } + return val.longValue(); + } + /** * Get an optional {@link Number} value associated with a key, or null * if there is no such key or if the value is not a number. If the value is a string, diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 97906b629..08eb8fd82 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1131,6 +1131,45 @@ public boolean optBoolean(String key, boolean defaultValue) { } } + /** + * Get an optional boolean object associated with a key. It returns false if there + * is no such key, or if the value is not Boolean.TRUE or the String "true". + * + * @param key + * A key string. + * @return The truth. + */ + public Boolean optBooleanObject(String key) { + return this.optBooleanObject(key, false); + } + + /** + * Get an optional boolean object associated with a key. It returns the + * defaultValue if there is no such key, or if it is not a Boolean or the + * String "true" or "false" (case insensitive). + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return The truth. + */ + public Boolean optBooleanObject(String key, Boolean defaultValue) { + Object val = this.opt(key); + if (NULL.equals(val)) { + return defaultValue; + } + if (val instanceof Boolean){ + return ((Boolean) val).booleanValue(); + } + try { + // we'll use the get anyway because it does string conversion. + return this.getBoolean(key); + } catch (Exception e) { + return defaultValue; + } + } + /** * Get an optional BigDecimal associated with a key, or the defaultValue if * there is no such key or if its value is not a number. If the value is a @@ -1294,7 +1333,39 @@ public double optDouble(String key, double defaultValue) { } /** - * Get the optional double value associated with an index. NaN is returned + * Get an optional Double object associated with a key, or NaN if there is no such + * key or if its value is not a number. If the value is a string, an attempt + * will be made to evaluate it as a number. + * + * @param key + * A string which is the key. + * @return An object which is the value. + */ + public Double optDoubleObject(String key) { + return this.optDoubleObject(key, Double.NaN); + } + + /** + * Get an optional Double object associated with a key, or the defaultValue if + * there is no such key or if its value is not a number. If the value is a + * string, an attempt will be made to evaluate it as a number. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return An object which is the value. + */ + public Double optDoubleObject(String key, Double defaultValue) { + Number val = this.optNumber(key); + if (val == null) { + return defaultValue; + } + return val.doubleValue(); + } + + /** + * Get the optional float value associated with an index. NaN is returned * if there is no value for the index, or if the value is not a number and * cannot be converted to a number. * @@ -1307,7 +1378,7 @@ public float optFloat(String key) { } /** - * Get the optional double value associated with an index. The defaultValue + * Get the optional float value associated with an index. The defaultValue * is returned if there is no value for the index, or if the value is not a * number and cannot be converted to a number. * @@ -1329,6 +1400,42 @@ public float optFloat(String key, float defaultValue) { return floatValue; } + /** + * Get the optional Float object associated with an index. NaN is returned + * if there is no value for the index, or if the value is not a number and + * cannot be converted to a number. + * + * @param key + * A key string. + * @return The object. + */ + public Float optFloatObject(String key) { + return this.optFloatObject(key, Float.NaN); + } + + /** + * Get the optional Float object associated with an index. The defaultValue + * is returned if there is no value for the index, or if the value is not a + * number and cannot be converted to a number. + * + * @param key + * A key string. + * @param defaultValue + * The default object. + * @return The object. + */ + public Float optFloatObject(String key, Float defaultValue) { + Number val = this.optNumber(key); + if (val == null) { + return defaultValue; + } + final Float floatValue = val.floatValue(); + // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { + // return defaultValue; + // } + return floatValue; + } + /** * Get an optional int value associated with a key, or zero if there is no * such key or if the value is not a number. If the value is a string, an @@ -1361,6 +1468,38 @@ public int optInt(String key, int defaultValue) { return val.intValue(); } + /** + * Get an optional Integer object associated with a key, or zero if there is no + * such key or if the value is not a number. If the value is a string, an + * attempt will be made to evaluate it as a number. + * + * @param key + * A key string. + * @return An object which is the value. + */ + public Integer optIntegerObject(String key) { + return this.optIntegerObject(key, 0); + } + + /** + * Get an optional Integer object associated with a key, or the default if there + * is no such key or if the value is not a number. If the value is a string, + * an attempt will be made to evaluate it as a number. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return An object which is the value. + */ + public Integer optIntegerObject(String key, Integer defaultValue) { + final Number val = this.optNumber(key, null); + if (val == null) { + return defaultValue; + } + return val.intValue(); + } + /** * Get an optional JSONArray associated with a key. It returns null if there * is no such key, or if its value is not a JSONArray. @@ -1432,6 +1571,39 @@ public long optLong(String key, long defaultValue) { return val.longValue(); } + /** + * Get an optional Long object associated with a key, or zero if there is no + * such key or if the value is not a number. If the value is a string, an + * attempt will be made to evaluate it as a number. + * + * @param key + * A key string. + * @return An object which is the value. + */ + public Long optLongObject(String key) { + return this.optLongObject(key, 0L); + } + + /** + * Get an optional Long object associated with a key, or the default if there + * is no such key or if the value is not a number. If the value is a string, + * an attempt will be made to evaluate it as a number. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return An object which is the value. + */ + public Long optLongObject(String key, Long defaultValue) { + final Number val = this.optNumber(key, null); + if (val == null) { + return defaultValue; + } + + return val.longValue(); + } + /** * Get an optional {@link Number} value associated with a key, or null * if there is no such key or if the value is not a number. If the value is a string, diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index aa8657f06..aea4e30e8 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -537,6 +537,13 @@ public void opt() { assertTrue("Array opt boolean implicit default", Boolean.FALSE == jsonArray.optBoolean(-1)); + assertTrue("Array opt boolean object", + Boolean.TRUE.equals(jsonArray.optBooleanObject(0))); + assertTrue("Array opt boolean object default", + Boolean.FALSE.equals(jsonArray.optBooleanObject(-1, Boolean.FALSE))); + assertTrue("Array opt boolean object implicit default", + Boolean.FALSE.equals(jsonArray.optBooleanObject(-1))); + assertTrue("Array opt double", new Double(23.45e-4).equals(jsonArray.optDouble(5))); assertTrue("Array opt double default", @@ -544,6 +551,13 @@ public void opt() { assertTrue("Array opt double default implicit", new Double(jsonArray.optDouble(99)).isNaN()); + assertTrue("Array opt double object", + Double.valueOf(23.45e-4).equals(jsonArray.optDoubleObject(5))); + assertTrue("Array opt double object default", + Double.valueOf(1).equals(jsonArray.optDoubleObject(0, 1D))); + assertTrue("Array opt double object default implicit", + jsonArray.optDoubleObject(99).isNaN()); + assertTrue("Array opt float", new Float(23.45e-4).equals(jsonArray.optFloat(5))); assertTrue("Array opt float default", @@ -551,6 +565,13 @@ public void opt() { assertTrue("Array opt float default implicit", new Float(jsonArray.optFloat(99)).isNaN()); + assertTrue("Array opt float object", + Float.valueOf(23.45e-4F).equals(jsonArray.optFloatObject(5))); + assertTrue("Array opt float object default", + Float.valueOf(1).equals(jsonArray.optFloatObject(0, 1F))); + assertTrue("Array opt float object default implicit", + jsonArray.optFloatObject(99).isNaN()); + assertTrue("Array opt Number", BigDecimal.valueOf(23.45e-4).equals(jsonArray.optNumber(5))); assertTrue("Array opt Number default", @@ -565,6 +586,13 @@ public void opt() { assertTrue("Array opt int default implicit", 0 == jsonArray.optInt(0)); + assertTrue("Array opt int object", + Integer.valueOf(42).equals(jsonArray.optIntegerObject(7))); + assertTrue("Array opt int object default", + Integer.valueOf(-1).equals(jsonArray.optIntegerObject(0, -1))); + assertTrue("Array opt int object default implicit", + Integer.valueOf(0).equals(jsonArray.optIntegerObject(0))); + JSONArray nestedJsonArray = jsonArray.optJSONArray(9); assertTrue("Array opt JSONArray", nestedJsonArray != null); assertTrue("Array opt JSONArray default", @@ -582,6 +610,13 @@ public void opt() { assertTrue("Array opt long default implicit", 0 == jsonArray.optLong(-1)); + assertTrue("Array opt long object", + Long.valueOf(0).equals(jsonArray.optLongObject(11))); + assertTrue("Array opt long object default", + Long.valueOf(-2).equals(jsonArray.optLongObject(-1, -2L))); + assertTrue("Array opt long object default implicit", + Long.valueOf(0).equals(jsonArray.optLongObject(-1))); + assertTrue("Array opt string", "hello".equals(jsonArray.optString(4))); assertTrue("Array opt string default implicit", @@ -599,10 +634,15 @@ public void opt() { public void optStringConversion(){ JSONArray ja = new JSONArray("[\"123\",\"true\",\"false\"]"); assertTrue("unexpected optBoolean value",ja.optBoolean(1,false)==true); + assertTrue("unexpected optBooleanObject value",Boolean.valueOf(true).equals(ja.optBooleanObject(1,false))); assertTrue("unexpected optBoolean value",ja.optBoolean(2,true)==false); + assertTrue("unexpected optBooleanObject value",Boolean.valueOf(false).equals(ja.optBooleanObject(2,true))); assertTrue("unexpected optInt value",ja.optInt(0,0)==123); + assertTrue("unexpected optIntegerObject value",Integer.valueOf(123).equals(ja.optIntegerObject(0,0))); assertTrue("unexpected optLong value",ja.optLong(0,0)==123); + assertTrue("unexpected optLongObject value",Long.valueOf(123).equals(ja.optLongObject(0,0L))); assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0); + assertTrue("unexpected optDoubleObject value",Double.valueOf(123.0).equals(ja.optDoubleObject(0,0.0))); assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0); assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); Util.checkJSONArrayMaps(ja); diff --git a/src/test/java/org/json/junit/JSONObjectNumberTest.java b/src/test/java/org/json/junit/JSONObjectNumberTest.java index f6e13c63d..43173a288 100644 --- a/src/test/java/org/json/junit/JSONObjectNumberTest.java +++ b/src/test/java/org/json/junit/JSONObjectNumberTest.java @@ -109,18 +109,38 @@ public void testOptFloat() { assertEquals(value.floatValue(), object.optFloat("value"), 0.0f); } + @Test + public void testOptFloatObject() { + assertEquals((Float) value.floatValue(), object.optFloatObject("value"), 0.0f); + } + @Test public void testOptDouble() { assertEquals(value.doubleValue(), object.optDouble("value"), 0.0d); } + @Test + public void testOptDoubleObject() { + assertEquals((Double) value.doubleValue(), object.optDoubleObject("value"), 0.0d); + } + @Test public void testOptInt() { assertEquals(value.intValue(), object.optInt("value")); } + @Test + public void testOptIntegerObject() { + assertEquals((Integer) value.intValue(), object.optIntegerObject("value")); + } + @Test public void testOptLong() { assertEquals(value.longValue(), object.optLong("value")); } + + @Test + public void testOptLongObject() { + assertEquals((Long) value.longValue(), object.optLongObject("value")); + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index ea0cec39c..ade552329 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -231,6 +231,11 @@ public void testLongFromString(){ assert 26315000000253009L == actualLong : "Incorrect key value. Got " + actualLong + " expected " + str; + final Long actualLongObject = json.optLongObject("key"); + assert actualLongObject != 0L : "Unable to extract Long value for string " + str; + assert Long.valueOf(26315000000253009L).equals(actualLongObject) : "Incorrect key value. Got " + + actualLongObject + " expected " + str; + final String actualString = json.optString("key"); assert str.equals(actualString) : "Incorrect key value. Got " + actualString + " expected " + str; @@ -866,9 +871,11 @@ public void jsonObjectValues() { JSONObject jsonObject = new JSONObject(str); assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey")); assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey")); + assertTrue("opt trueKey should be true", jsonObject.optBooleanObject("trueKey")); assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey")); assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey")); assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey")); + assertTrue("trueStrKey should be true", jsonObject.optBooleanObject("trueStrKey")); assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey")); assertTrue("stringKey should be string", jsonObject.getString("stringKey").equals("hello world!")); @@ -884,6 +891,10 @@ public void jsonObjectValues() { jsonObject.optDouble("doubleKey") == -23.45e7); assertTrue("opt doubleKey with Default should be double", jsonObject.optDouble("doubleStrKey", Double.NaN) == 1); + assertTrue("opt doubleKey should be Double", + Double.valueOf(-23.45e7).equals(jsonObject.optDoubleObject("doubleKey"))); + assertTrue("opt doubleKey with Default should be Double", + Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", Double.NaN))); assertTrue("opt negZeroKey should be a Double", jsonObject.opt("negZeroKey") instanceof Double); assertTrue("get negZeroKey should be a Double", @@ -896,6 +907,10 @@ public void jsonObjectValues() { Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0); assertTrue("opt negZeroStrKey with Default should be double", Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0); + assertTrue("opt negZeroKey should be Double", + Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroKey"))); + assertTrue("opt negZeroStrKey with Default should be Double", + Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroStrKey"))); assertTrue("optNumber negZeroKey should be -0.0", Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0); assertTrue("optNumber negZeroStrKey should be -0.0", @@ -904,10 +919,18 @@ public void jsonObjectValues() { jsonObject.optFloat("doubleKey") == -23.45e7f); assertTrue("optFloat doubleKey with Default should be float", jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f); + assertTrue("optFloat doubleKey should be Float", + Float.valueOf(-23.45e7f).equals(jsonObject.optFloatObject("doubleKey"))); + assertTrue("optFloat doubleKey with Default should be Float", + Float.valueOf(1f).equals(jsonObject.optFloatObject("doubleStrKey", Float.NaN))); assertTrue("intKey should be int", jsonObject.optInt("intKey") == 42); assertTrue("opt intKey should be int", jsonObject.optInt("intKey", 0) == 42); + assertTrue("intKey should be Integer", + Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey"))); + assertTrue("opt intKey should be Integer", + Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey", 0))); assertTrue("opt intKey with default should be int", jsonObject.getInt("intKey") == 42); assertTrue("intStrKey should be int", @@ -918,6 +941,10 @@ public void jsonObjectValues() { jsonObject.optLong("longKey") == 1234567890123456789L); assertTrue("opt longKey with default should be long", jsonObject.optLong("longKey", 0) == 1234567890123456789L); + assertTrue("opt longKey should be Long", + Long.valueOf(1234567890123456789L).equals(jsonObject.optLongObject("longKey"))); + assertTrue("opt longKey with default should be Long", + Long.valueOf(1234567890123456789L).equals(jsonObject.optLongObject("longKey", 0L))); assertTrue("longStrKey should be long", jsonObject.getLong("longStrKey") == 987654321098765432L); assertTrue("optNumber int should return Integer", @@ -2465,8 +2492,12 @@ public void jsonObjectOptDefault() { BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", jsonObject.optBoolean("myKey", true)); + assertTrue("optBooleanObject() should return default Boolean", + jsonObject.optBooleanObject("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optIntegerObject() should return default Integer", + Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42))); assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", @@ -2475,10 +2506,16 @@ public void jsonObjectOptDefault() { jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue")); assertTrue("optLong() should return default long", 42l == jsonObject.optLong("myKey", 42l)); + assertTrue("optLongObject() should return default Long", + Long.valueOf(42l).equals(jsonObject.optLongObject("myKey", 42l))); assertTrue("optDouble() should return default double", 42.3d == jsonObject.optDouble("myKey", 42.3d)); + assertTrue("optDoubleObject() should return default Double", + Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d))); assertTrue("optFloat() should return default float", 42.3f == jsonObject.optFloat("myKey", 42.3f)); + assertTrue("optFloatObject() should return default Float", + Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f))); assertTrue("optNumber() should return default Number", 42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue()); assertTrue("optString() should return default string", @@ -2502,8 +2539,12 @@ public void jsonObjectOptNoKey() { BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0); assertTrue("optBoolean() should return default boolean", jsonObject.optBoolean("myKey", true)); + assertTrue("optBooleanObject() should return default Boolean", + jsonObject.optBooleanObject("myKey", true)); assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42)); + assertTrue("optIntegerObject() should return default Integer", + Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42))); assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", @@ -2512,10 +2553,16 @@ public void jsonObjectOptNoKey() { jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue")); assertTrue("optLong() should return default long", 42l == jsonObject.optLong("myKey", 42l)); + assertTrue("optLongObject() should return default Long", + Long.valueOf(42l).equals(jsonObject.optLongObject("myKey", 42l))); assertTrue("optDouble() should return default double", 42.3d == jsonObject.optDouble("myKey", 42.3d)); + assertTrue("optDoubleObject() should return default Double", + Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d))); assertTrue("optFloat() should return default float", 42.3f == jsonObject.optFloat("myKey", 42.3f)); + assertTrue("optFloatObject() should return default Float", + Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f))); assertTrue("optNumber() should return default Number", 42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue()); assertTrue("optString() should return default string", @@ -2530,11 +2577,17 @@ public void jsonObjectOptNoKey() { public void jsonObjectOptStringConversion() { JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}"); assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true); + assertTrue("unexpected optBooleanObject value",Boolean.valueOf(true).equals(jo.optBooleanObject("true",false))); assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false); + assertTrue("unexpected optBooleanObject value",Boolean.valueOf(false).equals(jo.optBooleanObject("false",true))); assertTrue("unexpected optInt value",jo.optInt("int",0)==123); + assertTrue("unexpected optIntegerObject value",Integer.valueOf(123).equals(jo.optIntegerObject("int",0))); assertTrue("unexpected optLong value",jo.optLong("int",0)==123l); + assertTrue("unexpected optLongObject value",Long.valueOf(123l).equals(jo.optLongObject("int",0L))); assertTrue("unexpected optDouble value",jo.optDouble("int",0.0d)==123.0d); + assertTrue("unexpected optDoubleObject value",Double.valueOf(123.0d).equals(jo.optDoubleObject("int",0.0d))); assertTrue("unexpected optFloat value",jo.optFloat("int",0.0f)==123.0f); + assertTrue("unexpected optFloatObject value",Float.valueOf(123.0f).equals(jo.optFloatObject("int",0.0f))); assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0); assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); @@ -2555,23 +2608,35 @@ public void jsonObjectOptCoercion() { assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumber",null)); assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumber",null)); assertEquals(1.9007199254740992E16, jo.optDouble("largeNumber"),0.0); + assertEquals(1.9007199254740992E16, jo.optDoubleObject("largeNumber"),0.0); assertEquals(1.90071995E16f, jo.optFloat("largeNumber"),0.0f); + assertEquals(1.90071995E16f, jo.optFloatObject("largeNumber"),0.0f); assertEquals(19007199254740993l, jo.optLong("largeNumber")); + assertEquals(Long.valueOf(19007199254740993l), jo.optLongObject("largeNumber")); assertEquals(1874919425, jo.optInt("largeNumber")); + assertEquals(Integer.valueOf(1874919425), jo.optIntegerObject("largeNumber")); // conversion from a string assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumberStr",null)); assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumberStr",null)); assertEquals(1.9007199254740992E16, jo.optDouble("largeNumberStr"),0.0); + assertEquals(1.9007199254740992E16, jo.optDoubleObject("largeNumberStr"),0.0); assertEquals(1.90071995E16f, jo.optFloat("largeNumberStr"),0.0f); + assertEquals(1.90071995E16f, jo.optFloatObject("largeNumberStr"),0.0f); assertEquals(19007199254740993l, jo.optLong("largeNumberStr")); + assertEquals(Long.valueOf(19007199254740993l), jo.optLongObject("largeNumberStr")); assertEquals(1874919425, jo.optInt("largeNumberStr")); + assertEquals(Integer.valueOf(1874919425), jo.optIntegerObject("largeNumberStr")); // the integer portion of the actual value is larger than a double can hold. assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber")); + assertNotEquals(Long.valueOf((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optLongObject("largeNumber")); assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumber")); + assertNotEquals(Integer.valueOf((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optIntegerObject("largeNumber")); assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumberStr")); + assertNotEquals(Long.valueOf((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optLongObject("largeNumberStr")); assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumberStr")); + assertNotEquals(Integer.valueOf((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optIntegerObject("largeNumberStr")); assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")); Util.checkJSONObjectMaps(jo); From c8a9e15a57886dbf3e51cd450bde8e0c4599bff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Tue, 1 Aug 2023 13:11:25 -0700 Subject: [PATCH 008/233] Don't skip past `\0` when parsing JSON objects. A better solution might be to use -1 instead 0 to represent EOF everywhere, which of course means changing `char` variables to `int`. The solution here is enough to solve the immediate problem, though. Fixes #758. --- src/main/java/org/json/JSONObject.java | 6 +++++- src/test/java/org/json/junit/JSONObjectTest.java | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 08eb8fd82..36f02d6c2 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -253,7 +253,11 @@ public JSONObject(JSONTokener x) throws JSONException { switch (x.nextClean()) { case ';': case ',': - if (x.nextClean() == '}') { + c = x.nextClean(); + if (c == 0) { + throw x.syntaxError("A JSONObject text must end with '}'"); + } + if (c == '}') { return; } x.back(); diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index ade552329..76c46ef19 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2225,6 +2225,15 @@ public void jsonObjectParsingErrors() { "Expected a ',' or '}' at 15 [character 16 line 1]", e.getMessage()); } + try { + // \0 after , + String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "A JSONObject text must end with '}' at 15 [character 16 line 1]", + e.getMessage()); + } try { // append to wrong key String str = "{\"myKey\":true, \"myOtherKey\":false}"; From b6ff0db984a42550dabbef6d7fc9de2be4b56e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Tue, 1 Aug 2023 13:49:59 -0700 Subject: [PATCH 009/233] Fix indentation in test. --- src/test/java/org/json/junit/JSONObjectTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 76c46ef19..e869a8d5c 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2230,9 +2230,9 @@ public void jsonObjectParsingErrors() { String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { - assertEquals("Expecting an exception message", - "A JSONObject text must end with '}' at 15 [character 16 line 1]", - e.getMessage()); + assertEquals("Expecting an exception message", + "A JSONObject text must end with '}' at 15 [character 16 line 1]", + e.getMessage()); } try { // append to wrong key From 2a4bc3420acc10e30d99841279164d195d2a525e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Tue, 1 Aug 2023 14:38:45 -0700 Subject: [PATCH 010/233] Apply simplification suggested by @johnjaylward. --- src/main/java/org/json/JSONObject.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 36f02d6c2..5e00eb9a3 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -253,13 +253,12 @@ public JSONObject(JSONTokener x) throws JSONException { switch (x.nextClean()) { case ';': case ',': - c = x.nextClean(); - if (c == 0) { - throw x.syntaxError("A JSONObject text must end with '}'"); - } - if (c == '}') { + if (x.nextClean() == '}') { return; } + if (x.end()) { + throw x.syntaxError("A JSONObject text must end with '}'"); + } x.back(); break; case '}': From b2943eb395b41ea67cfee303e24b47dce6e24f1b Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Wed, 16 Aug 2023 11:24:57 -0400 Subject: [PATCH 011/233] Add module-info to maven build --- pom.xml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pom.xml b/pom.xml index b9e0e608d..fe1525f44 100644 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,31 @@ false + + org.moditect + moditect-maven-plugin + 1.0.0.Final + + + add-module-infos + package + + add-module-info + + + 9 + + + org.json + + org.json; + + + + + + + org.apache.maven.plugins maven-jar-plugin From 50dfcc59b3f9a376761e41dc3c6b2ad06d90c8ea Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Wed, 16 Aug 2023 11:25:15 -0400 Subject: [PATCH 012/233] Remove automatic module name --- pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pom.xml b/pom.xml index fe1525f44..3502b5b07 100644 --- a/pom.xml +++ b/pom.xml @@ -188,13 +188,6 @@ org.apache.maven.plugins maven-jar-plugin 3.2.0 - - - - org.json - - - From e563dbcaaae945a163349ad7c14ed8b7f58c3d2c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 29 May 2023 10:34:40 +0300 Subject: [PATCH 013/233] Setup java 8 as minimum version --- .github/workflows/pipeline.yml | 24 +++++++++++-------- pom.xml | 20 ++++++++-------- .../java/org/json/junit/JSONObjectTest.java | 6 ++--- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 08352a085..f55506da4 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -11,19 +11,21 @@ on: jobs: # old-school build and jar method. No tests run or compiled. - build-1_6: + build-11: runs-on: ubuntu-latest strategy: matrix: - # build for java 1.6, however don't run any tests - java: [ 1.6 ] + # build for java 11, however don't run any tests + java: [ 11, 17, 19, 20 ] name: Java ${{ matrix.java }} steps: - - uses: actions/checkout@v2 - - name: Setup java - uses: actions/setup-java@v1 + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v3 with: + distribution: 'temurin' java-version: ${{ matrix.java }} + cache: 'maven' - name: Compile Java ${{ matrix.java }} run: | mkdir -p target/classes @@ -42,14 +44,16 @@ jobs: strategy: matrix: # build against supported Java LTS versions: - java: [ 8, 11 ] + java: [ 11, 17 ] name: Java ${{ matrix.java }} steps: - - uses: actions/checkout@v2 - - name: Setup java - uses: actions/setup-java@v1 + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v3 with: + distribution: 'temurin' java-version: ${{ matrix.java }} + cache: 'maven' - name: Compile Java ${{ matrix.java }} run: mvn clean compile -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true - name: Run Tests ${{ matrix.java }} diff --git a/pom.xml b/pom.xml index b9e0e608d..d6ed899c9 100644 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,7 @@ org.mockito mockito-core - 1.9.5 + 4.2.0 test @@ -79,7 +79,7 @@ org.apache.felix maven-bundle-plugin - 3.0.1 + 5.1.9 true @@ -93,16 +93,16 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.2 + 3.11.0 - 1.6 - 1.6 + 1.8 + 1.8 org.apache.maven.plugins maven-source-plugin - 2.1.2 + 3.3.0 attach-sources @@ -115,7 +115,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.7 + 3.5.0 attach-javadocs @@ -131,7 +131,7 @@ org.apache.maven.plugins maven-gpg-plugin - 1.5 + 1.6 sign-artifacts @@ -162,7 +162,7 @@ org.apache.maven.plugins maven-jar-plugin - 3.2.0 + 3.3.0 @@ -173,4 +173,4 @@ - + \ No newline at end of file diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e869a8d5c..3250c258a 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -626,9 +626,9 @@ public void jsonObjectByBean1() { assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey"))); assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey"))); // sorry, mockito artifact - assertTrue("expected 2 callbacks items", ((List)(JsonPath.read(doc, "$.callbacks"))).size() == 2); - assertTrue("expected 0 handler items", ((Map)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0); - assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); + assertTrue("expected 2 mockitoInterceptor items", ((Map)(JsonPath.read(doc, "$.mockitoInterceptor"))).size() == 2); + assertTrue("expected 0 mockitoInterceptor.serializationSupport items", + ((Map)(JsonPath.read(doc, "$.mockitoInterceptor.serializationSupport"))).size() == 0); Util.checkJSONObjectMaps(jsonObject); } From bae0b0dac924ac446952fc7395158cc804fbd958 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 29 May 2023 11:01:16 +0300 Subject: [PATCH 014/233] Updated mockito --- src/test/java/org/json/junit/JSONObjectTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 3250c258a..a9935a608 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -625,10 +625,13 @@ public void jsonObjectByBean1() { assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey"))); assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey"))); +<<<<<<< HEAD // sorry, mockito artifact assertTrue("expected 2 mockitoInterceptor items", ((Map)(JsonPath.read(doc, "$.mockitoInterceptor"))).size() == 2); assertTrue("expected 0 mockitoInterceptor.serializationSupport items", ((Map)(JsonPath.read(doc, "$.mockitoInterceptor.serializationSupport"))).size() == 0); +======= +>>>>>>> 88968f3 (Updated mockito) Util.checkJSONObjectMaps(jsonObject); } From 3dd8f2ecd5ac7700c6403b2fe57ae281b9c057b3 Mon Sep 17 00:00:00 2001 From: dburbrid Date: Mon, 26 Jun 2023 09:33:03 +0100 Subject: [PATCH 015/233] Correction of bug when compiling/testing on Windows: Issue537 file must be read as UTF-8 (Issue 745) --- src/test/java/org/json/junit/JSONObjectTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index a9935a608..3250c258a 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -625,13 +625,10 @@ public void jsonObjectByBean1() { assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey"))); assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey"))); -<<<<<<< HEAD // sorry, mockito artifact assertTrue("expected 2 mockitoInterceptor items", ((Map)(JsonPath.read(doc, "$.mockitoInterceptor"))).size() == 2); assertTrue("expected 0 mockitoInterceptor.serializationSupport items", ((Map)(JsonPath.read(doc, "$.mockitoInterceptor.serializationSupport"))).size() == 0); -======= ->>>>>>> 88968f3 (Updated mockito) Util.checkJSONObjectMaps(jsonObject); } From a4e152f4f0bc4daa52d5ae4e32c679bc8eaecd80 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 27 Aug 2023 15:42:27 +0300 Subject: [PATCH 016/233] Update pipeline.yml --- .github/workflows/pipeline.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index f55506da4..a8e7ad606 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -11,12 +11,12 @@ on: jobs: # old-school build and jar method. No tests run or compiled. - build-11: + build-8: runs-on: ubuntu-latest strategy: matrix: # build for java 11, however don't run any tests - java: [ 11, 17, 19, 20 ] + java: [ 8, 11, 17, 19, 20 ] name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v3 @@ -44,7 +44,7 @@ jobs: strategy: matrix: # build against supported Java LTS versions: - java: [ 11, 17 ] + java: [ 8, 11, 17 ] name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v3 From 48089a4da75bbca566ce1c33a9b6b218d11cd609 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 28 Aug 2023 02:21:18 +0300 Subject: [PATCH 017/233] Update pipeline.yml --- .github/workflows/pipeline.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index a8e7ad606..5f7474ae9 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - # build for java 11, however don't run any tests + # build for java 8, however don't run any tests java: [ 8, 11, 17, 19, 20 ] name: Java ${{ matrix.java }} steps: From be33deb7d5d1276e5d428125df1b4cd7f78e04c6 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 28 Aug 2023 02:36:02 +0300 Subject: [PATCH 018/233] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e999230ba..0ecc12b73 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ JSON in Java [package org.json] [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230618/json-20230618.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230227/json-20230227.jar)** # Overview From 2c674be1b64971327451b5c1e9c06fcb83b3b5b6 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 28 Aug 2023 18:13:22 +0300 Subject: [PATCH 019/233] Update pipeline.yml --- .github/workflows/pipeline.yml | 29 ----------------------------- README.md | 2 +- pom.xml | 2 +- 3 files changed, 2 insertions(+), 31 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 5f7474ae9..5e1dd4251 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -10,35 +10,6 @@ on: branches: [ master ] jobs: - # old-school build and jar method. No tests run or compiled. - build-8: - runs-on: ubuntu-latest - strategy: - matrix: - # build for java 8, however don't run any tests - java: [ 8, 11, 17, 19, 20 ] - name: Java ${{ matrix.java }} - steps: - - uses: actions/checkout@v3 - - name: Set up JDK ${{ matrix.java }} - uses: actions/setup-java@v3 - with: - distribution: 'temurin' - java-version: ${{ matrix.java }} - cache: 'maven' - - name: Compile Java ${{ matrix.java }} - run: | - mkdir -p target/classes - javac -d target/classes/ src/main/java/org/json/*.java - - name: Create java ${{ matrix.java }} JAR - run: | - jar cvf target/org.json.jar -C target/classes . - - name: Upload Java ${{ matrix.java }} JAR - uses: actions/upload-artifact@v1 - with: - name: Java ${{ matrix.java }} JAR - path: target/org.json.jar - build: runs-on: ubuntu-latest strategy: diff --git a/README.md b/README.md index 0ecc12b73..e999230ba 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ JSON in Java [package org.json] [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230227/json-20230227.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230618/json-20230618.jar)** # Overview diff --git a/pom.xml b/pom.xml index d6ed899c9..e17f145ca 100644 --- a/pom.xml +++ b/pom.xml @@ -173,4 +173,4 @@ - \ No newline at end of file + From 9b69ec49adc404043540628dd8aa25d3862a0c58 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 28 Aug 2023 12:43:17 -0400 Subject: [PATCH 020/233] update CodeQL action version --- .github/workflows/codeql-analysis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 4afee8443..df4bf7981 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -25,11 +25,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -40,4 +40,4 @@ jobs: - run: "mvn clean compile -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true" - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 From af6d07cecb95af5cd5ed09b24c48734e2b26ce28 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 29 Aug 2023 03:22:20 +0300 Subject: [PATCH 021/233] Resolved Gradle build dependency --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 63a31a73e..91503d09d 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ repositories { dependencies { testImplementation 'junit:junit:4.13.1' testImplementation 'com.jayway.jsonpath:json-path:2.1.0' - testImplementation 'org.mockito:mockito-core:1.9.5' + testImplementation 'org.mockito:mockito-core:4.2.0' } subprojects { From e27da22e05b795ada5dba698c2781f1d8027ba3b Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 29 Aug 2023 05:00:13 +0300 Subject: [PATCH 022/233] Update build.gradle --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 91503d09d..8a3708a74 100644 --- a/build.gradle +++ b/build.gradle @@ -30,9 +30,9 @@ subprojects { } group = 'org.json' -version = 'v20211205-SNAPSHOT' +version = 'v20230618-SNAPSHOT' description = 'JSON in Java' -sourceCompatibility = '1.7' +sourceCompatibility = '1.8' configurations.all { } From 7c1b6531e7ed3044f3edd5565416eb25cd9057ce Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 3 Sep 2023 11:35:15 -0500 Subject: [PATCH 023/233] Update CONTRIBUTING.md Updated for Hacktoberfest 2023 --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d81ff6147..8102dcf63 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,8 +1,8 @@ # Contribution Guidelines -Feel free to work on any issue with a #hacktoberfest label. +Feel free to work on any open issue, you don't need to ask permission first. This year, the hacktoberfest label will be added to any PR and associated issue during the month of October. -If you discover an issue you would like to work on, you can add a new issue to the list. If it meets our criteria, a hacktoberfest label will be added. +If you discover an issue you would like to work on, you can add a new issue to the list. If it meets our criteria, it will be available to work on (if not, it will be closed after review). # Who is allowed to submit pull requests for this project? From 74cd73f97c469e1996112389685256fec29bcaba Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 8 Sep 2023 07:34:00 +0300 Subject: [PATCH 024/233] Addressed compile warnings --- .../java/org/json/junit/JSONArrayTest.java | 44 +++++++------- .../java/org/json/junit/JSONObjectTest.java | 57 +++++++++---------- 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index aea4e30e8..ad938cf50 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -368,16 +368,16 @@ public void getArrayValues() { "hello".equals(jsonArray.getString(4))); // doubles assertTrue("Array double", - new Double(23.45e-4).equals(jsonArray.getDouble(5))); + Double.valueOf(23.45e-4).equals(jsonArray.getDouble(5))); assertTrue("Array string double", - new Double(23.45).equals(jsonArray.getDouble(6))); + Double.valueOf(23.45).equals(jsonArray.getDouble(6))); assertTrue("Array double can be float", - new Float(23.45e-4f).equals(jsonArray.getFloat(5))); + Float.valueOf(23.45e-4f).equals(jsonArray.getFloat(5))); // ints assertTrue("Array value int", - new Integer(42).equals(jsonArray.getInt(7))); + Integer.valueOf(42).equals(jsonArray.getInt(7))); assertTrue("Array value string int", - new Integer(43).equals(jsonArray.getInt(8))); + Integer.valueOf(43).equals(jsonArray.getInt(8))); // nested objects JSONArray nestedJsonArray = jsonArray.getJSONArray(9); assertTrue("Array value JSONArray", nestedJsonArray != null); @@ -385,9 +385,9 @@ public void getArrayValues() { assertTrue("Array value JSONObject", nestedJsonObject != null); // longs assertTrue("Array value long", - new Long(0).equals(jsonArray.getLong(11))); + Long.valueOf(0).equals(jsonArray.getLong(11))); assertTrue("Array value string long", - new Long(-1).equals(jsonArray.getLong(12))); + Long.valueOf(-1).equals(jsonArray.getLong(12))); assertTrue("Array value null", jsonArray.isNull(-1)); Util.checkJSONArrayMaps(jsonArray); @@ -545,11 +545,11 @@ public void opt() { Boolean.FALSE.equals(jsonArray.optBooleanObject(-1))); assertTrue("Array opt double", - new Double(23.45e-4).equals(jsonArray.optDouble(5))); + Double.valueOf(23.45e-4).equals(jsonArray.optDouble(5))); assertTrue("Array opt double default", - new Double(1).equals(jsonArray.optDouble(0, 1))); + Double.valueOf(1).equals(jsonArray.optDouble(0, 1))); assertTrue("Array opt double default implicit", - new Double(jsonArray.optDouble(99)).isNaN()); + Double.valueOf(jsonArray.optDouble(99)).isNaN()); assertTrue("Array opt double object", Double.valueOf(23.45e-4).equals(jsonArray.optDoubleObject(5))); @@ -559,11 +559,11 @@ public void opt() { jsonArray.optDoubleObject(99).isNaN()); assertTrue("Array opt float", - new Float(23.45e-4).equals(jsonArray.optFloat(5))); + Float.valueOf(Double.valueOf(23.45e-4).floatValue()).equals(jsonArray.optFloat(5))); assertTrue("Array opt float default", - new Float(1).equals(jsonArray.optFloat(0, 1))); + Float.valueOf(1).equals(jsonArray.optFloat(0, 1))); assertTrue("Array opt float default implicit", - new Float(jsonArray.optFloat(99)).isNaN()); + Float.valueOf(jsonArray.optFloat(99)).isNaN()); assertTrue("Array opt float object", Float.valueOf(23.45e-4F).equals(jsonArray.optFloatObject(5))); @@ -575,14 +575,14 @@ public void opt() { assertTrue("Array opt Number", BigDecimal.valueOf(23.45e-4).equals(jsonArray.optNumber(5))); assertTrue("Array opt Number default", - new Double(1).equals(jsonArray.optNumber(0, 1d))); + Double.valueOf(1).equals(jsonArray.optNumber(0, 1d))); assertTrue("Array opt Number default implicit", - new Double(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN()); + Double.valueOf(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN()); assertTrue("Array opt int", - new Integer(42).equals(jsonArray.optInt(7))); + Integer.valueOf(42).equals(jsonArray.optInt(7))); assertTrue("Array opt int default", - new Integer(-1).equals(jsonArray.optInt(0, -1))); + Integer.valueOf(-1).equals(jsonArray.optInt(0, -1))); assertTrue("Array opt int default implicit", 0 == jsonArray.optInt(0)); @@ -1011,12 +1011,12 @@ public void iteratorTest() { assertTrue("Array double [23.45e-4]", new BigDecimal("0.002345").equals(it.next())); assertTrue("Array string double", - new Double(23.45).equals(Double.parseDouble((String)it.next()))); + Double.valueOf(23.45).equals(Double.parseDouble((String)it.next()))); assertTrue("Array value int", - new Integer(42).equals(it.next())); + Integer.valueOf(42).equals(it.next())); assertTrue("Array value string int", - new Integer(43).equals(Integer.parseInt((String)it.next()))); + Integer.valueOf(43).equals(Integer.parseInt((String)it.next()))); JSONArray nestedJsonArray = (JSONArray)it.next(); assertTrue("Array value JSONArray", nestedJsonArray != null); @@ -1025,9 +1025,9 @@ public void iteratorTest() { assertTrue("Array value JSONObject", nestedJsonObject != null); assertTrue("Array value long", - new Long(0).equals(((Number) it.next()).longValue())); + Long.valueOf(0).equals(((Number) it.next()).longValue())); assertTrue("Array value string long", - new Long(-1).equals(Long.parseLong((String) it.next()))); + Long.valueOf(-1).equals(Long.parseLong((String) it.next()))); assertTrue("should be at end of array", !it.hasNext()); Util.checkJSONArraysMaps(new ArrayList(Arrays.asList( jsonArray, nestedJsonArray diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 3250c258a..2de8f815c 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -54,7 +54,6 @@ import org.json.junit.data.SingletonEnum; import org.json.junit.data.WeirdList; import org.junit.Test; -import org.json.junit.Util; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; @@ -304,12 +303,12 @@ public void jsonObjectByNullMap() { @Test public void jsonObjectByMap() { Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); + map.put("trueKey", Boolean.valueOf(true)); + map.put("falseKey", Boolean.valueOf(false)); map.put("stringKey", "hello world!"); map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); + map.put("intKey", Long.valueOf(42)); + map.put("doubleKey", Double.valueOf(-23.45e67)); JSONObject jsonObject = new JSONObject(map); // validate JSON @@ -570,13 +569,13 @@ public void jsonObjectByMapWithUnsupportedValues() { @Test public void jsonObjectByMapWithNullValue() { Map map = new HashMap(); - map.put("trueKey", new Boolean(true)); - map.put("falseKey", new Boolean(false)); + map.put("trueKey", Boolean.valueOf(true)); + map.put("falseKey", Boolean.valueOf(false)); map.put("stringKey", "hello world!"); map.put("nullKey", null); map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); - map.put("intKey", new Long(42)); - map.put("doubleKey", new Double(-23.45e67)); + map.put("intKey", Long.valueOf(42)); + map.put("doubleKey", Double.valueOf(-23.45e67)); JSONObject jsonObject = new JSONObject(map); // validate JSON @@ -996,7 +995,7 @@ public void stringToValueNumbersTest() { assertTrue( "0.2 should be a BigDecimal!", JSONObject.stringToValue( "0.2" ) instanceof BigDecimal ); assertTrue( "Doubles should be BigDecimal, even when incorrectly converting floats!", - JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof BigDecimal ); + JSONObject.stringToValue( Double.valueOf( "0.2f" ).toString() ) instanceof BigDecimal ); /** * This test documents a need for BigDecimal conversion. */ @@ -1006,13 +1005,13 @@ public void stringToValueNumbersTest() { assertTrue( "1 should be an Integer!", JSONObject.stringToValue( "1" ) instanceof Integer ); assertTrue( "Integer.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); + JSONObject.stringToValue( Integer.valueOf( Integer.MAX_VALUE ).toString() ) instanceof Integer ); assertTrue( "Large integers should be a Long!", JSONObject.stringToValue( Long.valueOf(((long)Integer.MAX_VALUE) + 1 ) .toString() ) instanceof Long ); assertTrue( "Long.MAX_VALUE should still be an Integer!", - JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); + JSONObject.stringToValue( Long.valueOf( Long.MAX_VALUE ).toString() ) instanceof Long ); - String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); + String str = new BigInteger( Long.valueOf( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); assertTrue( "Really large integers currently evaluate to BigInteger", JSONObject.stringToValue(str).equals(new BigInteger("9223372036854775808"))); } @@ -1259,8 +1258,8 @@ public void unexpectedDoubleToIntConversion() { String key30 = "key30"; String key31 = "key31"; JSONObject jsonObject = new JSONObject(); - jsonObject.put(key30, new Double(3.0)); - jsonObject.put(key31, new Double(3.1)); + jsonObject.put(key30, Double.valueOf(3.0)); + jsonObject.put(key31, Double.valueOf(3.1)); assertTrue("3.0 should remain a double", jsonObject.getDouble(key30) == 3); @@ -1713,19 +1712,19 @@ public void jsonObjectIncrement() { */ assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d ); assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d ); - Double d1 = new Double( 1.1f ); - Double d2 = new Double( "1.1f" ); + Double d1 = Double.valueOf( 1.1f ); + Double d2 = Double.valueOf( "1.1f" ); assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) ); - assertTrue( "Correctly converting float to double via base10 (string) representation!", new Double( 3.1d ).equals( new Double( new Float( 3.1f ).toString() ) ) ); + assertTrue( "Correctly converting float to double via base10 (string) representation!", Double.valueOf( 3.1d ).equals( Double.valueOf( Float.valueOf( 3.1f ).toString() ) ) ); // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject JSONObject jo = new JSONObject(); jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double - assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( new Double( 3.1d ) ) ); + assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( Double.valueOf( 3.1d ) ) ); JSONObject inc = new JSONObject(); - inc.put( "bug", new Float( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) + inc.put( "bug", Float.valueOf( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value ) assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float ); inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double! // this.put(key, (Float) value + 1); @@ -2040,14 +2039,14 @@ public void valueToString() { assertTrue("map valueToString() incorrect", jsonObject.toString().equals(JSONObject.valueToString(map))); Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); + collection.add(Integer.valueOf(1)); + collection.add(Integer.valueOf(2)); + collection.add(Integer.valueOf(3)); assertTrue("collection valueToString() expected: "+ jsonArray.toString()+ " actual: "+ JSONObject.valueToString(collection), jsonArray.toString().equals(JSONObject.valueToString(collection))); - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }; assertTrue("array valueToString() incorrect", jsonArray.toString().equals(JSONObject.valueToString(array))); Util.checkJSONObjectMaps(jsonObject); @@ -2085,7 +2084,7 @@ public void wrapObject() { JSONObject.NULL == JSONObject.wrap(null)); // wrap(Integer) returns Integer - Integer in = new Integer(1); + Integer in = Integer.valueOf(1); assertTrue("Integer wrap() incorrect", in == JSONObject.wrap(in)); @@ -2112,9 +2111,9 @@ public void wrapObject() { // wrap collection returns JSONArray Collection collection = new ArrayList(); - collection.add(new Integer(1)); - collection.add(new Integer(2)); - collection.add(new Integer(3)); + collection.add(Integer.valueOf(1)); + collection.add(Integer.valueOf(2)); + collection.add(Integer.valueOf(3)); JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection)); // validate JSON @@ -2125,7 +2124,7 @@ public void wrapObject() { assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2"))); // wrap Array returns JSONArray - Integer[] array = { new Integer(1), new Integer(2), new Integer(3) }; + Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }; JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array)); // validate JSON From becc1631e6dbe41e3a0245d765b01509de2608b5 Mon Sep 17 00:00:00 2001 From: simonh5 Date: Mon, 18 Sep 2023 20:20:13 -0500 Subject: [PATCH 025/233] fix: flakiness in JSONMLTest#testToJSONObject_reversibility --- build.gradle | 1 + pom.xml | 7 +++++++ src/test/java/org/json/junit/JSONMLTest.java | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 8a3708a74..5a5be375e 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,7 @@ dependencies { testImplementation 'junit:junit:4.13.1' testImplementation 'com.jayway.jsonpath:json-path:2.1.0' testImplementation 'org.mockito:mockito-core:4.2.0' + testImplementation 'org.skyscreamer:jsonassert:1.5.1' } subprojects { diff --git a/pom.xml b/pom.xml index 720529c50..8bbcc3c55 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,13 @@ 4.2.0 test + + + org.skyscreamer + jsonassert + 1.5.1 + test + diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 35c0af2c4..9b5e5b612 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -8,6 +8,7 @@ import org.json.*; import org.junit.Test; +import org.skyscreamer.jsonassert.JSONAssert; /** * Tests for org.json.JSONML.java @@ -763,7 +764,7 @@ public void testToJSONObject_reversibility() { final JSONObject revertedObject = JSONML.toJSONObject(xml, false); final String newJson = revertedObject.toString(); assertTrue("JSON Objects are not similar",originalObject.similar(revertedObject)); - assertEquals("original JSON does not equal the new JSON",originalJson, newJson); + JSONAssert.assertEquals("original JSON does not equal the new JSON", originalJson, newJson, false); } // these tests do not pass for the following reasons: From 3e688afc66a8cb84d0dcd9a49f2431cb304ba72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Tue, 19 Sep 2023 07:38:13 -0700 Subject: [PATCH 026/233] Small test fixes. One test method was missing `@Test` so it was never run. One test method used another test class as the base for finding a test resource. While this works in practice with Maven, it is not strictly right. --- src/test/java/org/json/junit/JSONArrayTest.java | 2 +- src/test/java/org/json/junit/JSONObjectTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ad938cf50..cb97eeae5 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1369,7 +1369,7 @@ public void jsonArrayClearMethodTest() { @Test(expected = JSONException.class) public void issue654StackOverflowInputWellFormed() { //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes)); - final InputStream resourceAsStream = JSONObjectTest.class.getClassLoader().getResourceAsStream("Issue654WellFormedArray.json"); + final InputStream resourceAsStream = JSONArrayTest.class.getClassLoader().getResourceAsStream("Issue654WellFormedArray.json"); JSONTokener tokener = new JSONTokener(resourceAsStream); JSONArray json_input = new JSONArray(tokener); assertNotNull(json_input); diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2de8f815c..c3fb8f31e 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3288,6 +3288,7 @@ public void testWierdListBean() { * Sample test case from https://github.com/stleary/JSON-java/issues/531 * which verifies that no regression in double/BigDecimal support is present. */ + @Test public void testObjectToBigDecimal() { double value = 1412078745.01074; Reader reader = new StringReader("[{\"value\": " + value + "}]"); From ca88454f1cdaedf46bcce546c0a9ce79709c544c Mon Sep 17 00:00:00 2001 From: simonh5 Date: Tue, 19 Sep 2023 14:28:06 -0500 Subject: [PATCH 027/233] fix: flakiness in org.json.junit.JSONObjectTest#valueToString --- build.gradle | 1 + pom.xml | 7 +++++++ src/test/java/org/json/junit/JSONObjectTest.java | 9 +++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 8a3708a74..5a5be375e 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,7 @@ dependencies { testImplementation 'junit:junit:4.13.1' testImplementation 'com.jayway.jsonpath:json-path:2.1.0' testImplementation 'org.mockito:mockito-core:4.2.0' + testImplementation 'org.skyscreamer:jsonassert:1.5.1' } subprojects { diff --git a/pom.xml b/pom.xml index 720529c50..8bbcc3c55 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,13 @@ 4.2.0 test + + + org.skyscreamer + jsonassert + 1.5.1 + test + diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2de8f815c..4eefea832 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -57,6 +57,7 @@ import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; +import org.skyscreamer.jsonassert.JSONAssert; /** * JSONObject, along with JSONArray, are the central classes of the reference app. @@ -2025,8 +2026,8 @@ public void valueToString() { "\"key3\":\"val3\""+ "}"; JSONObject jsonObject = new JSONObject(jsonObjectStr); - assertTrue("jsonObject valueToString() incorrect", - JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); + JSONAssert.assertEquals("jsonObject valueToString() incorrect", + JSONObject.valueToString(jsonObject), jsonObject.toString(), false); String jsonArrayStr = "[1,2,3]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); @@ -2036,8 +2037,8 @@ public void valueToString() { map.put("key1", "val1"); map.put("key2", "val2"); map.put("key3", "val3"); - assertTrue("map valueToString() incorrect", - jsonObject.toString().equals(JSONObject.valueToString(map))); + JSONAssert.assertEquals("map valueToString() incorrect", + jsonObject.toString(), JSONObject.valueToString(map), false); Collection collection = new ArrayList(); collection.add(Integer.valueOf(1)); collection.add(Integer.valueOf(2)); From 661114c50dcfd53bb041aab66f14bb91e0a87c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 20 Sep 2023 10:50:48 -0700 Subject: [PATCH 028/233] Generalize the logic to disallow nested objects and arrays as keys in objects. Fixes #771. --- src/main/java/org/json/JSONObject.java | 16 ++++----------- src/main/java/org/json/JSONTokener.java | 20 ++++++++++++++----- .../java/org/json/junit/JSONObjectTest.java | 18 +++++++++++++++++ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 5e00eb9a3..3986c56f9 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -208,22 +208,14 @@ public JSONObject(JSONTokener x) throws JSONException { throw x.syntaxError("A JSONObject text must begin with '{'"); } for (;;) { - char prev = x.getPrevious(); c = x.nextClean(); switch (c) { case 0: throw x.syntaxError("A JSONObject text must end with '}'"); case '}': return; - case '{': - case '[': - if(prev=='{') { - throw x.syntaxError("A JSON Object can not directly nest another JSON Object or JSON Array."); - } - // fall through default: - x.back(); - key = x.nextValue().toString(); + key = x.nextSimpleValue(c).toString(); } // The key is followed by ':'. @@ -1712,12 +1704,12 @@ && isValidMethodName(method.getName())) { final Object result = method.invoke(bean); if (result != null) { // check cyclic dependency and throw error if needed - // the wrap and populateMap combination method is + // the wrap and populateMap combination method is // itself DFS recursive if (objectsRecord.contains(result)) { throw recursivelyDefinedObjectException(key); } - + objectsRecord.add(result); this.map.put(key, wrap(result, objectsRecord)); @@ -1726,7 +1718,7 @@ && isValidMethodName(method.getName())) { // we don't use the result anywhere outside of wrap // if it's a resource we should be sure to close it - // after calling toString + // after calling toString if (result instanceof Closeable) { try { ((Closeable) result).close(); diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 5dc8ae85a..4a7122f7d 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -402,12 +402,7 @@ public String nextTo(String delimiters) throws JSONException { */ public Object nextValue() throws JSONException { char c = this.nextClean(); - String string; - switch (c) { - case '"': - case '\'': - return this.nextString(c); case '{': this.back(); try { @@ -423,6 +418,21 @@ 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; + + switch (c) { + case '"': + case '\'': + return this.nextString(c); + case '{': + throw syntaxError("Nested object not expected here."); + case '[': + throw syntaxError("Nested array not expected here."); + } /* * Handle unquoted text. This could be the values true, false, or diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2de8f815c..23feda9d6 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2224,6 +2224,24 @@ public void jsonObjectParsingErrors() { "Expected a ',' or '}' at 15 [character 16 line 1]", e.getMessage()); } + try { + // key is a nested map + String str = "{{\"foo\": \"bar\"}: \"baz\"}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Nested object not expected here. at 2 [character 3 line 1]", + e.getMessage()); + } + try { + // key is a nested array containing a map + String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Nested array not expected here. at 10 [character 11 line 1]", + e.getMessage()); + } try { // \0 after , String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; From db0fde2a566f5333a3ad2e70e6d21fc5680422f1 Mon Sep 17 00:00:00 2001 From: Edijs Date: Mon, 25 Sep 2023 20:18:33 +0300 Subject: [PATCH 029/233] Add optJSONArray method to JSONObject with a default value --- src/main/java/org/json/JSONObject.java | 18 ++++++++++++++++-- .../java/org/json/junit/JSONObjectTest.java | 4 ++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 5e00eb9a3..c6ac47ac1 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1512,8 +1512,22 @@ public Integer optIntegerObject(String key, Integer defaultValue) { * @return A JSONArray which is the value. */ public JSONArray optJSONArray(String key) { - Object o = this.opt(key); - return o instanceof JSONArray ? (JSONArray) o : null; + return this.optJSONArray(key, null); + } + + /** + * Get an optional JSONArray associated with a key, or the default if there + * is no such key, or if its value is not a JSONArray. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return A JSONArray which is the value. + */ + public JSONArray optJSONArray(String key, JSONArray defaultValue) { + Object object = this.opt(key); + return object instanceof JSONArray ? (JSONArray) object : defaultValue; } /** diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2de8f815c..07eb38b98 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2510,6 +2510,8 @@ public void jsonObjectOptDefault() { MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONArray() should return default JSONArray", + "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0))); assertTrue("optJSONObject() should return default JSONObject ", jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue")); assertTrue("optLong() should return default long", @@ -2555,6 +2557,8 @@ public void jsonObjectOptNoKey() { Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42))); assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return default JSONArray", + "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); assertTrue("optJSONObject() should return default JSONObject ", From 284a31683898111b64ee5c92fa909b604bd9051d Mon Sep 17 00:00:00 2001 From: Edijs Date: Wed, 27 Sep 2023 19:30:45 +0300 Subject: [PATCH 030/233] Add optJSONArray and optJSONObject methods to JSONArray with a default value --- src/main/java/org/json/JSONArray.java | 47 +++++++++++++++---- .../java/org/json/junit/JSONArrayTest.java | 8 +++- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 375d03888..cc9531e22 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -924,30 +924,57 @@ public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) { } /** - * Get the optional JSONArray associated with an index. + * Get the optional JSONArray associated with an index. Null is returned if + * there is no value at that index or if the value is not a JSONArray. * * @param index - * subscript - * @return A JSONArray value, or null if the index has no value, or if the - * value is not a JSONArray. + * The index must be between 0 and length() - 1. + * @return A JSONArray value. */ public JSONArray optJSONArray(int index) { - Object o = this.opt(index); - return o instanceof JSONArray ? (JSONArray) o : null; + return this.optJSONArray(index, null); + } + + /** + * Get the optional JSONArray associated with an index. The defaultValue is returned if + * there is no value at that index or if the value is not a JSONArray. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default. + * @return A JSONArray value. + */ + public JSONArray optJSONArray(int index, JSONArray defaultValue) { + Object object = this.opt(index); + return object instanceof JSONArray ? (JSONArray) object : defaultValue; } /** * Get the optional JSONObject associated with an index. Null is returned if - * the key is not found, or null if the index has no value, or if the value - * is not a JSONObject. + * there is no value at that index or if the value is not a JSONObject. * * @param index * The index must be between 0 and length() - 1. * @return A JSONObject value. */ public JSONObject optJSONObject(int index) { - Object o = this.opt(index); - return o instanceof JSONObject ? (JSONObject) o : null; + return this.optJSONObject(index, null); + } + + /** + * Get the optional JSONObject associated with an index. The defaultValue is returned if + * there is no value at that index or if the value is not a JSONObject. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default. + * @return A JSONObject value. + */ + public JSONObject optJSONObject(int index, JSONObject defaultValue) { + Object object = this.opt(index); + return object instanceof JSONObject ? (JSONObject) object : defaultValue; } /** diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ad938cf50..d735bf5cf 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -595,13 +595,17 @@ public void opt() { JSONArray nestedJsonArray = jsonArray.optJSONArray(9); assertTrue("Array opt JSONArray", nestedJsonArray != null); - assertTrue("Array opt JSONArray default", + assertTrue("Array opt JSONArray null", null == jsonArray.optJSONArray(99)); + assertTrue("Array opt JSONArray default", + "value".equals(jsonArray.optJSONArray(99, new JSONArray("[\"value\"]")).getString(0))); JSONObject nestedJsonObject = jsonArray.optJSONObject(10); assertTrue("Array opt JSONObject", nestedJsonObject != null); - assertTrue("Array opt JSONObject default", + assertTrue("Array opt JSONObject null", null == jsonArray.optJSONObject(99)); + assertTrue("Array opt JSONObject default", + "value".equals(jsonArray.optJSONObject(99, new JSONObject("{\"key\":\"value\"}")).getString("key"))); assertTrue("Array opt long", 0 == jsonArray.optLong(11)); From 16967f322ee65c301b48fa79bb681e38896fd212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 27 Sep 2023 12:42:04 -0700 Subject: [PATCH 031/233] Simplify the check for object keys that are themselves objects. For object keys, we can just skip the part of `nextValue()` that parses values that are objects or arrays. Then the existing logic for unquoted values will already stop at `{` or `[`, and that will produce a `Missing value` exception. --- src/main/java/org/json/JSONTokener.java | 4 ---- src/test/java/org/json/junit/JSONObjectTest.java | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 4a7122f7d..4a83a6971 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -428,10 +428,6 @@ Object nextSimpleValue(char c) { case '"': case '\'': return this.nextString(c); - case '{': - throw syntaxError("Nested object not expected here."); - case '[': - throw syntaxError("Nested array not expected here."); } /* diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 23feda9d6..88115c844 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2230,7 +2230,7 @@ public void jsonObjectParsingErrors() { assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Nested object not expected here. at 2 [character 3 line 1]", + "Missing value at 1 [character 2 line 1]", e.getMessage()); } try { @@ -2239,7 +2239,7 @@ public void jsonObjectParsingErrors() { assertNull("Expected an exception",new JSONObject(str)); } catch (JSONException e) { assertEquals("Expecting an exception message", - "Nested array not expected here. at 10 [character 11 line 1]", + "Missing value at 9 [character 10 line 1]", e.getMessage()); } try { From dbb113176b143b519ad0a50b033a9997cc2248fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Thu, 28 Sep 2023 11:05:50 -0700 Subject: [PATCH 032/233] Add more test cases for unquoted text in objects and arrays. --- .../java/org/json/junit/JSONArrayTest.java | 16 ++++++++++++- .../java/org/json/junit/JSONObjectTest.java | 24 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ad938cf50..5a98878d6 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -118,7 +118,7 @@ public void nullException() { * Expects a JSONException. */ @Test - public void emptStr() { + public void emptyStr() { String str = ""; try { assertNull("Should throw an exception", new JSONArray(str)); @@ -460,6 +460,20 @@ public void failedGetArrayValues() { Util.checkJSONArrayMaps(jsonArray); } + /** + * The JSON parser is permissive of unambiguous unquoted keys and values. + * Such JSON text should be allowed, even if it does not strictly conform + * to the spec. However, after being parsed, toString() should emit strictly + * conforming JSON text. + */ + @Test + public void unquotedText() { + String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]"; + JSONArray jsonArray = new JSONArray(str); + List expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45"); + assertEquals(expected, jsonArray.toList()); + } + /** * Exercise JSONArray.join() by converting a JSONArray into a * comma-separated string. Since this is very nearly a JSON document, diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 88115c844..b9ff59e31 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -205,13 +205,17 @@ public void jsonObjectByNullBean() { */ @Test public void unquotedText() { - String str = "{key1:value1, key2:42}"; + String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}"; JSONObject jsonObject = new JSONObject(str); String textStr = jsonObject.toString(); assertTrue("expected key1", textStr.contains("\"key1\"")); assertTrue("expected value1", textStr.contains("\"value1\"")); assertTrue("expected key2", textStr.contains("\"key2\"")); assertTrue("expected 42", textStr.contains("42")); + assertTrue("expected 1.2", textStr.contains("\"1.2\"")); + assertTrue("expected 3.4", textStr.contains("3.4")); + assertTrue("expected -7E+5", textStr.contains("\"-7E+5\"")); + assertTrue("expected something!", textStr.contains("\"something!\"")); Util.checkJSONObjectMaps(jsonObject); } @@ -2242,6 +2246,24 @@ public void jsonObjectParsingErrors() { "Missing value at 9 [character 10 line 1]", e.getMessage()); } + try { + // key contains } + String str = "{foo}: 2}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Expected a ':' after a key at 5 [character 6 line 1]", + e.getMessage()); + } + try { + // key contains ] + String str = "{foo]: 2}"; + assertNull("Expected an exception",new JSONObject(str)); + } catch (JSONException e) { + assertEquals("Expecting an exception message", + "Expected a ':' after a key at 5 [character 6 line 1]", + e.getMessage()); + } try { // \0 after , String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; From 61bb60e7525a851b222e3129b90a008bd6025877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Ol=C4=99dzki?= Date: Sat, 30 Sep 2023 21:36:11 +0200 Subject: [PATCH 033/233] Removing excessive synchronization --- src/main/java/org/json/JSONArray.java | 4 +--- src/main/java/org/json/JSONObject.java | 16 ++++++---------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index cc9531e22..b0c912d1f 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -1646,9 +1646,7 @@ public String toString() { @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { StringWriter sw = new StringWriter(); - synchronized (sw.getBuffer()) { - return this.write(sw, indentFactor, 0).toString(); - } + return this.write(sw, indentFactor, 0).toString(); } /** diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index c6ac47ac1..3d9594cc0 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2191,13 +2191,11 @@ public Object optQuery(JSONPointer jsonPointer) { @SuppressWarnings("resource") public static String quote(String string) { StringWriter sw = new StringWriter(); - synchronized (sw.getBuffer()) { - try { - return quote(string, sw).toString(); - } catch (IOException ignored) { - // will never happen - we are writing to a string writer - return ""; - } + try { + return quote(string, sw).toString(); + } catch (IOException ignored) { + // will never happen - we are writing to a string writer + return ""; } } @@ -2584,9 +2582,7 @@ public String toString() { @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { StringWriter w = new StringWriter(); - synchronized (w.getBuffer()) { - return this.write(w, indentFactor, 0).toString(); - } + return this.write(w, indentFactor, 0).toString(); } /** From ff921db783a315a90797312f0d1fca469d97db90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Ol=C4=99dzki?= Date: Sat, 30 Sep 2023 21:53:36 +0200 Subject: [PATCH 034/233] Junit 4.13.2 --- build.gradle | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 8a3708a74..fbc2ff1f1 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ repositories { } dependencies { - testImplementation 'junit:junit:4.13.1' + testImplementation 'junit:junit:4.13.2' testImplementation 'com.jayway.jsonpath:json-path:2.1.0' testImplementation 'org.mockito:mockito-core:4.2.0' } diff --git a/pom.xml b/pom.xml index 720529c50..29592fcdf 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ junit junit - 4.13.1 + 4.13.2 test From fe45fa9cfbaf1a4b4df223ff3357a0d73d3a2932 Mon Sep 17 00:00:00 2001 From: Allon Mureinik Date: Thu, 5 Oct 2023 15:29:51 +0300 Subject: [PATCH 035/233] Fix XMLTest on Windows XMLTest.testIndentComplicatedJsonObjectWithArrayAndWithConfig fails when run on Windows due to mismatching linebreaks (that aren't important for the test's functionality) between the actual and expected strings. For the actual strings, linebreaks are canonized to the platform's native linebreak using `replaceAll("\\n|\\r\\n", System.getProperty("line.separator")`. However, the expected result is read from a file, and is left with the linebreaks that were originally used to create it. The solution is to perform the same canonization on both strings. Closes #781 --- src/test/java/org/json/junit/XMLTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index e940032e0..6e7b1a9cd 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1239,7 +1239,8 @@ public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){ for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { expected.append(buffer, 0, numRead); } - assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); + assertEquals(expected.toString().replaceAll("\\n|\\r\\n", System.getProperty("line.separator")), + actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); } finally { if (xmlStream != null) { xmlStream.close(); From 4c8cac22a89069209439809243ac4eddf6b0dd47 Mon Sep 17 00:00:00 2001 From: Allon Mureinik Date: Thu, 5 Oct 2023 19:47:33 +0300 Subject: [PATCH 036/233] Use System.lineSeparator() Use the built-in System.lineSeparator() instead of implementing it ourselves with System.getProperty("line.separator") in order to clean up the code and make it easier to maintain. --- src/test/java/org/json/junit/XMLTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 6e7b1a9cd..712b8eef8 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1239,8 +1239,8 @@ public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){ for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { expected.append(buffer, 0, numRead); } - assertEquals(expected.toString().replaceAll("\\n|\\r\\n", System.getProperty("line.separator")), - actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); + assertEquals(expected.toString().replaceAll("\\n|\\r\\n", System.lineSeparator()), + actualString.replaceAll("\\n|\\r\\n", System.lineSeparator())); } finally { if (xmlStream != null) { xmlStream.close(); From 1a38879c9099078a1cc63a80312da318235ad0f6 Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Fri, 6 Oct 2023 21:34:00 +0530 Subject: [PATCH 037/233] #653 - optLong vs getLong inconsistencies For exponential decimal conversion, number is not touched. Leading zeros removed from numeric number strings before converting to number. --- src/main/java/org/json/JSONObject.java | 36 +++++++++++++++++-- .../org/json/junit/JSONObjectNumberTest.java | 6 +++- .../java/org/json/junit/JSONObjectTest.java | 35 +++++++++++++++++- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index acef67d9f..5eb332225 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2379,12 +2379,13 @@ protected static boolean isDecimalNotation(final String val) { * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. * - * @param val value to convert + * @param input value to convert * @return Number representation of the value. * @throws NumberFormatException thrown if the value is not a valid number. A public * caller should catch this and wrap it in a {@link JSONException} if applicable. */ - protected static Number stringToNumber(final String val) throws NumberFormatException { + protected static Number stringToNumber(final String input) throws NumberFormatException { + String val = input; char initial = val.charAt(0); if ((initial >= '0' && initial <= '9') || initial == '-') { // decimal representation @@ -2411,6 +2412,8 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce } } } + val = removeLeadingZerosOfNumber(input); + initial = val.charAt(0); // block items like 00 01 etc. Java number parsers treat these as Octal. if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); @@ -2886,4 +2889,33 @@ private static JSONException recursivelyDefinedObjectException(String key) { "JavaBean object contains recursively defined member variable of key " + quote(key) ); } + + /** + * For a prospective number, remove the leading zeros + * @param value prospective number + * @return number without leading zeros + */ + private static String removeLeadingZerosOfNumber(String value){ + char[] chars = value.toCharArray(); + int leftMostUnsignedIndex = 0; + if (chars[0] == '-'){ + leftMostUnsignedIndex = 1; + } + int firstNonZeroCharIndex = -1; + for (int i=leftMostUnsignedIndex;i data() { return Arrays.asList(new Object[][]{ - {"{value:50}", 1}, + {"{value:0050}", 1}, + {"{value:0050.0000}", 1}, + {"{value:-0050}", -1}, + {"{value:-0050.0000}", -1}, {"{value:50.0}", 1}, {"{value:5e1}", 1}, {"{value:5E1}", 1}, @@ -32,6 +35,7 @@ public static Collection data() { {"{value:-50}", -1}, {"{value:-50.0}", -1}, {"{value:-5e1}", -1}, + {"{value:-0005e1}", -1}, {"{value:-5E1}", -1}, {"{value:-5e1}", -1}, {"{value:'-50'}", -1} diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 01889d54b..b63552141 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1063,12 +1063,16 @@ public void jsonInvalidNumberValues() { "\"tooManyZeros\":00,"+ "\"negativeInfinite\":-Infinity,"+ "\"negativeNaN\":-NaN,"+ + "\"negativeNaNWithLeadingZeros\":-00NaN,"+ "\"negativeFraction\":-.01,"+ "\"tooManyZerosFraction\":00.001,"+ "\"negativeHexFloat\":-0x1.fffp1,"+ "\"hexFloat\":0x1.0P-1074,"+ "\"floatIdentifier\":0.1f,"+ - "\"doubleIdentifier\":0.1d"+ + "\"doubleIdentifier\":0.1d,"+ + "\"integerWithLeadingZeros\":000900,"+ + "\"integerWithAllZeros\":00000,"+ + "\"compositeWithLeadingZeros\":00800.90d"+ "}"; JSONObject jsonObject = new JSONObject(str); Object obj; @@ -1085,10 +1089,17 @@ public void jsonInvalidNumberValues() { obj = jsonObject.get("negativeNaN"); assertTrue( "negativeNaN currently evaluates to string", obj.equals("-NaN")); + obj = jsonObject.get("negativeNaNWithLeadingZeros"); + assertTrue( "negativeNaNWithLeadingZeros currently evaluates to string", + obj.equals("-00NaN")); assertTrue( "negativeFraction currently evaluates to double -0.01", jsonObject.get( "negativeFraction" ).equals(BigDecimal.valueOf(-0.01))); assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", jsonObject.get( "tooManyZerosFraction" ).equals(BigDecimal.valueOf(0.001))); + assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.getLong( "tooManyZerosFraction" )==0); + assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", + jsonObject.optLong( "tooManyZerosFraction" )==0); assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", jsonObject.get( "negativeHexFloat" ).equals(Double.valueOf(-3.99951171875))); assertTrue("hexFloat currently evaluates to double 4.9E-324", @@ -1097,6 +1108,28 @@ public void jsonInvalidNumberValues() { jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1))); assertTrue("doubleIdentifier currently evaluates to double 0.1", jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1))); + assertTrue("Integer does not evaluate to 900", + jsonObject.get("integerWithLeadingZeros").equals(900)); + assertTrue("Integer does not evaluate to 900", + jsonObject.getInt("integerWithLeadingZeros")==900); + assertTrue("Integer does not evaluate to 900", + jsonObject.optInt("integerWithLeadingZeros")==900); + assertTrue("Integer does not evaluate to 0", + jsonObject.get("integerWithAllZeros").equals("00000")); + assertTrue("Integer does not evaluate to 0", + jsonObject.getInt("integerWithAllZeros")==0); + assertTrue("Integer does not evaluate to 0", + jsonObject.optInt("integerWithAllZeros")==0); + assertTrue("Double does not evaluate to 800.90", + jsonObject.get("compositeWithLeadingZeros").equals(800.90)); + assertTrue("Double does not evaluate to 800.90", + jsonObject.getDouble("compositeWithLeadingZeros")==800.9d); + assertTrue("Integer does not evaluate to 800", + jsonObject.optInt("compositeWithLeadingZeros")==800); + assertTrue("Long does not evaluate to 800.90", + jsonObject.getLong("compositeWithLeadingZeros")==800); + assertTrue("Long does not evaluate to 800.90", + jsonObject.optLong("compositeWithLeadingZeros")==800); Util.checkJSONObjectMaps(jsonObject); } From 0e4a94d91db64ae9eafed0ff7db5fff87770f0a4 Mon Sep 17 00:00:00 2001 From: Madjosz <28844868+Madjosz@users.noreply.github.com> Date: Wed, 4 Oct 2023 11:17:13 +0200 Subject: [PATCH 038/233] fix failing test XML test on Windows machines --- src/test/java/org/json/junit/XMLTest.java | 36 +++++++---------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index e940032e0..22d6131cb 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1223,32 +1223,18 @@ public void testIndentSimpleJsonArray(){ @Test public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){ - try { - InputStream jsonStream = null; - try { - jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json"); - final JSONObject object = new JSONObject(new JSONTokener(jsonStream)); - String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS,2); - InputStream xmlStream = null; - try { - xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml"); - int bufferSize = 1024; - char[] buffer = new char[bufferSize]; - StringBuilder expected = new StringBuilder(); - Reader in = new InputStreamReader(xmlStream, "UTF-8"); - for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { - expected.append(buffer, 0, numRead); - } - assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator"))); - } finally { - if (xmlStream != null) { - xmlStream.close(); - } - } - } finally { - if (jsonStream != null) { - jsonStream.close(); + try (InputStream jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json")) { + final JSONObject object = new JSONObject(new JSONTokener(jsonStream)); + String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS, 2); + try (InputStream xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml")) { + int bufferSize = 1024; + char[] buffer = new char[bufferSize]; + StringBuilder expected = new StringBuilder(); + Reader in = new InputStreamReader(xmlStream, "UTF-8"); + for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { + expected.append(buffer, 0, numRead); } + assertEquals(expected.toString(), actualString); } } catch (IOException e) { fail("file writer error: " +e.getMessage()); From c93014cb5379bb93f2155e48ee0a4382b4d05ae1 Mon Sep 17 00:00:00 2001 From: Madjosz <28844868+Madjosz@users.noreply.github.com> Date: Wed, 4 Oct 2023 12:00:47 +0200 Subject: [PATCH 039/233] add validity check for JSONObject constructors * fixes #713 * document JSONException in JavaDoc * remove unused Comparable boundary to reuse GenericBean in test --- src/main/java/org/json/JSONObject.java | 6 +++++ .../java/org/json/junit/JSONObjectTest.java | 26 +++++++++++++++++-- .../java/org/json/junit/data/GenericBean.java | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index acef67d9f..08ccdabb0 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -283,6 +283,7 @@ public JSONObject(Map m) { } final Object value = e.getValue(); if (value != null) { + testValidity(value); this.map.put(String.valueOf(e.getKey()), wrap(value)); } } @@ -346,6 +347,8 @@ public JSONObject(Map m) { * @param bean * An object that has getter methods that should be used to make * a JSONObject. + * @throws JSONException + * If a getter returned a non-finite number. */ public JSONObject(Object bean) { this(); @@ -1691,6 +1694,8 @@ public String optString(String key, String defaultValue) { * * @param bean * the bean + * @throws JSONException + * If a getter returned a non-finite number. */ private void populateMap(Object bean) { populateMap(bean, Collections.newSetFromMap(new IdentityHashMap())); @@ -1726,6 +1731,7 @@ && isValidMethodName(method.getName())) { objectsRecord.add(result); + testValidity(result); this.map.put(key, wrap(result, objectsRecord)); objectsRecord.remove(result); diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 01889d54b..0503dbb4f 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -9,6 +9,7 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -1972,7 +1973,7 @@ public void jsonObjectToStringIndent() { @Test public void jsonObjectToStringSuppressWarningOnCastToMap() { JSONObject jsonObject = new JSONObject(); - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("abc", "def"); jsonObject.put("key", map); @@ -3283,7 +3284,7 @@ public void testSingletonEnumBean() { @SuppressWarnings("boxing") @Test public void testGenericBean() { - GenericBean bean = new GenericBean(42); + GenericBean bean = new GenericBean<>(42); final JSONObject jo = new JSONObject(bean); assertEquals(jo.keySet().toString(), 8, jo.length()); assertEquals(42, jo.get("genericValue")); @@ -3627,4 +3628,25 @@ public String toJSONString() { .put("b", 2); assertFalse(jo1.similar(jo3)); } + + private static final Number[] NON_FINITE_NUMBERS = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN, + Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN }; + + @Test + public void issue713MapConstructorWithNonFiniteNumbers() { + for (Number nonFinite : NON_FINITE_NUMBERS) { + Map map = new HashMap<>(); + map.put("a", nonFinite); + + assertThrows(JSONException.class, () -> new JSONObject(map)); + } + } + + @Test + public void issue713BeanConstructorWithNonFiniteNumbers() { + for (Number nonFinite : NON_FINITE_NUMBERS) { + GenericBean bean = new GenericBean<>(nonFinite); + assertThrows(JSONException.class, () -> new JSONObject(bean)); + } + } } diff --git a/src/test/java/org/json/junit/data/GenericBean.java b/src/test/java/org/json/junit/data/GenericBean.java index da6370d48..dd46b88e6 100644 --- a/src/test/java/org/json/junit/data/GenericBean.java +++ b/src/test/java/org/json/junit/data/GenericBean.java @@ -9,7 +9,7 @@ * @param * generic number value */ -public class GenericBean> implements MyBean { +public class GenericBean implements MyBean { /** * @param genericValue * value to initiate with From 0cdc38ac24169f9515d929f9813c83bfbf55da83 Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Thu, 12 Oct 2023 00:53:36 +0530 Subject: [PATCH 040/233] #653 - review comments updated. --- src/main/java/org/json/JSONObject.java | 66 +++++++----- .../org/json/junit/JSONObjectDecimalTest.java | 100 ++++++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 49 +++++++-- .../org/json/junit/JsonNumberZeroTest.java | 55 ++++++++++ 4 files changed, 236 insertions(+), 34 deletions(-) create mode 100644 src/test/java/org/json/junit/JSONObjectDecimalTest.java create mode 100644 src/test/java/org/json/junit/JsonNumberZeroTest.java diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 0a730f4b7..7e8cbbe66 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2392,8 +2392,14 @@ protected static boolean isDecimalNotation(final String val) { */ protected static Number stringToNumber(final String input) throws NumberFormatException { String val = input; + if (val.startsWith(".")){ + val = "0"+val; + } + if (val.startsWith("-.")){ + val = "-0."+val.substring(2); + } char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if ((initial >= '0' && initial <= '9') || initial == '-' ) { // decimal representation if (isDecimalNotation(val)) { // Use a BigDecimal all the time so we keep the original @@ -2424,13 +2430,13 @@ protected static Number stringToNumber(final String input) throws NumberFormatEx if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } else if (initial == '-' && val.length() > 2) { char at1 = val.charAt(1); char at2 = val.charAt(2); if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } // integer representation. @@ -2450,7 +2456,7 @@ protected static Number stringToNumber(final String input) throws NumberFormatEx } return bi; } - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } /** @@ -2486,8 +2492,7 @@ public static Object stringToValue(String string) { * produced, then the value will just be a string. */ - char initial = string.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if (potentialNumber(string)) { try { return stringToNumber(string); } catch (Exception ignore) { @@ -2496,6 +2501,28 @@ public static Object stringToValue(String string) { return string; } + + private static boolean potentialNumber(String value){ + if (value == null || value.isEmpty()){ + return false; + } + return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); + } + + private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ + if (index >= value.length()){ + return false; + } + return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); + } + + private static boolean digitAtIndex(String value, int index){ + if (index >= value.length()){ + return false; + } + return value.charAt(index) >= '0' && value.charAt(index) <= '9'; + } + /** * Throw an exception if the object is a NaN or infinite number. * @@ -2902,26 +2929,15 @@ private static JSONException recursivelyDefinedObjectException(String key) { * @return number without leading zeros */ private static String removeLeadingZerosOfNumber(String value){ - char[] chars = value.toCharArray(); - int leftMostUnsignedIndex = 0; - if (chars[0] == '-'){ - leftMostUnsignedIndex = 1; - } - int firstNonZeroCharIndex = -1; - for (int i=leftMostUnsignedIndex;i Date: Thu, 12 Oct 2023 11:03:13 +0530 Subject: [PATCH 041/233] #653 - review comments updated. --- src/main/java/org/json/JSONObject.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 7e8cbbe66..fbf225e9f 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2416,17 +2416,16 @@ protected static Number stringToNumber(final String input) throws NumberFormatEx try { Double d = Double.valueOf(val); if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } return d; } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } } val = removeLeadingZerosOfNumber(input); initial = val.charAt(0); - // block items like 00 01 etc. Java number parsers treat these as Octal. if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); if(at1 >= '0' && at1 <= '9') { @@ -2934,10 +2933,12 @@ private static String removeLeadingZerosOfNumber(String value){ int counter = negativeFirstChar ? 1:0; while (counter < value.length()){ if (value.charAt(counter) != '0'){ - return String.format("%s%s", negativeFirstChar?'-':"",value.substring(counter)); + if (negativeFirstChar) {return "-".concat(value.substring(counter));} + return value.substring(counter); } ++counter; } - return String.format("%s%s", negativeFirstChar?'-':"",'0'); + if (negativeFirstChar) {return "-0";} + return "0"; } } From e4aa7f1308722b1283fc828dcb2326915a5d29da Mon Sep 17 00:00:00 2001 From: simonh5 Date: Thu, 12 Oct 2023 21:09:27 -0500 Subject: [PATCH 042/233] fix: change from JSONAssert to checking the similarity of JSONObjects --- src/test/java/org/json/junit/JSONMLTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 9b5e5b612..4d3649889 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -8,7 +8,6 @@ import org.json.*; import org.junit.Test; -import org.skyscreamer.jsonassert.JSONAssert; /** * Tests for org.json.JSONML.java @@ -763,8 +762,7 @@ public void testToJSONObject_reversibility() { final String xml = JSONML.toString(originalObject); final JSONObject revertedObject = JSONML.toJSONObject(xml, false); final String newJson = revertedObject.toString(); - assertTrue("JSON Objects are not similar",originalObject.similar(revertedObject)); - JSONAssert.assertEquals("original JSON does not equal the new JSON", originalJson, newJson, false); + assertTrue("original JSON does not equal the new JSON", originalObject.similar(revertedObject)); } // these tests do not pass for the following reasons: From af5f780d5bda393ae0f609ca2504a16a808e86de Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 13 Oct 2023 15:30:31 -0500 Subject: [PATCH 043/233] update the docs for release 20231013 --- README.md | 2 +- docs/RELEASES.md | 2 ++ pom.xml | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e999230ba..9f0134206 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ JSON in Java [package org.json] [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20230618/json-20230618.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20231013/json-20231013.jar)** # Overview diff --git a/docs/RELEASES.md b/docs/RELEASES.md index ae439831c..2b8aaa267 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -5,6 +5,8 @@ and artifactId "json". For example: [https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav) ~~~ +20231013 First release with minimum Java version 1.8. Recent commits, including fixes for CVE-2023-5072. + 20230618 Final release with Java 1.6 compatibility. Future releases will require Java 1.8 or greater. 20230227 Fix for CVE-2022-45688 and recent commits diff --git a/pom.xml b/pom.xml index 29592fcdf..59cb44f05 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.json json - 20230618 + 20231013 bundle JSON in Java @@ -15,7 +15,7 @@ It also includes the capability to convert between JSON and XML, HTTP headers, Cookies, and CDL. - This is a reference implementation. There is a large number of JSON packages + This is a reference implementation. There are a large number of JSON packages in Java. Perhaps someday the Java community will standardize on one. Until then, choose carefully. From b180dbedbc99bb177e5b277f1bff2a1b79cebda6 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 13 Oct 2023 16:04:14 -0500 Subject: [PATCH 044/233] Reverting #761 --- pom.xml | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 59cb44f05..77bbdaca3 100644 --- a/pom.xml +++ b/pom.xml @@ -159,35 +159,17 @@ false - - org.moditect - moditect-maven-plugin - 1.0.0.Final - - - add-module-infos - package - - add-module-info - - - 9 - - - org.json - - org.json; - - - - - - - org.apache.maven.plugins maven-jar-plugin 3.3.0 + + + + org.json + + + From 29a7f4622d887a90e15d719f88fad770b523ed69 Mon Sep 17 00:00:00 2001 From: simonh5 Date: Fri, 13 Oct 2023 20:58:50 -0500 Subject: [PATCH 045/233] remove JSONAssert --- build.gradle | 1 - pom.xml | 7 ------- src/test/java/org/json/junit/JSONMLTest.java | 3 ++- src/test/java/org/json/junit/JSONObjectTest.java | 12 +++++++----- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index 5a5be375e..8a3708a74 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,6 @@ dependencies { testImplementation 'junit:junit:4.13.1' testImplementation 'com.jayway.jsonpath:json-path:2.1.0' testImplementation 'org.mockito:mockito-core:4.2.0' - testImplementation 'org.skyscreamer:jsonassert:1.5.1' } subprojects { diff --git a/pom.xml b/pom.xml index 8bbcc3c55..720529c50 100644 --- a/pom.xml +++ b/pom.xml @@ -72,13 +72,6 @@ 4.2.0 test - - - org.skyscreamer - jsonassert - 1.5.1 - test - diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 4d3649889..154af645f 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -762,7 +762,8 @@ public void testToJSONObject_reversibility() { final String xml = JSONML.toString(originalObject); final JSONObject revertedObject = JSONML.toJSONObject(xml, false); final String newJson = revertedObject.toString(); - assertTrue("original JSON does not equal the new JSON", originalObject.similar(revertedObject)); + assertTrue("JSON Objects are not similar", originalObject.similar(revertedObject)); + assertTrue("JSON Strings are not similar", new JSONObject(originalJson).similar(new JSONObject(newJson))); } // these tests do not pass for the following reasons: diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 4eefea832..ac9a287ec 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -57,7 +57,6 @@ import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; -import org.skyscreamer.jsonassert.JSONAssert; /** * JSONObject, along with JSONArray, are the central classes of the reference app. @@ -2026,8 +2025,10 @@ public void valueToString() { "\"key3\":\"val3\""+ "}"; JSONObject jsonObject = new JSONObject(jsonObjectStr); - JSONAssert.assertEquals("jsonObject valueToString() incorrect", - JSONObject.valueToString(jsonObject), jsonObject.toString(), false); + assertTrue("jsonObject valueToString() incorrect", + new JSONObject(JSONObject.valueToString(jsonObject)) + .similar(new JSONObject(jsonObject.toString())) + ); String jsonArrayStr = "[1,2,3]"; JSONArray jsonArray = new JSONArray(jsonArrayStr); @@ -2037,8 +2038,9 @@ public void valueToString() { map.put("key1", "val1"); map.put("key2", "val2"); map.put("key3", "val3"); - JSONAssert.assertEquals("map valueToString() incorrect", - jsonObject.toString(), JSONObject.valueToString(map), false); + assertTrue("map valueToString() incorrect", + new JSONObject(jsonObject.toString()) + .similar(new JSONObject(JSONObject.valueToString(map)))); Collection collection = new ArrayList(); collection.add(Integer.valueOf(1)); collection.add(Integer.valueOf(2)); From 7b2677ac5a8fdf586ceb4bfe2d6d8ec4194cdd69 Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Sat, 14 Oct 2023 10:05:36 +0530 Subject: [PATCH 046/233] #790 - Update XML with changes for string to number conversion. Moved the code logic to a common utility to de-duplicate. --- src/main/java/org/json/JSONArray.java | 4 +- src/main/java/org/json/JSONObject.java | 99 +--------- .../java/org/json/NumberConversionUtil.java | 142 +++++++++++++++ src/main/java/org/json/XML.java | 80 +-------- src/test/java/org/json/junit/JSONMLTest.java | 2 +- .../json/junit/NumberConversionUtilTest.java | 169 ++++++++++++++++++ .../org/json/junit/XMLConfigurationTest.java | 2 +- src/test/java/org/json/junit/XMLTest.java | 2 +- 8 files changed, 323 insertions(+), 177 deletions(-) create mode 100644 src/main/java/org/json/NumberConversionUtil.java create mode 100644 src/test/java/org/json/junit/NumberConversionUtilTest.java diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index b0c912d1f..ed7982f8a 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -331,7 +331,7 @@ public Number getNumber(int index) throws JSONException { if (object instanceof Number) { return (Number)object; } - return JSONObject.stringToNumber(object.toString()); + return NumberConversionUtil.stringToNumber(object.toString()); } catch (Exception e) { throw wrongValueFormatException(index, "number", object, e); } @@ -1078,7 +1078,7 @@ public Number optNumber(int index, Number defaultValue) { if (val instanceof String) { try { - return JSONObject.stringToNumber((String) val); + return NumberConversionUtil.stringToNumber((String) val); } catch (Exception e) { return defaultValue; } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index fbf225e9f..9b2e3e095 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -28,6 +28,8 @@ import java.util.Set; import java.util.regex.Pattern; +import static org.json.NumberConversionUtil.stringToNumber; + /** * A JSONObject is an unordered collection of name/value pairs. Its external * form is a string wrapped in curly braces with colons between the names and @@ -2380,83 +2382,7 @@ protected static boolean isDecimalNotation(final String val) { || val.indexOf('E') > -1 || "-0".equals(val); } - /** - * Converts a string to a number using the narrowest possible type. Possible - * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. - * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. - * - * @param input value to convert - * @return Number representation of the value. - * @throws NumberFormatException thrown if the value is not a valid number. A public - * caller should catch this and wrap it in a {@link JSONException} if applicable. - */ - protected static Number stringToNumber(final String input) throws NumberFormatException { - String val = input; - if (val.startsWith(".")){ - val = "0"+val; - } - if (val.startsWith("-.")){ - val = "-0."+val.substring(2); - } - char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-' ) { - // decimal representation - if (isDecimalNotation(val)) { - // Use a BigDecimal all the time so we keep the original - // representation. BigDecimal doesn't support -0.0, ensure we - // keep that by forcing a decimal. - try { - BigDecimal bd = new BigDecimal(val); - if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { - return Double.valueOf(-0.0); - } - return bd; - } catch (NumberFormatException retryAsDouble) { - // this is to support "Hex Floats" like this: 0x1.0P-1074 - try { - Double d = Double.valueOf(val); - if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - return d; - } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - } - val = removeLeadingZerosOfNumber(input); - initial = val.charAt(0); - if(initial == '0' && val.length() > 1) { - char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } else if (initial == '-' && val.length() > 2) { - char at1 = val.charAt(1); - char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - // integer representation. - // This will narrow any values to the smallest reasonable Object representation - // (Integer, Long, or BigInteger) - - // BigInteger down conversion: We use a similar bitLength compare as - // BigInteger#intValueExact uses. Increases GC, but objects hold - // only what they need. i.e. Less runtime overhead if the value is - // long lived. - BigInteger bi = new BigInteger(val); - if(bi.bitLength() <= 31){ - return Integer.valueOf(bi.intValue()); - } - if(bi.bitLength() <= 63){ - return Long.valueOf(bi.longValue()); - } - return bi; - } - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } + /** * Try to convert a string into a number, boolean, or null. If the string @@ -2922,23 +2848,4 @@ private static JSONException recursivelyDefinedObjectException(String key) { ); } - /** - * For a prospective number, remove the leading zeros - * @param value prospective number - * @return number without leading zeros - */ - private static String removeLeadingZerosOfNumber(String value){ - if (value.equals("-")){return value;} - boolean negativeFirstChar = (value.charAt(0) == '-'); - int counter = negativeFirstChar ? 1:0; - while (counter < value.length()){ - if (value.charAt(counter) != '0'){ - if (negativeFirstChar) {return "-".concat(value.substring(counter));} - return value.substring(counter); - } - ++counter; - } - if (negativeFirstChar) {return "-0";} - return "0"; - } } diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java new file mode 100644 index 000000000..08da6bdfa --- /dev/null +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -0,0 +1,142 @@ +package org.json; + +import java.math.BigDecimal; +import java.math.BigInteger; + +public class NumberConversionUtil { + + /** + * Converts a string to a number using the narrowest possible type. Possible + * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. + * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. + * + * @param input value to convert + * @return Number representation of the value. + * @throws NumberFormatException thrown if the value is not a valid number. A public + * caller should catch this and wrap it in a {@link JSONException} if applicable. + */ + public static Number stringToNumber(final String input) throws NumberFormatException { + String val = input; + if (val.startsWith(".")){ + val = "0"+val; + } + if (val.startsWith("-.")){ + val = "-0."+val.substring(2); + } + char initial = val.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-' ) { + // decimal representation + if (isDecimalNotation(val)) { + // Use a BigDecimal all the time so we keep the original + // representation. BigDecimal doesn't support -0.0, ensure we + // keep that by forcing a decimal. + try { + BigDecimal bd = new BigDecimal(val); + if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { + return Double.valueOf(-0.0); + } + return bd; + } catch (NumberFormatException retryAsDouble) { + // this is to support "Hex Floats" like this: 0x1.0P-1074 + try { + Double d = Double.valueOf(val); + if(d.isNaN() || d.isInfinite()) { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + return d; + } catch (NumberFormatException ignore) { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } + } + val = removeLeadingZerosOfNumber(input); + initial = val.charAt(0); + if(initial == '0' && val.length() > 1) { + char at1 = val.charAt(1); + if(at1 >= '0' && at1 <= '9') { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } else if (initial == '-' && val.length() > 2) { + char at1 = val.charAt(1); + char at2 = val.charAt(2); + if(at1 == '0' && at2 >= '0' && at2 <= '9') { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } + // integer representation. + // This will narrow any values to the smallest reasonable Object representation + // (Integer, Long, or BigInteger) + + // BigInteger down conversion: We use a similar bitLength compare as + // BigInteger#intValueExact uses. Increases GC, but objects hold + // only what they need. i.e. Less runtime overhead if the value is + // long lived. + BigInteger bi = new BigInteger(val); + if(bi.bitLength() <= 31){ + return Integer.valueOf(bi.intValue()); + } + if(bi.bitLength() <= 63){ + return Long.valueOf(bi.longValue()); + } + return bi; + } + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + + /** + * Checks if the value could be considered a number in decimal number system. + * @param value + * @return + */ + public static boolean potentialNumber(String value){ + if (value == null || value.isEmpty()){ + return false; + } + return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); + } + + /** + * Tests if the value should be tried as a decimal. It makes no test if there are actual digits. + * + * @param val value to test + * @return true if the string is "-0" or if it contains '.', 'e', or 'E', false otherwise. + */ + private static boolean isDecimalNotation(final String val) { + return val.indexOf('.') > -1 || val.indexOf('e') > -1 + || val.indexOf('E') > -1 || "-0".equals(val); + } + + private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ + if (index >= value.length()){ + return false; + } + return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); + } + + private static boolean digitAtIndex(String value, int index){ + if (index >= value.length()){ + return false; + } + return value.charAt(index) >= '0' && value.charAt(index) <= '9'; + } + + /** + * For a prospective number, remove the leading zeros + * @param value prospective number + * @return number without leading zeros + */ + private static String removeLeadingZerosOfNumber(String value){ + if (value.equals("-")){return value;} + boolean negativeFirstChar = (value.charAt(0) == '-'); + int counter = negativeFirstChar ? 1:0; + while (counter < value.length()){ + if (value.charAt(counter) != '0'){ + if (negativeFirstChar) {return "-".concat(value.substring(counter));} + return value.substring(counter); + } + ++counter; + } + if (negativeFirstChar) {return "-0";} + return "0"; + } +} diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 925f056b1..78a3a59dc 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -6,10 +6,11 @@ import java.io.Reader; import java.io.StringReader; -import java.math.BigDecimal; -import java.math.BigInteger; import java.util.Iterator; +import static org.json.NumberConversionUtil.potentialNumber; +import static org.json.NumberConversionUtil.stringToNumber; + /** * This provides static methods to convert an XML text into a JSONObject, and to @@ -486,8 +487,7 @@ public static Object stringToValue(String string) { * produced, then the value will just be a string. */ - char initial = string.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if (potentialNumber(string)) { try { return stringToNumber(string); } catch (Exception ignore) { @@ -496,78 +496,6 @@ public static Object stringToValue(String string) { return string; } - /** - * direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support. - */ - private static Number stringToNumber(final String val) throws NumberFormatException { - char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { - // decimal representation - if (isDecimalNotation(val)) { - // Use a BigDecimal all the time so we keep the original - // representation. BigDecimal doesn't support -0.0, ensure we - // keep that by forcing a decimal. - try { - BigDecimal bd = new BigDecimal(val); - if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { - return Double.valueOf(-0.0); - } - return bd; - } catch (NumberFormatException retryAsDouble) { - // this is to support "Hex Floats" like this: 0x1.0P-1074 - try { - Double d = Double.valueOf(val); - if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); - } - return d; - } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); - } - } - } - // block items like 00 01 etc. Java number parsers treat these as Octal. - if(initial == '0' && val.length() > 1) { - char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); - } - } else if (initial == '-' && val.length() > 2) { - char at1 = val.charAt(1); - char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); - } - } - // integer representation. - // This will narrow any values to the smallest reasonable Object representation - // (Integer, Long, or BigInteger) - - // BigInteger down conversion: We use a similar bitLength compare as - // BigInteger#intValueExact uses. Increases GC, but objects hold - // only what they need. i.e. Less runtime overhead if the value is - // long lived. - BigInteger bi = new BigInteger(val); - if(bi.bitLength() <= 31){ - return Integer.valueOf(bi.intValue()); - } - if(bi.bitLength() <= 63){ - return Long.valueOf(bi.longValue()); - } - return bi; - } - throw new NumberFormatException("val ["+val+"] is not a valid number."); - } - - /** - * direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support. - */ - private static boolean isDecimalNotation(final String val) { - return val.indexOf('.') > -1 || val.indexOf('e') > -1 - || val.indexOf('E') > -1 || "-0".equals(val); - } - - /** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject. Some information may be lost in this transformation because diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 35c0af2c4..ae71aed6a 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -709,7 +709,7 @@ public void commentsInXML() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]"; + final String expectedJsonString = "[\"root\",[\"id\",1],[\"id\",1],[\"id\",0],[\"id\",0],[\"item\",{\"id\":1}],[\"title\",true]]"; final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false); assertEquals(expectedJsonString, actualJsonOutput.toString()); } diff --git a/src/test/java/org/json/junit/NumberConversionUtilTest.java b/src/test/java/org/json/junit/NumberConversionUtilTest.java new file mode 100644 index 000000000..4ac7c8369 --- /dev/null +++ b/src/test/java/org/json/junit/NumberConversionUtilTest.java @@ -0,0 +1,169 @@ +package org.json.junit; + +import org.json.NumberConversionUtil; +import org.junit.Test; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import static org.junit.Assert.*; + +public class NumberConversionUtilTest { + + @Test + public void shouldParseDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("00.10d"); + assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 0, number.longValue(),0); + assertEquals("Do not match", 0, number.intValue(),0); + } + + @Test + public void shouldParseDecimalFractionNumbersWithSingleLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("0.10d"); + assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 0, number.longValue(),0); + assertEquals("Do not match", 0, number.intValue(),0); + } + + + @Test + public void shouldParseDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("0.010d"); + assertEquals("Do not match", 0.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", 0.010f, number.floatValue(),0.0f); + assertEquals("Do not match", 0, number.longValue(),0); + assertEquals("Do not match", 0, number.intValue(),0); + } + + @Test + public void shouldParseMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("00200.10d"); + assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 200, number.longValue(),0); + assertEquals("Do not match", 200, number.intValue(),0); + } + + @Test + public void shouldParseMixedDecimalFractionNumbersWithoutLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("200.10d"); + assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 200, number.longValue(),0); + assertEquals("Do not match", 200, number.intValue(),0); + } + + + @Test + public void shouldParseMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("200.010d"); + assertEquals("Do not match", 200.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", 200.010f, number.floatValue(),0.0f); + assertEquals("Do not match", 200, number.longValue(),0); + assertEquals("Do not match", 200, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("-00.10d"); + assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -0, number.longValue(),0); + assertEquals("Do not match", -0, number.intValue(),0); + } + + @Test + public void shouldParseNegativeDecimalFractionNumbersWithSingleLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("-0.10d"); + assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -0, number.longValue(),0); + assertEquals("Do not match", -0, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("-0.010d"); + assertEquals("Do not match", -0.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", -0.010f, number.floatValue(),0.0f); + assertEquals("Do not match", -0, number.longValue(),0); + assertEquals("Do not match", -0, number.intValue(),0); + } + + @Test + public void shouldParseNegativeMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("-00200.10d"); + assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -200, number.longValue(),0); + assertEquals("Do not match", -200, number.intValue(),0); + } + + @Test + public void shouldParseNegativeMixedDecimalFractionNumbersWithoutLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("-200.10d"); + assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -200, number.longValue(),0); + assertEquals("Do not match", -200, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("-200.010d"); + assertEquals("Do not match", -200.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", -200.010f, number.floatValue(),0.0f); + assertEquals("Do not match", -200, number.longValue(),0); + assertEquals("Do not match", -200, number.intValue(),0); + } + + @Test + public void shouldParseNumbersWithExponents(){ + Number number = NumberConversionUtil.stringToNumber("23.45e7"); + assertEquals("Do not match", 23.45e7d, number.doubleValue(),0.0d); + assertEquals("Do not match", 23.45e7f, number.floatValue(),0.0f); + assertEquals("Do not match", 2.345E8, number.longValue(),0); + assertEquals("Do not match", 2.345E8, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeNumbersWithExponents(){ + Number number = NumberConversionUtil.stringToNumber("-23.45e7"); + assertEquals("Do not match", -23.45e7d, number.doubleValue(),0.0d); + assertEquals("Do not match", -23.45e7f, number.floatValue(),0.0f); + assertEquals("Do not match", -2.345E8, number.longValue(),0); + assertEquals("Do not match", -2.345E8, number.intValue(),0); + } + + @Test + public void shouldParseBigDecimal(){ + Number number = NumberConversionUtil.stringToNumber("19007199254740993.35481234487103587486413587843213584"); + assertTrue(number instanceof BigDecimal); + } + + @Test + public void shouldParseBigInteger(){ + Number number = NumberConversionUtil.stringToNumber("1900719925474099335481234487103587486413587843213584"); + assertTrue(number instanceof BigInteger); + } + + @Test + public void shouldIdentifyPotentialNumber(){ + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112.123")); + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112e123")); + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112.123")); + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112e23")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("--112.123")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("-a112.123")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("a112.123")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("e112.123")); + } + +} \ No newline at end of file diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 21a2b595e..2eaaf99e8 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -733,7 +733,7 @@ public void contentOperations() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); + final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, new XMLParserConfiguration().withKeepStrings(false)); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected); diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 22d6131cb..9702d3dcc 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -791,7 +791,7 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); + final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expectedJson); From 4dfd779b1c84e66e03e9ff13b46b238fd9dc72ea Mon Sep 17 00:00:00 2001 From: simonh5 Date: Sat, 14 Oct 2023 17:21:06 -0500 Subject: [PATCH 047/233] fix: flakiness in org.json.junit.XMLTest#testIndentComplicatedJsonObjectWithArrayAndWithConfig --- src/test/java/org/json/junit/XMLTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 22d6131cb..2399b8161 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1234,7 +1234,7 @@ public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){ for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { expected.append(buffer, 0, numRead); } - assertEquals(expected.toString(), actualString); + assertTrue(XML.toJSONObject(expected.toString()).similar(XML.toJSONObject(actualString))); } } catch (IOException e) { fail("file writer error: " +e.getMessage()); From 8540bb80c07eabe021ecbefce242cb83b6fa4c32 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sun, 15 Oct 2023 20:14:44 -0400 Subject: [PATCH 048/233] Validate that the `mvn package` step completes --- .github/workflows/pipeline.yml | 4 +++- pom.xml | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 5e1dd4251..797b9a247 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -1,5 +1,5 @@ # This workflow will build a Java project with Maven -# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven +# For more information see: https://docs.github.com/en/actions/learn-github-actions or https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions name: Java CI with Maven @@ -47,3 +47,5 @@ jobs: with: name: Test Report ${{ matrix.java }} path: target/site/ + - name: Package Jar ${{ matrix.java }} + run: mvn clean package -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true diff --git a/pom.xml b/pom.xml index 77bbdaca3..2b3c8b243 100644 --- a/pom.xml +++ b/pom.xml @@ -52,7 +52,6 @@ UTF-8 - junit From 134074aeaa8ae47c9782e1e84e19682bd93599ee Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Sun, 15 Oct 2023 20:19:58 -0400 Subject: [PATCH 049/233] Revert "Reverting #761" This reverts commit b180dbedbc99bb177e5b277f1bff2a1b79cebda6. --- pom.xml | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 2b3c8b243..681207385 100644 --- a/pom.xml +++ b/pom.xml @@ -158,17 +158,35 @@ false + + org.moditect + moditect-maven-plugin + 1.0.0.Final + + + add-module-infos + package + + add-module-info + + + 9 + + + org.json + + org.json; + + + + + + + org.apache.maven.plugins maven-jar-plugin 3.3.0 - - - - org.json - - - From 9a9efac2af7068940173bf20efce9419e5206efa Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 13:16:27 -0400 Subject: [PATCH 050/233] Correct moditect configuration to work on java8 --- .gitignore | 2 ++ pom.xml | 11 +++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 7794c4cbe..b78af4db7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # ignore eclipse project files .project .classpath +# ignore vscode files +.vscode # ignore Intellij Idea project files .idea *.iml diff --git a/pom.xml b/pom.xml index 681207385..80c111d5c 100644 --- a/pom.xml +++ b/pom.xml @@ -172,12 +172,11 @@ 9 - - org.json - - org.json; - - + + module org.json { + exports org.json; + } + From 2b41cf44b52b25cae7c189b54f9bab87ff47eda6 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 13:24:40 -0400 Subject: [PATCH 051/233] include jar in job artifacts --- .github/workflows/pipeline.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 797b9a247..59a7658b4 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -37,15 +37,21 @@ jobs: mvn site -DgenerateReports=false -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} - name: Upload Test Results ${{ matrix.java }} if: ${{ always() }} - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v3 with: name: Test Results ${{ matrix.java }} path: target/surefire-reports/ - name: Upload Test Report ${{ matrix.java }} if: ${{ always() }} - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v3 with: name: Test Report ${{ matrix.java }} path: target/site/ - name: Package Jar ${{ matrix.java }} run: mvn clean package -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true + - name: Upload Package Results ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Package Jar ${{ matrix.java }} + path: target/*.jar From be115059e9455d5f434027204b6fe02d4c6b07a9 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 13:19:16 -0400 Subject: [PATCH 052/233] Correct supported java versions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f0134206..cd856ab67 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Project goals include: * No external dependencies * Fast execution and low memory footprint * Maintain backward compatibility -* Designed and tested to use on Java versions 1.6 - 1.11 +* Designed and tested to use on Java versions 1.8 - 17 The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL. From 3894483560216341ea4c9b58ac6c06ab19f6dc4d Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 15:30:55 -0400 Subject: [PATCH 053/233] Add build badges to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index cd856ab67..32b59bb8b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ JSON in Java [package org.json] =============================== [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) +[![Java CI with Maven](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml) +[![CodeQL](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml) **[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20231013/json-20231013.jar)** From 7c4f98c42c5d1ee27d6ce73c9bd0e4e5e3f4aea2 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 17:30:57 -0400 Subject: [PATCH 054/233] Add new deployment pipeline. This should only trigger when a release is published --- .github/workflows/deployment.yml | 40 ++++++++++++++++++++++++++++++++ pom.xml | 7 ++++++ 2 files changed, 47 insertions(+) create mode 100644 .github/workflows/deployment.yml diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml new file mode 100644 index 000000000..d69b4781b --- /dev/null +++ b/.github/workflows/deployment.yml @@ -0,0 +1,40 @@ +# For more information see: https://docs.github.com/en/actions/learn-github-actions or https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions + +name: Deployment workflow + +on: + release: + types: [published] + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v4 + - name: Set up Java for publishing to Maven Central Repository + uses: actions/setup-java@v3 + with: + # Use lowest supported LTS Java version + java-version: '8' + distribution: 'temurin' + server-id: ossrh + server-username: MAVEN_USERNAME + server-password: MAVEN_PASSWORD + - name: Publish to the Maven Central Repository + run: mvn --batch-mode deploy + env: + MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} + MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} + # - name: Set up Java for publishing to GitHub Packages + # uses: actions/setup-java@v3 + # with: + # # Use lowest supported LTS Java version + # java-version: '8' + # distribution: 'temurin' + # - name: Publish to GitHub Packages + # run: mvn --batch-mode deploy + # env: + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/pom.xml b/pom.xml index 77bbdaca3..fb983f09c 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,13 @@ UTF-8 + + + ossrh + Central Repository OSSRH + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + From a86786a5f5c40701b053a6f1b88cd1af3bcaa053 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 18:03:39 -0400 Subject: [PATCH 055/233] Add snapshot repository --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index fb983f09c..fae575bd5 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,10 @@ Central Repository OSSRH https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + From ed183e61427589dea729c42be519ce75437b1dc0 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 18:22:08 -0400 Subject: [PATCH 056/233] remove deprecated parent pom per Sonatype docs --- pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pom.xml b/pom.xml index fae575bd5..7b7754298 100644 --- a/pom.xml +++ b/pom.xml @@ -21,12 +21,6 @@ https://github.com/douglascrockford/JSON-java - - org.sonatype.oss - oss-parent - 9 - - https://github.com/douglascrockford/JSON-java.git scm:git:git://github.com/douglascrockford/JSON-java.git From e8f125fb6ea827448408698b1a50a61984cea6b3 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 16 Oct 2023 18:22:22 -0400 Subject: [PATCH 057/233] update workflow to use GPG --- .github/workflows/deployment.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index d69b4781b..54457d905 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -1,4 +1,8 @@ -# For more information see: https://docs.github.com/en/actions/learn-github-actions or https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions +# For more information see: +# * https://docs.github.com/en/actions/learn-github-actions +# * https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions +# * https://github.com/actions/setup-java/blob/v3.13.0/docs/advanced-usage.md#Publishing-using-Apache-Maven +# name: Deployment workflow @@ -20,14 +24,18 @@ jobs: # Use lowest supported LTS Java version java-version: '8' distribution: 'temurin' - server-id: ossrh - server-username: MAVEN_USERNAME - server-password: MAVEN_PASSWORD + server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml + server-username: MAVEN_USERNAME # env variable for username in deploy + server-password: MAVEN_PASSWORD # env variable for token in deploy + gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import + gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase + - name: Publish to the Maven Central Repository run: mvn --batch-mode deploy env: MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} + MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} # - name: Set up Java for publishing to GitHub Packages # uses: actions/setup-java@v3 # with: From f074bed732dbccccf7e4dd2b4414790249358ffd Mon Sep 17 00:00:00 2001 From: theKnightsOfRohan Date: Mon, 16 Oct 2023 17:48:03 -0700 Subject: [PATCH 058/233] fix(ParserConfiguration): add params to docs --- src/main/java/org/json/ParserConfiguration.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/json/ParserConfiguration.java b/src/main/java/org/json/ParserConfiguration.java index 519e2099d..36fa50833 100644 --- a/src/main/java/org/json/ParserConfiguration.java +++ b/src/main/java/org/json/ParserConfiguration.java @@ -71,7 +71,8 @@ public boolean isKeepStrings() { * * @param newVal * new value to use for the keepStrings configuration option. - * + * @param the type of the configuration object + * * @return The existing configuration will not be modified. A new configuration is returned. */ public T withKeepStrings(final boolean newVal) { @@ -96,6 +97,8 @@ public int getMaxNestingDepth() { * Using any negative value as a parameter is equivalent to setting no limit to the nesting depth, * which means the parses will go as deep as the maximum call stack size allows. * @param maxNestingDepth the maximum nesting depth allowed to the XML parser + * @param the type of the configuration object + * * @return The existing configuration will not be modified. A new configuration is returned. */ public T withMaxNestingDepth(int maxNestingDepth) { From 1d0775cce7a4679fbbb0d8a5bb61dbe21a12f40e Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Thu, 19 Oct 2023 10:28:11 +0530 Subject: [PATCH 059/233] Revert changes with feature and refactor together. --- src/main/java/org/json/JSONArray.java | 4 +- src/main/java/org/json/JSONObject.java | 99 +++++++++- .../java/org/json/NumberConversionUtil.java | 142 --------------- src/main/java/org/json/XML.java | 80 ++++++++- src/test/java/org/json/junit/JSONMLTest.java | 2 +- .../json/junit/NumberConversionUtilTest.java | 169 ------------------ .../org/json/junit/XMLConfigurationTest.java | 2 +- src/test/java/org/json/junit/XMLTest.java | 2 +- 8 files changed, 177 insertions(+), 323 deletions(-) delete mode 100644 src/main/java/org/json/NumberConversionUtil.java delete mode 100644 src/test/java/org/json/junit/NumberConversionUtilTest.java diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index ed7982f8a..b0c912d1f 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -331,7 +331,7 @@ public Number getNumber(int index) throws JSONException { if (object instanceof Number) { return (Number)object; } - return NumberConversionUtil.stringToNumber(object.toString()); + return JSONObject.stringToNumber(object.toString()); } catch (Exception e) { throw wrongValueFormatException(index, "number", object, e); } @@ -1078,7 +1078,7 @@ public Number optNumber(int index, Number defaultValue) { if (val instanceof String) { try { - return NumberConversionUtil.stringToNumber((String) val); + return JSONObject.stringToNumber((String) val); } catch (Exception e) { return defaultValue; } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 9b2e3e095..fbf225e9f 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -28,8 +28,6 @@ import java.util.Set; import java.util.regex.Pattern; -import static org.json.NumberConversionUtil.stringToNumber; - /** * A JSONObject is an unordered collection of name/value pairs. Its external * form is a string wrapped in curly braces with colons between the names and @@ -2382,7 +2380,83 @@ protected static boolean isDecimalNotation(final String val) { || val.indexOf('E') > -1 || "-0".equals(val); } - + /** + * Converts a string to a number using the narrowest possible type. Possible + * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. + * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. + * + * @param input value to convert + * @return Number representation of the value. + * @throws NumberFormatException thrown if the value is not a valid number. A public + * caller should catch this and wrap it in a {@link JSONException} if applicable. + */ + protected static Number stringToNumber(final String input) throws NumberFormatException { + String val = input; + if (val.startsWith(".")){ + val = "0"+val; + } + if (val.startsWith("-.")){ + val = "-0."+val.substring(2); + } + char initial = val.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-' ) { + // decimal representation + if (isDecimalNotation(val)) { + // Use a BigDecimal all the time so we keep the original + // representation. BigDecimal doesn't support -0.0, ensure we + // keep that by forcing a decimal. + try { + BigDecimal bd = new BigDecimal(val); + if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { + return Double.valueOf(-0.0); + } + return bd; + } catch (NumberFormatException retryAsDouble) { + // this is to support "Hex Floats" like this: 0x1.0P-1074 + try { + Double d = Double.valueOf(val); + if(d.isNaN() || d.isInfinite()) { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + return d; + } catch (NumberFormatException ignore) { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } + } + val = removeLeadingZerosOfNumber(input); + initial = val.charAt(0); + if(initial == '0' && val.length() > 1) { + char at1 = val.charAt(1); + if(at1 >= '0' && at1 <= '9') { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } else if (initial == '-' && val.length() > 2) { + char at1 = val.charAt(1); + char at2 = val.charAt(2); + if(at1 == '0' && at2 >= '0' && at2 <= '9') { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } + // integer representation. + // This will narrow any values to the smallest reasonable Object representation + // (Integer, Long, or BigInteger) + + // BigInteger down conversion: We use a similar bitLength compare as + // BigInteger#intValueExact uses. Increases GC, but objects hold + // only what they need. i.e. Less runtime overhead if the value is + // long lived. + BigInteger bi = new BigInteger(val); + if(bi.bitLength() <= 31){ + return Integer.valueOf(bi.intValue()); + } + if(bi.bitLength() <= 63){ + return Long.valueOf(bi.longValue()); + } + return bi; + } + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } /** * Try to convert a string into a number, boolean, or null. If the string @@ -2848,4 +2922,23 @@ private static JSONException recursivelyDefinedObjectException(String key) { ); } + /** + * For a prospective number, remove the leading zeros + * @param value prospective number + * @return number without leading zeros + */ + private static String removeLeadingZerosOfNumber(String value){ + if (value.equals("-")){return value;} + boolean negativeFirstChar = (value.charAt(0) == '-'); + int counter = negativeFirstChar ? 1:0; + while (counter < value.length()){ + if (value.charAt(counter) != '0'){ + if (negativeFirstChar) {return "-".concat(value.substring(counter));} + return value.substring(counter); + } + ++counter; + } + if (negativeFirstChar) {return "-0";} + return "0"; + } } diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java deleted file mode 100644 index 08da6bdfa..000000000 --- a/src/main/java/org/json/NumberConversionUtil.java +++ /dev/null @@ -1,142 +0,0 @@ -package org.json; - -import java.math.BigDecimal; -import java.math.BigInteger; - -public class NumberConversionUtil { - - /** - * Converts a string to a number using the narrowest possible type. Possible - * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. - * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. - * - * @param input value to convert - * @return Number representation of the value. - * @throws NumberFormatException thrown if the value is not a valid number. A public - * caller should catch this and wrap it in a {@link JSONException} if applicable. - */ - public static Number stringToNumber(final String input) throws NumberFormatException { - String val = input; - if (val.startsWith(".")){ - val = "0"+val; - } - if (val.startsWith("-.")){ - val = "-0."+val.substring(2); - } - char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-' ) { - // decimal representation - if (isDecimalNotation(val)) { - // Use a BigDecimal all the time so we keep the original - // representation. BigDecimal doesn't support -0.0, ensure we - // keep that by forcing a decimal. - try { - BigDecimal bd = new BigDecimal(val); - if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { - return Double.valueOf(-0.0); - } - return bd; - } catch (NumberFormatException retryAsDouble) { - // this is to support "Hex Floats" like this: 0x1.0P-1074 - try { - Double d = Double.valueOf(val); - if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - return d; - } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - } - val = removeLeadingZerosOfNumber(input); - initial = val.charAt(0); - if(initial == '0' && val.length() > 1) { - char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } else if (initial == '-' && val.length() > 2) { - char at1 = val.charAt(1); - char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - // integer representation. - // This will narrow any values to the smallest reasonable Object representation - // (Integer, Long, or BigInteger) - - // BigInteger down conversion: We use a similar bitLength compare as - // BigInteger#intValueExact uses. Increases GC, but objects hold - // only what they need. i.e. Less runtime overhead if the value is - // long lived. - BigInteger bi = new BigInteger(val); - if(bi.bitLength() <= 31){ - return Integer.valueOf(bi.intValue()); - } - if(bi.bitLength() <= 63){ - return Long.valueOf(bi.longValue()); - } - return bi; - } - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - - /** - * Checks if the value could be considered a number in decimal number system. - * @param value - * @return - */ - public static boolean potentialNumber(String value){ - if (value == null || value.isEmpty()){ - return false; - } - return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); - } - - /** - * Tests if the value should be tried as a decimal. It makes no test if there are actual digits. - * - * @param val value to test - * @return true if the string is "-0" or if it contains '.', 'e', or 'E', false otherwise. - */ - private static boolean isDecimalNotation(final String val) { - return val.indexOf('.') > -1 || val.indexOf('e') > -1 - || val.indexOf('E') > -1 || "-0".equals(val); - } - - private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ - if (index >= value.length()){ - return false; - } - return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); - } - - private static boolean digitAtIndex(String value, int index){ - if (index >= value.length()){ - return false; - } - return value.charAt(index) >= '0' && value.charAt(index) <= '9'; - } - - /** - * For a prospective number, remove the leading zeros - * @param value prospective number - * @return number without leading zeros - */ - private static String removeLeadingZerosOfNumber(String value){ - if (value.equals("-")){return value;} - boolean negativeFirstChar = (value.charAt(0) == '-'); - int counter = negativeFirstChar ? 1:0; - while (counter < value.length()){ - if (value.charAt(counter) != '0'){ - if (negativeFirstChar) {return "-".concat(value.substring(counter));} - return value.substring(counter); - } - ++counter; - } - if (negativeFirstChar) {return "-0";} - return "0"; - } -} diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 78a3a59dc..925f056b1 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -6,11 +6,10 @@ import java.io.Reader; import java.io.StringReader; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Iterator; -import static org.json.NumberConversionUtil.potentialNumber; -import static org.json.NumberConversionUtil.stringToNumber; - /** * This provides static methods to convert an XML text into a JSONObject, and to @@ -487,7 +486,8 @@ public static Object stringToValue(String string) { * produced, then the value will just be a string. */ - if (potentialNumber(string)) { + char initial = string.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-') { try { return stringToNumber(string); } catch (Exception ignore) { @@ -496,6 +496,78 @@ public static Object stringToValue(String string) { return string; } + /** + * direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support. + */ + private static Number stringToNumber(final String val) throws NumberFormatException { + char initial = val.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-') { + // decimal representation + if (isDecimalNotation(val)) { + // Use a BigDecimal all the time so we keep the original + // representation. BigDecimal doesn't support -0.0, ensure we + // keep that by forcing a decimal. + try { + BigDecimal bd = new BigDecimal(val); + if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { + return Double.valueOf(-0.0); + } + return bd; + } catch (NumberFormatException retryAsDouble) { + // this is to support "Hex Floats" like this: 0x1.0P-1074 + try { + Double d = Double.valueOf(val); + if(d.isNaN() || d.isInfinite()) { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + return d; + } catch (NumberFormatException ignore) { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } + } + // block items like 00 01 etc. Java number parsers treat these as Octal. + if(initial == '0' && val.length() > 1) { + char at1 = val.charAt(1); + if(at1 >= '0' && at1 <= '9') { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } else if (initial == '-' && val.length() > 2) { + char at1 = val.charAt(1); + char at2 = val.charAt(2); + if(at1 == '0' && at2 >= '0' && at2 <= '9') { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } + // integer representation. + // This will narrow any values to the smallest reasonable Object representation + // (Integer, Long, or BigInteger) + + // BigInteger down conversion: We use a similar bitLength compare as + // BigInteger#intValueExact uses. Increases GC, but objects hold + // only what they need. i.e. Less runtime overhead if the value is + // long lived. + BigInteger bi = new BigInteger(val); + if(bi.bitLength() <= 31){ + return Integer.valueOf(bi.intValue()); + } + if(bi.bitLength() <= 63){ + return Long.valueOf(bi.longValue()); + } + return bi; + } + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + + /** + * direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support. + */ + private static boolean isDecimalNotation(final String val) { + return val.indexOf('.') > -1 || val.indexOf('e') > -1 + || val.indexOf('E') > -1 || "-0".equals(val); + } + + /** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject. Some information may be lost in this transformation because diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index ae71aed6a..35c0af2c4 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -709,7 +709,7 @@ public void commentsInXML() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final String expectedJsonString = "[\"root\",[\"id\",1],[\"id\",1],[\"id\",0],[\"id\",0],[\"item\",{\"id\":1}],[\"title\",true]]"; + final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]"; final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false); assertEquals(expectedJsonString, actualJsonOutput.toString()); } diff --git a/src/test/java/org/json/junit/NumberConversionUtilTest.java b/src/test/java/org/json/junit/NumberConversionUtilTest.java deleted file mode 100644 index 4ac7c8369..000000000 --- a/src/test/java/org/json/junit/NumberConversionUtilTest.java +++ /dev/null @@ -1,169 +0,0 @@ -package org.json.junit; - -import org.json.NumberConversionUtil; -import org.junit.Test; - -import java.math.BigDecimal; -import java.math.BigInteger; - -import static org.junit.Assert.*; - -public class NumberConversionUtilTest { - - @Test - public void shouldParseDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("00.10d"); - assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 0, number.longValue(),0); - assertEquals("Do not match", 0, number.intValue(),0); - } - - @Test - public void shouldParseDecimalFractionNumbersWithSingleLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("0.10d"); - assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 0, number.longValue(),0); - assertEquals("Do not match", 0, number.intValue(),0); - } - - - @Test - public void shouldParseDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("0.010d"); - assertEquals("Do not match", 0.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", 0.010f, number.floatValue(),0.0f); - assertEquals("Do not match", 0, number.longValue(),0); - assertEquals("Do not match", 0, number.intValue(),0); - } - - @Test - public void shouldParseMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("00200.10d"); - assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 200, number.longValue(),0); - assertEquals("Do not match", 200, number.intValue(),0); - } - - @Test - public void shouldParseMixedDecimalFractionNumbersWithoutLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("200.10d"); - assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 200, number.longValue(),0); - assertEquals("Do not match", 200, number.intValue(),0); - } - - - @Test - public void shouldParseMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("200.010d"); - assertEquals("Do not match", 200.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", 200.010f, number.floatValue(),0.0f); - assertEquals("Do not match", 200, number.longValue(),0); - assertEquals("Do not match", 200, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("-00.10d"); - assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -0, number.longValue(),0); - assertEquals("Do not match", -0, number.intValue(),0); - } - - @Test - public void shouldParseNegativeDecimalFractionNumbersWithSingleLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("-0.10d"); - assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -0, number.longValue(),0); - assertEquals("Do not match", -0, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("-0.010d"); - assertEquals("Do not match", -0.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", -0.010f, number.floatValue(),0.0f); - assertEquals("Do not match", -0, number.longValue(),0); - assertEquals("Do not match", -0, number.intValue(),0); - } - - @Test - public void shouldParseNegativeMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("-00200.10d"); - assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -200, number.longValue(),0); - assertEquals("Do not match", -200, number.intValue(),0); - } - - @Test - public void shouldParseNegativeMixedDecimalFractionNumbersWithoutLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("-200.10d"); - assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -200, number.longValue(),0); - assertEquals("Do not match", -200, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("-200.010d"); - assertEquals("Do not match", -200.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", -200.010f, number.floatValue(),0.0f); - assertEquals("Do not match", -200, number.longValue(),0); - assertEquals("Do not match", -200, number.intValue(),0); - } - - @Test - public void shouldParseNumbersWithExponents(){ - Number number = NumberConversionUtil.stringToNumber("23.45e7"); - assertEquals("Do not match", 23.45e7d, number.doubleValue(),0.0d); - assertEquals("Do not match", 23.45e7f, number.floatValue(),0.0f); - assertEquals("Do not match", 2.345E8, number.longValue(),0); - assertEquals("Do not match", 2.345E8, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeNumbersWithExponents(){ - Number number = NumberConversionUtil.stringToNumber("-23.45e7"); - assertEquals("Do not match", -23.45e7d, number.doubleValue(),0.0d); - assertEquals("Do not match", -23.45e7f, number.floatValue(),0.0f); - assertEquals("Do not match", -2.345E8, number.longValue(),0); - assertEquals("Do not match", -2.345E8, number.intValue(),0); - } - - @Test - public void shouldParseBigDecimal(){ - Number number = NumberConversionUtil.stringToNumber("19007199254740993.35481234487103587486413587843213584"); - assertTrue(number instanceof BigDecimal); - } - - @Test - public void shouldParseBigInteger(){ - Number number = NumberConversionUtil.stringToNumber("1900719925474099335481234487103587486413587843213584"); - assertTrue(number instanceof BigInteger); - } - - @Test - public void shouldIdentifyPotentialNumber(){ - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112.123")); - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112e123")); - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112.123")); - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112e23")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("--112.123")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("-a112.123")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("a112.123")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("e112.123")); - } - -} \ No newline at end of file diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 2eaaf99e8..21a2b595e 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -733,7 +733,7 @@ public void contentOperations() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); + final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, new XMLParserConfiguration().withKeepStrings(false)); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected); diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 9702d3dcc..22d6131cb 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -791,7 +791,7 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); + final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expectedJson); From 2374766018f10318605fa4b6991c398923093c1f Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Thu, 19 Oct 2023 14:07:53 +0530 Subject: [PATCH 060/233] #790 - Update XML with changes for string to number conversion. For now the code remains duplicated in JSON and XML parsers. Unit test cases updated to comply with number expectations. --- src/main/java/org/json/XML.java | 60 ++++++++++++++++--- src/test/java/org/json/junit/JSONMLTest.java | 2 +- .../org/json/junit/XMLConfigurationTest.java | 2 +- src/test/java/org/json/junit/XMLTest.java | 2 +- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 925f056b1..533192313 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -486,8 +486,7 @@ public static Object stringToValue(String string) { * produced, then the value will just be a string. */ - char initial = string.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { + if (potentialNumber(string)) { try { return stringToNumber(string); } catch (Exception ignore) { @@ -496,10 +495,38 @@ public static Object stringToValue(String string) { return string; } + private static boolean potentialNumber(String value){ + if (value == null || value.isEmpty()){ + return false; + } + return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); + } + + private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ + if (index >= value.length()){ + return false; + } + return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); + } + + private static boolean digitAtIndex(String value, int index){ + if (index >= value.length()){ + return false; + } + return value.charAt(index) >= '0' && value.charAt(index) <= '9'; + } + /** * direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support. */ - private static Number stringToNumber(final String val) throws NumberFormatException { + private static Number stringToNumber(final String input) throws NumberFormatException { + String val = input; + if (val.startsWith(".")){ + val = "0"+val; + } + if (val.startsWith("-.")){ + val = "-0."+val.substring(2); + } char initial = val.charAt(0); if ((initial >= '0' && initial <= '9') || initial == '-') { // decimal representation @@ -518,25 +545,25 @@ private static Number stringToNumber(final String val) throws NumberFormatExcept try { Double d = Double.valueOf(val); if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } return d; } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } } - // block items like 00 01 etc. Java number parsers treat these as Octal. + val = removeLeadingZerosOfNumber(input); if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } else if (initial == '-' && val.length() > 2) { char at1 = val.charAt(1); char at2 = val.charAt(2); if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } } // integer representation. @@ -556,7 +583,7 @@ private static Number stringToNumber(final String val) throws NumberFormatExcept } return bi; } - throw new NumberFormatException("val ["+val+"] is not a valid number."); + throw new NumberFormatException("val ["+input+"] is not a valid number."); } /** @@ -973,4 +1000,19 @@ private static final String indent(int indent) { } return sb.toString(); } + + private static String removeLeadingZerosOfNumber(String value){ + if (value.equals("-")){return value;} + boolean negativeFirstChar = (value.charAt(0) == '-'); + int counter = negativeFirstChar ? 1:0; + while (counter < value.length()){ + if (value.charAt(counter) != '0'){ + if (negativeFirstChar) {return "-".concat(value.substring(counter));} + return value.substring(counter); + } + ++counter; + } + if (negativeFirstChar) {return "-0";} + return "0"; + } } diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 35c0af2c4..ae71aed6a 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -709,7 +709,7 @@ public void commentsInXML() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]"; + final String expectedJsonString = "[\"root\",[\"id\",1],[\"id\",1],[\"id\",0],[\"id\",0],[\"item\",{\"id\":1}],[\"title\",true]]"; final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false); assertEquals(expectedJsonString, actualJsonOutput.toString()); } diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 21a2b595e..2eaaf99e8 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -733,7 +733,7 @@ public void contentOperations() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); + final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, new XMLParserConfiguration().withKeepStrings(false)); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected); diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 22d6131cb..9702d3dcc 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -791,7 +791,7 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); + final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expectedJson); From de745e9c810c2d121437aee492624d928e6f71a0 Mon Sep 17 00:00:00 2001 From: Yeikel Date: Mon, 16 Oct 2023 12:38:42 -0400 Subject: [PATCH 061/233] ci: test with Java 21 --- .github/workflows/pipeline.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 5e1dd4251..8b4c87e8a 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: # build against supported Java LTS versions: - java: [ 8, 11, 17 ] + java: [ 8, 11, 17, 21 ] name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v3 diff --git a/README.md b/README.md index 9f0134206..a351a48d6 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Project goals include: * No external dependencies * Fast execution and low memory footprint * Maintain backward compatibility -* Designed and tested to use on Java versions 1.6 - 1.11 +* Designed and tested to use on Java versions 1.8 - 21 The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL. From 6007165c17f349b81787657d104c967ef5dcc9ae Mon Sep 17 00:00:00 2001 From: Yeikel Date: Sat, 21 Oct 2023 00:10:42 -0400 Subject: [PATCH 062/233] docs: use syntax highlighting use syntax highlighting to improve the format of the readme --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5f1f5b0bd..1db1f54f4 100644 --- a/README.md +++ b/README.md @@ -44,24 +44,24 @@ The org.json package can be built from the command line, Maven, and Gradle. The **Building from the command line** *Build the class files from the package root directory src/main/java* -```` +```shell javac org/json/*.java -```` +``` *Create the jar file in the current directory* -```` +```shell jar cf json-java.jar org/json/*.class -```` +``` *Compile a program that uses the jar (see example code below)* -```` +```shell javac -cp .;json-java.jar Test.java (Windows) javac -cp .:json-java.jar Test.java (Unix Systems) -```` +``` *Test file contents* -```` +```java import org.json.JSONObject; public class Test { public static void main(String args[]){ @@ -69,31 +69,31 @@ public class Test { System.out.println(jo.toString()); } } -```` +``` *Execute the Test file* -```` +```shell java -cp .;json-java.jar Test (Windows) java -cp .:json-java.jar Test (Unix Systems) -```` +``` *Expected output* -```` +```json {"abc":"def"} -```` +``` **Tools to build the package and execute the unit tests** Execute the test suite with Maven: -``` +```shell mvn clean test ``` Execute the test suite with Gradlew: -``` +```shell gradlew clean build test ``` From 98b79ae7bf182397dded8a669c2432d618d03a07 Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Mon, 23 Oct 2023 19:16:25 +0530 Subject: [PATCH 063/233] #813 - moved number conversion related common changes to utility static method. --- src/main/java/org/json/JSONArray.java | 4 +- src/main/java/org/json/JSONObject.java | 120 +----------- .../java/org/json/NumberConversionUtil.java | 142 ++++++++++++++ src/main/java/org/json/XML.java | 112 +---------- .../json/junit/NumberConversionUtilTest.java | 175 ++++++++++++++++++ 5 files changed, 326 insertions(+), 227 deletions(-) create mode 100644 src/main/java/org/json/NumberConversionUtil.java create mode 100644 src/test/java/org/json/junit/NumberConversionUtilTest.java diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index b0c912d1f..ed7982f8a 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -331,7 +331,7 @@ public Number getNumber(int index) throws JSONException { if (object instanceof Number) { return (Number)object; } - return JSONObject.stringToNumber(object.toString()); + return NumberConversionUtil.stringToNumber(object.toString()); } catch (Exception e) { throw wrongValueFormatException(index, "number", object, e); } @@ -1078,7 +1078,7 @@ public Number optNumber(int index, Number defaultValue) { if (val instanceof String) { try { - return JSONObject.stringToNumber((String) val); + return NumberConversionUtil.stringToNumber((String) val); } catch (Exception e) { return defaultValue; } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index fbf225e9f..7f4885e00 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -28,6 +28,9 @@ import java.util.Set; import java.util.regex.Pattern; +import static org.json.NumberConversionUtil.potentialNumber; +import static org.json.NumberConversionUtil.stringToNumber; + /** * A JSONObject is an unordered collection of name/value pairs. Its external * form is a string wrapped in curly braces with colons between the names and @@ -2380,84 +2383,6 @@ protected static boolean isDecimalNotation(final String val) { || val.indexOf('E') > -1 || "-0".equals(val); } - /** - * Converts a string to a number using the narrowest possible type. Possible - * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. - * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. - * - * @param input value to convert - * @return Number representation of the value. - * @throws NumberFormatException thrown if the value is not a valid number. A public - * caller should catch this and wrap it in a {@link JSONException} if applicable. - */ - protected static Number stringToNumber(final String input) throws NumberFormatException { - String val = input; - if (val.startsWith(".")){ - val = "0"+val; - } - if (val.startsWith("-.")){ - val = "-0."+val.substring(2); - } - char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-' ) { - // decimal representation - if (isDecimalNotation(val)) { - // Use a BigDecimal all the time so we keep the original - // representation. BigDecimal doesn't support -0.0, ensure we - // keep that by forcing a decimal. - try { - BigDecimal bd = new BigDecimal(val); - if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { - return Double.valueOf(-0.0); - } - return bd; - } catch (NumberFormatException retryAsDouble) { - // this is to support "Hex Floats" like this: 0x1.0P-1074 - try { - Double d = Double.valueOf(val); - if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - return d; - } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - } - val = removeLeadingZerosOfNumber(input); - initial = val.charAt(0); - if(initial == '0' && val.length() > 1) { - char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } else if (initial == '-' && val.length() > 2) { - char at1 = val.charAt(1); - char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - // integer representation. - // This will narrow any values to the smallest reasonable Object representation - // (Integer, Long, or BigInteger) - - // BigInteger down conversion: We use a similar bitLength compare as - // BigInteger#intValueExact uses. Increases GC, but objects hold - // only what they need. i.e. Less runtime overhead if the value is - // long lived. - BigInteger bi = new BigInteger(val); - if(bi.bitLength() <= 31){ - return Integer.valueOf(bi.intValue()); - } - if(bi.bitLength() <= 63){ - return Long.valueOf(bi.longValue()); - } - return bi; - } - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - /** * Try to convert a string into a number, boolean, or null. If the string * can't be converted, return the string. @@ -2501,26 +2426,7 @@ public static Object stringToValue(String string) { } - private static boolean potentialNumber(String value){ - if (value == null || value.isEmpty()){ - return false; - } - return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); - } - - private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ - if (index >= value.length()){ - return false; - } - return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); - } - private static boolean digitAtIndex(String value, int index){ - if (index >= value.length()){ - return false; - } - return value.charAt(index) >= '0' && value.charAt(index) <= '9'; - } /** * Throw an exception if the object is a NaN or infinite number. @@ -2922,23 +2828,5 @@ private static JSONException recursivelyDefinedObjectException(String key) { ); } - /** - * For a prospective number, remove the leading zeros - * @param value prospective number - * @return number without leading zeros - */ - private static String removeLeadingZerosOfNumber(String value){ - if (value.equals("-")){return value;} - boolean negativeFirstChar = (value.charAt(0) == '-'); - int counter = negativeFirstChar ? 1:0; - while (counter < value.length()){ - if (value.charAt(counter) != '0'){ - if (negativeFirstChar) {return "-".concat(value.substring(counter));} - return value.substring(counter); - } - ++counter; - } - if (negativeFirstChar) {return "-0";} - return "0"; - } + } diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java new file mode 100644 index 000000000..08da6bdfa --- /dev/null +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -0,0 +1,142 @@ +package org.json; + +import java.math.BigDecimal; +import java.math.BigInteger; + +public class NumberConversionUtil { + + /** + * Converts a string to a number using the narrowest possible type. Possible + * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. + * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. + * + * @param input value to convert + * @return Number representation of the value. + * @throws NumberFormatException thrown if the value is not a valid number. A public + * caller should catch this and wrap it in a {@link JSONException} if applicable. + */ + public static Number stringToNumber(final String input) throws NumberFormatException { + String val = input; + if (val.startsWith(".")){ + val = "0"+val; + } + if (val.startsWith("-.")){ + val = "-0."+val.substring(2); + } + char initial = val.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-' ) { + // decimal representation + if (isDecimalNotation(val)) { + // Use a BigDecimal all the time so we keep the original + // representation. BigDecimal doesn't support -0.0, ensure we + // keep that by forcing a decimal. + try { + BigDecimal bd = new BigDecimal(val); + if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { + return Double.valueOf(-0.0); + } + return bd; + } catch (NumberFormatException retryAsDouble) { + // this is to support "Hex Floats" like this: 0x1.0P-1074 + try { + Double d = Double.valueOf(val); + if(d.isNaN() || d.isInfinite()) { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + return d; + } catch (NumberFormatException ignore) { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } + } + val = removeLeadingZerosOfNumber(input); + initial = val.charAt(0); + if(initial == '0' && val.length() > 1) { + char at1 = val.charAt(1); + if(at1 >= '0' && at1 <= '9') { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } else if (initial == '-' && val.length() > 2) { + char at1 = val.charAt(1); + char at2 = val.charAt(2); + if(at1 == '0' && at2 >= '0' && at2 <= '9') { + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + } + // integer representation. + // This will narrow any values to the smallest reasonable Object representation + // (Integer, Long, or BigInteger) + + // BigInteger down conversion: We use a similar bitLength compare as + // BigInteger#intValueExact uses. Increases GC, but objects hold + // only what they need. i.e. Less runtime overhead if the value is + // long lived. + BigInteger bi = new BigInteger(val); + if(bi.bitLength() <= 31){ + return Integer.valueOf(bi.intValue()); + } + if(bi.bitLength() <= 63){ + return Long.valueOf(bi.longValue()); + } + return bi; + } + throw new NumberFormatException("val ["+input+"] is not a valid number."); + } + + /** + * Checks if the value could be considered a number in decimal number system. + * @param value + * @return + */ + public static boolean potentialNumber(String value){ + if (value == null || value.isEmpty()){ + return false; + } + return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); + } + + /** + * Tests if the value should be tried as a decimal. It makes no test if there are actual digits. + * + * @param val value to test + * @return true if the string is "-0" or if it contains '.', 'e', or 'E', false otherwise. + */ + private static boolean isDecimalNotation(final String val) { + return val.indexOf('.') > -1 || val.indexOf('e') > -1 + || val.indexOf('E') > -1 || "-0".equals(val); + } + + private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ + if (index >= value.length()){ + return false; + } + return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); + } + + private static boolean digitAtIndex(String value, int index){ + if (index >= value.length()){ + return false; + } + return value.charAt(index) >= '0' && value.charAt(index) <= '9'; + } + + /** + * For a prospective number, remove the leading zeros + * @param value prospective number + * @return number without leading zeros + */ + private static String removeLeadingZerosOfNumber(String value){ + if (value.equals("-")){return value;} + boolean negativeFirstChar = (value.charAt(0) == '-'); + int counter = negativeFirstChar ? 1:0; + while (counter < value.length()){ + if (value.charAt(counter) != '0'){ + if (negativeFirstChar) {return "-".concat(value.substring(counter));} + return value.substring(counter); + } + ++counter; + } + if (negativeFirstChar) {return "-0";} + return "0"; + } +} diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 533192313..5105a9e9e 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -10,6 +10,9 @@ import java.math.BigInteger; import java.util.Iterator; +import static org.json.NumberConversionUtil.potentialNumber; +import static org.json.NumberConversionUtil.stringToNumber; + /** * This provides static methods to convert an XML text into a JSONObject, and to @@ -495,104 +498,9 @@ public static Object stringToValue(String string) { return string; } - private static boolean potentialNumber(String value){ - if (value == null || value.isEmpty()){ - return false; - } - return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); - } - private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ - if (index >= value.length()){ - return false; - } - return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); - } - private static boolean digitAtIndex(String value, int index){ - if (index >= value.length()){ - return false; - } - return value.charAt(index) >= '0' && value.charAt(index) <= '9'; - } - /** - * direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support. - */ - private static Number stringToNumber(final String input) throws NumberFormatException { - String val = input; - if (val.startsWith(".")){ - val = "0"+val; - } - if (val.startsWith("-.")){ - val = "-0."+val.substring(2); - } - char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-') { - // decimal representation - if (isDecimalNotation(val)) { - // Use a BigDecimal all the time so we keep the original - // representation. BigDecimal doesn't support -0.0, ensure we - // keep that by forcing a decimal. - try { - BigDecimal bd = new BigDecimal(val); - if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { - return Double.valueOf(-0.0); - } - return bd; - } catch (NumberFormatException retryAsDouble) { - // this is to support "Hex Floats" like this: 0x1.0P-1074 - try { - Double d = Double.valueOf(val); - if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - return d; - } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - } - val = removeLeadingZerosOfNumber(input); - if(initial == '0' && val.length() > 1) { - char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } else if (initial == '-' && val.length() > 2) { - char at1 = val.charAt(1); - char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - // integer representation. - // This will narrow any values to the smallest reasonable Object representation - // (Integer, Long, or BigInteger) - - // BigInteger down conversion: We use a similar bitLength compare as - // BigInteger#intValueExact uses. Increases GC, but objects hold - // only what they need. i.e. Less runtime overhead if the value is - // long lived. - BigInteger bi = new BigInteger(val); - if(bi.bitLength() <= 31){ - return Integer.valueOf(bi.intValue()); - } - if(bi.bitLength() <= 63){ - return Long.valueOf(bi.longValue()); - } - return bi; - } - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - - /** - * direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support. - */ - private static boolean isDecimalNotation(final String val) { - return val.indexOf('.') > -1 || val.indexOf('e') > -1 - || val.indexOf('E') > -1 || "-0".equals(val); - } /** @@ -1001,18 +909,4 @@ private static final String indent(int indent) { return sb.toString(); } - private static String removeLeadingZerosOfNumber(String value){ - if (value.equals("-")){return value;} - boolean negativeFirstChar = (value.charAt(0) == '-'); - int counter = negativeFirstChar ? 1:0; - while (counter < value.length()){ - if (value.charAt(counter) != '0'){ - if (negativeFirstChar) {return "-".concat(value.substring(counter));} - return value.substring(counter); - } - ++counter; - } - if (negativeFirstChar) {return "-0";} - return "0"; - } } diff --git a/src/test/java/org/json/junit/NumberConversionUtilTest.java b/src/test/java/org/json/junit/NumberConversionUtilTest.java new file mode 100644 index 000000000..bf2586d40 --- /dev/null +++ b/src/test/java/org/json/junit/NumberConversionUtilTest.java @@ -0,0 +1,175 @@ +package org.json.junit; + +import org.json.NumberConversionUtil; +import org.junit.Test; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import static org.junit.Assert.*; + +public class NumberConversionUtilTest { + + @Test + public void shouldParseDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("00.10d"); + assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 0, number.longValue(),0); + assertEquals("Do not match", 0, number.intValue(),0); + } + + @Test + public void shouldParseDecimalFractionNumbersWithSingleLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("0.10d"); + assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 0, number.longValue(),0); + assertEquals("Do not match", 0, number.intValue(),0); + } + + + @Test + public void shouldParseDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("0.010d"); + assertEquals("Do not match", 0.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", 0.010f, number.floatValue(),0.0f); + assertEquals("Do not match", 0, number.longValue(),0); + assertEquals("Do not match", 0, number.intValue(),0); + } + + @Test + public void shouldParseMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("00200.10d"); + assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 200, number.longValue(),0); + assertEquals("Do not match", 200, number.intValue(),0); + } + + @Test + public void shouldParseMixedDecimalFractionNumbersWithoutLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("200.10d"); + assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", 200, number.longValue(),0); + assertEquals("Do not match", 200, number.intValue(),0); + } + + + @Test + public void shouldParseMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("200.010d"); + assertEquals("Do not match", 200.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", 200.010f, number.floatValue(),0.0f); + assertEquals("Do not match", 200, number.longValue(),0); + assertEquals("Do not match", 200, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("-00.10d"); + assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -0, number.longValue(),0); + assertEquals("Do not match", -0, number.intValue(),0); + } + + @Test + public void shouldParseNegativeDecimalFractionNumbersWithSingleLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("-0.10d"); + assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -0, number.longValue(),0); + assertEquals("Do not match", -0, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("-0.010d"); + assertEquals("Do not match", -0.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", -0.010f, number.floatValue(),0.0f); + assertEquals("Do not match", -0, number.longValue(),0); + assertEquals("Do not match", -0, number.intValue(),0); + } + + @Test + public void shouldParseNegativeMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ + Number number = NumberConversionUtil.stringToNumber("-00200.10d"); + assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -200, number.longValue(),0); + assertEquals("Do not match", -200, number.intValue(),0); + } + + @Test + public void shouldParseNegativeMixedDecimalFractionNumbersWithoutLeadingZero(){ + Number number = NumberConversionUtil.stringToNumber("-200.10d"); + assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); + assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); + assertEquals("Do not match", -200, number.longValue(),0); + assertEquals("Do not match", -200, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ + Number number = NumberConversionUtil.stringToNumber("-200.010d"); + assertEquals("Do not match", -200.010d, number.doubleValue(),0.0d); + assertEquals("Do not match", -200.010f, number.floatValue(),0.0f); + assertEquals("Do not match", -200, number.longValue(),0); + assertEquals("Do not match", -200, number.intValue(),0); + } + + @Test + public void shouldParseNumbersWithExponents(){ + Number number = NumberConversionUtil.stringToNumber("23.45e7"); + assertEquals("Do not match", 23.45e7d, number.doubleValue(),0.0d); + assertEquals("Do not match", 23.45e7f, number.floatValue(),0.0f); + assertEquals("Do not match", 2.345E8, number.longValue(),0); + assertEquals("Do not match", 2.345E8, number.intValue(),0); + } + + + @Test + public void shouldParseNegativeNumbersWithExponents(){ + Number number = NumberConversionUtil.stringToNumber("-23.45e7"); + assertEquals("Do not match", -23.45e7d, number.doubleValue(),0.0d); + assertEquals("Do not match", -23.45e7f, number.floatValue(),0.0f); + assertEquals("Do not match", -2.345E8, number.longValue(),0); + assertEquals("Do not match", -2.345E8, number.intValue(),0); + } + + @Test + public void shouldParseBigDecimal(){ + Number number = NumberConversionUtil.stringToNumber("19007199254740993.35481234487103587486413587843213584"); + assertTrue(number instanceof BigDecimal); + } + + @Test + public void shouldParseBigInteger(){ + Number number = NumberConversionUtil.stringToNumber("1900719925474099335481234487103587486413587843213584"); + assertTrue(number instanceof BigInteger); + } + + @Test + public void shouldIdentifyPotentialNumber(){ + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112.123")); + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112e123")); + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112.123")); + assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112e23")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("--112.123")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("-a112.123")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("a112.123")); + assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("e112.123")); + } + + @Test(expected = NumberFormatException.class) + public void shouldExpectExceptionWhenNumberIsNotFormatted(){ + NumberConversionUtil.stringToNumber("112.aa123"); + } + + +} \ No newline at end of file From 5539722c693d4b19ec61fce4d584af5f0e37c86d Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Mon, 23 Oct 2023 23:03:35 +0530 Subject: [PATCH 064/233] #813 - address PR review comment - brought down visibility. --- src/main/java/org/json/NumberConversionUtil.java | 6 +++--- .../java/org/json/{junit => }/NumberConversionUtilTest.java | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) rename src/test/java/org/json/{junit => }/NumberConversionUtilTest.java (99%) diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java index 08da6bdfa..d53c5e277 100644 --- a/src/main/java/org/json/NumberConversionUtil.java +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -3,7 +3,7 @@ import java.math.BigDecimal; import java.math.BigInteger; -public class NumberConversionUtil { +class NumberConversionUtil { /** * Converts a string to a number using the narrowest possible type. Possible @@ -15,7 +15,7 @@ public class NumberConversionUtil { * @throws NumberFormatException thrown if the value is not a valid number. A public * caller should catch this and wrap it in a {@link JSONException} if applicable. */ - public static Number stringToNumber(final String input) throws NumberFormatException { + static Number stringToNumber(final String input) throws NumberFormatException { String val = input; if (val.startsWith(".")){ val = "0"+val; @@ -88,7 +88,7 @@ public static Number stringToNumber(final String input) throws NumberFormatExcep * @param value * @return */ - public static boolean potentialNumber(String value){ + static boolean potentialNumber(String value){ if (value == null || value.isEmpty()){ return false; } diff --git a/src/test/java/org/json/junit/NumberConversionUtilTest.java b/src/test/java/org/json/NumberConversionUtilTest.java similarity index 99% rename from src/test/java/org/json/junit/NumberConversionUtilTest.java rename to src/test/java/org/json/NumberConversionUtilTest.java index bf2586d40..0fedcebb9 100644 --- a/src/test/java/org/json/junit/NumberConversionUtilTest.java +++ b/src/test/java/org/json/NumberConversionUtilTest.java @@ -1,6 +1,5 @@ -package org.json.junit; +package org.json; -import org.json.NumberConversionUtil; import org.junit.Test; import java.math.BigDecimal; From 1ab11d08024f9d815772811c3ebdd110ad228ce8 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 23 Oct 2023 14:50:21 -0400 Subject: [PATCH 065/233] ensure java 6 compatable --- .github/workflows/pipeline.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index a66ae7b8d..39d23e23b 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -10,6 +10,34 @@ on: branches: [ master ] jobs: + # old-school build and jar method. No tests run or compiled. + build-1_6: + runs-on: ubuntu-latest + strategy: + matrix: + # build for java 1.6, however don't run any tests + java: [ 1.6 ] + name: Java ${{ matrix.java }} + steps: + - uses: actions/checkout@v3 + - name: Setup java + uses: actions/setup-java@v1 + with: + java-version: ${{ matrix.java }} + - name: Compile Java ${{ matrix.java }} + run: | + mkdir -p target/classes + javac -version + javac -d target/classes/ src/main/java/org/json/*.java + - name: Create java ${{ matrix.java }} JAR + run: | + jar cvf target/org.json.jar -C target/classes . + - name: Upload JAR ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Create java ${{ matrix.java }} JAR + path: target/*.jar build: runs-on: ubuntu-latest strategy: From a2a8240d0d83836c509dd59d060d7d08b0429a3c Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 23 Oct 2023 16:18:23 -0400 Subject: [PATCH 066/233] upload jar files to GitHub release --- .github/workflows/deployment.yml | 104 ++++++++++++++++++++----------- .github/workflows/pipeline.yml | 4 +- 2 files changed, 72 insertions(+), 36 deletions(-) diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 54457d905..809c4a0bd 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -11,38 +11,72 @@ on: types: [published] jobs: - publish: - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - uses: actions/checkout@v4 - - name: Set up Java for publishing to Maven Central Repository - uses: actions/setup-java@v3 - with: - # Use lowest supported LTS Java version - java-version: '8' - distribution: 'temurin' - server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml - server-username: MAVEN_USERNAME # env variable for username in deploy - server-password: MAVEN_PASSWORD # env variable for token in deploy - gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import - gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase - - - name: Publish to the Maven Central Repository - run: mvn --batch-mode deploy - env: - MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} - MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} - MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} - # - name: Set up Java for publishing to GitHub Packages - # uses: actions/setup-java@v3 - # with: - # # Use lowest supported LTS Java version - # java-version: '8' - # distribution: 'temurin' - # - name: Publish to GitHub Packages - # run: mvn --batch-mode deploy - # env: - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # old-school build and jar method. No tests run or compiled. + publish-1_6: + name: Publish Java 1.6 to GitHub Release + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + - name: Setup java + uses: actions/setup-java@v1 + with: + java-version: 1.6 + - name: Compile Java 1.6 + run: | + mkdir -p target/classes + javac -version + javac -d target/classes/ src/main/java/org/json/*.java + - name: Create JAR 1.6 + run: | + jar cvf "target/org.json-1.6-${{ github.ref_name }}.jar" -C target/classes . + - name: Add 1.6 Jar To Release + uses: softprops/action-gh-release@v1 + with: + append_body: true + files: | + target/*.jar + publish: + name: Publish Java 8 to Maven Central and GitHub Release + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + steps: + - uses: actions/checkout@v4 + - name: Set up Java for publishing to Maven Central Repository + uses: actions/setup-java@v3 + with: + # Use lowest supported LTS Java version + java-version: '8' + distribution: 'temurin' + server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml + server-username: MAVEN_USERNAME # env variable for username in deploy + server-password: MAVEN_PASSWORD # env variable for token in deploy + gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import + gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase + + - name: Publish to the Maven Central Repository + run: mvn --batch-mode deploy + env: + MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} + MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} + MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} + + - name: Add Jar To Release + uses: softprops/action-gh-release@v1 + with: + append_body: true + files: | + target/*.jar + # - name: Set up Java for publishing to GitHub Packages + # uses: actions/setup-java@v3 + # with: + # # Use lowest supported LTS Java version + # java-version: '8' + # distribution: 'temurin' + # - name: Publish to GitHub Packages + # run: mvn --batch-mode deploy + # env: + # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 39d23e23b..b5b5f3f47 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -19,7 +19,7 @@ jobs: java: [ 1.6 ] name: Java ${{ matrix.java }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup java uses: actions/setup-java@v1 with: @@ -41,6 +41,8 @@ jobs: build: runs-on: ubuntu-latest strategy: + fail-fast: false + max-parallel: 2 matrix: # build against supported Java LTS versions: java: [ 8, 11, 17, 21 ] From ea842b437cf552de8394bd843e40cf12f92a0c94 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 23 Oct 2023 17:11:55 -0400 Subject: [PATCH 067/233] remove unneeded matrix build typ for java 1.6 --- .github/workflows/deployment.yml | 2 +- .github/workflows/pipeline.yml | 28 ++++++++++++---------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 809c4a0bd..e87064441 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -27,7 +27,7 @@ jobs: run: | mkdir -p target/classes javac -version - javac -d target/classes/ src/main/java/org/json/*.java + javac -source 1.6 -target 1.6 -d target/classes/ src/main/java/org/json/*.java - name: Create JAR 1.6 run: | jar cvf "target/org.json-1.6-${{ github.ref_name }}.jar" -C target/classes . diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index b5b5f3f47..63540cc6c 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -12,31 +12,27 @@ on: jobs: # old-school build and jar method. No tests run or compiled. build-1_6: + name: Java 1.6 runs-on: ubuntu-latest - strategy: - matrix: - # build for java 1.6, however don't run any tests - java: [ 1.6 ] - name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v4 - name: Setup java uses: actions/setup-java@v1 with: - java-version: ${{ matrix.java }} - - name: Compile Java ${{ matrix.java }} + java-version: 1.6 + - name: Compile Java 1.6 run: | mkdir -p target/classes javac -version - javac -d target/classes/ src/main/java/org/json/*.java - - name: Create java ${{ matrix.java }} JAR + javac -source 1.6 -target 1.6 -d target/classes/ src/main/java/org/json/*.java + - name: Create java 1.6 JAR run: | jar cvf target/org.json.jar -C target/classes . - - name: Upload JAR ${{ matrix.java }} + - name: Upload JAR 1.6 if: ${{ always() }} uses: actions/upload-artifact@v3 with: - name: Create java ${{ matrix.java }} JAR + name: Create java 1.6 JAR path: target/*.jar build: runs-on: ubuntu-latest @@ -56,15 +52,15 @@ jobs: java-version: ${{ matrix.java }} cache: 'maven' - name: Compile Java ${{ matrix.java }} - run: mvn clean compile -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true + run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true - name: Run Tests ${{ matrix.java }} run: | - mvn test -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} + mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} - name: Build Test Report ${{ matrix.java }} if: ${{ always() }} run: | - mvn surefire-report:report-only -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} - mvn site -DgenerateReports=false -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} + mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} - name: Upload Test Results ${{ matrix.java }} if: ${{ always() }} uses: actions/upload-artifact@v3 @@ -78,7 +74,7 @@ jobs: name: Test Report ${{ matrix.java }} path: target/site/ - name: Package Jar ${{ matrix.java }} - run: mvn clean package -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true + run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true - name: Upload Package Results ${{ matrix.java }} if: ${{ always() }} uses: actions/upload-artifact@v3 From c6ec2f0e4cadf6c9efbcfa1245f3182ef50be292 Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Wed, 25 Oct 2023 23:23:00 +0530 Subject: [PATCH 068/233] #748 - close XML tag explicitly for empty tags with configuration. --- src/main/java/org/json/XML.java | 25 ++++++++++++++----- .../java/org/json/XMLParserConfiguration.java | 22 ++++++++++++++-- .../org/json/junit/XMLConfigurationTest.java | 11 ++++++++ src/test/java/org/json/junit/XMLTest.java | 20 +++++++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 533192313..efdfd9f66 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -877,12 +877,25 @@ private static String toString(final Object object, final String tagName, final } } } else if ("".equals(value)) { - sb.append(indent(indent)); - sb.append('<'); - sb.append(key); - sb.append("/>"); - if(indentFactor > 0){ - sb.append("\n"); + if (config.isCloseEmptyTag()){ + sb.append(indent(indent)); + sb.append('<'); + sb.append(key); + sb.append(">"); + sb.append(""); + if (indentFactor > 0) { + sb.append("\n"); + } + }else { + sb.append(indent(indent)); + sb.append('<'); + sb.append(key); + sb.append("/>"); + if (indentFactor > 0) { + sb.append("\n"); + } } // Emit a new tag diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 566146d6d..5d7ecfa00 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -43,6 +43,13 @@ public class XMLParserConfiguration extends ParserConfiguration { */ private boolean convertNilAttributeToNull; + /** + * When creating an XML from JSON Object, an empty tag by default will self-close. + * If it has to be closed explicitly, with empty content between start and end tag, + * this flag is to be turned on. + */ + private boolean closeEmptyTag; + /** * This will allow type conversion for values in XML if xsi:type attribute is defined */ @@ -145,12 +152,13 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN */ private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull, final Map> xsiTypeMap, final Set forceList, - final int maxNestingDepth) { + final int maxNestingDepth, final boolean closeEmptyTag) { super(keepStrings, maxNestingDepth); this.cDataTagName = cDataTagName; this.convertNilAttributeToNull = convertNilAttributeToNull; this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap); this.forceList = Collections.unmodifiableSet(forceList); + this.closeEmptyTag = closeEmptyTag; } /** @@ -169,7 +177,8 @@ protected XMLParserConfiguration clone() { this.convertNilAttributeToNull, this.xsiTypeMap, this.forceList, - this.maxNestingDepth + this.maxNestingDepth, + this.closeEmptyTag ); } @@ -303,4 +312,13 @@ public XMLParserConfiguration withForceList(final Set forceList) { public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); } + + public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ + this.closeEmptyTag = closeEmptyTag; + return this; + } + + public boolean isCloseEmptyTag() { + return this.closeEmptyTag; + } } diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 2eaaf99e8..153d4ed11 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -557,6 +557,17 @@ public void shouldHandleNullNodeValue() assertEquals(actualXML, resultXML); } + @Test + public void shouldHandleEmptyNodeValue() + { + JSONObject inputJSON = new JSONObject(); + inputJSON.put("Emptyness", ""); + String expectedXmlWithoutExplicitEndTag = ""; + String expectedXmlWithExplicitEndTag = ""; + assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(false))); + assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(true))); + } + /** * Investigate exactly how the "content" keyword works */ diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 63135eb00..d8aedb350 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1177,6 +1177,26 @@ public void testIndentComplicatedJsonObject(){ } + + @Test + public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){ + String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}"; + JSONObject jsonObject = new JSONObject(jsonString); + String expectedXmlString = "two"; + String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true)); + assertEquals(expectedXmlString, xmlForm); + } + + @Test + public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){ + String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}"; + JSONObject jsonObject = new JSONObject(jsonString); + String expectedXmlString = "two"; + String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false)); + assertEquals(expectedXmlString, xmlForm); + } + + @Test public void testIndentSimpleJsonObject(){ String str = "{ \"employee\": { \n" + From c05d7058ff7bfd0a1520cd8bcf94eebd2b9a11be Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Fri, 27 Oct 2023 17:17:20 +0530 Subject: [PATCH 069/233] #748 - javadoc updated for methods. --- src/main/java/org/json/XMLParserConfiguration.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 5d7ecfa00..e85ce536a 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -149,6 +149,7 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN * xsi:type="integer" as integer, xsi:type="string" as string * @param forceList new HashSet() to parse the provided tags' values as arrays * @param maxNestingDepth int to limit the nesting depth + * @param closeEmptyTag boolean to turn on explicit end tag for tag with empty value */ private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull, final Map> xsiTypeMap, final Set forceList, @@ -313,6 +314,11 @@ public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); } + /** + * To enable explicit end tag with empty value. + * @param closeEmptyTag + * @return same instance of configuration with empty tag config updated + */ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ this.closeEmptyTag = closeEmptyTag; return this; From 1ceb70b525c9a0e6bd3c36cd8ce219ad42ec7985 Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Sat, 28 Oct 2023 07:09:37 +0530 Subject: [PATCH 070/233] #813 - PR comments - alignments --- src/test/java/org/json/NumberConversionUtilTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/json/NumberConversionUtilTest.java b/src/test/java/org/json/NumberConversionUtilTest.java index 0fedcebb9..c6f07254d 100644 --- a/src/test/java/org/json/NumberConversionUtilTest.java +++ b/src/test/java/org/json/NumberConversionUtilTest.java @@ -28,7 +28,7 @@ public void shouldParseDecimalFractionNumbersWithSingleLeadingZero(){ } - @Test + @Test public void shouldParseDecimalFractionNumbersWithZerosAfterDecimalPoint(){ Number number = NumberConversionUtil.stringToNumber("0.010d"); assertEquals("Do not match", 0.010d, number.doubleValue(),0.0d); @@ -56,7 +56,7 @@ public void shouldParseMixedDecimalFractionNumbersWithoutLeadingZero(){ } - @Test + @Test public void shouldParseMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ Number number = NumberConversionUtil.stringToNumber("200.010d"); assertEquals("Do not match", 200.010d, number.doubleValue(),0.0d); @@ -85,7 +85,7 @@ public void shouldParseNegativeDecimalFractionNumbersWithSingleLeadingZero(){ } - @Test + @Test public void shouldParseNegativeDecimalFractionNumbersWithZerosAfterDecimalPoint(){ Number number = NumberConversionUtil.stringToNumber("-0.010d"); assertEquals("Do not match", -0.010d, number.doubleValue(),0.0d); @@ -113,7 +113,7 @@ public void shouldParseNegativeMixedDecimalFractionNumbersWithoutLeadingZero(){ } - @Test + @Test public void shouldParseNegativeMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ Number number = NumberConversionUtil.stringToNumber("-200.010d"); assertEquals("Do not match", -200.010d, number.doubleValue(),0.0d); From 8ec822c5756acbf78ae83ba2c4800ebf25b5336e Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Sat, 28 Oct 2023 07:36:31 +0530 Subject: [PATCH 071/233] #748 - PR comments - follow convention of configuration builder. --- .../java/org/json/XMLParserConfiguration.java | 5 +-- .../org/json/junit/XMLConfigurationTest.java | 31 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index e85ce536a..2e4907f74 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -320,8 +320,9 @@ public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { * @return same instance of configuration with empty tag config updated */ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ - this.closeEmptyTag = closeEmptyTag; - return this; + XMLParserConfiguration clonedConfiguration = this.clone(); + clonedConfiguration.closeEmptyTag = closeEmptyTag; + return clonedConfiguration; } public boolean isCloseEmptyTag() { diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 153d4ed11..ffdc20cd2 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -4,11 +4,6 @@ Public Domain. */ -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.File; import java.io.FileReader; import java.io.FileWriter; @@ -27,6 +22,8 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; +import static org.junit.Assert.*; + /** * Tests for JSON-Java XML.java with XMLParserConfiguration.java @@ -564,8 +561,28 @@ public void shouldHandleEmptyNodeValue() inputJSON.put("Emptyness", ""); String expectedXmlWithoutExplicitEndTag = ""; String expectedXmlWithExplicitEndTag = ""; - assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(false))); - assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(true))); + assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null, + new XMLParserConfiguration().withCloseEmptyTag(false))); + assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null, + new XMLParserConfiguration().withCloseEmptyTag(true))); + } + + @Test + public void shouldKeepConfigurationIntactAndUpdateCloseEmptyTagChoice() + { + XMLParserConfiguration keepStrings = XMLParserConfiguration.KEEP_STRINGS; + XMLParserConfiguration keepStringsAndCloseEmptyTag = keepStrings.withCloseEmptyTag(true); + XMLParserConfiguration keepDigits = keepStringsAndCloseEmptyTag.withKeepStrings(false); + XMLParserConfiguration keepDigitsAndNoCloseEmptyTag = keepDigits.withCloseEmptyTag(false); + assertTrue(keepStrings.isKeepStrings()); + assertFalse(keepStrings.isCloseEmptyTag()); + assertTrue(keepStringsAndCloseEmptyTag.isKeepStrings()); + assertTrue(keepStringsAndCloseEmptyTag.isCloseEmptyTag()); + assertFalse(keepDigits.isKeepStrings()); + assertTrue(keepDigits.isCloseEmptyTag()); + assertFalse(keepDigitsAndNoCloseEmptyTag.isKeepStrings()); + assertFalse(keepDigitsAndNoCloseEmptyTag.isCloseEmptyTag()); + } /** From a3742acf74947b9ccd45bc6190111f619cb95cb6 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 6 Nov 2023 17:48:18 -0500 Subject: [PATCH 072/233] Fixes #821 add ignore annotation to tests that may fail due to differences in machine resources and can't be controlled via the tests --- src/test/java/org/json/junit/JSONArrayTest.java | 4 +++- src/test/java/org/json/junit/JSONObjectTest.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index e877070d0..7b0d52eca 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -32,6 +32,7 @@ import org.json.JSONString; import org.json.JSONTokener; import org.json.junit.data.MyJsonString; +import org.junit.Ignore; import org.junit.Test; import com.jayway.jsonpath.Configuration; @@ -1384,6 +1385,7 @@ public void jsonArrayClearMethodTest() { /** * Tests for stack overflow. See https://github.com/stleary/JSON-java/issues/654 */ + @Ignore("This test relies on system constraints and may not always pass. See: https://github.com/stleary/JSON-java/issues/821") @Test(expected = JSONException.class) public void issue654StackOverflowInputWellFormed() { //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes)); @@ -1391,7 +1393,7 @@ public void issue654StackOverflowInputWellFormed() { JSONTokener tokener = new JSONTokener(resourceAsStream); JSONArray json_input = new JSONArray(tokener); assertNotNull(json_input); - fail("Excepected Exception."); + fail("Excepected Exception due to stack overflow."); Util.checkJSONArrayMaps(json_input); } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 42b41b30f..d9adbcade 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -55,6 +55,7 @@ import org.json.junit.data.Singleton; import org.json.junit.data.SingletonEnum; import org.json.junit.data.WeirdList; +import org.junit.Ignore; import org.junit.Test; import com.jayway.jsonpath.Configuration; @@ -3665,6 +3666,7 @@ public void issue654IncorrectNestingNoKey2() { /** * Tests for stack overflow. See https://github.com/stleary/JSON-java/issues/654 */ + @Ignore("This test relies on system constraints and may not always pass. See: https://github.com/stleary/JSON-java/issues/821") @Test(expected = JSONException.class) public void issue654StackOverflowInputWellFormed() { //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes)); @@ -3672,7 +3674,7 @@ public void issue654StackOverflowInputWellFormed() { JSONTokener tokener = new JSONTokener(resourceAsStream); JSONObject json_input = new JSONObject(tokener); assertNotNull(json_input); - fail("Excepected Exception."); + fail("Excepected Exception due to stack overflow."); } @Test From 1a61af8255e559f7b3766e5a858d7286800e4746 Mon Sep 17 00:00:00 2001 From: Saiharshith Karuneegar Ramesh Date: Mon, 13 Nov 2023 13:25:30 -0600 Subject: [PATCH 073/233] Fixed flaky tests in XMLTest.java --- src/test/java/org/json/junit/XMLTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index d8aedb350..823a06591 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1184,7 +1184,9 @@ public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){ JSONObject jsonObject = new JSONObject(jsonString); String expectedXmlString = "two"; String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true)); - assertEquals(expectedXmlString, xmlForm); + JSONObject actualJsonObject = XML.toJSONObject(xmlForm); + JSONObject expectedJsonObject = XML.toJSONObject(expectedXmlString); + assertTrue(expectedJsonObject.similar(actualJsonObject)); } @Test @@ -1193,7 +1195,9 @@ public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){ JSONObject jsonObject = new JSONObject(jsonString); String expectedXmlString = "two"; String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false)); - assertEquals(expectedXmlString, xmlForm); + JSONObject actualJsonObject = XML.toJSONObject(xmlForm); + JSONObject expectedJsonObject = XML.toJSONObject(expectedXmlString); + assertTrue(expectedJsonObject.similar(actualJsonObject)); } From b5f9febfe90d9667f47b22d72c4ff7693735b2dc Mon Sep 17 00:00:00 2001 From: HappyHacker123 Date: Fri, 17 Nov 2023 21:31:06 +0800 Subject: [PATCH 074/233] Upgrade json-path's version to 2.4 to avoid dependency conflict. --- build.gradle | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index fbc2ff1f1..30a85785b 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ repositories { dependencies { testImplementation 'junit:junit:4.13.2' - testImplementation 'com.jayway.jsonpath:json-path:2.1.0' + testImplementation 'com.jayway.jsonpath:json-path:2.4.0' testImplementation 'org.mockito:mockito-core:4.2.0' } diff --git a/pom.xml b/pom.xml index ba0efd12d..927a989b1 100644 --- a/pom.xml +++ b/pom.xml @@ -70,7 +70,7 @@ com.jayway.jsonpath json-path - 2.1.0 + 2.4.0 test From 097a401f3f38f7ac8ec6b4cc28fca1b6486f6d48 Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Sun, 19 Nov 2023 09:11:32 -0400 Subject: [PATCH 075/233] refactor: rename variable boolean 'b' to 'isEndOfPair' in CookieList.toString() --- src/main/java/org/json/CookieList.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/CookieList.java b/src/main/java/org/json/CookieList.java index 8ad8b589d..03e54b997 100644 --- a/src/main/java/org/json/CookieList.java +++ b/src/main/java/org/json/CookieList.java @@ -46,19 +46,19 @@ public static JSONObject toJSONObject(String string) throws JSONException { * @throws JSONException if a called function fails */ public static String toString(JSONObject jo) throws JSONException { - boolean b = false; + boolean isEndOfPair = false; final StringBuilder sb = new StringBuilder(); // Don't use the new entrySet API to maintain Android support for (final String key : jo.keySet()) { final Object value = jo.opt(key); if (!JSONObject.NULL.equals(value)) { - if (b) { + if (isEndOfPair) { sb.append(';'); } sb.append(Cookie.escape(key)); sb.append("="); sb.append(Cookie.escape(value.toString())); - b = true; + isEndOfPair = true; } } return sb.toString(); From 75419e3f257af9c365b0ef3e5f4428a335564f8d Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Sun, 19 Nov 2023 09:21:05 -0400 Subject: [PATCH 076/233] refactor: introduce explaining variable 'indentationSuffix' in XML.toString() --- src/main/java/org/json/XML.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 04c8bcd2e..a94c3fc4b 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -847,14 +847,14 @@ private static String toString(final Object object, final String tagName, final string = (object == null) ? "null" : escape(object.toString()); - + String indentationSuffix = (indentFactor > 0) ? "\n" : ""; if(tagName == null){ - return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : ""); + return indent(indent) + "\"" + string + "\"" + indentationSuffix; } else if(string.length() == 0){ - return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : ""); + return indent(indent) + "<" + tagName + "/>" + indentationSuffix; } else { return indent(indent) + "<" + tagName - + ">" + string + "" + ((indentFactor > 0) ? "\n" : ""); + + ">" + string + "" + indentationSuffix; } } From 7f1cb8bf62015016d4b02879b00cc7477b62c570 Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Sun, 19 Nov 2023 09:51:44 -0400 Subject: [PATCH 077/233] refactor: decompose condition of digit checks by using extra method 'isNumericChar(...)' in NumberConversionUtil. --- src/main/java/org/json/NumberConversionUtil.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java index d53c5e277..30ca74d86 100644 --- a/src/main/java/org/json/NumberConversionUtil.java +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -24,7 +24,7 @@ static Number stringToNumber(final String input) throws NumberFormatException { val = "-0."+val.substring(2); } char initial = val.charAt(0); - if ((initial >= '0' && initial <= '9') || initial == '-' ) { + if ( isNumericChar(initial) || initial == '-' ) { // decimal representation if (isDecimalNotation(val)) { // Use a BigDecimal all the time so we keep the original @@ -53,13 +53,13 @@ static Number stringToNumber(final String input) throws NumberFormatException { initial = val.charAt(0); if(initial == '0' && val.length() > 1) { char at1 = val.charAt(1); - if(at1 >= '0' && at1 <= '9') { + if(isNumericChar(at1)) { throw new NumberFormatException("val ["+input+"] is not a valid number."); } } else if (initial == '-' && val.length() > 2) { char at1 = val.charAt(1); char at2 = val.charAt(2); - if(at1 == '0' && at2 >= '0' && at2 <= '9') { + if(at1 == '0' && isNumericChar(at2)) { throw new NumberFormatException("val ["+input+"] is not a valid number."); } } @@ -83,6 +83,16 @@ static Number stringToNumber(final String input) throws NumberFormatException { throw new NumberFormatException("val ["+input+"] is not a valid number."); } + /** + * Checks if the character is a numeric digit ('0' to '9'). + * + * @param c The character to be checked. + * @return true if the character is a numeric digit, false otherwise. + */ + private static boolean isNumericChar(char c) { + return (c >= '0' && c <= '9'); + } + /** * Checks if the value could be considered a number in decimal number system. * @param value From 30f5b2de79b15cac0e5c905445567f01c3560a17 Mon Sep 17 00:00:00 2001 From: Keaton Taylor Date: Mon, 20 Nov 2023 12:11:47 +0200 Subject: [PATCH 078/233] Add a config flag to disable whitespace trimming --- src/main/java/org/json/XML.java | 47 +++++++++++- .../java/org/json/XMLParserConfiguration.java | 27 ++++++- src/main/java/org/json/XMLTokener.java | 17 ++++- .../org/json/junit/XMLConfigurationTest.java | 2 +- src/test/java/org/json/junit/XMLTest.java | 74 +++++++++++++++++++ 5 files changed, 160 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 04c8bcd2e..e50f7ff07 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -431,6 +431,9 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP && jsonObject.opt(config.getcDataTagName()) != null) { context.accumulate(tagName, jsonObject.opt(config.getcDataTagName())); } else { + if (!config.shouldTrimWhiteSpace()) { + removeEmpty(jsonObject, config); + } context.accumulate(tagName, jsonObject); } } @@ -445,6 +448,48 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP } } } + /** + * This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName + * and contains whitespace as this is caused by whitespace between tags. See test XMLTest.testNestedWithWhitespaceTrimmingDisabled. + * @param jsonObject JSONObject which may require deletion + * @param config The XMLParserConfiguration which includes the cDataTagName + */ + private static void removeEmpty(final JSONObject jsonObject, final XMLParserConfiguration config) { + if (jsonObject.has(config.getcDataTagName())) { + final Object s = jsonObject.get(config.getcDataTagName()); + if (s instanceof String) { + if (isStringAllWhiteSpace(s.toString())) { + jsonObject.remove(config.getcDataTagName()); + } + } + else if (s instanceof JSONArray) { + final JSONArray sArray = (JSONArray) s; + for (int k = sArray.length()-1; k >= 0; k--){ + final Object eachString = sArray.get(k); + if (eachString instanceof String) { + String s1 = (String) eachString; + if (isStringAllWhiteSpace(s1)) { + sArray.remove(k); + } + } + } + if (sArray.isEmpty()) { + jsonObject.remove(config.getcDataTagName()); + } + } + } + } + + private static boolean isStringAllWhiteSpace(final String s) { + for (int k = 0; k forceList; + private boolean shouldTrimWhiteSpace; + /** * Default parser configuration. Does not keep strings (tries to implicitly convert - * values), and the CDATA Tag Name is "content". + * values), and the CDATA Tag Name is "content". Trims whitespace. */ public XMLParserConfiguration () { super(); @@ -71,6 +73,7 @@ public XMLParserConfiguration () { this.convertNilAttributeToNull = false; this.xsiTypeMap = Collections.emptyMap(); this.forceList = Collections.emptySet(); + this.shouldTrimWhiteSpace = true; } /** @@ -153,13 +156,14 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN */ private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull, final Map> xsiTypeMap, final Set forceList, - final int maxNestingDepth, final boolean closeEmptyTag) { + final int maxNestingDepth, final boolean closeEmptyTag, final boolean shouldTrimWhiteSpace) { super(keepStrings, maxNestingDepth); this.cDataTagName = cDataTagName; this.convertNilAttributeToNull = convertNilAttributeToNull; this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap); this.forceList = Collections.unmodifiableSet(forceList); this.closeEmptyTag = closeEmptyTag; + this.shouldTrimWhiteSpace = shouldTrimWhiteSpace; } /** @@ -179,7 +183,8 @@ protected XMLParserConfiguration clone() { this.xsiTypeMap, this.forceList, this.maxNestingDepth, - this.closeEmptyTag + this.closeEmptyTag, + this.shouldTrimWhiteSpace ); } @@ -325,7 +330,23 @@ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ return clonedConfiguration; } + /** + * Sets whether whitespace should be trimmed inside of tags. *NOTE* Do not use this if + * you expect your XML tags to have names that are the same as cDataTagName as this is unsupported. + * cDataTagName should be set to a distinct value in these cases. + * @param shouldTrimWhiteSpace boolean to set trimming on or off. Off is default. + * @return same instance of configuration with empty tag config updated + */ + public XMLParserConfiguration withShouldTrimWhitespace(boolean shouldTrimWhiteSpace){ + XMLParserConfiguration clonedConfiguration = this.clone(); + clonedConfiguration.shouldTrimWhiteSpace = shouldTrimWhiteSpace; + return clonedConfiguration; + } + public boolean isCloseEmptyTag() { return this.closeEmptyTag; } + public boolean shouldTrimWhiteSpace() { + return this.shouldTrimWhiteSpace; + } } diff --git a/src/main/java/org/json/XMLTokener.java b/src/main/java/org/json/XMLTokener.java index 957498ca2..eb77e649b 100644 --- a/src/main/java/org/json/XMLTokener.java +++ b/src/main/java/org/json/XMLTokener.java @@ -20,6 +20,8 @@ public class XMLTokener extends JSONTokener { */ public static final java.util.HashMap entity; + private static XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;; + static { entity = new java.util.HashMap(8); entity.put("amp", XML.AMP); @@ -45,6 +47,15 @@ public XMLTokener(String s) { super(s); } + public XMLTokener(Reader r, XMLParserConfiguration configuration) { + super(r); + XMLTokener.configuration = configuration; + } + public XMLTokener(String s, XMLParserConfiguration configuration) { + super(s); + XMLTokener.configuration = configuration; + } + /** * Get the text in the CDATA block. * @return The string up to the ]]>. @@ -83,7 +94,7 @@ public Object nextContent() throws JSONException { StringBuilder sb; do { c = next(); - } while (Character.isWhitespace(c)); + } while (Character.isWhitespace(c) && configuration.shouldTrimWhiteSpace()); if (c == 0) { return null; } @@ -97,7 +108,9 @@ public Object nextContent() throws JSONException { } if (c == '<') { back(); - return sb.toString().trim(); + if (configuration.shouldTrimWhiteSpace()) { + return sb.toString().trim(); + } else return sb.toString(); } if (c == '&') { sb.append(nextEntity(c)); diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index ffdc20cd2..ba8418cb6 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -1181,4 +1181,4 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { assertTrue("Error: " +e.getMessage(), false); } } -} \ No newline at end of file +} diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index d8aedb350..d305ff4c5 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1319,6 +1319,80 @@ public void testMaxNestingDepthWithValidFittingXML() { "parameter of the XMLParserConfiguration used"); } } + @Test + public void testWithWhitespaceTrimmingDisabled() { + String originalXml = " Test Whitespace String \t "; + + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false)); + String expectedJsonString = "{\"testXml\":\" Test Whitespace String \t \"}"; + JSONObject expectedJson = new JSONObject(expectedJsonString); + Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); + } + @Test + public void testNestedWithWhitespaceTrimmingDisabled() { + String originalXml = + "\n"+ + "\n"+ + "
\n"+ + " Sherlock Holmes \n"+ + "
\n"+ + "
"; + + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false)); + String expectedJsonString = "{\"addresses\":{\"address\":{\"name\":\" Sherlock Holmes \"}}}"; + JSONObject expectedJson = new JSONObject(expectedJsonString); + Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); + } + @Test + public void shouldTrimWhitespaceDoesNotSupportTagsEqualingCDataTagName() { + // When using withShouldTrimWhitespace = true, input containing tags with same name as cDataTagName is unsupported and should not be used in conjunction + String originalXml = + "\n"+ + "\n"+ + "
\n"+ + " Sherlock Holmes \n"+ + "
\n"+ + "
"; + + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false).withcDataTagName("content")); + String expectedJsonString = "{\"addresses\":{\"address\":[[\"\\n \",\" Sherlock Holmes \",\"\\n \"]]}}"; + JSONObject expectedJson = new JSONObject(expectedJsonString); + Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); + } + @Test + public void shouldTrimWhitespaceEnabledDropsTagsEqualingCDataTagNameButValueRemains() { + String originalXml = + "\n"+ + "\n"+ + "
\n"+ + " Sherlock Holmes \n"+ + "
\n"+ + "
"; + + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(true).withcDataTagName("content")); + String expectedJsonString = "{\"addresses\":{\"address\":\"Sherlock Holmes\"}}"; + JSONObject expectedJson = new JSONObject(expectedJsonString); + Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); + } + @Test + public void testWithWhitespaceTrimmingEnabled() { + String originalXml = " Test Whitespace String \t "; + + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(true)); + String expectedJsonString = "{\"testXml\":\"Test Whitespace String\"}"; + JSONObject expectedJson = new JSONObject(expectedJsonString); + Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); + } + @Test + public void testWithWhitespaceTrimmingEnabledByDefault() { + String originalXml = " Test Whitespace String \t "; + + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration()); + String expectedJsonString = "{\"testXml\":\"Test Whitespace String\"}"; + JSONObject expectedJson = new JSONObject(expectedJsonString); + Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); + } + } From 09f35372d4e00256cba20efd812243fb56d8bef6 Mon Sep 17 00:00:00 2001 From: Keaton Taylor Date: Wed, 22 Nov 2023 11:14:50 +0200 Subject: [PATCH 079/233] Update clone() method so that default constructor does not need to be changed --- src/main/java/org/json/XMLParserConfiguration.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 5f5a412ea..9682d0360 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -156,14 +156,13 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN */ private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull, final Map> xsiTypeMap, final Set forceList, - final int maxNestingDepth, final boolean closeEmptyTag, final boolean shouldTrimWhiteSpace) { + final int maxNestingDepth, final boolean closeEmptyTag) { super(keepStrings, maxNestingDepth); this.cDataTagName = cDataTagName; this.convertNilAttributeToNull = convertNilAttributeToNull; this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap); this.forceList = Collections.unmodifiableSet(forceList); this.closeEmptyTag = closeEmptyTag; - this.shouldTrimWhiteSpace = shouldTrimWhiteSpace; } /** @@ -176,16 +175,17 @@ protected XMLParserConfiguration clone() { // item, a new map instance should be created and if possible each value in the // map should be cloned as well. If the values of the map are known to also // be immutable, then a shallow clone of the map is acceptable. - return new XMLParserConfiguration( + final XMLParserConfiguration config = new XMLParserConfiguration( this.keepStrings, this.cDataTagName, this.convertNilAttributeToNull, this.xsiTypeMap, this.forceList, this.maxNestingDepth, - this.closeEmptyTag, - this.shouldTrimWhiteSpace + this.closeEmptyTag ); + config.shouldTrimWhiteSpace = this.shouldTrimWhiteSpace; + return config; } /** From aba82d9cc474c13ee981737432af98c19bb6f5b2 Mon Sep 17 00:00:00 2001 From: Aditya Purohit Date: Tue, 28 Nov 2023 02:56:10 +0000 Subject: [PATCH 080/233] isNumericChar() - switch comparison order --- src/main/java/org/json/NumberConversionUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java index 30ca74d86..c2f16d74c 100644 --- a/src/main/java/org/json/NumberConversionUtil.java +++ b/src/main/java/org/json/NumberConversionUtil.java @@ -90,7 +90,7 @@ static Number stringToNumber(final String input) throws NumberFormatException { * @return true if the character is a numeric digit, false otherwise. */ private static boolean isNumericChar(char c) { - return (c >= '0' && c <= '9'); + return (c <= '9' && c >= '0'); } /** From 7cbeb35498798dad51c7d8bd0e904858b1b91074 Mon Sep 17 00:00:00 2001 From: LaFriska Date: Tue, 28 Nov 2023 17:39:46 -0500 Subject: [PATCH 081/233] deleted redundant .toString() call in README test method Sysout --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1db1f54f4..9806f2a88 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ import org.json.JSONObject; public class Test { public static void main(String args[]){ JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }"); - System.out.println(jo.toString()); + System.out.println(jo); } } ``` From e430db40aa316a8525974aa64f9aee107b4a5a28 Mon Sep 17 00:00:00 2001 From: Keaton Taylor Date: Thu, 30 Nov 2023 10:05:08 +0200 Subject: [PATCH 082/233] Update XMLParserConfiguration to not be static and add a comment about the use of shouldTrimWhiteSpace --- src/main/java/org/json/XMLParserConfiguration.java | 7 +++++++ src/main/java/org/json/XMLTokener.java | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 9682d0360..b87a3085a 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -61,6 +61,13 @@ public class XMLParserConfiguration extends ParserConfiguration { */ private Set forceList; + + /** + * Flag to indicate whether white space should be trimmed when parsing XML. + * The default behaviour is to trim white space. When this is set to false, inputting XML + * with tags that are the same as the value of cDataTagName is unsupported. It is recommended to set cDataTagName + * to a distinct value in this case. + */ private boolean shouldTrimWhiteSpace; /** diff --git a/src/main/java/org/json/XMLTokener.java b/src/main/java/org/json/XMLTokener.java index eb77e649b..4b03f9729 100644 --- a/src/main/java/org/json/XMLTokener.java +++ b/src/main/java/org/json/XMLTokener.java @@ -20,7 +20,7 @@ public class XMLTokener extends JSONTokener { */ public static final java.util.HashMap entity; - private static XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;; + private XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;; static { entity = new java.util.HashMap(8); @@ -49,11 +49,11 @@ public XMLTokener(String s) { public XMLTokener(Reader r, XMLParserConfiguration configuration) { super(r); - XMLTokener.configuration = configuration; + this.configuration = configuration; } public XMLTokener(String s, XMLParserConfiguration configuration) { super(s); - XMLTokener.configuration = configuration; + this.configuration = configuration; } /** From 4d6de8c00a99370578e3c591fa0d38801c3adeef Mon Sep 17 00:00:00 2001 From: Keaton Taylor Date: Wed, 13 Dec 2023 14:04:05 +0200 Subject: [PATCH 083/233] Remove unused constructor and add comment above other constructor --- src/main/java/org/json/XMLTokener.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/XMLTokener.java b/src/main/java/org/json/XMLTokener.java index 4b03f9729..bc18b31c9 100644 --- a/src/main/java/org/json/XMLTokener.java +++ b/src/main/java/org/json/XMLTokener.java @@ -20,7 +20,7 @@ public class XMLTokener extends JSONTokener { */ public static final java.util.HashMap entity; - private XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;; + private XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL; static { entity = new java.util.HashMap(8); @@ -47,14 +47,15 @@ public XMLTokener(String s) { super(s); } + /** + * Construct an XMLTokener from a Reader and an XMLParserConfiguration. + * @param r A source reader. + * @param configuration the configuration that can be used to set certain flags + */ public XMLTokener(Reader r, XMLParserConfiguration configuration) { super(r); this.configuration = configuration; } - public XMLTokener(String s, XMLParserConfiguration configuration) { - super(s); - this.configuration = configuration; - } /** * Get the text in the CDATA block. From 6d811607ddf4922a49fb37e3a813e2a59ca809a7 Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Fri, 3 Nov 2023 19:54:23 +0530 Subject: [PATCH 084/233] Resolving issue #743 - Recursive depth issue found in JSONObject - Recursive depth issue found in JSONArray --- src/main/java/org/json/JSONArray.java | 29 ++++++++++++++----- src/main/java/org/json/JSONObject.java | 26 +++++++++++++++-- .../java/org/json/junit/JSONArrayTest.java | 21 ++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 17 +++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index ed7982f8a..eec7852d5 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -149,11 +149,18 @@ public JSONArray(String source) throws JSONException { * A Collection. */ public JSONArray(Collection collection) { + this(collection, 0); + } + + protected JSONArray(Collection collection, int recursionDepth) { + if (recursionDepth > JSONObject.RECURSION_DEPTH_LIMIT) { + throw new JSONException("JSONArray has reached recursion depth limit of " + JSONObject.RECURSION_DEPTH_LIMIT); + } if (collection == null) { this.myArrayList = new ArrayList(); } else { this.myArrayList = new ArrayList(collection.size()); - this.addAll(collection, true); + this.addAll(collection, true, recursionDepth); } } @@ -205,7 +212,7 @@ public JSONArray(Object array) throws JSONException { throw new JSONException( "JSONArray initial value should be a string or collection or array."); } - this.addAll(array, true); + this.addAll(array, true, 0); } /** @@ -1779,13 +1786,15 @@ public boolean isEmpty() { * @param wrap * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly + * @param recursionDepth + * variable to keep the count of how nested the object creation is happening. * */ - private void addAll(Collection collection, boolean wrap) { + private void addAll(Collection collection, boolean wrap, int recursionDepth) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); if (wrap) { for (Object o: collection){ - this.put(JSONObject.wrap(o)); + this.put(JSONObject.wrap(o, recursionDepth + 1)); } } else { for (Object o: collection){ @@ -1815,6 +1824,10 @@ private void addAll(Iterable iter, boolean wrap) { } } + private void addAll(Object array, boolean wrap) throws JSONException { + this.addAll(array, wrap, 0); + } + /** * Add an array's elements to the JSONArray. * @@ -1825,19 +1838,21 @@ private void addAll(Iterable iter, boolean wrap) { * @param wrap * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly + * @param recursionDepth + * Variable to keep the count of how nested the object creation is happening. * * @throws JSONException * If not an array or if an array value is non-finite number. * @throws NullPointerException * Thrown if the array parameter is null. */ - private void addAll(Object array, boolean wrap) throws JSONException { + private void addAll(Object array, boolean wrap, int recursionDepth) throws JSONException { if (array.getClass().isArray()) { int length = Array.getLength(array); this.myArrayList.ensureCapacity(this.myArrayList.size() + length); if (wrap) { for (int i = 0; i < length; i += 1) { - this.put(JSONObject.wrap(Array.get(array, i))); + this.put(JSONObject.wrap(Array.get(array, i), recursionDepth + 1)); } } else { for (int i = 0; i < length; i += 1) { @@ -1850,7 +1865,7 @@ private void addAll(Object array, boolean wrap) throws JSONException { // JSONArray this.myArrayList.addAll(((JSONArray)array).myArrayList); } else if (array instanceof Collection) { - this.addAll((Collection)array, wrap); + this.addAll((Collection)array, wrap, recursionDepth); } else if (array instanceof Iterable) { this.addAll((Iterable)array, wrap); } else { diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 7f4885e00..72c0ebd78 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -147,6 +147,7 @@ public String toString() { * The map where the JSONObject's properties are kept. */ private final Map map; + public static final int RECURSION_DEPTH_LIMIT = 1000; public Class getMapType() { return map.getClass(); @@ -276,6 +277,17 @@ public JSONObject(JSONTokener x) throws JSONException { * If a key in the map is null */ public JSONObject(Map m) { + this(m, 0); + } + + /** + * Construct a JSONObject from a map with recursion depth. + * + */ + protected JSONObject(Map m, int recursionDepth) { + if (recursionDepth > RECURSION_DEPTH_LIMIT) { + throw new JSONException("JSONObject has reached recursion depth limit of " + RECURSION_DEPTH_LIMIT); + } if (m == null) { this.map = new HashMap(); } else { @@ -287,7 +299,7 @@ public JSONObject(Map m) { final Object value = e.getValue(); if (value != null) { testValidity(value); - this.map.put(String.valueOf(e.getKey()), wrap(value)); + this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1)); } } } @@ -2566,7 +2578,15 @@ public static Object wrap(Object object) { return wrap(object, null); } + public static Object wrap(Object object, int recursionDepth) { + return wrap(object, null, recursionDepth); + } + private static Object wrap(Object object, Set objectsRecord) { + return wrap(object, objectsRecord, 0); + } + + private static Object wrap(Object object, Set objectsRecord, int recursionDepth) { try { if (NULL.equals(object)) { return NULL; @@ -2584,14 +2604,14 @@ private static Object wrap(Object object, Set objectsRecord) { if (object instanceof Collection) { Collection coll = (Collection) object; - return new JSONArray(coll); + return new JSONArray(coll, recursionDepth); } if (object.getClass().isArray()) { return new JSONArray(object); } if (object instanceof Map) { Map map = (Map) object; - return new JSONObject(map); + return new JSONObject(map, recursionDepth); } Package objectPackage = object.getClass().getPackage(); String objectPackageName = objectPackage != null ? objectPackage diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 7b0d52eca..44a1d7bdd 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1417,4 +1417,25 @@ public String toJSONString() { .put(2); assertFalse(ja1.similar(ja3)); } + + @Test(expected = JSONException.class) + public void testRecursiveDepth() { + HashMap map = new HashMap<>(); + map.put("t", map); + new JSONArray().put(map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthAtPosition() { + HashMap map = new HashMap<>(); + map.put("t", map); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArray() { + ArrayList array = new ArrayList<>(); + array.add(array); + new JSONArray(array); + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index d9adbcade..dff4503e2 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3718,4 +3718,21 @@ public void issue713BeanConstructorWithNonFiniteNumbers() { assertThrows(JSONException.class, () -> new JSONObject(bean)); } } + + @Test(expected = JSONException.class) + public void issue743SerializationMap() { + HashMap map = new HashMap<>(); + map.put("t", map); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test(expected = JSONException.class) + public void testCircularReferenceMultipleLevel() { + HashMap inside = new HashMap<>(); + HashMap jsonObject = new HashMap<>(); + inside.put("inside", jsonObject); + jsonObject.put("test", inside); + new JSONObject(jsonObject); + } } From dcac3bc18e2acbdf617f3debab234bae857fe5b8 Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Tue, 28 Nov 2023 10:29:30 +0530 Subject: [PATCH 085/233] Adding test case for nested json with depth of 999, 1000, 1001 --- .../java/org/json/junit/JSONArrayTest.java | 52 +++++++++++++++++++ .../java/org/json/junit/JSONObjectTest.java | 37 +++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 44a1d7bdd..5f50fc706 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1438,4 +1438,56 @@ public void testRecursiveDepthArray() { array.add(array); new JSONArray(array); } + + @Test + public void testRecursiveDepthAtPosition999Object() { + HashMap map = JSONObjectTest.buildNestedMap(999); + new JSONArray().put(0, map); + } + + @Test + public void testRecursiveDepthAtPosition1000Object() { + HashMap map = JSONObjectTest.buildNestedMap(1000); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthAtPosition1001Object() { + HashMap map = JSONObjectTest.buildNestedMap(1001); + new JSONArray().put(0, map); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArrayLimitedMaps() { + ArrayList array = new ArrayList<>(); + array.add(array); + new JSONArray(array); + } + + @Test + public void testRecursiveDepthArrayFor999Levels() { + ArrayList array = buildNestedArray(999); + new JSONArray(array); + } + + @Test + public void testRecursiveDepthArrayFor1000Levels() { + ArrayList array = buildNestedArray(1000); + new JSONArray(array); + } + + @Test(expected = JSONException.class) + public void testRecursiveDepthArrayFor1001Levels() { + ArrayList array = buildNestedArray(1001); + new JSONArray(array); + } + + public static ArrayList buildNestedArray(int maxDepth) { + if (maxDepth <= 0) { + return new ArrayList<>(); + } + ArrayList nestedArray = new ArrayList<>(); + nestedArray.add(buildNestedArray(maxDepth - 1)); + return nestedArray; + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index dff4503e2..e157fd58e 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3735,4 +3735,41 @@ public void testCircularReferenceMultipleLevel() { jsonObject.put("test", inside); new JSONObject(jsonObject); } + + @Test + public void issue743SerializationMapWith999Objects() { + HashMap map = buildNestedMap(999); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test + public void issue743SerializationMapWith1000Objects() { + HashMap map = buildNestedMap(1000); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + @Test(expected = JSONException.class) + public void issue743SerializationMapWith1001Objects() { + HashMap map = buildNestedMap(1001); + JSONObject object = new JSONObject(map); + String jsonString = object.toString(); + } + + /** + * Method to build nested map of max maxDepth + * + * @param maxDepth + * @return + */ + public static HashMap buildNestedMap(int maxDepth) { + if (maxDepth <= 0) { + return new HashMap<>(); + } + HashMap nestedMap = new HashMap<>(); + nestedMap.put("t", buildNestedMap(maxDepth - 1)); + return nestedMap; + } + } From abea194120cbfe684444a9012aac70fafaef5cfa Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Fri, 22 Dec 2023 15:44:33 +0530 Subject: [PATCH 086/233] Adding JSONParserConfiguration for configuring the depth of nested maps --- src/main/java/org/json/JSONArray.java | 69 +++++++++++++++---- src/main/java/org/json/JSONObject.java | 27 ++++---- .../org/json/JSONParserConfiguration.java | 29 ++++++++ .../java/org/json/junit/JSONArrayTest.java | 16 +++-- .../java/org/json/junit/JSONObjectTest.java | 8 ++- 5 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/json/JSONParserConfiguration.java diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index eec7852d5..6e19a5482 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -149,18 +149,22 @@ public JSONArray(String source) throws JSONException { * A Collection. */ public JSONArray(Collection collection) { - this(collection, 0); + this(collection, 0, new JSONParserConfiguration()); } - protected JSONArray(Collection collection, int recursionDepth) { - if (recursionDepth > JSONObject.RECURSION_DEPTH_LIMIT) { - throw new JSONException("JSONArray has reached recursion depth limit of " + JSONObject.RECURSION_DEPTH_LIMIT); + public JSONArray(Collection collection, JSONParserConfiguration jsonParserConfiguration) { + this(collection, 0, jsonParserConfiguration); + } + + protected JSONArray(Collection collection, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { + throw new JSONException("JSONArray has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } if (collection == null) { this.myArrayList = new ArrayList(); } else { this.myArrayList = new ArrayList(collection.size()); - this.addAll(collection, true, recursionDepth); + this.addAll(collection, true, recursionDepth, jsonParserConfiguration); } } @@ -1345,7 +1349,27 @@ public JSONArray put(int index, long value) throws JSONException { * If a key in the map is null */ public JSONArray put(int index, Map value) throws JSONException { - this.put(index, new JSONObject(value)); + this.put(index, new JSONObject(value, new JSONParserConfiguration())); + return this; + } + + /** + * Put a value in the JSONArray, where the value will be a JSONObject that + * is produced from a Map. + * + * @param index + * The subscript + * @param value + * The Map value. + * @param jsonParserConfiguration + * Configuration for recursive depth + * @return + * @throws JSONException + * If the index is negative or if the value is an invalid + * number. + */ + public JSONArray put(int index, Map value, JSONParserConfiguration jsonParserConfiguration) throws JSONException { + this.put(index, new JSONObject(value, jsonParserConfiguration)); return this; } @@ -1790,11 +1814,11 @@ public boolean isEmpty() { * variable to keep the count of how nested the object creation is happening. * */ - private void addAll(Collection collection, boolean wrap, int recursionDepth) { + private void addAll(Collection collection, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); if (wrap) { for (Object o: collection){ - this.put(JSONObject.wrap(o, recursionDepth + 1)); + this.put(JSONObject.wrap(o, recursionDepth + 1, jsonParserConfiguration)); } } else { for (Object o: collection){ @@ -1823,7 +1847,14 @@ private void addAll(Iterable iter, boolean wrap) { } } } - + + /** + * Add an array's elements to the JSONArray. + * + * @param array + * @param wrap + * @throws JSONException + */ private void addAll(Object array, boolean wrap) throws JSONException { this.addAll(array, wrap, 0); } @@ -1836,23 +1867,37 @@ private void addAll(Object array, boolean wrap) throws JSONException { * JSONArray, Collection, or Iterable, an exception will be * thrown. * @param wrap + * @param recursionDepth + */ + private void addAll(Object array, boolean wrap, int recursionDepth) { + addAll(array, wrap, recursionDepth, new JSONParserConfiguration()); + } + /** + * Add an array's elements to the JSONArray. + *` + * @param array + * Array. If the parameter passed is null, or not an array, + * JSONArray, Collection, or Iterable, an exception will be + * thrown. + * @param wrap * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly * @param recursionDepth * Variable to keep the count of how nested the object creation is happening. - * + * @param recursionDepth + * Variable to pass parser custom configuration for json parsing. * @throws JSONException * If not an array or if an array value is non-finite number. * @throws NullPointerException * Thrown if the array parameter is null. */ - private void addAll(Object array, boolean wrap, int recursionDepth) throws JSONException { + private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) throws JSONException { if (array.getClass().isArray()) { int length = Array.getLength(array); this.myArrayList.ensureCapacity(this.myArrayList.size() + length); if (wrap) { for (int i = 0; i < length; i += 1) { - this.put(JSONObject.wrap(Array.get(array, i), recursionDepth + 1)); + this.put(JSONObject.wrap(Array.get(array, i), recursionDepth + 1, jsonParserConfiguration)); } } else { for (int i = 0; i < length; i += 1) { diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 72c0ebd78..18721f7f6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -147,7 +147,6 @@ public String toString() { * The map where the JSONObject's properties are kept. */ private final Map map; - public static final int RECURSION_DEPTH_LIMIT = 1000; public Class getMapType() { return map.getClass(); @@ -277,16 +276,20 @@ public JSONObject(JSONTokener x) throws JSONException { * If a key in the map is null */ public JSONObject(Map m) { - this(m, 0); + this(m, 0, new JSONParserConfiguration()); + } + + public JSONObject(Map m, JSONParserConfiguration jsonParserConfiguration) { + this(m, 0, jsonParserConfiguration); } /** * Construct a JSONObject from a map with recursion depth. * */ - protected JSONObject(Map m, int recursionDepth) { - if (recursionDepth > RECURSION_DEPTH_LIMIT) { - throw new JSONException("JSONObject has reached recursion depth limit of " + RECURSION_DEPTH_LIMIT); + protected JSONObject(Map m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { + throw new JSONException("JSONObject has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } if (m == null) { this.map = new HashMap(); @@ -299,7 +302,7 @@ protected JSONObject(Map m, int recursionDepth) { final Object value = e.getValue(); if (value != null) { testValidity(value); - this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1)); + this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration)); } } } @@ -2578,15 +2581,15 @@ public static Object wrap(Object object) { return wrap(object, null); } - public static Object wrap(Object object, int recursionDepth) { - return wrap(object, null, recursionDepth); + public static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + return wrap(object, null, recursionDepth, jsonParserConfiguration); } private static Object wrap(Object object, Set objectsRecord) { - return wrap(object, objectsRecord, 0); + return wrap(object, objectsRecord, 0, new JSONParserConfiguration()); } - private static Object wrap(Object object, Set objectsRecord, int recursionDepth) { + private static Object wrap(Object object, Set objectsRecord, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { try { if (NULL.equals(object)) { return NULL; @@ -2604,14 +2607,14 @@ private static Object wrap(Object object, Set objectsRecord, int recursi if (object instanceof Collection) { Collection coll = (Collection) object; - return new JSONArray(coll, recursionDepth); + return new JSONArray(coll, recursionDepth, jsonParserConfiguration); } if (object.getClass().isArray()) { return new JSONArray(object); } if (object instanceof Map) { Map map = (Map) object; - return new JSONObject(map, recursionDepth); + return new JSONObject(map, recursionDepth, jsonParserConfiguration); } Package objectPackage = object.getClass().getPackage(); String objectPackageName = objectPackage != null ? objectPackage diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java new file mode 100644 index 000000000..910d1cfa5 --- /dev/null +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -0,0 +1,29 @@ +package org.json; + +/** + * Configuration object for the JSON parser. The configuration is immutable. + */ +public class JSONParserConfiguration extends ParserConfiguration { + + /** + * We can override the default maximum nesting depth if needed. + */ + public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH; + + /** + * Configuration with the default values. + */ + public JSONParserConfiguration() { + this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; + } + + public JSONParserConfiguration(int maxNestingDepth) { + this.maxNestingDepth = maxNestingDepth; + } + + @Override + protected JSONParserConfiguration clone() { + return new JSONParserConfiguration(DEFAULT_MAXIMUM_NESTING_DEPTH); + } + +} diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 5f50fc706..349422dcf 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -28,6 +28,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONParserConfiguration; import org.json.JSONPointerException; import org.json.JSONString; import org.json.JSONTokener; @@ -1440,15 +1441,15 @@ public void testRecursiveDepthArray() { } @Test - public void testRecursiveDepthAtPosition999Object() { - HashMap map = JSONObjectTest.buildNestedMap(999); + public void testRecursiveDepthAtPositionDefaultObject() { + HashMap map = JSONObjectTest.buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); new JSONArray().put(0, map); } @Test public void testRecursiveDepthAtPosition1000Object() { HashMap map = JSONObjectTest.buildNestedMap(1000); - new JSONArray().put(0, map); + new JSONArray().put(0, map, new JSONParserConfiguration(1000)); } @Test(expected = JSONException.class) @@ -1465,15 +1466,16 @@ public void testRecursiveDepthArrayLimitedMaps() { } @Test - public void testRecursiveDepthArrayFor999Levels() { - ArrayList array = buildNestedArray(999); - new JSONArray(array); + public void testRecursiveDepthArrayForDefaultLevels() { + ArrayList array = buildNestedArray(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + new JSONArray(array, new JSONParserConfiguration()); } @Test public void testRecursiveDepthArrayFor1000Levels() { ArrayList array = buildNestedArray(1000); - new JSONArray(array); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + new JSONArray(array, parserConfiguration); } @Test(expected = JSONException.class) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e157fd58e..1a911d8a9 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -32,6 +32,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.json.JSONPointerException; +import org.json.JSONParserConfiguration; import org.json.JSONString; import org.json.JSONTokener; import org.json.XML; @@ -3737,8 +3738,8 @@ public void testCircularReferenceMultipleLevel() { } @Test - public void issue743SerializationMapWith999Objects() { - HashMap map = buildNestedMap(999); + public void issue743SerializationMapWith512Objects() { + HashMap map = buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); JSONObject object = new JSONObject(map); String jsonString = object.toString(); } @@ -3746,7 +3747,8 @@ public void issue743SerializationMapWith999Objects() { @Test public void issue743SerializationMapWith1000Objects() { HashMap map = buildNestedMap(1000); - JSONObject object = new JSONObject(map); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + JSONObject object = new JSONObject(map, parserConfiguration); String jsonString = object.toString(); } From ffd48afa42f014dfbd48f86c3cef96ccc4e6c7df Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Sat, 23 Dec 2023 10:53:54 +0530 Subject: [PATCH 087/233] Review comments --- .../java/org/json/JSONParserConfiguration.java | 16 ++++++---------- src/test/java/org/json/junit/JSONArrayTest.java | 9 +++++---- src/test/java/org/json/junit/JSONObjectTest.java | 5 +++-- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 910d1cfa5..1ec171029 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -5,25 +5,21 @@ */ public class JSONParserConfiguration extends ParserConfiguration { - /** - * We can override the default maximum nesting depth if needed. - */ - public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH; - /** * Configuration with the default values. */ public JSONParserConfiguration() { - this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; + super(); } - public JSONParserConfiguration(int maxNestingDepth) { - this.maxNestingDepth = maxNestingDepth; + @Override + protected JSONParserConfiguration clone() { + return new JSONParserConfiguration(); } @Override - protected JSONParserConfiguration clone() { - return new JSONParserConfiguration(DEFAULT_MAXIMUM_NESTING_DEPTH); + public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { + return super.withMaxNestingDepth(maxNestingDepth); } } diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 349422dcf..fd0137978 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -32,6 +32,7 @@ import org.json.JSONPointerException; import org.json.JSONString; import org.json.JSONTokener; +import org.json.ParserConfiguration; import org.json.junit.data.MyJsonString; import org.junit.Ignore; import org.junit.Test; @@ -1442,14 +1443,14 @@ public void testRecursiveDepthArray() { @Test public void testRecursiveDepthAtPositionDefaultObject() { - HashMap map = JSONObjectTest.buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + HashMap map = JSONObjectTest.buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); new JSONArray().put(0, map); } @Test public void testRecursiveDepthAtPosition1000Object() { HashMap map = JSONObjectTest.buildNestedMap(1000); - new JSONArray().put(0, map, new JSONParserConfiguration(1000)); + new JSONArray().put(0, map, new JSONParserConfiguration().withMaxNestingDepth(1000)); } @Test(expected = JSONException.class) @@ -1467,14 +1468,14 @@ public void testRecursiveDepthArrayLimitedMaps() { @Test public void testRecursiveDepthArrayForDefaultLevels() { - ArrayList array = buildNestedArray(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + ArrayList array = buildNestedArray(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); new JSONArray(array, new JSONParserConfiguration()); } @Test public void testRecursiveDepthArrayFor1000Levels() { ArrayList array = buildNestedArray(1000); - JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); new JSONArray(array, parserConfiguration); } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 1a911d8a9..96f36735d 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -35,6 +35,7 @@ import org.json.JSONParserConfiguration; import org.json.JSONString; import org.json.JSONTokener; +import org.json.ParserConfiguration; import org.json.XML; import org.json.junit.data.BrokenToString; import org.json.junit.data.ExceptionalBean; @@ -3739,7 +3740,7 @@ public void testCircularReferenceMultipleLevel() { @Test public void issue743SerializationMapWith512Objects() { - HashMap map = buildNestedMap(JSONParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); + HashMap map = buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH); JSONObject object = new JSONObject(map); String jsonString = object.toString(); } @@ -3747,7 +3748,7 @@ public void issue743SerializationMapWith512Objects() { @Test public void issue743SerializationMapWith1000Objects() { HashMap map = buildNestedMap(1000); - JSONParserConfiguration parserConfiguration = new JSONParserConfiguration(1000); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); JSONObject object = new JSONObject(map, parserConfiguration); String jsonString = object.toString(); } From 7701f2183966dd4f1efad1b762b47a89009e60ad Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Sun, 24 Dec 2023 11:02:47 +0530 Subject: [PATCH 088/233] Adding comments --- src/main/java/org/json/JSONArray.java | 38 ++++++++++++++++++++++---- src/main/java/org/json/JSONObject.java | 29 ++++++++++++++++++-- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 6e19a5482..50146a803 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -152,11 +152,29 @@ public JSONArray(Collection collection) { this(collection, 0, new JSONParserConfiguration()); } + /** + * Construct a JSONArray from a Collection. + * + * @param collection + * A Collection. + * @param jsonParserConfiguration + * Configuration object for the JSON parser + */ public JSONArray(Collection collection, JSONParserConfiguration jsonParserConfiguration) { this(collection, 0, jsonParserConfiguration); } - protected JSONArray(Collection collection, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + /** + * Construct a JSONArray from a collection with recursion depth. + * + * @param collection + * A Collection. + * @param recursionDepth + * Variable for tracking the count of nested object creations. + * @param jsonParserConfiguration + * Configuration object for the JSON parser + */ + JSONArray(Collection collection, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { throw new JSONException("JSONArray has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } @@ -1362,7 +1380,7 @@ public JSONArray put(int index, Map value) throws JSONException { * @param value * The Map value. * @param jsonParserConfiguration - * Configuration for recursive depth + * Configuration object for the JSON parser * @return * @throws JSONException * If the index is negative or if the value is an invalid @@ -1811,8 +1829,7 @@ public boolean isEmpty() { * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly * @param recursionDepth - * variable to keep the count of how nested the object creation is happening. - * + * Variable for tracking the count of nested object creations. */ private void addAll(Collection collection, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); @@ -1852,8 +1869,14 @@ private void addAll(Iterable iter, boolean wrap) { * Add an array's elements to the JSONArray. * * @param array + * Array. If the parameter passed is null, or not an array, + * JSONArray, Collection, or Iterable, an exception will be + * thrown. * @param wrap + * {@code true} to call {@link JSONObject#wrap(Object)} for each item, + * {@code false} to add the items directly * @throws JSONException + * If not an array or if an array value is non-finite number. */ private void addAll(Object array, boolean wrap) throws JSONException { this.addAll(array, wrap, 0); @@ -1867,7 +1890,10 @@ private void addAll(Object array, boolean wrap) throws JSONException { * JSONArray, Collection, or Iterable, an exception will be * thrown. * @param wrap + * {@code true} to call {@link JSONObject#wrap(Object)} for each item, + * {@code false} to add the items directly * @param recursionDepth + * Variable for tracking the count of nested object creations. */ private void addAll(Object array, boolean wrap, int recursionDepth) { addAll(array, wrap, recursionDepth, new JSONParserConfiguration()); @@ -1883,8 +1909,8 @@ private void addAll(Object array, boolean wrap, int recursionDepth) { * {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code false} to add the items directly * @param recursionDepth - * Variable to keep the count of how nested the object creation is happening. - * @param recursionDepth + * Variable for tracking the count of nested object creations. + * @param jsonParserConfiguration * Variable to pass parser custom configuration for json parsing. * @throws JSONException * If not an array or if an array value is non-finite number. diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 18721f7f6..4bd032bdb 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -279,6 +279,15 @@ public JSONObject(Map m) { this(m, 0, new JSONParserConfiguration()); } + /** + * Construct a JSONObject from a Map with custom json parse configurations. + * + * @param m + * A map object that can be used to initialize the contents of + * the JSONObject. + * @param jsonParserConfiguration + * Variable to pass parser custom configuration for json parsing. + */ public JSONObject(Map m, JSONParserConfiguration jsonParserConfiguration) { this(m, 0, jsonParserConfiguration); } @@ -287,7 +296,7 @@ public JSONObject(Map m, JSONParserConfiguration jsonParserConfiguration) * Construct a JSONObject from a map with recursion depth. * */ - protected JSONObject(Map m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + private JSONObject(Map m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { throw new JSONException("JSONObject has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth()); } @@ -2581,7 +2590,23 @@ public static Object wrap(Object object) { return wrap(object, null); } - public static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { + /** + * Wrap an object, if necessary. If the object is null, return the NULL + * object. If it is an array or collection, wrap it in a JSONArray. If it is + * a map, wrap it in a JSONObject. If it is a standard property (Double, + * String, et al) then it is already wrapped. Otherwise, if it comes from + * one of the java packages, turn it into a string. And if it doesn't, try + * to wrap it in a JSONObject. If the wrapping fails, then null is returned. + * + * @param object + * The object to wrap + * @param recursionDepth + * Variable for tracking the count of nested object creations. + * @param jsonParserConfiguration + * Variable to pass parser custom configuration for json parsing. + * @return The wrapped value + */ + static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { return wrap(object, null, recursionDepth, jsonParserConfiguration); } From 23ac2e7bcaf6ec2c62d7e21d8d4254feaf56bbff Mon Sep 17 00:00:00 2001 From: Thomas Gress Date: Fri, 29 Dec 2023 12:28:24 +0100 Subject: [PATCH 089/233] improved annotation search performance --- src/main/java/org/json/JSONObject.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 4bd032bdb..039f136de 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1865,6 +1865,10 @@ private static A getAnnotation(final Method m, final Clas } } + //If the superclass is Object, no annotations will be found any more + if (c.getSuperclass().equals(Object.class)) + return null; + try { return getAnnotation( c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()), @@ -1919,6 +1923,10 @@ private static int getAnnotationDepth(final Method m, final Class Date: Sat, 30 Dec 2023 16:30:19 -0600 Subject: [PATCH 090/233] cleanup-and-merge-tests: fix warnings, set gradlew permissions, enable unchecked warnings in maven --- gradlew | 0 pom.xml | 3 +++ src/main/java/org/json/JSONArray.java | 5 +++-- src/main/java/org/json/JSONMLParserConfiguration.java | 2 ++ src/main/java/org/json/JSONParserConfiguration.java | 1 + src/main/java/org/json/ParserConfiguration.java | 2 ++ src/main/java/org/json/XMLParserConfiguration.java | 4 +++- src/test/java/org/json/junit/data/WeirdList.java | 6 +++--- 8 files changed, 17 insertions(+), 6 deletions(-) mode change 100644 => 100755 gradlew diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml index 927a989b1..d01283c6b 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,9 @@ 1.8 1.8 + + -Xlint:unchecked + diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 50146a803..38b0b31ae 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -1359,7 +1359,8 @@ public JSONArray put(int index, long value) throws JSONException { * The subscript. * @param value * The Map value. - * @return this. + * @return + * reference to self * @throws JSONException * If the index is negative or if the value is an invalid * number. @@ -1381,7 +1382,7 @@ public JSONArray put(int index, Map value) throws JSONException { * The Map value. * @param jsonParserConfiguration * Configuration object for the JSON parser - * @return + * @return reference to self * @throws JSONException * If the index is negative or if the value is an invalid * number. diff --git a/src/main/java/org/json/JSONMLParserConfiguration.java b/src/main/java/org/json/JSONMLParserConfiguration.java index b2514ab6e..43ba0db62 100644 --- a/src/main/java/org/json/JSONMLParserConfiguration.java +++ b/src/main/java/org/json/JSONMLParserConfiguration.java @@ -55,11 +55,13 @@ protected JSONMLParserConfiguration clone() { ); } + @SuppressWarnings("unchecked") @Override public JSONMLParserConfiguration withKeepStrings(final boolean newVal) { return super.withKeepStrings(newVal); } + @SuppressWarnings("unchecked") @Override public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 1ec171029..f95e24429 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -17,6 +17,7 @@ protected JSONParserConfiguration clone() { return new JSONParserConfiguration(); } + @SuppressWarnings("unchecked") @Override public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); diff --git a/src/main/java/org/json/ParserConfiguration.java b/src/main/java/org/json/ParserConfiguration.java index 36fa50833..ede2fc59e 100644 --- a/src/main/java/org/json/ParserConfiguration.java +++ b/src/main/java/org/json/ParserConfiguration.java @@ -75,6 +75,7 @@ public boolean isKeepStrings() { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") public T withKeepStrings(final boolean newVal) { T newConfig = (T)this.clone(); newConfig.keepStrings = newVal; @@ -101,6 +102,7 @@ public int getMaxNestingDepth() { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") public T withMaxNestingDepth(int maxNestingDepth) { T newConfig = (T)this.clone(); diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 2e4907f74..0ac7834b9 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -192,6 +192,7 @@ protected XMLParserConfiguration clone() { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") @Override public XMLParserConfiguration withKeepStrings(final boolean newVal) { return super.withKeepStrings(newVal); @@ -309,6 +310,7 @@ public XMLParserConfiguration withForceList(final Set forceList) { * @param maxNestingDepth the maximum nesting depth allowed to the XML parser * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") @Override public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); @@ -316,7 +318,7 @@ public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { /** * To enable explicit end tag with empty value. - * @param closeEmptyTag + * @param closeEmptyTag new value for the closeEmptyTag property * @return same instance of configuration with empty tag config updated */ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ diff --git a/src/test/java/org/json/junit/data/WeirdList.java b/src/test/java/org/json/junit/data/WeirdList.java index 834b81e86..35605863a 100644 --- a/src/test/java/org/json/junit/data/WeirdList.java +++ b/src/test/java/org/json/junit/data/WeirdList.java @@ -12,7 +12,7 @@ */ public class WeirdList { /** */ - private final List list = new ArrayList(); + private final List list = new ArrayList<>(); /** * @param vals @@ -25,14 +25,14 @@ public WeirdList(Integer... vals) { * @return a copy of the list */ public List get() { - return new ArrayList(this.list); + return new ArrayList<>(this.list); } /** * @return a copy of the list */ public List getALL() { - return new ArrayList(this.list); + return new ArrayList<>(this.list); } /** From 86bb0a1a02d49d2d1799153a45f7b8bcf2ed7015 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 30 Dec 2023 17:00:02 -0600 Subject: [PATCH 091/233] cleanup-and-merge-tests: pull in unit tests from #809 --- .../java/org/json/junit/JSONObjectTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 96f36735d..053f17a91 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3760,6 +3760,48 @@ public void issue743SerializationMapWith1001Objects() { String jsonString = object.toString(); } + @Test(expected = JSONException.class) + public void testCircleReferenceFirstLevel() { + Map jsonObject = new HashMap<>(); + + jsonObject.put("test", jsonObject); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test(expected = StackOverflowError.class) + public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration().withMaxNestingDepth(99999)); + } + + @Test(expected = JSONException.class) + public void testCircleReferenceMultiplyLevel_configured_expectedJSONException() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test + public void testDifferentKeySameInstanceNotACircleReference() { + Map map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + + map1.put("test1", map2); + map1.put("test2", map2); + + new JSONObject(map1); + } + /** * Method to build nested map of max maxDepth * From 19dec1bb5fcff839d606edd5cf0d5d2059bce626 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Fri, 2 Feb 2024 13:11:37 -0600 Subject: [PATCH 092/233] Fixing JSONArrayTest testRecursiveDepthArrayFor1000Levels() --- .../java/org/json/junit/JSONArrayTest.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index fd0137978..fcaa8cea0 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -1474,9 +1474,23 @@ public void testRecursiveDepthArrayForDefaultLevels() { @Test public void testRecursiveDepthArrayFor1000Levels() { - ArrayList array = buildNestedArray(1000); - JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); - new JSONArray(array, parserConfiguration); + try { + ArrayList array = buildNestedArray(1000); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); + new JSONArray(array, parserConfiguration); + } catch (StackOverflowError e) { + String javaVersion = System.getProperty("java.version"); + if (javaVersion.startsWith("11.")) { + System.out.println( + "testRecursiveDepthArrayFor1000Levels() allowing intermittent stackoverflow, Java Version: " + + javaVersion); + } else { + String errorStr = "testRecursiveDepthArrayFor1000Levels() unexpected stackoverflow, Java Version: " + + javaVersion; + System.out.println(errorStr); + throw new RuntimeException(errorStr); + } + } } @Test(expected = JSONException.class) From 4548696c8dc5d05a11c840ed224ab0dc3b037ed8 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 5 Feb 2024 20:22:23 -0600 Subject: [PATCH 093/233] Update README.md for release 20240205 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9806f2a88..6d17373ce 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ JSON in Java [package org.json] [![Java CI with Maven](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml) [![CodeQL](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20231013/json-20231013.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20240205/json-20240205.jar)** # Overview @@ -26,7 +26,7 @@ Project goals include: * No external dependencies * Fast execution and low memory footprint * Maintain backward compatibility -* Designed and tested to use on Java versions 1.8 - 21 +* Designed and tested to use on Java versions 1.6 - 21 The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL. From 9865dbbebebea5d7e7018907ead0ad508cc6c4f8 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 5 Feb 2024 20:23:59 -0600 Subject: [PATCH 094/233] Update pom.xml for release 20240205 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d01283c6b..7196978d0 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.json json - 20231013 + 20240205 bundle JSON in Java From 010e83b925642a654bd02a90fdddc26222e425f0 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Mon, 5 Feb 2024 20:44:18 -0600 Subject: [PATCH 095/233] Update RELEASES.md for release 20240205 --- docs/RELEASES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 2b8aaa267..3308e6ecf 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -5,6 +5,8 @@ and artifactId "json". For example: [https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav) ~~~ +20240205 Recent commits. + 20231013 First release with minimum Java version 1.8. Recent commits, including fixes for CVE-2023-5072. 20230618 Final release with Java 1.6 compatibility. Future releases will require Java 1.8 or greater. From 99c84fdf3a7f4218734a3b2e4b1e36d5ecb60601 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 2 Feb 2024 09:07:48 +0200 Subject: [PATCH 096/233] Enhanced documentation for Java classes --- pom.xml | 3 +++ src/main/java/org/json/JSONObject.java | 14 +++++++++++++- src/main/java/org/json/JSONPointer.java | 6 ++++++ src/main/java/org/json/JSONPointerException.java | 11 +++++++++++ src/main/java/org/json/JSONTokener.java | 5 +++++ src/main/java/org/json/ParserConfiguration.java | 9 +++++++++ src/main/java/org/json/XML.java | 3 +++ src/main/java/org/json/XMLParserConfiguration.java | 11 +++++++++++ src/main/java/org/json/XMLXsiTypeConverter.java | 7 +++++++ 9 files changed, 68 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7196978d0..1488f4983 100644 --- a/pom.xml +++ b/pom.xml @@ -126,6 +126,9 @@ org.apache.maven.plugins maven-javadoc-plugin 3.5.0 + + 8 + attach-javadocs diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 039f136de..4c08b0b9c 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -148,6 +148,11 @@ public String toString() { */ private final Map map; + /** + * Retrieves the type of the underlying Map in this class. + * + * @return The class object representing the type of the underlying Map. + */ public Class getMapType() { return map.getClass(); } @@ -369,7 +374,6 @@ private JSONObject(Map m, int recursionDepth, JSONParserConfiguration json * @JSONPropertyIgnore * public String getName() { return this.name; } * - *

* * @param bean * An object that has getter methods that should be used to make @@ -2232,6 +2236,14 @@ public static String quote(String string) { } } + /** + * Quotes a string and appends the result to a given Writer. + * + * @param string The input string to be quoted. + * @param w The Writer to which the quoted string will be appended. + * @return The same Writer instance after appending the quoted string. + * @throws IOException If an I/O error occurs while writing to the Writer. + */ public static Writer quote(String string, Writer w) throws IOException { if (string == null || string.isEmpty()) { w.write("\"\""); diff --git a/src/main/java/org/json/JSONPointer.java b/src/main/java/org/json/JSONPointer.java index 963fdec3e..91bd137ca 100644 --- a/src/main/java/org/json/JSONPointer.java +++ b/src/main/java/org/json/JSONPointer.java @@ -163,6 +163,12 @@ public JSONPointer(final String pointer) { //} } + /** + * Constructs a new JSONPointer instance with the provided list of reference tokens. + * + * @param refTokens A list of strings representing the reference tokens for the JSON Pointer. + * Each token identifies a step in the path to the targeted value. + */ public JSONPointer(List refTokens) { this.refTokens = new ArrayList(refTokens); } diff --git a/src/main/java/org/json/JSONPointerException.java b/src/main/java/org/json/JSONPointerException.java index a0e128cd5..dc5a25ad6 100644 --- a/src/main/java/org/json/JSONPointerException.java +++ b/src/main/java/org/json/JSONPointerException.java @@ -14,10 +14,21 @@ public class JSONPointerException extends JSONException { private static final long serialVersionUID = 8872944667561856751L; + /** + * Constructs a new JSONPointerException with the specified error message. + * + * @param message The detail message describing the reason for the exception. + */ public JSONPointerException(String message) { super(message); } + /** + * Constructs a new JSONPointerException with the specified error message and cause. + * + * @param message The detail message describing the reason for the exception. + * @param cause The cause of the exception. + */ public JSONPointerException(String message, Throwable cause) { super(message, cause); } diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 4a83a6971..0bc6dfb68 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -525,6 +525,11 @@ public String toString() { this.line + "]"; } + /** + * Closes the underlying reader, releasing any resources associated with it. + * + * @throws IOException If an I/O error occurs while closing the reader. + */ public void close() throws IOException { if(reader!=null){ reader.close(); diff --git a/src/main/java/org/json/ParserConfiguration.java b/src/main/java/org/json/ParserConfiguration.java index ede2fc59e..5cdc10d89 100644 --- a/src/main/java/org/json/ParserConfiguration.java +++ b/src/main/java/org/json/ParserConfiguration.java @@ -29,11 +29,20 @@ public class ParserConfiguration { */ protected int maxNestingDepth; + /** + * Constructs a new ParserConfiguration with default settings. + */ public ParserConfiguration() { this.keepStrings = false; this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; } + /** + * Constructs a new ParserConfiguration with the specified settings. + * + * @param keepStrings A boolean indicating whether to preserve strings during parsing. + * @param maxNestingDepth An integer representing the maximum allowed nesting depth. + */ protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) { this.keepStrings = keepStrings; this.maxNestingDepth = maxNestingDepth; diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 9d42bb3b0..301c8ba8d 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -56,6 +56,9 @@ public class XML { */ public static final String NULL_ATTR = "xsi:nil"; + /** + * Represents the XML attribute name for specifying type information. + */ public static final String TYPE_ATTR = "xsi:type"; /** diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 5087aa1fb..bc4a80074 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -352,9 +352,20 @@ public XMLParserConfiguration withShouldTrimWhitespace(boolean shouldTrimWhiteSp return clonedConfiguration; } + /** + * Checks if the parser should automatically close empty XML tags. + * + * @return {@code true} if empty XML tags should be automatically closed, {@code false} otherwise. + */ public boolean isCloseEmptyTag() { return this.closeEmptyTag; } + + /** + * Checks if the parser should trim white spaces from XML content. + * + * @return {@code true} if white spaces should be trimmed, {@code false} otherwise. + */ public boolean shouldTrimWhiteSpace() { return this.shouldTrimWhiteSpace; } diff --git a/src/main/java/org/json/XMLXsiTypeConverter.java b/src/main/java/org/json/XMLXsiTypeConverter.java index 0011effae..ea6739d34 100644 --- a/src/main/java/org/json/XMLXsiTypeConverter.java +++ b/src/main/java/org/json/XMLXsiTypeConverter.java @@ -42,5 +42,12 @@ * @param return type of convert method */ public interface XMLXsiTypeConverter { + + /** + * Converts an XML xsi:type attribute value to the specified type {@code T}. + * + * @param value The string representation of the XML xsi:type attribute value to be converted. + * @return An object of type {@code T} representing the converted value. + */ T convert(String value); } From 72214f1b43947bb8e148c02d128ae5e84839b7b0 Mon Sep 17 00:00:00 2001 From: mameri Date: Fri, 9 Feb 2024 11:52:18 +0100 Subject: [PATCH 097/233] add ability for custom delimiters --- src/main/java/org/json/CDL.java | 183 +++++++++++++++------- src/test/java/org/json/junit/CDLTest.java | 60 ++++--- 2 files changed, 164 insertions(+), 79 deletions(-) diff --git a/src/main/java/org/json/CDL.java b/src/main/java/org/json/CDL.java index 848831d3b..251386b26 100644 --- a/src/main/java/org/json/CDL.java +++ b/src/main/java/org/json/CDL.java @@ -5,15 +5,15 @@ */ /** - * This provides static methods to convert comma delimited text into a - * JSONArray, and to convert a JSONArray into comma delimited text. Comma + * This provides static methods to convert comma (or otherwise) delimited text into a + * JSONArray, and to convert a JSONArray into comma (or otherwise) delimited text. Comma * delimited text is a very popular format for data interchange. It is * understood by most database, spreadsheet, and organizer programs. *

* Each row of text represents a row in a table or a data record. Each row * ends with a NEWLINE character. Each row contains one or more values. * Values are separated by commas. A value can contain any character except - * for comma, unless is is wrapped in single quotes or double quotes. + * for comma, unless it is wrapped in single quotes or double quotes. *

* The first row usually contains the names of the columns. *

@@ -29,50 +29,48 @@ public class CDL { * Get the next value. The value can be wrapped in quotes. The value can * be empty. * @param x A JSONTokener of the source text. + * @param delimiter used in the file * @return The value string, or null if empty. * @throws JSONException if the quoted string is badly formed. */ - private static String getValue(JSONTokener x) throws JSONException { + private static String getValue(JSONTokener x, char delimiter) throws JSONException { char c; char q; StringBuilder sb; do { c = x.next(); } while (c == ' ' || c == '\t'); - switch (c) { - case 0: - return null; - case '"': - case '\'': - q = c; - sb = new StringBuilder(); - for (;;) { - c = x.next(); - if (c == q) { - //Handle escaped double-quote - char nextC = x.next(); - if(nextC != '\"') { - // if our quote was the end of the file, don't step - if(nextC > 0) { - x.back(); - } - break; - } - } - if (c == 0 || c == '\n' || c == '\r') { - throw x.syntaxError("Missing close quote '" + q + "'."); - } - sb.append(c); - } - return sb.toString(); - case ',': - x.back(); - return ""; - default: - x.back(); - return x.nextTo(','); - } - } + if (c == 0) { + return null; + } else if (c == '"' || c == '\'') { + q = c; + sb = new StringBuilder(); + for (;;) { + c = x.next(); + if (c == q) { + //Handle escaped double-quote + char nextC = x.next(); + if (nextC != '\"') { + // if our quote was the end of the file, don't step + if (nextC > 0) { + x.back(); + } + break; + } + } + if (c == 0 || c == '\n' || c == '\r') { + throw x.syntaxError("Missing close quote '" + q + "'."); + } + sb.append(c); + } + return sb.toString(); + } else if (c == delimiter) { + x.back(); + return ""; + } + x.back(); + return x.nextTo(delimiter); + } /** * Produce a JSONArray of strings from a row of comma delimited values. @@ -81,17 +79,25 @@ private static String getValue(JSONTokener x) throws JSONException { * @throws JSONException if a called function fails */ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { + return rowToJSONArray(x, ','); + } + + /** + * Same as {@link #rowToJSONArray(JSONTokener)}, but with a custom delimiter. + * @see #rowToJSONArray(JSONTokener) + */ + public static JSONArray rowToJSONArray(JSONTokener x, char delimiter) throws JSONException { JSONArray ja = new JSONArray(); for (;;) { - String value = getValue(x); + String value = getValue(x,delimiter); char c = x.next(); if (value == null || - (ja.length() == 0 && value.length() == 0 && c != ',')) { + (ja.length() == 0 && value.length() == 0 && c != delimiter)) { return null; } ja.put(value); for (;;) { - if (c == ',') { + if (c == delimiter) { break; } if (c != ' ') { @@ -116,9 +122,17 @@ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { * @return A JSONObject combining the names and values. * @throws JSONException if a called function fails */ - public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) - throws JSONException { - JSONArray ja = rowToJSONArray(x); + public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws JSONException { + return rowToJSONObject(names, x, ','); + } + + /** + * Same as {@link #rowToJSONObject(JSONArray, JSONTokener)}, but with a custom {@code delimiter}. + * + * @see #rowToJSONObject(JSONArray, JSONTokener) + */ + public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x, char delimiter) throws JSONException { + JSONArray ja = rowToJSONArray(x, delimiter); return ja != null ? ja.toJSONObject(names) : null; } @@ -130,15 +144,23 @@ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) * @return A string ending in NEWLINE. */ public static String rowToString(JSONArray ja) { + return rowToString(ja, ','); + } + + /** + * Same as {@link #rowToString(JSONArray)}, but with a custom delimiter. + * @see #rowToString(JSONArray) + */ + public static String rowToString(JSONArray ja, char delimiter) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ja.length(); i += 1) { if (i > 0) { - sb.append(','); + sb.append(delimiter); } Object object = ja.opt(i); if (object != null) { String string = object.toString(); - if (string.length() > 0 && (string.indexOf(',') >= 0 || + if (string.length() > 0 && (string.indexOf(delimiter) >= 0 || string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 || string.indexOf(0) >= 0 || string.charAt(0) == '"')) { sb.append('"'); @@ -167,7 +189,15 @@ public static String rowToString(JSONArray ja) { * @throws JSONException if a called function fails */ public static JSONArray toJSONArray(String string) throws JSONException { - return toJSONArray(new JSONTokener(string)); + return toJSONArray(string, ','); + } + + /** + * Same as {@link #toJSONArray(String)}, but with a custom delimiter. + * @see #toJSONArray(String) + */ + public static JSONArray toJSONArray(String string, char delimiter) throws JSONException { + return toJSONArray(new JSONTokener(string), delimiter); } /** @@ -178,7 +208,15 @@ public static JSONArray toJSONArray(String string) throws JSONException { * @throws JSONException if a called function fails */ public static JSONArray toJSONArray(JSONTokener x) throws JSONException { - return toJSONArray(rowToJSONArray(x), x); + return toJSONArray(x, ','); + } + + /** + * Same as {@link #toJSONArray(JSONTokener)}, but with a custom delimiter. + * @see #toJSONArray(JSONTokener) + */ + public static JSONArray toJSONArray(JSONTokener x, char delimiter) throws JSONException { + return toJSONArray(rowToJSONArray(x, delimiter), x, delimiter); } /** @@ -189,9 +227,16 @@ public static JSONArray toJSONArray(JSONTokener x) throws JSONException { * @return A JSONArray of JSONObjects. * @throws JSONException if a called function fails */ - public static JSONArray toJSONArray(JSONArray names, String string) - throws JSONException { - return toJSONArray(names, new JSONTokener(string)); + public static JSONArray toJSONArray(JSONArray names, String string) throws JSONException { + return toJSONArray(names, string, ','); + } + + /** + * Same as {@link #toJSONArray(JSONArray, String)}, but with a custom delimiter. + * @see #toJSONArray(JSONArray, String) + */ + public static JSONArray toJSONArray(JSONArray names, String string, char delimiter) throws JSONException { + return toJSONArray(names, new JSONTokener(string), delimiter); } /** @@ -202,14 +247,21 @@ public static JSONArray toJSONArray(JSONArray names, String string) * @return A JSONArray of JSONObjects. * @throws JSONException if a called function fails */ - public static JSONArray toJSONArray(JSONArray names, JSONTokener x) - throws JSONException { + public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException { + return toJSONArray(names, x, ','); + } + + /** + * Same as {@link #toJSONArray(JSONArray, JSONTokener)}, but with a custom delimiter. + * @see #toJSONArray(JSONArray, JSONTokener) + */ + public static JSONArray toJSONArray(JSONArray names, JSONTokener x, char delimiter) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (;;) { - JSONObject jo = rowToJSONObject(names, x); + JSONObject jo = rowToJSONObject(names, x, delimiter); if (jo == null) { break; } @@ -231,11 +283,19 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) * @throws JSONException if a called function fails */ public static String toString(JSONArray ja) throws JSONException { + return toString(ja, ','); + } + + /** + * Same as {@link #toString(JSONArray)}, but with a custom delimiter. + * @see #toString(JSONArray) + */ + public static String toString(JSONArray ja, char delimiter) throws JSONException { JSONObject jo = ja.optJSONObject(0); if (jo != null) { JSONArray names = jo.names(); if (names != null) { - return rowToString(names) + toString(names, ja); + return rowToString(names, delimiter) + toString(names, ja, delimiter); } } return null; @@ -250,8 +310,15 @@ public static String toString(JSONArray ja) throws JSONException { * @return A comma delimited text. * @throws JSONException if a called function fails */ - public static String toString(JSONArray names, JSONArray ja) - throws JSONException { + public static String toString(JSONArray names, JSONArray ja) throws JSONException { + return toString(names, ja, ','); + } + + /** + * Same as {@link #toString(JSONArray,JSONArray)}, but with a custom delimiter. + * @see #toString(JSONArray,JSONArray) + */ + public static String toString(JSONArray names, JSONArray ja, char delimiter) throws JSONException { if (names == null || names.length() == 0) { return null; } @@ -259,7 +326,7 @@ public static String toString(JSONArray names, JSONArray ja) for (int i = 0; i < ja.length(); i += 1) { JSONObject jo = ja.optJSONObject(i); if (jo != null) { - sb.append(rowToString(jo.toJSONArray(names))); + sb.append(rowToString(jo.toJSONArray(names), delimiter)); } } return sb.toString(); diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java index f3364fbba..cc3da2983 100644 --- a/src/test/java/org/json/junit/CDLTest.java +++ b/src/test/java/org/json/junit/CDLTest.java @@ -24,14 +24,13 @@ public class CDLTest { * String of lines where the column names are in the first row, * and all subsequent rows are values. All keys and values should be legal. */ - String lines = new String( - "Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" + - "val1, val2, val3, val4, val5, val6, val7\n" + - "1, 2, 3, 4\t, 5, 6, 7\n" + - "true, false, true, true, false, false, false\n" + - "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + - "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n" - ); + private static final String LINES = "Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" + + "val1, val2, val3, val4, val5, val6, val7\n" + + "1, 2, 3, 4\t, 5, 6, 7\n" + + "true, false, true, true, false, false, false\n" + + "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" + + "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va'l6, val7\n"; + /** * CDL.toJSONArray() adds all values as strings, with no filtering or @@ -39,12 +38,11 @@ public class CDLTest { * values all must be quoted in the cases where the JSONObject parsing * might normally convert the value into a non-string. */ - String expectedLines = new String( - "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, "+ - "{Col 1:\"1\", Col 2:\"2\", Col 3:\"3\", Col 4:\"4\", Col 5:\"5\", Col 6:\"6\", Col 7:\"7\"}, "+ - "{Col 1:\"true\", Col 2:\"false\", Col 3:\"true\", Col 4:\"true\", Col 5:\"false\", Col 6:\"false\", Col 7:\"false\"}, "+ - "{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, "+ - "{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va\'l6, Col 7:val7}]"); + private static final String EXPECTED_LINES = "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, " + + "{Col 1:\"1\", Col 2:\"2\", Col 3:\"3\", Col 4:\"4\", Col 5:\"5\", Col 6:\"6\", Col 7:\"7\"}, " + + "{Col 1:\"true\", Col 2:\"false\", Col 3:\"true\", Col 4:\"true\", Col 5:\"false\", Col 6:\"false\", Col 7:\"false\"}, " + + "{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, " + + "{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va'l6, Col 7:val7}]"; /** * Attempts to create a JSONArray from a null string. @@ -194,8 +192,7 @@ public void nullJSONArrayToString() { public void emptyString() { String emptyStr = ""; JSONArray jsonArray = CDL.toJSONArray(emptyStr); - assertTrue("CDL should return null when the input string is empty", - jsonArray == null); + assertNull("CDL should return null when the input string is empty", jsonArray); } /** @@ -254,7 +251,7 @@ public void checkSpecialChars() { jsonObject.put("Col \r1", "V1"); // \r will be filtered from value jsonObject.put("Col 2", "V2\r"); - assertTrue("expected length should be 1",jsonArray.length() == 1); + assertEquals("expected length should be 1", 1, jsonArray.length()); String cdlStr = CDL.toString(jsonArray); jsonObject = jsonArray.getJSONObject(0); assertTrue(cdlStr.contains("\"Col 1\"")); @@ -268,8 +265,15 @@ public void checkSpecialChars() { */ @Test public void textToJSONArray() { - JSONArray jsonArray = CDL.toJSONArray(this.lines); - JSONArray expectedJsonArray = new JSONArray(this.expectedLines); + JSONArray jsonArray = CDL.toJSONArray(LINES); + JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES); + Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); + } + @Test + public void textToJSONArrayPipeDelimited() { + char delimiter = '|'; + JSONArray jsonArray = CDL.toJSONArray(LINES.replaceAll(",", String.valueOf(delimiter)), delimiter); + JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); } @@ -293,10 +297,24 @@ public void jsonArrayToJSONArray() { */ @Test public void textToJSONArrayAndBackToString() { - JSONArray jsonArray = CDL.toJSONArray(this.lines); + JSONArray jsonArray = CDL.toJSONArray(LINES); String jsonStr = CDL.toString(jsonArray); JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); - JSONArray expectedJsonArray = new JSONArray(this.expectedLines); + JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES); + Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); + } + + /** + * Create a JSONArray from a string of lines, + * then convert to string and then back to JSONArray + * with a custom delimiter + */ + @Test + public void textToJSONArrayAndBackToStringCustomDelimiter() { + JSONArray jsonArray = CDL.toJSONArray(LINES, ','); + String jsonStr = CDL.toString(jsonArray, ';'); + JSONArray finalJsonArray = CDL.toJSONArray(jsonStr, ';'); + JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES); Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); } From 10514e48cb665a973e8c9b04db577fa6fdaddf07 Mon Sep 17 00:00:00 2001 From: XIAYM-gh Date: Tue, 13 Feb 2024 18:56:10 +0800 Subject: [PATCH 098/233] Implemented custom duplicate key handling - Supports: throw an exception (by default), ignore, overwrite & merge into a JSONArray - With tests, 4/4 passed. --- .../org/json/JSONDuplicateKeyStrategy.java | 28 ++++++ src/main/java/org/json/JSONObject.java | 96 +++++++++++++++---- .../org/json/JSONParserConfiguration.java | 54 ++++++++--- .../junit/JSONObjectDuplicateKeyTest.java | 47 +++++++++ 4 files changed, 191 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/json/JSONDuplicateKeyStrategy.java create mode 100644 src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java diff --git a/src/main/java/org/json/JSONDuplicateKeyStrategy.java b/src/main/java/org/json/JSONDuplicateKeyStrategy.java new file mode 100644 index 000000000..4652dbcf5 --- /dev/null +++ b/src/main/java/org/json/JSONDuplicateKeyStrategy.java @@ -0,0 +1,28 @@ +package org.json; + +/** + * An enum class that is supposed to be used in {@link JSONParserConfiguration}, + * it dedicates which way should be used to handle duplicate keys. + */ +public enum JSONDuplicateKeyStrategy { + /** + * The default value. And this is the way it used to be in the previous versions.
+ * The JSONParser will throw an {@link JSONException} when meet duplicate key. + */ + THROW_EXCEPTION, + + /** + * The JSONParser will ignore duplicate keys and won't overwrite the value of the key. + */ + IGNORE, + + /** + * The JSONParser will overwrite the old value of the key. + */ + OVERWRITE, + + /** + * The JSONParser will try to merge the values of the duplicate key into a {@link JSONArray}. + */ + MERGE_INTO_ARRAY +} diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 039f136de..e7d5cd51d 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -15,17 +15,8 @@ import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.Iterator; -import java.util.Locale; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.ResourceBundle; -import java.util.Set; import java.util.regex.Pattern; import static org.json.NumberConversionUtil.potentialNumber; @@ -203,9 +194,28 @@ public JSONObject(JSONObject jo, String ... names) { * duplicated key. */ public JSONObject(JSONTokener x) throws JSONException { + this(x, new JSONParserConfiguration()); + } + + /** + * Construct a JSONObject from a JSONTokener with custom json parse configurations. + * + * @param x + * A JSONTokener object containing the source string. + * @param jsonParserConfiguration + * Variable to pass parser custom configuration for json parsing. + * @throws JSONException + * If there is a syntax error in the source string or a + * duplicated key. + */ + public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException { this(); char c; String key; + JSONDuplicateKeyStrategy duplicateKeyStrategy = jsonParserConfiguration.getDuplicateKeyStrategy(); + + // A list to store merged keys + List mergedKeys = null; if (x.nextClean() != '{') { throw x.syntaxError("A JSONObject text must begin with '{'"); @@ -232,14 +242,45 @@ public JSONObject(JSONTokener x) throws JSONException { if (key != null) { // Check if key exists - if (this.opt(key) != null) { - // key already exists - throw x.syntaxError("Duplicate key \"" + key + "\""); + boolean keyExists = this.opt(key) != null; + // Read value early to make the tokener work well + Object value = null; + if (!keyExists || duplicateKeyStrategy != JSONDuplicateKeyStrategy.THROW_EXCEPTION) { + value = x.nextValue(); } - // Only add value if non-null - Object value = x.nextValue(); - if (value!=null) { - this.put(key, value); + + if (keyExists) { + switch (duplicateKeyStrategy) { + case THROW_EXCEPTION: + throw x.syntaxError("Duplicate key \"" + key + "\""); + + case MERGE_INTO_ARRAY: + if (mergedKeys == null) { + mergedKeys = new ArrayList<>(); + } + + Object current = this.get(key); + if (current instanceof JSONArray && mergedKeys.contains(key)) { + ((JSONArray) current).put(value); + break; + } + + JSONArray merged = new JSONArray(); + merged.put(current); + merged.put(value); + this.put(key, merged); + mergedKeys.add(key); + break; + } + + // == IGNORE, ignored :) + } + + if (!keyExists || duplicateKeyStrategy == JSONDuplicateKeyStrategy.OVERWRITE) { + // Only add value if non-null + if (value != null) { + this.put(key, value); + } } } @@ -294,7 +335,6 @@ public JSONObject(Map m, JSONParserConfiguration jsonParserConfiguration) /** * Construct a JSONObject from a map with recursion depth. - * */ private JSONObject(Map m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) { @@ -426,7 +466,25 @@ public JSONObject(Object object, String ... names) { * duplicated key. */ public JSONObject(String source) throws JSONException { - this(new JSONTokener(source)); + this(source, new JSONParserConfiguration()); + } + + /** + * Construct a JSONObject from a source JSON text string with custom json parse configurations. + * This is the most commonly used JSONObject constructor. + * + * @param source + * A string beginning with { (left + * brace) and ending with } + *  (right brace). + * @param jsonParserConfiguration + * Variable to pass parser custom configuration for json parsing. + * @exception JSONException + * If there is a syntax error in the source string or a + * duplicated key. + */ + public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException { + this(new JSONTokener(source), jsonParserConfiguration); } /** diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index f95e24429..f1ea2b22e 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -4,23 +4,47 @@ * Configuration object for the JSON parser. The configuration is immutable. */ public class JSONParserConfiguration extends ParserConfiguration { + /** + * The way should be used to handle duplicate keys. + */ + private JSONDuplicateKeyStrategy duplicateKeyStrategy; - /** - * Configuration with the default values. - */ - public JSONParserConfiguration() { - super(); - } + /** + * Configuration with the default values. + */ + public JSONParserConfiguration() { + this(JSONDuplicateKeyStrategy.THROW_EXCEPTION); + } - @Override - protected JSONParserConfiguration clone() { - return new JSONParserConfiguration(); - } + /** + * Configure the parser with {@link JSONDuplicateKeyStrategy}. + * + * @param duplicateKeyStrategy Indicate which way should be used to handle duplicate keys. + */ + public JSONParserConfiguration(JSONDuplicateKeyStrategy duplicateKeyStrategy) { + super(); + this.duplicateKeyStrategy = duplicateKeyStrategy; + } - @SuppressWarnings("unchecked") - @Override - public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { - return super.withMaxNestingDepth(maxNestingDepth); - } + @Override + protected JSONParserConfiguration clone() { + return new JSONParserConfiguration(); + } + @SuppressWarnings("unchecked") + @Override + public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { + return super.withMaxNestingDepth(maxNestingDepth); + } + + public JSONParserConfiguration withDuplicateKeyStrategy(final JSONDuplicateKeyStrategy duplicateKeyStrategy) { + JSONParserConfiguration newConfig = this.clone(); + newConfig.duplicateKeyStrategy = duplicateKeyStrategy; + + return newConfig; + } + + public JSONDuplicateKeyStrategy getDuplicateKeyStrategy() { + return this.duplicateKeyStrategy; + } } diff --git a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java new file mode 100644 index 000000000..73dc70b90 --- /dev/null +++ b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java @@ -0,0 +1,47 @@ +package org.json.junit; + +import org.json.*; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class JSONObjectDuplicateKeyTest { + private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\", \"key\": \"value3\"}"; + + @Test(expected = JSONException.class) + public void testThrowException() { + new JSONObject(TEST_SOURCE); + } + + @Test + public void testIgnore() { + JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration( + JSONDuplicateKeyStrategy.IGNORE + )); + + assertEquals("duplicate key shouldn't be overwritten", "value1", jsonObject.getString("key")); + } + + @Test + public void testOverwrite() { + JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration( + JSONDuplicateKeyStrategy.OVERWRITE + )); + + assertEquals("duplicate key should be overwritten", "value3", jsonObject.getString("key")); + } + + @Test + public void testMergeIntoArray() { + JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration( + JSONDuplicateKeyStrategy.MERGE_INTO_ARRAY + )); + + JSONArray jsonArray; + assertTrue("duplicate key should be merged into JSONArray", jsonObject.get("key") instanceof JSONArray + && (jsonArray = jsonObject.getJSONArray("key")).length() == 3 + && jsonArray.getString(0).equals("value1") && jsonArray.getString(1).equals("value2") + && jsonArray.getString(2).equals("value3")); + } +} From 21a9fae7b042829f76e1f8ae7aab1ece66f72fb3 Mon Sep 17 00:00:00 2001 From: XIAYM-gh Date: Tue, 13 Feb 2024 22:33:30 +0800 Subject: [PATCH 099/233] Try making java 6 & old version javadoc generator compatible --- src/main/java/org/json/JSONDuplicateKeyStrategy.java | 2 +- src/main/java/org/json/JSONObject.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONDuplicateKeyStrategy.java b/src/main/java/org/json/JSONDuplicateKeyStrategy.java index 4652dbcf5..954ac3a25 100644 --- a/src/main/java/org/json/JSONDuplicateKeyStrategy.java +++ b/src/main/java/org/json/JSONDuplicateKeyStrategy.java @@ -6,7 +6,7 @@ */ public enum JSONDuplicateKeyStrategy { /** - * The default value. And this is the way it used to be in the previous versions.
+ * The default value. And this is the way it used to be in the previous versions.
* The JSONParser will throw an {@link JSONException} when meet duplicate key. */ THROW_EXCEPTION, diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index e7d5cd51d..1572a81be 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -256,7 +256,7 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration case MERGE_INTO_ARRAY: if (mergedKeys == null) { - mergedKeys = new ArrayList<>(); + mergedKeys = new ArrayList(); } Object current = this.get(key); From f164b8c597e172aec537c791b067a7cce2e3de26 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Tue, 13 Feb 2024 20:08:54 -0600 Subject: [PATCH 100/233] cleanup-after-commit reverted pom.xml version 8 change and tabs in cdl. Updated JavaDocs in cdl --- pom.xml | 3 - src/main/java/org/json/CDL.java | 142 +++++++++++++++++++++----------- 2 files changed, 92 insertions(+), 53 deletions(-) diff --git a/pom.xml b/pom.xml index 1488f4983..7196978d0 100644 --- a/pom.xml +++ b/pom.xml @@ -126,9 +126,6 @@ org.apache.maven.plugins maven-javadoc-plugin 3.5.0 - - 8 - attach-javadocs diff --git a/src/main/java/org/json/CDL.java b/src/main/java/org/json/CDL.java index 251386b26..26dc2dae2 100644 --- a/src/main/java/org/json/CDL.java +++ b/src/main/java/org/json/CDL.java @@ -40,37 +40,37 @@ private static String getValue(JSONTokener x, char delimiter) throws JSONExcepti do { c = x.next(); } while (c == ' ' || c == '\t'); - if (c == 0) { - return null; - } else if (c == '"' || c == '\'') { - q = c; - sb = new StringBuilder(); - for (;;) { - c = x.next(); - if (c == q) { - //Handle escaped double-quote - char nextC = x.next(); - if (nextC != '\"') { - // if our quote was the end of the file, don't step - if (nextC > 0) { - x.back(); - } - break; - } - } - if (c == 0 || c == '\n' || c == '\r') { - throw x.syntaxError("Missing close quote '" + q + "'."); - } - sb.append(c); - } - return sb.toString(); - } else if (c == delimiter) { - x.back(); - return ""; - } - x.back(); - return x.nextTo(delimiter); - } + if (c == 0) { + return null; + } else if (c == '"' || c == '\'') { + q = c; + sb = new StringBuilder(); + for (;;) { + c = x.next(); + if (c == q) { + //Handle escaped double-quote + char nextC = x.next(); + if (nextC != '\"') { + // if our quote was the end of the file, don't step + if (nextC > 0) { + x.back(); + } + break; + } + } + if (c == 0 || c == '\n' || c == '\r') { + throw x.syntaxError("Missing close quote '" + q + "'."); + } + sb.append(c); + } + return sb.toString(); + } else if (c == delimiter) { + x.back(); + return ""; + } + x.back(); + return x.nextTo(delimiter); + } /** * Produce a JSONArray of strings from a row of comma delimited values. @@ -83,8 +83,11 @@ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { } /** - * Same as {@link #rowToJSONArray(JSONTokener)}, but with a custom delimiter. - * @see #rowToJSONArray(JSONTokener) + * Produce a JSONArray of strings from a row of comma delimited values. + * @param x A JSONTokener of the source text. + * @param delimiter custom delimiter char + * @return A JSONArray of strings. + * @throws JSONException if a called function fails */ public static JSONArray rowToJSONArray(JSONTokener x, char delimiter) throws JSONException { JSONArray ja = new JSONArray(); @@ -127,9 +130,15 @@ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws } /** - * Same as {@link #rowToJSONObject(JSONArray, JSONTokener)}, but with a custom {@code delimiter}. - * - * @see #rowToJSONObject(JSONArray, JSONTokener) + * Produce a JSONObject from a row of comma delimited text, using a + * parallel JSONArray of strings to provides the names of the elements. + * @param names A JSONArray of names. This is commonly obtained from the + * first row of a comma delimited text file using the rowToJSONArray + * method. + * @param x A JSONTokener of the source text. + * @param delimiter custom delimiter char + * @return A JSONObject combining the names and values. + * @throws JSONException if a called function fails */ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x, char delimiter) throws JSONException { JSONArray ja = rowToJSONArray(x, delimiter); @@ -148,8 +157,12 @@ public static String rowToString(JSONArray ja) { } /** - * Same as {@link #rowToString(JSONArray)}, but with a custom delimiter. - * @see #rowToString(JSONArray) + * Produce a comma delimited text row from a JSONArray. Values containing + * the comma character will be quoted. Troublesome characters may be + * removed. + * @param ja A JSONArray of strings. + * @param delimiter custom delimiter char + * @return A string ending in NEWLINE. */ public static String rowToString(JSONArray ja, char delimiter) { StringBuilder sb = new StringBuilder(); @@ -193,8 +206,12 @@ public static JSONArray toJSONArray(String string) throws JSONException { } /** - * Same as {@link #toJSONArray(String)}, but with a custom delimiter. - * @see #toJSONArray(String) + * Produce a JSONArray of JSONObjects from a comma delimited text string, + * using the first row as a source of names. + * @param string The comma delimited text. + * @param delimiter custom delimiter char + * @return A JSONArray of JSONObjects. + * @throws JSONException if a called function fails */ public static JSONArray toJSONArray(String string, char delimiter) throws JSONException { return toJSONArray(new JSONTokener(string), delimiter); @@ -212,8 +229,12 @@ public static JSONArray toJSONArray(JSONTokener x) throws JSONException { } /** - * Same as {@link #toJSONArray(JSONTokener)}, but with a custom delimiter. - * @see #toJSONArray(JSONTokener) + * Produce a JSONArray of JSONObjects from a comma delimited text string, + * using the first row as a source of names. + * @param x The JSONTokener containing the comma delimited text. + * @param delimiter custom delimiter char + * @return A JSONArray of JSONObjects. + * @throws JSONException if a called function fails */ public static JSONArray toJSONArray(JSONTokener x, char delimiter) throws JSONException { return toJSONArray(rowToJSONArray(x, delimiter), x, delimiter); @@ -232,8 +253,13 @@ public static JSONArray toJSONArray(JSONArray names, String string) throws JSONE } /** - * Same as {@link #toJSONArray(JSONArray, String)}, but with a custom delimiter. - * @see #toJSONArray(JSONArray, String) + * Produce a JSONArray of JSONObjects from a comma delimited text string + * using a supplied JSONArray as the source of element names. + * @param names A JSONArray of strings. + * @param string The comma delimited text. + * @param delimiter custom delimiter char + * @return A JSONArray of JSONObjects. + * @throws JSONException if a called function fails */ public static JSONArray toJSONArray(JSONArray names, String string, char delimiter) throws JSONException { return toJSONArray(names, new JSONTokener(string), delimiter); @@ -252,8 +278,13 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONE } /** - * Same as {@link #toJSONArray(JSONArray, JSONTokener)}, but with a custom delimiter. - * @see #toJSONArray(JSONArray, JSONTokener) + * Produce a JSONArray of JSONObjects from a comma delimited text string + * using a supplied JSONArray as the source of element names. + * @param names A JSONArray of strings. + * @param x A JSONTokener of the source text. + * @param delimiter custom delimiter char + * @return A JSONArray of JSONObjects. + * @throws JSONException if a called function fails */ public static JSONArray toJSONArray(JSONArray names, JSONTokener x, char delimiter) throws JSONException { if (names == null || names.length() == 0) { @@ -287,8 +318,13 @@ public static String toString(JSONArray ja) throws JSONException { } /** - * Same as {@link #toString(JSONArray)}, but with a custom delimiter. - * @see #toString(JSONArray) + * Produce a comma delimited text from a JSONArray of JSONObjects. The + * first row will be a list of names obtained by inspecting the first + * JSONObject. + * @param ja A JSONArray of JSONObjects. + * @param delimiter custom delimiter char + * @return A comma delimited text. + * @throws JSONException if a called function fails */ public static String toString(JSONArray ja, char delimiter) throws JSONException { JSONObject jo = ja.optJSONObject(0); @@ -315,8 +351,14 @@ public static String toString(JSONArray names, JSONArray ja) throws JSONExceptio } /** - * Same as {@link #toString(JSONArray,JSONArray)}, but with a custom delimiter. - * @see #toString(JSONArray,JSONArray) + * Produce a comma delimited text from a JSONArray of JSONObjects using + * a provided list of names. The list of names is not included in the + * output. + * @param names A JSONArray of strings. + * @param ja A JSONArray of JSONObjects. + * @param delimiter custom delimiter char + * @return A comma delimited text. + * @throws JSONException if a called function fails */ public static String toString(JSONArray names, JSONArray ja, char delimiter) throws JSONException { if (names == null || names.length() == 0) { From cb2c8d39629115e740fbd05d56669f44daa597e0 Mon Sep 17 00:00:00 2001 From: XIAYM-gh Date: Wed, 14 Feb 2024 17:53:58 +0800 Subject: [PATCH 101/233] Revert some unnecessary changes (mentioned in #840) --- .../org/json/JSONDuplicateKeyStrategy.java | 28 ----------- src/main/java/org/json/JSONObject.java | 46 +++---------------- .../org/json/JSONParserConfiguration.java | 24 +++++----- .../junit/JSONObjectDuplicateKeyTest.java | 30 ++---------- 4 files changed, 22 insertions(+), 106 deletions(-) delete mode 100644 src/main/java/org/json/JSONDuplicateKeyStrategy.java diff --git a/src/main/java/org/json/JSONDuplicateKeyStrategy.java b/src/main/java/org/json/JSONDuplicateKeyStrategy.java deleted file mode 100644 index 954ac3a25..000000000 --- a/src/main/java/org/json/JSONDuplicateKeyStrategy.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.json; - -/** - * An enum class that is supposed to be used in {@link JSONParserConfiguration}, - * it dedicates which way should be used to handle duplicate keys. - */ -public enum JSONDuplicateKeyStrategy { - /** - * The default value. And this is the way it used to be in the previous versions.
- * The JSONParser will throw an {@link JSONException} when meet duplicate key. - */ - THROW_EXCEPTION, - - /** - * The JSONParser will ignore duplicate keys and won't overwrite the value of the key. - */ - IGNORE, - - /** - * The JSONParser will overwrite the old value of the key. - */ - OVERWRITE, - - /** - * The JSONParser will try to merge the values of the duplicate key into a {@link JSONArray}. - */ - MERGE_INTO_ARRAY -} diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 1572a81be..317fd3dc9 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -212,10 +212,6 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration this(); char c; String key; - JSONDuplicateKeyStrategy duplicateKeyStrategy = jsonParserConfiguration.getDuplicateKeyStrategy(); - - // A list to store merged keys - List mergedKeys = null; if (x.nextClean() != '{') { throw x.syntaxError("A JSONObject text must begin with '{'"); @@ -243,44 +239,14 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration if (key != null) { // Check if key exists boolean keyExists = this.opt(key) != null; - // Read value early to make the tokener work well - Object value = null; - if (!keyExists || duplicateKeyStrategy != JSONDuplicateKeyStrategy.THROW_EXCEPTION) { - value = x.nextValue(); - } - - if (keyExists) { - switch (duplicateKeyStrategy) { - case THROW_EXCEPTION: - throw x.syntaxError("Duplicate key \"" + key + "\""); - - case MERGE_INTO_ARRAY: - if (mergedKeys == null) { - mergedKeys = new ArrayList(); - } - - Object current = this.get(key); - if (current instanceof JSONArray && mergedKeys.contains(key)) { - ((JSONArray) current).put(value); - break; - } - - JSONArray merged = new JSONArray(); - merged.put(current); - merged.put(value); - this.put(key, merged); - mergedKeys.add(key); - break; - } - - // == IGNORE, ignored :) + if (keyExists && !jsonParserConfiguration.isOverwriteDuplicateKey()) { + throw x.syntaxError("Duplicate key \"" + key + "\""); } - if (!keyExists || duplicateKeyStrategy == JSONDuplicateKeyStrategy.OVERWRITE) { - // Only add value if non-null - if (value != null) { - this.put(key, value); - } + Object value = x.nextValue(); + // Only add value if non-null + if (value != null) { + this.put(key, value); } } diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index f1ea2b22e..0d8706c66 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -5,25 +5,27 @@ */ public class JSONParserConfiguration extends ParserConfiguration { /** - * The way should be used to handle duplicate keys. + * Used to indicate whether to overwrite duplicate key or not. */ - private JSONDuplicateKeyStrategy duplicateKeyStrategy; + private boolean overwriteDuplicateKey; /** * Configuration with the default values. */ public JSONParserConfiguration() { - this(JSONDuplicateKeyStrategy.THROW_EXCEPTION); + this(false); } /** - * Configure the parser with {@link JSONDuplicateKeyStrategy}. + * Configure the parser with argument overwriteDuplicateKey. * - * @param duplicateKeyStrategy Indicate which way should be used to handle duplicate keys. + * @param overwriteDuplicateKey Indicate whether to overwrite duplicate key or not.
+ * If not, the JSONParser will throw a {@link JSONException} + * when meeting duplicate keys. */ - public JSONParserConfiguration(JSONDuplicateKeyStrategy duplicateKeyStrategy) { + public JSONParserConfiguration(boolean overwriteDuplicateKey) { super(); - this.duplicateKeyStrategy = duplicateKeyStrategy; + this.overwriteDuplicateKey = overwriteDuplicateKey; } @Override @@ -37,14 +39,14 @@ public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); } - public JSONParserConfiguration withDuplicateKeyStrategy(final JSONDuplicateKeyStrategy duplicateKeyStrategy) { + public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) { JSONParserConfiguration newConfig = this.clone(); - newConfig.duplicateKeyStrategy = duplicateKeyStrategy; + newConfig.overwriteDuplicateKey = overwriteDuplicateKey; return newConfig; } - public JSONDuplicateKeyStrategy getDuplicateKeyStrategy() { - return this.duplicateKeyStrategy; + public boolean isOverwriteDuplicateKey() { + return this.overwriteDuplicateKey; } } diff --git a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java index 73dc70b90..1a3525bac 100644 --- a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java +++ b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java @@ -7,41 +7,17 @@ import org.junit.Test; public class JSONObjectDuplicateKeyTest { - private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\", \"key\": \"value3\"}"; + private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}"; @Test(expected = JSONException.class) public void testThrowException() { new JSONObject(TEST_SOURCE); } - @Test - public void testIgnore() { - JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration( - JSONDuplicateKeyStrategy.IGNORE - )); - - assertEquals("duplicate key shouldn't be overwritten", "value1", jsonObject.getString("key")); - } - @Test public void testOverwrite() { - JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration( - JSONDuplicateKeyStrategy.OVERWRITE - )); - - assertEquals("duplicate key should be overwritten", "value3", jsonObject.getString("key")); - } - - @Test - public void testMergeIntoArray() { - JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration( - JSONDuplicateKeyStrategy.MERGE_INTO_ARRAY - )); + JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true)); - JSONArray jsonArray; - assertTrue("duplicate key should be merged into JSONArray", jsonObject.get("key") instanceof JSONArray - && (jsonArray = jsonObject.getJSONArray("key")).length() == 3 - && jsonArray.getString(0).equals("value1") && jsonArray.getString(1).equals("value2") - && jsonArray.getString(2).equals("value3")); + assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); } } From 86253211c293b59a19e0d52eff42566bbd7d3d45 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 18 Feb 2024 04:20:33 +0200 Subject: [PATCH 102/233] Added missing Javadocs for Java 21 --- src/main/java/org/json/CDL.java | 6 ++++++ src/main/java/org/json/Cookie.java | 6 ++++++ src/main/java/org/json/CookieList.java | 6 ++++++ src/main/java/org/json/HTTP.java | 6 ++++++ src/main/java/org/json/JSONML.java | 7 +++++++ src/main/java/org/json/JSONPointer.java | 6 ++++++ src/main/java/org/json/JSONPropertyName.java | 1 + src/main/java/org/json/Property.java | 7 +++++++ src/main/java/org/json/XML.java | 6 ++++++ 9 files changed, 51 insertions(+) diff --git a/src/main/java/org/json/CDL.java b/src/main/java/org/json/CDL.java index 26dc2dae2..b495de12b 100644 --- a/src/main/java/org/json/CDL.java +++ b/src/main/java/org/json/CDL.java @@ -25,6 +25,12 @@ */ public class CDL { + /** + * Constructs a new CDL object. + */ + public CDL() { + } + /** * Get the next value. The value can be wrapped in quotes. The value can * be empty. diff --git a/src/main/java/org/json/Cookie.java b/src/main/java/org/json/Cookie.java index 7a7e02846..ab908a304 100644 --- a/src/main/java/org/json/Cookie.java +++ b/src/main/java/org/json/Cookie.java @@ -15,6 +15,12 @@ */ public class Cookie { + /** + * Constructs a new Cookie object. + */ + public Cookie() { + } + /** * Produce a copy of a string in which the characters '+', '%', '=', ';' * and control characters are replaced with "%hh". This is a gentle form diff --git a/src/main/java/org/json/CookieList.java b/src/main/java/org/json/CookieList.java index 03e54b997..d1064db52 100644 --- a/src/main/java/org/json/CookieList.java +++ b/src/main/java/org/json/CookieList.java @@ -11,6 +11,12 @@ */ public class CookieList { + /** + * Constructs a new CookieList object. + */ + public CookieList() { + } + /** * Convert a cookie list into a JSONObject. A cookie list is a sequence * of name/value pairs. The names are separated from the values by '='. diff --git a/src/main/java/org/json/HTTP.java b/src/main/java/org/json/HTTP.java index 6fee6ba16..44ab3a6d3 100644 --- a/src/main/java/org/json/HTTP.java +++ b/src/main/java/org/json/HTTP.java @@ -13,6 +13,12 @@ */ public class HTTP { + /** + * Constructs a new HTTP object. + */ + public HTTP() { + } + /** Carriage return/line feed. */ public static final String CRLF = "\r\n"; diff --git a/src/main/java/org/json/JSONML.java b/src/main/java/org/json/JSONML.java index 4aea014d1..7b53e4da7 100644 --- a/src/main/java/org/json/JSONML.java +++ b/src/main/java/org/json/JSONML.java @@ -13,6 +13,13 @@ * @version 2016-01-30 */ public class JSONML { + + /** + * Constructs a new JSONML object. + */ + public JSONML() { + } + /** * Parse XML values and store them in a JSONArray. * @param x The XMLTokener containing the source string. diff --git a/src/main/java/org/json/JSONPointer.java b/src/main/java/org/json/JSONPointer.java index 91bd137ca..859e1e644 100644 --- a/src/main/java/org/json/JSONPointer.java +++ b/src/main/java/org/json/JSONPointer.java @@ -42,6 +42,12 @@ public class JSONPointer { */ public static class Builder { + /** + * Constructs a new Builder object. + */ + public Builder() { + } + // Segments for the eventual JSONPointer string private final List refTokens = new ArrayList(); diff --git a/src/main/java/org/json/JSONPropertyName.java b/src/main/java/org/json/JSONPropertyName.java index 4391bb76c..0e4123f37 100644 --- a/src/main/java/org/json/JSONPropertyName.java +++ b/src/main/java/org/json/JSONPropertyName.java @@ -21,6 +21,7 @@ @Target({METHOD}) public @interface JSONPropertyName { /** + * The value of the JSON property. * @return The name of the property as to be used in the JSON Object. */ String value(); diff --git a/src/main/java/org/json/Property.java b/src/main/java/org/json/Property.java index 83694c055..ba6c56967 100644 --- a/src/main/java/org/json/Property.java +++ b/src/main/java/org/json/Property.java @@ -13,6 +13,13 @@ * @version 2015-05-05 */ public class Property { + + /** + * Constructs a new Property object. + */ + public Property() { + } + /** * Converts a property file object into a JSONObject. The property file object is a table of name value pairs. * @param properties java.util.Properties diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 301c8ba8d..484463b72 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -24,6 +24,12 @@ @SuppressWarnings("boxing") public class XML { + /** + * Constructs a new XML object. + */ + public XML() { + } + /** The Character '&'. */ public static final Character AMP = '&'; From b4b39bb441d903da0597e9b626ff18c046935309 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Feb 2024 15:29:44 -0600 Subject: [PATCH 103/233] pipeline-updates: Java 11 intermittent test failures, try not running in parallel --- .github/workflows/pipeline.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 63540cc6c..c0d2c1a20 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -38,7 +38,7 @@ jobs: runs-on: ubuntu-latest strategy: fail-fast: false - max-parallel: 2 + max-parallel: 1 matrix: # build against supported Java LTS versions: java: [ 8, 11, 17, 21 ] From f0289413d6138f6da1beefba412bd3a3a1b4f02d Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Feb 2024 15:45:13 -0600 Subject: [PATCH 104/233] pipeline-updates: Java 11 intermittent fail - try increasing stack size --- .github/workflows/pipeline.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index c0d2c1a20..46c1592cf 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -52,7 +52,12 @@ jobs: java-version: ${{ matrix.java }} cache: 'maven' - name: Compile Java ${{ matrix.java }} - run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true + run: | + if [ "${{ matrix.java }}" = "11" ]; then + MAVEN_OPTS="-Xss4m" mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true + else + mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true + fi - name: Run Tests ${{ matrix.java }} run: | mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} From cd631d970e964f3e42ed0175f7bba4430334740b Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Feb 2024 15:54:29 -0600 Subject: [PATCH 105/233] pipeline-updates: Java 11 intermittent fail - try an earlier release (there is no later release --- .github/workflows/pipeline.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 46c1592cf..edd005724 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -41,7 +41,7 @@ jobs: max-parallel: 1 matrix: # build against supported Java LTS versions: - java: [ 8, 11, 17, 21 ] + java: [ 8, '11.0.21', 17, 21 ] name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v3 From c1107fa987a986dd41a876e7d85c1e82c96e407f Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Feb 2024 16:17:41 -0600 Subject: [PATCH 106/233] pipeline-updates: Java 11 intermittent fail - try separate build --- .github/workflows/pipeline.yml | 59 ++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index edd005724..7350f7a96 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -34,14 +34,15 @@ jobs: with: name: Create java 1.6 JAR path: target/*.jar - build: + + build-11: runs-on: ubuntu-latest strategy: fail-fast: false max-parallel: 1 matrix: # build against supported Java LTS versions: - java: [ 8, '11.0.21', 17, 21 ] + java: [ 11 ] name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v3 @@ -52,12 +53,56 @@ jobs: java-version: ${{ matrix.java }} cache: 'maven' - name: Compile Java ${{ matrix.java }} + run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true + - name: Run Tests ${{ matrix.java }} + run: | + mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + - name: Build Test Report ${{ matrix.java }} + if: ${{ always() }} run: | - if [ "${{ matrix.java }}" = "11" ]; then - MAVEN_OPTS="-Xss4m" mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true - else - mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true - fi + mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + - name: Upload Test Results ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Test Results ${{ matrix.java }} + path: target/surefire-reports/ + - name: Upload Test Report ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Test Report ${{ matrix.java }} + path: target/site/ + - name: Package Jar ${{ matrix.java }} + run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true + - name: Upload Package Results ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Package Jar ${{ matrix.java }} + path: target/*.jar + + + build-matrix: + runs-on: ubuntu-latest + strategy: + fail-fast: false + max-parallel: 2 + matrix: + # build against supported Java LTS versions: + java: [ 8, 17, 21 ] + name: Java ${{ matrix.java }} + steps: + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: ${{ matrix.java }} + cache: 'maven' + - name: Compile Java ${{ matrix.java }} + run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true - name: Run Tests ${{ matrix.java }} run: | mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} From af8cb376c2015ef10dfcd80d99c49c7af0e2808f Mon Sep 17 00:00:00 2001 From: XIAYM-gh Date: Mon, 19 Feb 2024 18:58:25 +0800 Subject: [PATCH 107/233] Add tests (+ fix bugs) & missing javadoc --- .../org/json/JSONParserConfiguration.java | 38 +++++++++++++--- .../java/org/json/ParserConfiguration.java | 32 ++++++------- .../junit/JSONObjectDuplicateKeyTest.java | 23 ---------- .../junit/JSONParserConfigurationTest.java | 45 +++++++++++++++++++ 4 files changed, 94 insertions(+), 44 deletions(-) delete mode 100644 src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java create mode 100644 src/test/java/org/json/junit/JSONParserConfigurationTest.java diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 0d8706c66..fc16f617c 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -30,22 +30,50 @@ public JSONParserConfiguration(boolean overwriteDuplicateKey) { @Override protected JSONParserConfiguration clone() { - return new JSONParserConfiguration(); + JSONParserConfiguration clone = new JSONParserConfiguration(overwriteDuplicateKey); + clone.maxNestingDepth = maxNestingDepth; + return clone; } + /** + * Defines the maximum nesting depth that the parser will descend before throwing an exception + * when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into + * JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException + * if the maximum depth is reached. + * + * @param maxNestingDepth the maximum nesting depth allowed to the JSON parser + * @return The existing configuration will not be modified. A new configuration is returned. + */ @SuppressWarnings("unchecked") @Override public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { - return super.withMaxNestingDepth(maxNestingDepth); + JSONParserConfiguration clone = this.clone(); + clone.maxNestingDepth = maxNestingDepth; + + return clone; } + /** + * Controls the parser's behavior when meeting duplicate keys. + * If set to false, the parser will throw a JSONException when meeting a duplicate key. + * Or the duplicate key's value will be overwritten. + * + * @param overwriteDuplicateKey defines should the parser overwrite duplicate keys. + * @return The existing configuration will not be modified. A new configuration is returned. + */ public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) { - JSONParserConfiguration newConfig = this.clone(); - newConfig.overwriteDuplicateKey = overwriteDuplicateKey; + JSONParserConfiguration clone = this.clone(); + clone.overwriteDuplicateKey = overwriteDuplicateKey; - return newConfig; + return clone; } + /** + * The parser's behavior when meeting duplicate keys, controls whether the parser should + * overwrite duplicate keys or not. + * + * @return The overwriteDuplicateKey configuration value. + */ public boolean isOverwriteDuplicateKey() { return this.overwriteDuplicateKey; } diff --git a/src/main/java/org/json/ParserConfiguration.java b/src/main/java/org/json/ParserConfiguration.java index 5cdc10d89..06cc44366 100644 --- a/src/main/java/org/json/ParserConfiguration.java +++ b/src/main/java/org/json/ParserConfiguration.java @@ -20,12 +20,12 @@ public class ParserConfiguration { /** * Specifies if values should be kept as strings (true), or if - * they should try to be guessed into JSON values (numeric, boolean, string) + * they should try to be guessed into JSON values (numeric, boolean, string). */ protected boolean keepStrings; /** - * The maximum nesting depth when parsing a document. + * The maximum nesting depth when parsing an object. */ protected int maxNestingDepth; @@ -59,14 +59,14 @@ protected ParserConfiguration clone() { // map should be cloned as well. If the values of the map are known to also // be immutable, then a shallow clone of the map is acceptable. return new ParserConfiguration( - this.keepStrings, - this.maxNestingDepth + this.keepStrings, + this.maxNestingDepth ); } /** * When parsing the XML into JSONML, specifies if values should be kept as strings (true), or if - * they should try to be guessed into JSON values (numeric, boolean, string) + * they should try to be guessed into JSON values (numeric, boolean, string). * * @return The keepStrings configuration value. */ @@ -78,22 +78,21 @@ public boolean isKeepStrings() { * When parsing the XML into JSONML, specifies if values should be kept as strings (true), or if * they should try to be guessed into JSON values (numeric, boolean, string) * - * @param newVal - * new value to use for the keepStrings configuration option. - * @param the type of the configuration object - * + * @param newVal new value to use for the keepStrings configuration option. + * @param the type of the configuration object * @return The existing configuration will not be modified. A new configuration is returned. */ @SuppressWarnings("unchecked") public T withKeepStrings(final boolean newVal) { - T newConfig = (T)this.clone(); + T newConfig = (T) this.clone(); newConfig.keepStrings = newVal; return newConfig; } /** * The maximum nesting depth that the parser will descend before throwing an exception - * when parsing the XML into JSONML. + * when parsing an object (e.g. Map, Collection) into JSON-related objects. + * * @return the maximum nesting depth set for this configuration */ public int getMaxNestingDepth() { @@ -102,18 +101,19 @@ public int getMaxNestingDepth() { /** * Defines the maximum nesting depth that the parser will descend before throwing an exception - * when parsing the XML into JSONML. The default max nesting depth is 512, which means the parser - * will throw a JsonException if the maximum depth is reached. + * when parsing an object (e.g. Map, Collection) into JSON-related objects. + * The default max nesting depth is 512, which means the parser will throw a JsonException if + * the maximum depth is reached. * Using any negative value as a parameter is equivalent to setting no limit to the nesting depth, * which means the parses will go as deep as the maximum call stack size allows. + * * @param maxNestingDepth the maximum nesting depth allowed to the XML parser - * @param the type of the configuration object - * + * @param the type of the configuration object * @return The existing configuration will not be modified. A new configuration is returned. */ @SuppressWarnings("unchecked") public T withMaxNestingDepth(int maxNestingDepth) { - T newConfig = (T)this.clone(); + T newConfig = (T) this.clone(); if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) { newConfig.maxNestingDepth = maxNestingDepth; diff --git a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java deleted file mode 100644 index 1a3525bac..000000000 --- a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.json.junit; - -import org.json.*; - -import static org.junit.Assert.*; - -import org.junit.Test; - -public class JSONObjectDuplicateKeyTest { - private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}"; - - @Test(expected = JSONException.class) - public void testThrowException() { - new JSONObject(TEST_SOURCE); - } - - @Test - public void testOverwrite() { - JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true)); - - assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); - } -} diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java new file mode 100644 index 000000000..0e80d77fe --- /dev/null +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -0,0 +1,45 @@ +package org.json.junit; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONParserConfiguration; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class JSONParserConfigurationTest { + private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}"; + + @Test(expected = JSONException.class) + public void testThrowException() { + new JSONObject(TEST_SOURCE); + } + + @Test + public void testOverwrite() { + JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true)); + + assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); + } + + @Test + public void verifyDuplicateKeyThenMaxDepth() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withOverwriteDuplicateKey(true) + .withMaxNestingDepth(42); + + assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); + assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); + } + + @Test + public void verifyMaxDepthThenDuplicateKey() { + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration() + .withMaxNestingDepth(42) + .withOverwriteDuplicateKey(true); + + assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey()); + assertEquals(42, jsonParserConfiguration.getMaxNestingDepth()); + } +} From 7c7a98da71f925abb9b4b81574fe70b7459ec791 Mon Sep 17 00:00:00 2001 From: Simulant Date: Fri, 23 Feb 2024 21:48:25 +0100 Subject: [PATCH 108/233] #863 use StringBuilderWriter to toString methods resulting in a faster toString generation. --- src/main/java/org/json/JSONArray.java | 3 +- src/main/java/org/json/JSONObject.java | 5 +- .../java/org/json/StringBuilderWriter.java | 82 +++++++++++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/json/StringBuilderWriter.java diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 38b0b31ae..cda56944a 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -5,7 +5,6 @@ */ import java.io.IOException; -import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Array; import java.math.BigDecimal; @@ -1695,7 +1694,7 @@ public String toString() { */ @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { - StringWriter sw = new StringWriter(); + Writer sw = new StringBuilderWriter(); return this.write(sw, indentFactor, 0).toString(); } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 4c08b0b9c..36a7c7fe3 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -6,7 +6,6 @@ import java.io.Closeable; import java.io.IOException; -import java.io.StringWriter; import java.io.Writer; import java.lang.annotation.Annotation; import java.lang.reflect.Field; @@ -2227,7 +2226,7 @@ public Object optQuery(JSONPointer jsonPointer) { */ @SuppressWarnings("resource") public static String quote(String string) { - StringWriter sw = new StringWriter(); + Writer sw = new StringBuilderWriter(); try { return quote(string, sw).toString(); } catch (IOException ignored) { @@ -2558,7 +2557,7 @@ public String toString() { */ @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { - StringWriter w = new StringWriter(); + Writer w = new StringBuilderWriter(); return this.write(w, indentFactor, 0).toString(); } diff --git a/src/main/java/org/json/StringBuilderWriter.java b/src/main/java/org/json/StringBuilderWriter.java new file mode 100644 index 000000000..25d2dbe87 --- /dev/null +++ b/src/main/java/org/json/StringBuilderWriter.java @@ -0,0 +1,82 @@ +package org.json; + +import java.io.IOException; +import java.io.Writer; + +/** + * Performance optimised alternative for {@link java.io.StringWriter} + * using internally a {@link StringBuilder} instead of a {@link StringBuffer}. + */ +class StringBuilderWriter extends Writer { + private final StringBuilder builder; + + StringBuilderWriter() { + builder = new StringBuilder(); + lock = builder; + } + + StringBuilderWriter(int initialSize) { + if (initialSize < 0) { + throw new IllegalArgumentException("Negative buffer size"); + } + builder = new StringBuilder(initialSize); + lock = builder; + } + + @Override + public void write(int c) { + builder.append((char) c); + } + + @Override + public void write(char cbuf[], int offset, int length) { + if ((offset < 0) || (offset > cbuf.length) || (length < 0) || + ((offset + length) > cbuf.length) || ((offset + length) < 0)) { + throw new IndexOutOfBoundsException(); + } else if (length == 0) { + return; + } + builder.append(cbuf, offset, length); + } + + @Override + public void write(String str) { + builder.append(str); + } + + @Override + public void write(String str, int offset, int length) { + builder.append(str, offset, offset + length); + } + + @Override + public StringBuilderWriter append(CharSequence csq) { + write(String.valueOf(csq)); + return this; + } + + @Override + public StringBuilderWriter append(CharSequence csq, int start, int end) { + if (csq == null) csq = "null"; + return append(csq.subSequence(start, end)); + } + + @Override + public StringBuilderWriter append(char c) { + write(c); + return this; + } + + @Override + public String toString() { + return builder.toString(); + } + + @Override + public void flush() { + } + + @Override + public void close() throws IOException { + } +} From 0ff635c456d92d6f85b3585cc4e85a04cc0ed27f Mon Sep 17 00:00:00 2001 From: Simulant Date: Fri, 23 Feb 2024 21:56:40 +0100 Subject: [PATCH 109/233] #863 improve formatting --- src/main/java/org/json/StringBuilderWriter.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/json/StringBuilderWriter.java b/src/main/java/org/json/StringBuilderWriter.java index 25d2dbe87..26b4c372b 100644 --- a/src/main/java/org/json/StringBuilderWriter.java +++ b/src/main/java/org/json/StringBuilderWriter.java @@ -10,26 +10,21 @@ class StringBuilderWriter extends Writer { private final StringBuilder builder; + /** + * Create a new string builder writer using the default initial string-builder buffer size. + */ StringBuilderWriter() { builder = new StringBuilder(); lock = builder; } - StringBuilderWriter(int initialSize) { - if (initialSize < 0) { - throw new IllegalArgumentException("Negative buffer size"); - } - builder = new StringBuilder(initialSize); - lock = builder; - } - @Override public void write(int c) { builder.append((char) c); } @Override - public void write(char cbuf[], int offset, int length) { + public void write(char[] cbuf, int offset, int length) { if ((offset < 0) || (offset > cbuf.length) || (length < 0) || ((offset + length) > cbuf.length) || ((offset + length) < 0)) { throw new IndexOutOfBoundsException(); @@ -57,7 +52,9 @@ public StringBuilderWriter append(CharSequence csq) { @Override public StringBuilderWriter append(CharSequence csq, int start, int end) { - if (csq == null) csq = "null"; + if (csq == null) { + csq = "null"; + } return append(csq.subSequence(start, end)); } From 6660e4091569fc48e582ab77c6626491f8bab8db Mon Sep 17 00:00:00 2001 From: Simulant Date: Fri, 23 Feb 2024 22:02:35 +0100 Subject: [PATCH 110/233] #863 increase compiler stack size on build pipeline --- .github/workflows/pipeline.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 63540cc6c..92b55283a 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -52,6 +52,8 @@ jobs: java-version: ${{ matrix.java }} cache: 'maven' - name: Compile Java ${{ matrix.java }} + env: + MAVEN_OPTS: -Xss4m run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true - name: Run Tests ${{ matrix.java }} run: | From 771c82c4eb2feed9584273ffcab8a37d1f903569 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 24 Feb 2024 13:07:51 -0600 Subject: [PATCH 111/233] backing out recent changes to optLong, getLong. See #868 --- src/main/java/org/json/JSONArray.java | 4 +- src/main/java/org/json/JSONObject.java | 97 +++++++++- .../java/org/json/NumberConversionUtil.java | 152 --------------- src/main/java/org/json/XML.java | 83 +++++++-- .../org/json/NumberConversionUtilTest.java | 174 ------------------ src/test/java/org/json/junit/JSONMLTest.java | 2 +- .../org/json/junit/JSONObjectDecimalTest.java | 100 ---------- .../org/json/junit/JSONObjectNumberTest.java | 6 +- .../java/org/json/junit/JSONObjectTest.java | 80 +------- .../org/json/junit/JsonNumberZeroTest.java | 55 ------ .../org/json/junit/XMLConfigurationTest.java | 2 +- src/test/java/org/json/junit/XMLTest.java | 2 +- 12 files changed, 176 insertions(+), 581 deletions(-) delete mode 100644 src/main/java/org/json/NumberConversionUtil.java delete mode 100644 src/test/java/org/json/NumberConversionUtilTest.java delete mode 100644 src/test/java/org/json/junit/JSONObjectDecimalTest.java delete mode 100644 src/test/java/org/json/junit/JsonNumberZeroTest.java diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 38b0b31ae..f86075e6b 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -360,7 +360,7 @@ public Number getNumber(int index) throws JSONException { if (object instanceof Number) { return (Number)object; } - return NumberConversionUtil.stringToNumber(object.toString()); + return JSONObject.stringToNumber(object.toString()); } catch (Exception e) { throw wrongValueFormatException(index, "number", object, e); } @@ -1107,7 +1107,7 @@ public Number optNumber(int index, Number defaultValue) { if (val instanceof String) { try { - return NumberConversionUtil.stringToNumber((String) val); + return JSONObject.stringToNumber((String) val); } catch (Exception e) { return defaultValue; } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 4c08b0b9c..6494f9349 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -28,9 +28,6 @@ import java.util.Set; import java.util.regex.Pattern; -import static org.json.NumberConversionUtil.potentialNumber; -import static org.json.NumberConversionUtil.stringToNumber; - /** * A JSONObject is an unordered collection of name/value pairs. Its external * form is a string wrapped in curly braces with colons between the names and @@ -2460,7 +2457,8 @@ public static Object stringToValue(String string) { * produced, then the value will just be a string. */ - if (potentialNumber(string)) { + char initial = string.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-') { try { return stringToNumber(string); } catch (Exception ignore) { @@ -2469,8 +2467,75 @@ public static Object stringToValue(String string) { return string; } - - + /** + * Converts a string to a number using the narrowest possible type. Possible + * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. + * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. + * + * @param val value to convert + * @return Number representation of the value. + * @throws NumberFormatException thrown if the value is not a valid number. A public + * caller should catch this and wrap it in a {@link JSONException} if applicable. + */ + protected static Number stringToNumber(final String val) throws NumberFormatException { + char initial = val.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-') { + // decimal representation + if (isDecimalNotation(val)) { + // Use a BigDecimal all the time so we keep the original + // representation. BigDecimal doesn't support -0.0, ensure we + // keep that by forcing a decimal. + try { + BigDecimal bd = new BigDecimal(val); + if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { + return Double.valueOf(-0.0); + } + return bd; + } catch (NumberFormatException retryAsDouble) { + // this is to support "Hex Floats" like this: 0x1.0P-1074 + try { + Double d = Double.valueOf(val); + if(d.isNaN() || d.isInfinite()) { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + return d; + } catch (NumberFormatException ignore) { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } + } + // block items like 00 01 etc. Java number parsers treat these as Octal. + if(initial == '0' && val.length() > 1) { + char at1 = val.charAt(1); + if(at1 >= '0' && at1 <= '9') { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } else if (initial == '-' && val.length() > 2) { + char at1 = val.charAt(1); + char at2 = val.charAt(2); + if(at1 == '0' && at2 >= '0' && at2 <= '9') { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } + // integer representation. + // This will narrow any values to the smallest reasonable Object representation + // (Integer, Long, or BigInteger) + + // BigInteger down conversion: We use a similar bitLength compare as + // BigInteger#intValueExact uses. Increases GC, but objects hold + // only what they need. i.e. Less runtime overhead if the value is + // long lived. + BigInteger bi = new BigInteger(val); + if(bi.bitLength() <= 31){ + return Integer.valueOf(bi.intValue()); + } + if(bi.bitLength() <= 63){ + return Long.valueOf(bi.longValue()); + } + return bi; + } + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } /** * Throw an exception if the object is a NaN or infinite number. @@ -2896,5 +2961,23 @@ private static JSONException recursivelyDefinedObjectException(String key) { ); } - + /** + * For a prospective number, remove the leading zeros + * @param value prospective number + * @return number without leading zeros + */ + private static String removeLeadingZerosOfNumber(String value){ + if (value.equals("-")){return value;} + boolean negativeFirstChar = (value.charAt(0) == '-'); + int counter = negativeFirstChar ? 1:0; + while (counter < value.length()){ + if (value.charAt(counter) != '0'){ + if (negativeFirstChar) {return "-".concat(value.substring(counter));} + return value.substring(counter); + } + ++counter; + } + if (negativeFirstChar) {return "-0";} + return "0"; + } } diff --git a/src/main/java/org/json/NumberConversionUtil.java b/src/main/java/org/json/NumberConversionUtil.java deleted file mode 100644 index c2f16d74c..000000000 --- a/src/main/java/org/json/NumberConversionUtil.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.json; - -import java.math.BigDecimal; -import java.math.BigInteger; - -class NumberConversionUtil { - - /** - * Converts a string to a number using the narrowest possible type. Possible - * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer. - * When a Double is returned, it should always be a valid Double and not NaN or +-infinity. - * - * @param input value to convert - * @return Number representation of the value. - * @throws NumberFormatException thrown if the value is not a valid number. A public - * caller should catch this and wrap it in a {@link JSONException} if applicable. - */ - static Number stringToNumber(final String input) throws NumberFormatException { - String val = input; - if (val.startsWith(".")){ - val = "0"+val; - } - if (val.startsWith("-.")){ - val = "-0."+val.substring(2); - } - char initial = val.charAt(0); - if ( isNumericChar(initial) || initial == '-' ) { - // decimal representation - if (isDecimalNotation(val)) { - // Use a BigDecimal all the time so we keep the original - // representation. BigDecimal doesn't support -0.0, ensure we - // keep that by forcing a decimal. - try { - BigDecimal bd = new BigDecimal(val); - if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { - return Double.valueOf(-0.0); - } - return bd; - } catch (NumberFormatException retryAsDouble) { - // this is to support "Hex Floats" like this: 0x1.0P-1074 - try { - Double d = Double.valueOf(val); - if(d.isNaN() || d.isInfinite()) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - return d; - } catch (NumberFormatException ignore) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - } - val = removeLeadingZerosOfNumber(input); - initial = val.charAt(0); - if(initial == '0' && val.length() > 1) { - char at1 = val.charAt(1); - if(isNumericChar(at1)) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } else if (initial == '-' && val.length() > 2) { - char at1 = val.charAt(1); - char at2 = val.charAt(2); - if(at1 == '0' && isNumericChar(at2)) { - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - } - // integer representation. - // This will narrow any values to the smallest reasonable Object representation - // (Integer, Long, or BigInteger) - - // BigInteger down conversion: We use a similar bitLength compare as - // BigInteger#intValueExact uses. Increases GC, but objects hold - // only what they need. i.e. Less runtime overhead if the value is - // long lived. - BigInteger bi = new BigInteger(val); - if(bi.bitLength() <= 31){ - return Integer.valueOf(bi.intValue()); - } - if(bi.bitLength() <= 63){ - return Long.valueOf(bi.longValue()); - } - return bi; - } - throw new NumberFormatException("val ["+input+"] is not a valid number."); - } - - /** - * Checks if the character is a numeric digit ('0' to '9'). - * - * @param c The character to be checked. - * @return true if the character is a numeric digit, false otherwise. - */ - private static boolean isNumericChar(char c) { - return (c <= '9' && c >= '0'); - } - - /** - * Checks if the value could be considered a number in decimal number system. - * @param value - * @return - */ - static boolean potentialNumber(String value){ - if (value == null || value.isEmpty()){ - return false; - } - return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); - } - - /** - * Tests if the value should be tried as a decimal. It makes no test if there are actual digits. - * - * @param val value to test - * @return true if the string is "-0" or if it contains '.', 'e', or 'E', false otherwise. - */ - private static boolean isDecimalNotation(final String val) { - return val.indexOf('.') > -1 || val.indexOf('e') > -1 - || val.indexOf('E') > -1 || "-0".equals(val); - } - - private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){ - if (index >= value.length()){ - return false; - } - return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index)); - } - - private static boolean digitAtIndex(String value, int index){ - if (index >= value.length()){ - return false; - } - return value.charAt(index) >= '0' && value.charAt(index) <= '9'; - } - - /** - * For a prospective number, remove the leading zeros - * @param value prospective number - * @return number without leading zeros - */ - private static String removeLeadingZerosOfNumber(String value){ - if (value.equals("-")){return value;} - boolean negativeFirstChar = (value.charAt(0) == '-'); - int counter = negativeFirstChar ? 1:0; - while (counter < value.length()){ - if (value.charAt(counter) != '0'){ - if (negativeFirstChar) {return "-".concat(value.substring(counter));} - return value.substring(counter); - } - ++counter; - } - if (negativeFirstChar) {return "-0";} - return "0"; - } -} diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 484463b72..e59ec7a4a 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -10,10 +10,6 @@ import java.math.BigInteger; import java.util.Iterator; -import static org.json.NumberConversionUtil.potentialNumber; -import static org.json.NumberConversionUtil.stringToNumber; - - /** * This provides static methods to convert an XML text into a JSONObject, and to * covert a JSONObject into an XML text. @@ -499,6 +495,76 @@ private static boolean isStringAllWhiteSpace(final String s) { return true; } + /** + * direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support. + */ + private static Number stringToNumber(final String val) throws NumberFormatException { + char initial = val.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-') { + // decimal representation + if (isDecimalNotation(val)) { + // Use a BigDecimal all the time so we keep the original + // representation. BigDecimal doesn't support -0.0, ensure we + // keep that by forcing a decimal. + try { + BigDecimal bd = new BigDecimal(val); + if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) { + return Double.valueOf(-0.0); + } + return bd; + } catch (NumberFormatException retryAsDouble) { + // this is to support "Hex Floats" like this: 0x1.0P-1074 + try { + Double d = Double.valueOf(val); + if(d.isNaN() || d.isInfinite()) { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + return d; + } catch (NumberFormatException ignore) { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } + } + // block items like 00 01 etc. Java number parsers treat these as Octal. + if(initial == '0' && val.length() > 1) { + char at1 = val.charAt(1); + if(at1 >= '0' && at1 <= '9') { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } else if (initial == '-' && val.length() > 2) { + char at1 = val.charAt(1); + char at2 = val.charAt(2); + if(at1 == '0' && at2 >= '0' && at2 <= '9') { + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + } + // integer representation. + // This will narrow any values to the smallest reasonable Object representation + // (Integer, Long, or BigInteger) + + // BigInteger down conversion: We use a similar bitLength compare as + // BigInteger#intValueExact uses. Increases GC, but objects hold + // only what they need. i.e. Less runtime overhead if the value is + // long lived. + BigInteger bi = new BigInteger(val); + if(bi.bitLength() <= 31){ + return Integer.valueOf(bi.intValue()); + } + if(bi.bitLength() <= 63){ + return Long.valueOf(bi.longValue()); + } + return bi; + } + throw new NumberFormatException("val ["+val+"] is not a valid number."); + } + + /** + * direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support. + */ + private static boolean isDecimalNotation(final String val) { + return val.indexOf('.') > -1 || val.indexOf('e') > -1 + || val.indexOf('E') > -1 || "-0".equals(val); + } /** * This method tries to convert the given string value to the target object @@ -543,7 +609,8 @@ public static Object stringToValue(String string) { * produced, then the value will just be a string. */ - if (potentialNumber(string)) { + char initial = string.charAt(0); + if ((initial >= '0' && initial <= '9') || initial == '-') { try { return stringToNumber(string); } catch (Exception ignore) { @@ -552,11 +619,6 @@ public static Object stringToValue(String string) { return string; } - - - - - /** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject. Some information may be lost in this transformation because @@ -975,5 +1037,4 @@ private static final String indent(int indent) { } return sb.toString(); } - } diff --git a/src/test/java/org/json/NumberConversionUtilTest.java b/src/test/java/org/json/NumberConversionUtilTest.java deleted file mode 100644 index c6f07254d..000000000 --- a/src/test/java/org/json/NumberConversionUtilTest.java +++ /dev/null @@ -1,174 +0,0 @@ -package org.json; - -import org.junit.Test; - -import java.math.BigDecimal; -import java.math.BigInteger; - -import static org.junit.Assert.*; - -public class NumberConversionUtilTest { - - @Test - public void shouldParseDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("00.10d"); - assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 0, number.longValue(),0); - assertEquals("Do not match", 0, number.intValue(),0); - } - - @Test - public void shouldParseDecimalFractionNumbersWithSingleLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("0.10d"); - assertEquals("Do not match", 0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 0, number.longValue(),0); - assertEquals("Do not match", 0, number.intValue(),0); - } - - - @Test - public void shouldParseDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("0.010d"); - assertEquals("Do not match", 0.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", 0.010f, number.floatValue(),0.0f); - assertEquals("Do not match", 0, number.longValue(),0); - assertEquals("Do not match", 0, number.intValue(),0); - } - - @Test - public void shouldParseMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("00200.10d"); - assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 200, number.longValue(),0); - assertEquals("Do not match", 200, number.intValue(),0); - } - - @Test - public void shouldParseMixedDecimalFractionNumbersWithoutLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("200.10d"); - assertEquals("Do not match", 200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", 200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", 200, number.longValue(),0); - assertEquals("Do not match", 200, number.intValue(),0); - } - - - @Test - public void shouldParseMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("200.010d"); - assertEquals("Do not match", 200.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", 200.010f, number.floatValue(),0.0f); - assertEquals("Do not match", 200, number.longValue(),0); - assertEquals("Do not match", 200, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("-00.10d"); - assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -0, number.longValue(),0); - assertEquals("Do not match", -0, number.intValue(),0); - } - - @Test - public void shouldParseNegativeDecimalFractionNumbersWithSingleLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("-0.10d"); - assertEquals("Do not match", -0.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -0.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -0, number.longValue(),0); - assertEquals("Do not match", -0, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("-0.010d"); - assertEquals("Do not match", -0.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", -0.010f, number.floatValue(),0.0f); - assertEquals("Do not match", -0, number.longValue(),0); - assertEquals("Do not match", -0, number.intValue(),0); - } - - @Test - public void shouldParseNegativeMixedDecimalFractionNumbersWithMultipleLeadingZeros(){ - Number number = NumberConversionUtil.stringToNumber("-00200.10d"); - assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -200, number.longValue(),0); - assertEquals("Do not match", -200, number.intValue(),0); - } - - @Test - public void shouldParseNegativeMixedDecimalFractionNumbersWithoutLeadingZero(){ - Number number = NumberConversionUtil.stringToNumber("-200.10d"); - assertEquals("Do not match", -200.10d, number.doubleValue(),0.0d); - assertEquals("Do not match", -200.10f, number.floatValue(),0.0f); - assertEquals("Do not match", -200, number.longValue(),0); - assertEquals("Do not match", -200, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeMixedDecimalFractionNumbersWithZerosAfterDecimalPoint(){ - Number number = NumberConversionUtil.stringToNumber("-200.010d"); - assertEquals("Do not match", -200.010d, number.doubleValue(),0.0d); - assertEquals("Do not match", -200.010f, number.floatValue(),0.0f); - assertEquals("Do not match", -200, number.longValue(),0); - assertEquals("Do not match", -200, number.intValue(),0); - } - - @Test - public void shouldParseNumbersWithExponents(){ - Number number = NumberConversionUtil.stringToNumber("23.45e7"); - assertEquals("Do not match", 23.45e7d, number.doubleValue(),0.0d); - assertEquals("Do not match", 23.45e7f, number.floatValue(),0.0f); - assertEquals("Do not match", 2.345E8, number.longValue(),0); - assertEquals("Do not match", 2.345E8, number.intValue(),0); - } - - - @Test - public void shouldParseNegativeNumbersWithExponents(){ - Number number = NumberConversionUtil.stringToNumber("-23.45e7"); - assertEquals("Do not match", -23.45e7d, number.doubleValue(),0.0d); - assertEquals("Do not match", -23.45e7f, number.floatValue(),0.0f); - assertEquals("Do not match", -2.345E8, number.longValue(),0); - assertEquals("Do not match", -2.345E8, number.intValue(),0); - } - - @Test - public void shouldParseBigDecimal(){ - Number number = NumberConversionUtil.stringToNumber("19007199254740993.35481234487103587486413587843213584"); - assertTrue(number instanceof BigDecimal); - } - - @Test - public void shouldParseBigInteger(){ - Number number = NumberConversionUtil.stringToNumber("1900719925474099335481234487103587486413587843213584"); - assertTrue(number instanceof BigInteger); - } - - @Test - public void shouldIdentifyPotentialNumber(){ - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112.123")); - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("112e123")); - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112.123")); - assertTrue("Does not identify as number", NumberConversionUtil.potentialNumber("-112e23")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("--112.123")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("-a112.123")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("a112.123")); - assertFalse("Does not identify as not number", NumberConversionUtil.potentialNumber("e112.123")); - } - - @Test(expected = NumberFormatException.class) - public void shouldExpectExceptionWhenNumberIsNotFormatted(){ - NumberConversionUtil.stringToNumber("112.aa123"); - } - - -} \ No newline at end of file diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index e6abd151e..154af645f 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -709,7 +709,7 @@ public void commentsInXML() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final String expectedJsonString = "[\"root\",[\"id\",1],[\"id\",1],[\"id\",0],[\"id\",0],[\"item\",{\"id\":1}],[\"title\",true]]"; + final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]"; final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false); assertEquals(expectedJsonString, actualJsonOutput.toString()); } diff --git a/src/test/java/org/json/junit/JSONObjectDecimalTest.java b/src/test/java/org/json/junit/JSONObjectDecimalTest.java deleted file mode 100644 index 3302f0a24..000000000 --- a/src/test/java/org/json/junit/JSONObjectDecimalTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.json.junit; - -import org.json.JSONObject; -import org.junit.Test; - -import java.math.BigDecimal; -import java.math.BigInteger; - -import static org.junit.Assert.assertEquals; - -public class JSONObjectDecimalTest { - - @Test - public void shouldParseDecimalNumberThatStartsWithDecimalPoint(){ - JSONObject jsonObject = new JSONObject("{value:0.50}"); - assertEquals("Float not recognized", 0.5f, jsonObject.getFloat("value"), 0.0f); - assertEquals("Float not recognized", 0.5f, jsonObject.optFloat("value"), 0.0f); - assertEquals("Float not recognized", 0.5f, jsonObject.optFloatObject("value"), 0.0f); - assertEquals("Double not recognized", 0.5d, jsonObject.optDouble("value"), 0.0f); - assertEquals("Double not recognized", 0.5d, jsonObject.optDoubleObject("value"), 0.0f); - assertEquals("Double not recognized", 0.5d, jsonObject.getDouble("value"), 0.0f); - assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); - assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.5).compareTo(jsonObject.getBigDecimal("value"))); - assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); - } - - - - @Test - public void shouldParseNegativeDecimalNumberThatStartsWithDecimalPoint(){ - JSONObject jsonObject = new JSONObject("{value:-.50}"); - assertEquals("Float not recognized", -0.5f, jsonObject.getFloat("value"), 0.0f); - assertEquals("Float not recognized", -0.5f, jsonObject.optFloat("value"), 0.0f); - assertEquals("Float not recognized", -0.5f, jsonObject.optFloatObject("value"), 0.0f); - assertEquals("Double not recognized", -0.5d, jsonObject.optDouble("value"), 0.0f); - assertEquals("Double not recognized", -0.5d, jsonObject.optDoubleObject("value"), 0.0f); - assertEquals("Double not recognized", -0.5d, jsonObject.getDouble("value"), 0.0f); - assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); - assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.5).compareTo(jsonObject.getBigDecimal("value"))); - assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); - } - - @Test - public void shouldParseDecimalNumberThatHasZeroBeforeWithDecimalPoint(){ - JSONObject jsonObject = new JSONObject("{value:00.050}"); - assertEquals("Float not recognized", 0.05f, jsonObject.getFloat("value"), 0.0f); - assertEquals("Float not recognized", 0.05f, jsonObject.optFloat("value"), 0.0f); - assertEquals("Float not recognized", 0.05f, jsonObject.optFloatObject("value"), 0.0f); - assertEquals("Double not recognized", 0.05d, jsonObject.optDouble("value"), 0.0f); - assertEquals("Double not recognized", 0.05d, jsonObject.optDoubleObject("value"), 0.0f); - assertEquals("Double not recognized", 0.05d, jsonObject.getDouble("value"), 0.0f); - assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); - assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.05).compareTo(jsonObject.getBigDecimal("value"))); - assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); - } - - @Test - public void shouldParseNegativeDecimalNumberThatHasZeroBeforeWithDecimalPoint(){ - JSONObject jsonObject = new JSONObject("{value:-00.050}"); - assertEquals("Float not recognized", -0.05f, jsonObject.getFloat("value"), 0.0f); - assertEquals("Float not recognized", -0.05f, jsonObject.optFloat("value"), 0.0f); - assertEquals("Float not recognized", -0.05f, jsonObject.optFloatObject("value"), 0.0f); - assertEquals("Double not recognized", -0.05d, jsonObject.optDouble("value"), 0.0f); - assertEquals("Double not recognized", -0.05d, jsonObject.optDoubleObject("value"), 0.0f); - assertEquals("Double not recognized", -0.05d, jsonObject.getDouble("value"), 0.0f); - assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); - assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.05).compareTo(jsonObject.getBigDecimal("value"))); - assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); - } - - -} diff --git a/src/test/java/org/json/junit/JSONObjectNumberTest.java b/src/test/java/org/json/junit/JSONObjectNumberTest.java index 14e68d66b..43173a288 100644 --- a/src/test/java/org/json/junit/JSONObjectNumberTest.java +++ b/src/test/java/org/json/junit/JSONObjectNumberTest.java @@ -23,10 +23,7 @@ public class JSONObjectNumberTest { @Parameters(name = "{index}: {0}") public static Collection data() { return Arrays.asList(new Object[][]{ - {"{value:0050}", 1}, - {"{value:0050.0000}", 1}, - {"{value:-0050}", -1}, - {"{value:-0050.0000}", -1}, + {"{value:50}", 1}, {"{value:50.0}", 1}, {"{value:5e1}", 1}, {"{value:5E1}", 1}, @@ -35,7 +32,6 @@ public static Collection data() { {"{value:-50}", -1}, {"{value:-50.0}", -1}, {"{value:-5e1}", -1}, - {"{value:-0005e1}", -1}, {"{value:-5E1}", -1}, {"{value:-5e1}", -1}, {"{value:'-50'}", -1} diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 053f17a91..d90297df3 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -4,7 +4,6 @@ Public Domain. */ -import static java.lang.Double.NaN; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -786,7 +785,7 @@ public void jsonObjectAccumulate() { jsonObject.accumulate("myArray", -23.45e7); // include an unsupported object for coverage try { - jsonObject.accumulate("myArray", NaN); + jsonObject.accumulate("myArray", Double.NaN); fail("Expected exception"); } catch (JSONException ignored) {} @@ -818,7 +817,7 @@ public void jsonObjectAppend() { jsonObject.append("myArray", -23.45e7); // include an unsupported object for coverage try { - jsonObject.append("myArray", NaN); + jsonObject.append("myArray", Double.NaN); fail("Expected exception"); } catch (JSONException ignored) {} @@ -843,7 +842,7 @@ public void jsonObjectAppend() { public void jsonObjectDoubleToString() { String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, - NaN, Double.NEGATIVE_INFINITY }; + Double.NaN, Double.NEGATIVE_INFINITY }; for (int i = 0; i < expectedStrs.length; ++i) { String actualStr = JSONObject.doubleToString(doubles[i]); assertTrue("value expected ["+expectedStrs[i]+ @@ -898,11 +897,11 @@ public void jsonObjectValues() { assertTrue("opt doubleKey should be double", jsonObject.optDouble("doubleKey") == -23.45e7); assertTrue("opt doubleKey with Default should be double", - jsonObject.optDouble("doubleStrKey", NaN) == 1); + jsonObject.optDouble("doubleStrKey", Double.NaN) == 1); assertTrue("opt doubleKey should be Double", Double.valueOf(-23.45e7).equals(jsonObject.optDoubleObject("doubleKey"))); assertTrue("opt doubleKey with Default should be Double", - Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", NaN))); + Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", Double.NaN))); assertTrue("opt negZeroKey should be a Double", jsonObject.opt("negZeroKey") instanceof Double); assertTrue("get negZeroKey should be a Double", @@ -1068,21 +1067,12 @@ public void jsonInvalidNumberValues() { "\"tooManyZeros\":00,"+ "\"negativeInfinite\":-Infinity,"+ "\"negativeNaN\":-NaN,"+ - "\"negativeNaNWithLeadingZeros\":-00NaN,"+ "\"negativeFraction\":-.01,"+ "\"tooManyZerosFraction\":00.001,"+ "\"negativeHexFloat\":-0x1.fffp1,"+ "\"hexFloat\":0x1.0P-1074,"+ "\"floatIdentifier\":0.1f,"+ - "\"doubleIdentifier\":0.1d,"+ - "\"doubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":0000000.1d,"+ - "\"negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":-0000000.1d,"+ - "\"doubleIdentifierWithMultipleLeadingZerosAfterDecimal\":0000000.0001d,"+ - "\"negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal\":-0000000.0001d,"+ - "\"integerWithLeadingZeros\":000900,"+ - "\"integerWithAllZeros\":00000,"+ - "\"compositeWithLeadingZeros\":00800.90d,"+ - "\"decimalPositiveWithoutNumberBeforeDecimalPoint\":.90,"+ + "\"doubleIdentifier\":0.1d"+ "}"; JSONObject jsonObject = new JSONObject(str); Object obj; @@ -1092,22 +1082,15 @@ public void jsonInvalidNumberValues() { assertTrue("hexNumber currently evaluates to string", obj.equals("-0x123")); assertTrue( "tooManyZeros currently evaluates to string", - jsonObject.get( "tooManyZeros" ).equals(0)); + jsonObject.get( "tooManyZeros" ).equals("00")); obj = jsonObject.get("negativeInfinite"); assertTrue( "negativeInfinite currently evaluates to string", obj.equals("-Infinity")); obj = jsonObject.get("negativeNaN"); assertTrue( "negativeNaN currently evaluates to string", obj.equals("-NaN")); - obj = jsonObject.get("negativeNaNWithLeadingZeros"); - assertTrue( "negativeNaNWithLeadingZeros currently evaluates to string", - obj.equals("-00NaN")); assertTrue( "negativeFraction currently evaluates to double -0.01", jsonObject.get( "negativeFraction" ).equals(BigDecimal.valueOf(-0.01))); - assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", - jsonObject.get( "tooManyZerosFraction" ).equals(BigDecimal.valueOf(0.001))); - assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", - jsonObject.getLong( "tooManyZerosFraction" )==0); assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", jsonObject.optLong( "tooManyZerosFraction" )==0); assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", @@ -1118,53 +1101,6 @@ public void jsonInvalidNumberValues() { jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1))); assertTrue("doubleIdentifier currently evaluates to double 0.1", jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1))); - assertTrue("doubleIdentifierWithMultipleLeadingZerosBeforeDecimal currently evaluates to double 0.1", - jsonObject.get("doubleIdentifierWithMultipleLeadingZerosBeforeDecimal").equals(Double.valueOf(0.1))); - assertTrue("negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal currently evaluates to double -0.1", - jsonObject.get("negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal").equals(Double.valueOf(-0.1))); - assertTrue("doubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double 0.0001", - jsonObject.get("doubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(0.0001))); - assertTrue("doubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double 0.0001", - jsonObject.get("doubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(0.0001))); - assertTrue("negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double -0.0001", - jsonObject.get("negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(-0.0001))); - assertTrue("Integer does not evaluate to 900", - jsonObject.get("integerWithLeadingZeros").equals(900)); - assertTrue("Integer does not evaluate to 900", - jsonObject.getInt("integerWithLeadingZeros")==900); - assertTrue("Integer does not evaluate to 900", - jsonObject.optInt("integerWithLeadingZeros")==900); - assertTrue("Integer does not evaluate to 0", - jsonObject.get("integerWithAllZeros").equals(0)); - assertTrue("Integer does not evaluate to 0", - jsonObject.getInt("integerWithAllZeros")==0); - assertTrue("Integer does not evaluate to 0", - jsonObject.optInt("integerWithAllZeros")==0); - assertTrue("Double does not evaluate to 800.90", - jsonObject.get("compositeWithLeadingZeros").equals(800.90)); - assertTrue("Double does not evaluate to 800.90", - jsonObject.getDouble("compositeWithLeadingZeros")==800.9d); - assertTrue("Integer does not evaluate to 800", - jsonObject.optInt("compositeWithLeadingZeros")==800); - assertTrue("Long does not evaluate to 800.90", - jsonObject.getLong("compositeWithLeadingZeros")==800); - assertTrue("Long does not evaluate to 800.90", - jsonObject.optLong("compositeWithLeadingZeros")==800); - assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match", - 0.9d,jsonObject.getDouble("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d); - assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match", - 0.9d,jsonObject.optDouble("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d); - assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match", - 0.0d,jsonObject.optLong("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d); - - assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", - 0.0001d,jsonObject.getDouble("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d); - assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", - 0.0001d,jsonObject.optDouble("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d); - assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", - 0.0d, jsonObject.getLong("doubleIdentifierWithMultipleLeadingZerosAfterDecimal") , 0.0d); - assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match", - 0.0d,jsonObject.optLong("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d); Util.checkJSONObjectMaps(jsonObject); } @@ -2398,7 +2334,7 @@ public void jsonObjectParsingErrors() { } try { // test validity of invalid double - JSONObject.testValidity(NaN); + JSONObject.testValidity(Double.NaN); fail("Expected an exception"); } catch (JSONException e) { assertTrue("", true); diff --git a/src/test/java/org/json/junit/JsonNumberZeroTest.java b/src/test/java/org/json/junit/JsonNumberZeroTest.java deleted file mode 100644 index bfa4ca9d8..000000000 --- a/src/test/java/org/json/junit/JsonNumberZeroTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.json.junit; - -import org.json.JSONObject; -import org.junit.Test; - -import java.math.BigDecimal; -import java.math.BigInteger; - -import static org.junit.Assert.assertEquals; - -public class JsonNumberZeroTest { - - @Test - public void shouldParseNegativeZeroValueWithMultipleZeroDigit(){ - JSONObject jsonObject = new JSONObject("{value:-0000}"); - assertEquals("Float not recognized", -0f, jsonObject.getFloat("value"), 0.0f); - assertEquals("Float not recognized", -0f, jsonObject.optFloat("value"), 0.0f); - assertEquals("Float not recognized", -0f, jsonObject.optFloatObject("value"), 0.0f); - assertEquals("Double not recognized", -0d, jsonObject.optDouble("value"), 0.0f); - assertEquals("Double not recognized", -0.0d, jsonObject.optDoubleObject("value"), 0.0f); - assertEquals("Double not recognized", -0.0d, jsonObject.getDouble("value"), 0.0f); - assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); - assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-0).compareTo(jsonObject.getBigDecimal("value"))); - assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); - } - - @Test - public void shouldParseZeroValueWithMultipleZeroDigit(){ - JSONObject jsonObject = new JSONObject("{value:0000}"); - assertEquals("Float not recognized", 0f, jsonObject.getFloat("value"), 0.0f); - assertEquals("Float not recognized", 0f, jsonObject.optFloat("value"), 0.0f); - assertEquals("Float not recognized", 0f, jsonObject.optFloatObject("value"), 0.0f); - assertEquals("Double not recognized", 0d, jsonObject.optDouble("value"), 0.0f); - assertEquals("Double not recognized", 0.0d, jsonObject.optDoubleObject("value"), 0.0f); - assertEquals("Double not recognized", 0.0d, jsonObject.getDouble("value"), 0.0f); - assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); - assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); - assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); - assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); - assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-0).compareTo(jsonObject.getBigDecimal("value"))); - assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); - } - -} diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index ba8418cb6..e9714afe7 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -761,7 +761,7 @@ public void contentOperations() { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); + final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, new XMLParserConfiguration().withKeepStrings(false)); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected); diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 9ae1ee236..db905921d 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -791,7 +791,7 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) { @Test public void testToJSONArray_jsonOutput() { final String originalXml = "011000True"; - final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":1},\"id\":[1,1,0,0],\"title\":true}}"); + final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}"); final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false); Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expectedJson); From 06778bd2d9ffe761812cc1040e67f89603ead4ca Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 24 Feb 2024 21:21:06 +0100 Subject: [PATCH 112/233] #863 compute initial capacity for StringBuilderWriter --- src/main/java/org/json/JSONArray.java | 5 ++++- src/main/java/org/json/JSONObject.java | 16 +++++++++++++--- src/main/java/org/json/StringBuilderWriter.java | 13 +++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index cda56944a..ba4c1d5cf 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -1694,7 +1694,10 @@ public String toString() { */ @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { - Writer sw = new StringBuilderWriter(); + // each value requires a comma, so multiply the count my 2 + // We don't want to oversize the initial capacity + int initialSize = myArrayList.size() * 2; + Writer sw = new StringBuilderWriter(Math.max(initialSize, 16)); return this.write(sw, indentFactor, 0).toString(); } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 36a7c7fe3..5980c87ab 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2226,7 +2226,10 @@ public Object optQuery(JSONPointer jsonPointer) { */ @SuppressWarnings("resource") public static String quote(String string) { - Writer sw = new StringBuilderWriter(); + if (string == null || string.isEmpty()) { + return "\"\""; + } + Writer sw = new StringBuilderWriter(string.length() + 2); try { return quote(string, sw).toString(); } catch (IOException ignored) { @@ -2557,7 +2560,10 @@ public String toString() { */ @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { - Writer w = new StringBuilderWriter(); + // 6 characters are the minimum to serialise a key value pair e.g.: "k":1, + // and we don't want to oversize the initial capacity + int initialSize = map.size() * 6; + Writer w = new StringBuilderWriter(Math.max(initialSize, 16)); return this.write(w, indentFactor, 0).toString(); } @@ -2699,6 +2705,10 @@ static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent) throws JSONException, IOException { if (value == null || value.equals(null)) { writer.write("null"); + } else if (value instanceof String) { + // assuming most values are Strings, so testing it earlier + quote(value.toString(), writer); + return writer; } else if (value instanceof JSONString) { Object o; try { @@ -2706,7 +2716,7 @@ static final Writer writeValue(Writer writer, Object value, } catch (Exception e) { throw new JSONException(e); } - writer.write(o != null ? o.toString() : quote(value.toString())); + writer.write(o != null ? o.toString() : "\"\""); } else if (value instanceof Number) { // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary final String numberAsString = numberToString((Number) value); diff --git a/src/main/java/org/json/StringBuilderWriter.java b/src/main/java/org/json/StringBuilderWriter.java index 26b4c372b..b598482ef 100644 --- a/src/main/java/org/json/StringBuilderWriter.java +++ b/src/main/java/org/json/StringBuilderWriter.java @@ -18,6 +18,19 @@ class StringBuilderWriter extends Writer { lock = builder; } + /** + * Create a new string builder writer using the specified initial string-builder buffer size. + * + * @param initialSize The number of {@code char} values that will fit into this buffer + * before it is automatically expanded + * + * @throws IllegalArgumentException If {@code initialSize} is negative + */ + StringBuilderWriter(int initialSize) { + builder = new StringBuilder(initialSize); + lock = builder; + } + @Override public void write(int c) { builder.append((char) c); From d672b44a259868dc85c6bd090cb80f1c1051ae15 Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 24 Feb 2024 21:29:28 +0100 Subject: [PATCH 113/233] #863 add StringBuilderWriter unit test --- .../org/json/StringBuilderWriterTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/java/org/json/StringBuilderWriterTest.java diff --git a/src/test/java/org/json/StringBuilderWriterTest.java b/src/test/java/org/json/StringBuilderWriterTest.java new file mode 100644 index 000000000..00f9d3c2c --- /dev/null +++ b/src/test/java/org/json/StringBuilderWriterTest.java @@ -0,0 +1,59 @@ +package org.json; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class StringBuilderWriterTest { + private StringBuilderWriter writer; + + @Before + public void setUp() { + writer = new StringBuilderWriter(); + } + + @Test + public void testWriteChar() { + writer.write('a'); + assertEquals("a", writer.toString()); + } + + @Test + public void testWriteCharArray() { + char[] chars = {'a', 'b', 'c'}; + writer.write(chars, 0, 3); + assertEquals("abc", writer.toString()); + } + + @Test + public void testWriteString() { + writer.write("hello"); + assertEquals("hello", writer.toString()); + } + + @Test + public void testWriteStringWithOffsetAndLength() { + writer.write("hello world", 6, 5); + assertEquals("world", writer.toString()); + } + + @Test + public void testAppendCharSequence() { + writer.append("hello"); + assertEquals("hello", writer.toString()); + } + + @Test + public void testAppendCharSequenceWithStartAndEnd() { + CharSequence csq = "hello world"; + writer.append(csq, 6, 11); + assertEquals("world", writer.toString()); + } + + @Test + public void testAppendChar() { + writer.append('a'); + assertEquals("a", writer.toString()); + } +} \ No newline at end of file From e2194bc1909f39ee8afd383abda6f2791cd4457e Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 24 Feb 2024 21:35:29 +0100 Subject: [PATCH 114/233] #863 undo wrong optimisation, fixing failing test --- src/main/java/org/json/JSONObject.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 5980c87ab..98105efea 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2716,7 +2716,7 @@ static final Writer writeValue(Writer writer, Object value, } catch (Exception e) { throw new JSONException(e); } - writer.write(o != null ? o.toString() : "\"\""); + writer.write(o != null ? o.toString() : quote(value.toString())); } else if (value instanceof Number) { // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary final String numberAsString = numberToString((Number) value); From d878c38d4071fd38c4b7fe1272d2e5649020449b Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 24 Feb 2024 22:36:14 +0100 Subject: [PATCH 115/233] #863 reorder instanceof checks by assumed frequency --- src/main/java/org/json/JSONObject.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 98105efea..37c8a5e9c 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2706,17 +2706,9 @@ static final Writer writeValue(Writer writer, Object value, if (value == null || value.equals(null)) { writer.write("null"); } else if (value instanceof String) { - // assuming most values are Strings, so testing it earlier + // assuming most values are Strings, so testing it early quote(value.toString(), writer); return writer; - } else if (value instanceof JSONString) { - Object o; - try { - o = ((JSONString) value).toJSONString(); - } catch (Exception e) { - throw new JSONException(e); - } - writer.write(o != null ? o.toString() : quote(value.toString())); } else if (value instanceof Number) { // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary final String numberAsString = numberToString((Number) value); @@ -2729,8 +2721,6 @@ static final Writer writeValue(Writer writer, Object value, } } else if (value instanceof Boolean) { writer.write(value.toString()); - } else if (value instanceof Enum) { - writer.write(quote(((Enum)value).name())); } else if (value instanceof JSONObject) { ((JSONObject) value).write(writer, indentFactor, indent); } else if (value instanceof JSONArray) { @@ -2741,6 +2731,16 @@ static final Writer writeValue(Writer writer, Object value, } else if (value instanceof Collection) { Collection coll = (Collection) value; new JSONArray(coll).write(writer, indentFactor, indent); + } else if (value instanceof Enum) { + writer.write(quote(((Enum)value).name())); + } else if (value instanceof JSONString) { + Object o; + try { + o = ((JSONString) value).toJSONString(); + } catch (Exception e) { + throw new JSONException(e); + } + writer.write(o != null ? o.toString() : quote(value.toString())); } else if (value.getClass().isArray()) { new JSONArray(value).write(writer, indentFactor, indent); } else { From 898288810fb55f06dc6e046212f00dcd639773c3 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 24 Feb 2024 21:07:12 -0600 Subject: [PATCH 116/233] add unit tests to clarify current behavior for JSONObject and XML --- .../java/org/json/junit/JSONObjectTest.java | 37 +++++++++++++++++++ src/test/java/org/json/junit/XMLTest.java | 15 ++++++++ 2 files changed, 52 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index d90297df3..fac8c5388 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3738,6 +3738,43 @@ public void testDifferentKeySameInstanceNotACircleReference() { new JSONObject(map1); } + @Test + public void clarifyCurrentBehavior() { + // Behavior documented in #653 optLong vs getLong inconsistencies + // This problem still exists. + // Internally, both number_1 and number_2 are stored as strings. This is reasonable since they are parsed as strings. + // However, getLong and optLong should return similar results + JSONObject json = new JSONObject("{\"number_1\":\"01234\", \"number_2\": \"332211\"}"); + assertEquals(json.getLong("number_1"), 1234L); + assertEquals(json.optLong("number_1"), 0); //THIS VALUE IS NOT RETURNED AS A NUMBER + assertEquals(json.getLong("number_2"), 332211L); + assertEquals(json.optLong("number_2"), 332211L); + + // Behavior documented in #826 JSONObject parsing 0-led numeric strings as ints + // After reverting the code, personId is stored as a string, and the behavior is as expected + String personId = "0123"; + JSONObject j1 = new JSONObject("{personId: " + personId + "}"); + assertEquals(j1.getString("personId"), "0123"); + + // Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number. + // This example was mentioned in the same ticket + // After reverting the code, personId is stored as a string, and the behavior is as expected + JSONObject j2 = new JSONObject("{\"personId\":0123}"); + assertEquals(j2.getString("personId"), "0123"); + + // Behavior uncovered while working on the code + // All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect + JSONObject j3 = new JSONObject("{ " + + "\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", " + + "\"hex4\": 00e0, \"hex5\": 00f0, \"hex6\": 0011 }"); + assertEquals(j3.getString("hex1"), "010e4"); + assertEquals(j3.getString("hex2"), "00f0"); + assertEquals(j3.getString("hex3"), "0011"); + assertEquals(j3.getLong("hex4"), 0, .1); + assertEquals(j3.getString("hex5"), "00f0"); + assertEquals(j3.getString("hex6"), "0011"); + } + /** * Method to build nested map of max maxDepth * diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index db905921d..9bb3d9f84 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1397,6 +1397,21 @@ public void testWithWhitespaceTrimmingEnabledByDefault() { Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); } + @Test + public void clarifyCurrentBehavior() { + + // Behavior documented in #852 + // After reverting the code, value is still stored as a number. This is due to how XML.isDecimalNotation() works + // and is probably a bug. JSONObject has a similar problem. + String str = " primary 008E97 "; + JSONObject jsonObject = XML.toJSONObject(str); + assertEquals(jsonObject.getJSONObject("color").getLong("value"), 0e897, .1); + + // Workaround for now is to use keepStrings + JSONObject jsonObject1 = XML.toJSONObject(str, new XMLParserConfiguration().withKeepStrings(true)); + assertEquals(jsonObject1.getJSONObject("color").getString("value"), "008E97"); + } + } From 4f456d94324e661e6499a9bcf1b29c8db975a097 Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 25 Feb 2024 09:42:06 +0100 Subject: [PATCH 117/233] #863 fix changed behaviour of changing order in writeValue with JSONString --- src/main/java/org/json/JSONObject.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 37c8a5e9c..d18429a24 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2705,6 +2705,15 @@ static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent) throws JSONException, IOException { if (value == null || value.equals(null)) { writer.write("null"); + } else if (value instanceof JSONString) { + // JSONString must be checked first, so it can overwrite behaviour of other types + Object o; + try { + o = ((JSONString) value).toJSONString(); + } catch (Exception e) { + throw new JSONException(e); + } + writer.write(o != null ? o.toString() : quote(value.toString())); } else if (value instanceof String) { // assuming most values are Strings, so testing it early quote(value.toString(), writer); @@ -2733,14 +2742,6 @@ static final Writer writeValue(Writer writer, Object value, new JSONArray(coll).write(writer, indentFactor, indent); } else if (value instanceof Enum) { writer.write(quote(((Enum)value).name())); - } else if (value instanceof JSONString) { - Object o; - try { - o = ((JSONString) value).toJSONString(); - } catch (Exception e) { - throw new JSONException(e); - } - writer.write(o != null ? o.toString() : quote(value.toString())); } else if (value.getClass().isArray()) { new JSONArray(value).write(writer, indentFactor, indent); } else { From f38452a00c3f17edf718b09c43f1570138ac2e9c Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 25 Feb 2024 09:47:40 +0100 Subject: [PATCH 118/233] add a comment explaining the ordering (cherry picked from commit df0e3e9ab73d99f1256055a17bd86c8a1a000b59) --- src/main/java/org/json/JSONObject.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index d18429a24..0f56c496e 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2706,7 +2706,7 @@ static final Writer writeValue(Writer writer, Object value, if (value == null || value.equals(null)) { writer.write("null"); } else if (value instanceof JSONString) { - // JSONString must be checked first, so it can overwrite behaviour of other types + // JSONString must be checked first, so it can overwrite behaviour of other types below Object o; try { o = ((JSONString) value).toJSONString(); From d520210ea26883965506d04153557e101903754a Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 25 Feb 2024 10:45:34 -0600 Subject: [PATCH 119/233] Added one more example to XMLTest clarifyCurrentBehavior() --- src/test/java/org/json/junit/XMLTest.java | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 9bb3d9f84..3b26b22e2 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1400,16 +1400,30 @@ public void testWithWhitespaceTrimmingEnabledByDefault() { @Test public void clarifyCurrentBehavior() { + // Behavior documented in #826 + // After reverting the code, amount is stored as numeric, and phone is stored as string + String str1 = + " \n" + + " 0123456789\n" + + " 0.1230\n" + + " true\n" + + " "; + JSONObject jsonObject1 = XML.toJSONObject(str1, + new XMLParserConfiguration().withKeepStrings(false)); + assertEquals(jsonObject1.getJSONObject("datatypes").getFloat("amount"), 0.123, .1); + assertEquals(jsonObject1.getJSONObject("datatypes").getString("telephone"), "0123456789"); + + // Behavior documented in #852 // After reverting the code, value is still stored as a number. This is due to how XML.isDecimalNotation() works // and is probably a bug. JSONObject has a similar problem. - String str = " primary 008E97 "; - JSONObject jsonObject = XML.toJSONObject(str); - assertEquals(jsonObject.getJSONObject("color").getLong("value"), 0e897, .1); + String str2 = " primary 008E97 "; + JSONObject jsonObject2 = XML.toJSONObject(str2); + assertEquals(jsonObject2.getJSONObject("color").getLong("value"), 0e897, .1); // Workaround for now is to use keepStrings - JSONObject jsonObject1 = XML.toJSONObject(str, new XMLParserConfiguration().withKeepStrings(true)); - assertEquals(jsonObject1.getJSONObject("color").getString("value"), "008E97"); + JSONObject jsonObject3 = XML.toJSONObject(str2, new XMLParserConfiguration().withKeepStrings(true)); + assertEquals(jsonObject3.getJSONObject("color").getString("value"), "008E97"); } } From 8de0628bd11912cbcaf11da566751f6396e096fe Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 2 Mar 2024 08:55:24 -0600 Subject: [PATCH 120/233] pipeline-updates - disable deployment.yml workflow for now (it's not set up in secrets yet) --- .github/workflows/deployment.yml | 153 ++++++++++++++++--------------- 1 file changed, 77 insertions(+), 76 deletions(-) diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index e87064441..8cda38efc 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -4,79 +4,80 @@ # * https://github.com/actions/setup-java/blob/v3.13.0/docs/advanced-usage.md#Publishing-using-Apache-Maven # -name: Deployment workflow - -on: - release: - types: [published] - -jobs: - # old-school build and jar method. No tests run or compiled. - publish-1_6: - name: Publish Java 1.6 to GitHub Release - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - uses: actions/checkout@v4 - - name: Setup java - uses: actions/setup-java@v1 - with: - java-version: 1.6 - - name: Compile Java 1.6 - run: | - mkdir -p target/classes - javac -version - javac -source 1.6 -target 1.6 -d target/classes/ src/main/java/org/json/*.java - - name: Create JAR 1.6 - run: | - jar cvf "target/org.json-1.6-${{ github.ref_name }}.jar" -C target/classes . - - name: Add 1.6 Jar To Release - uses: softprops/action-gh-release@v1 - with: - append_body: true - files: | - target/*.jar - publish: - name: Publish Java 8 to Maven Central and GitHub Release - runs-on: ubuntu-latest - permissions: - contents: write - packages: write - steps: - - uses: actions/checkout@v4 - - name: Set up Java for publishing to Maven Central Repository - uses: actions/setup-java@v3 - with: - # Use lowest supported LTS Java version - java-version: '8' - distribution: 'temurin' - server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml - server-username: MAVEN_USERNAME # env variable for username in deploy - server-password: MAVEN_PASSWORD # env variable for token in deploy - gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import - gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase - - - name: Publish to the Maven Central Repository - run: mvn --batch-mode deploy - env: - MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} - MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} - MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} - - - name: Add Jar To Release - uses: softprops/action-gh-release@v1 - with: - append_body: true - files: | - target/*.jar - # - name: Set up Java for publishing to GitHub Packages - # uses: actions/setup-java@v3 - # with: - # # Use lowest supported LTS Java version - # java-version: '8' - # distribution: 'temurin' - # - name: Publish to GitHub Packages - # run: mvn --batch-mode deploy - # env: - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# COMMENTING OUT UNTIL SECRETS CAN BE ADDED +#name: Deployment workflow +# +#on: +# release: +# types: [published] +# +#jobs: +# # old-school build and jar method. No tests run or compiled. +# publish-1_6: +# name: Publish Java 1.6 to GitHub Release +# runs-on: ubuntu-latest +# permissions: +# contents: write +# steps: +# - uses: actions/checkout@v4 +# - name: Setup java +# uses: actions/setup-java@v1 +# with: +# java-version: 1.6 +# - name: Compile Java 1.6 +# run: | +# mkdir -p target/classes +# javac -version +# javac -source 1.6 -target 1.6 -d target/classes/ src/main/java/org/json/*.java +# - name: Create JAR 1.6 +# run: | +# jar cvf "target/org.json-1.6-${{ github.ref_name }}.jar" -C target/classes . +# - name: Add 1.6 Jar To Release +# uses: softprops/action-gh-release@v1 +# with: +# append_body: true +# files: | +# target/*.jar +# publish: +# name: Publish Java 8 to Maven Central and GitHub Release +# runs-on: ubuntu-latest +# permissions: +# contents: write +# packages: write +# steps: +# - uses: actions/checkout@v4 +# - name: Set up Java for publishing to Maven Central Repository +# uses: actions/setup-java@v3 +# with: +# # Use lowest supported LTS Java version +# java-version: '8' +# distribution: 'temurin' +# server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml +# server-username: MAVEN_USERNAME # env variable for username in deploy +# server-password: MAVEN_PASSWORD # env variable for token in deploy +# gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import +# gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase +# +# - name: Publish to the Maven Central Repository +# run: mvn --batch-mode deploy +# env: +# MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} +# MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} +# MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} +# +# - name: Add Jar To Release +# uses: softprops/action-gh-release@v1 +# with: +# append_body: true +# files: | +# target/*.jar +# # - name: Set up Java for publishing to GitHub Packages +# # uses: actions/setup-java@v3 +# # with: +# # # Use lowest supported LTS Java version +# # java-version: '8' +# # distribution: 'temurin' +# # - name: Publish to GitHub Packages +# # run: mvn --batch-mode deploy +# # env: +# # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 989cdb61bcf46ad8a9d3759ec4c65ed2956330d3 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 2 Mar 2024 09:15:32 -0600 Subject: [PATCH 121/233] pipeline-updates - do not build in parallel --- .github/workflows/pipeline.yml | 101 ++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 7350f7a96..bb4cf0723 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -35,6 +35,54 @@ jobs: name: Create java 1.6 JAR path: target/*.jar + build-8: + runs-on: ubuntu-latest + strategy: + fail-fast: false + max-parallel: 1 + matrix: + # build against supported Java LTS versions: + java: [ 8 ] + name: Java ${{ matrix.java }} + steps: + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: ${{ matrix.java }} + cache: 'maven' + - name: Compile Java ${{ matrix.java }} + run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true + - name: Run Tests ${{ matrix.java }} + run: | + mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + - name: Build Test Report ${{ matrix.java }} + if: ${{ always() }} + run: | + mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + - name: Upload Test Results ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Test Results ${{ matrix.java }} + path: target/surefire-reports/ + - name: Upload Test Report ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Test Report ${{ matrix.java }} + path: target/site/ + - name: Package Jar ${{ matrix.java }} + run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true + - name: Upload Package Results ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Package Jar ${{ matrix.java }} + path: target/*.jar + build-11: runs-on: ubuntu-latest strategy: @@ -83,15 +131,62 @@ jobs: name: Package Jar ${{ matrix.java }} path: target/*.jar + build-17: + runs-on: ubuntu-latest + strategy: + fail-fast: false + max-parallel: 1 + matrix: + # build against supported Java LTS versions: + java: [ 17 ] + name: Java ${{ matrix.java }} + steps: + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: ${{ matrix.java }} + cache: 'maven' + - name: Compile Java ${{ matrix.java }} + run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true + - name: Run Tests ${{ matrix.java }} + run: | + mvn test -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + - name: Build Test Report ${{ matrix.java }} + if: ${{ always() }} + run: | + mvn surefire-report:report-only -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + mvn site -D generateReports=false -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} + - name: Upload Test Results ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Test Results ${{ matrix.java }} + path: target/surefire-reports/ + - name: Upload Test Report ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Test Report ${{ matrix.java }} + path: target/site/ + - name: Package Jar ${{ matrix.java }} + run: mvn clean package -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true + - name: Upload Package Results ${{ matrix.java }} + if: ${{ always() }} + uses: actions/upload-artifact@v3 + with: + name: Package Jar ${{ matrix.java }} + path: target/*.jar - build-matrix: + build-21: runs-on: ubuntu-latest strategy: fail-fast: false - max-parallel: 2 + max-parallel: 1 matrix: # build against supported Java LTS versions: - java: [ 8, 17, 21 ] + java: [ 21 ] name: Java ${{ matrix.java }} steps: - uses: actions/checkout@v3 From 3eb8a62af614c2507c6027c55414c15c93a197f2 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 2 Mar 2024 09:57:40 -0600 Subject: [PATCH 122/233] pipeline-updates - space after # char? --- .github/workflows/deployment.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 8cda38efc..35a74f57c 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -3,15 +3,15 @@ # * https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions # * https://github.com/actions/setup-java/blob/v3.13.0/docs/advanced-usage.md#Publishing-using-Apache-Maven # - +# # COMMENTING OUT UNTIL SECRETS CAN BE ADDED -#name: Deployment workflow +# name: Deployment workflow # -#on: +# on: # release: # types: [published] # -#jobs: +# jobs: # # old-school build and jar method. No tests run or compiled. # publish-1_6: # name: Publish Java 1.6 to GitHub Release From 390d442054c1591c895710d4df8da024dd95fe0a Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 2 Mar 2024 10:00:13 -0600 Subject: [PATCH 123/233] pipeline-updates - remove deployment.yml for now, will restore after setting up secrets --- .github/workflows/deployment.yml | 83 -------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 .github/workflows/deployment.yml diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml deleted file mode 100644 index 35a74f57c..000000000 --- a/.github/workflows/deployment.yml +++ /dev/null @@ -1,83 +0,0 @@ -# For more information see: -# * https://docs.github.com/en/actions/learn-github-actions -# * https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions -# * https://github.com/actions/setup-java/blob/v3.13.0/docs/advanced-usage.md#Publishing-using-Apache-Maven -# -# -# COMMENTING OUT UNTIL SECRETS CAN BE ADDED -# name: Deployment workflow -# -# on: -# release: -# types: [published] -# -# jobs: -# # old-school build and jar method. No tests run or compiled. -# publish-1_6: -# name: Publish Java 1.6 to GitHub Release -# runs-on: ubuntu-latest -# permissions: -# contents: write -# steps: -# - uses: actions/checkout@v4 -# - name: Setup java -# uses: actions/setup-java@v1 -# with: -# java-version: 1.6 -# - name: Compile Java 1.6 -# run: | -# mkdir -p target/classes -# javac -version -# javac -source 1.6 -target 1.6 -d target/classes/ src/main/java/org/json/*.java -# - name: Create JAR 1.6 -# run: | -# jar cvf "target/org.json-1.6-${{ github.ref_name }}.jar" -C target/classes . -# - name: Add 1.6 Jar To Release -# uses: softprops/action-gh-release@v1 -# with: -# append_body: true -# files: | -# target/*.jar -# publish: -# name: Publish Java 8 to Maven Central and GitHub Release -# runs-on: ubuntu-latest -# permissions: -# contents: write -# packages: write -# steps: -# - uses: actions/checkout@v4 -# - name: Set up Java for publishing to Maven Central Repository -# uses: actions/setup-java@v3 -# with: -# # Use lowest supported LTS Java version -# java-version: '8' -# distribution: 'temurin' -# server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml -# server-username: MAVEN_USERNAME # env variable for username in deploy -# server-password: MAVEN_PASSWORD # env variable for token in deploy -# gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import -# gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase -# -# - name: Publish to the Maven Central Repository -# run: mvn --batch-mode deploy -# env: -# MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} -# MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} -# MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} -# -# - name: Add Jar To Release -# uses: softprops/action-gh-release@v1 -# with: -# append_body: true -# files: | -# target/*.jar -# # - name: Set up Java for publishing to GitHub Packages -# # uses: actions/setup-java@v3 -# # with: -# # # Use lowest supported LTS Java version -# # java-version: '8' -# # distribution: 'temurin' -# # - name: Publish to GitHub Packages -# # run: mvn --batch-mode deploy -# # env: -# # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 3d69990ab56185fef67c2b95a1af3379e679dac1 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 3 Mar 2024 08:47:53 -0600 Subject: [PATCH 124/233] 20240303-pre-release-updates updates for release --- README.md | 2 +- docs/RELEASES.md | 2 ++ pom.xml | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6d17373ce..e46d25700 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ JSON in Java [package org.json] [![Java CI with Maven](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml) [![CodeQL](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20240205/json-20240205.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20240205/json-20240303.jar)** # Overview diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 3308e6ecf..30b8af2bc 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -5,6 +5,8 @@ and artifactId "json". For example: [https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav) ~~~ +20240303 Revert optLong/getLong changes, and recent commits. + 20240205 Recent commits. 20231013 First release with minimum Java version 1.8. Recent commits, including fixes for CVE-2023-5072. diff --git a/pom.xml b/pom.xml index 7196978d0..7b102433b 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.json json - 20240205 + 20240303 bundle JSON in Java From 63625b3c622407273d2654660be7659ac5d74db7 Mon Sep 17 00:00:00 2001 From: Simulant Date: Tue, 5 Mar 2024 09:43:54 +0100 Subject: [PATCH 125/233] #863 improve performance of JSONTokener#nextString replacing a switch-case statement with few branches by if-else cases --- src/main/java/org/json/JSONTokener.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 0bc6dfb68..96dc71c72 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -295,12 +295,9 @@ public String nextString(char quote) throws JSONException { StringBuilder sb = new StringBuilder(); for (;;) { c = this.next(); - switch (c) { - case 0: - case '\n': - case '\r': - throw this.syntaxError("Unterminated string"); - case '\\': + if (c == quote) { + return sb.toString(); + } else if (c == '\\') { c = this.next(); switch (c) { case 'b': @@ -334,11 +331,9 @@ public String nextString(char quote) throws JSONException { default: throw this.syntaxError("Illegal escape."); } - break; - default: - if (c == quote) { - return sb.toString(); - } + } else if (c == 0 || c == '\n' || c == '\r') { + throw this.syntaxError("Unterminated string"); + } else { sb.append(c); } } From 5407423e439dfb4095e371666a65cf4f6603606d Mon Sep 17 00:00:00 2001 From: Simulant Date: Tue, 5 Mar 2024 22:11:24 +0100 Subject: [PATCH 126/233] #863 replace usage of back() method in JSONObject parsing --- src/main/java/org/json/JSONObject.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 0f56c496e..8b74607aa 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -214,8 +214,8 @@ public JSONObject(JSONTokener x) throws JSONException { if (x.nextClean() != '{') { throw x.syntaxError("A JSONObject text must begin with '{'"); } + c = x.nextClean(); for (;;) { - c = x.nextClean(); switch (c) { case 0: throw x.syntaxError("A JSONObject text must end with '}'"); @@ -252,13 +252,13 @@ public JSONObject(JSONTokener x) throws JSONException { switch (x.nextClean()) { case ';': case ',': - if (x.nextClean() == '}') { + c = x.nextClean(); + if (c == '}') { return; } if (x.end()) { throw x.syntaxError("A JSONObject text must end with '}'"); } - x.back(); break; case '}': return; From c010033591c192a0821bdd8fe23bc61cdc6fe738 Mon Sep 17 00:00:00 2001 From: Simulant Date: Tue, 5 Mar 2024 22:12:57 +0100 Subject: [PATCH 127/233] #863 replace short switch statements with if-else --- src/main/java/org/json/JSONObject.java | 7 +++---- src/main/java/org/json/JSONTokener.java | 9 +++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 8b74607aa..38c6852b6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -216,12 +216,11 @@ public JSONObject(JSONTokener x) throws JSONException { } c = x.nextClean(); for (;;) { - switch (c) { - case 0: + if (c == 0) { throw x.syntaxError("A JSONObject text must end with '}'"); - case '}': + } else if (c == '}') { return; - default: + } else { key = x.nextSimpleValue(c).toString(); } diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 96dc71c72..0e78909ef 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -397,15 +397,14 @@ 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); } 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); @@ -419,9 +418,7 @@ public Object nextValue() throws JSONException { Object nextSimpleValue(char c) { String string; - switch (c) { - case '"': - case '\'': + if (c == '"' || c == '\'') { return this.nextString(c); } From dab29ec1d537c3f2f124fe1505f56b9756b45b33 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 9 Mar 2024 09:15:53 -0600 Subject: [PATCH 128/233] remove-jsonparserconfig-ctor - just use the withOverwriteDuplicateKey() method --- .../java/org/json/JSONParserConfiguration.java | 16 +++------------- .../json/junit/JSONParserConfigurationTest.java | 3 ++- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index fc16f617c..190daeb88 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -13,24 +13,14 @@ public class JSONParserConfiguration extends ParserConfiguration { * Configuration with the default values. */ public JSONParserConfiguration() { - this(false); - } - - /** - * Configure the parser with argument overwriteDuplicateKey. - * - * @param overwriteDuplicateKey Indicate whether to overwrite duplicate key or not.
- * If not, the JSONParser will throw a {@link JSONException} - * when meeting duplicate keys. - */ - public JSONParserConfiguration(boolean overwriteDuplicateKey) { super(); - this.overwriteDuplicateKey = overwriteDuplicateKey; + this.overwriteDuplicateKey = false; } @Override protected JSONParserConfiguration clone() { - JSONParserConfiguration clone = new JSONParserConfiguration(overwriteDuplicateKey); + JSONParserConfiguration clone = new JSONParserConfiguration(); + clone.overwriteDuplicateKey = overwriteDuplicateKey; clone.maxNestingDepth = maxNestingDepth; return clone; } diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java index 0e80d77fe..509b98879 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -18,7 +18,8 @@ public void testThrowException() { @Test public void testOverwrite() { - JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true)); + JSONObject jsonObject = new JSONObject(TEST_SOURCE, + new JSONParserConfiguration().withOverwriteDuplicateKey(true)); assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); } From eda08415cadc8bdd24d4a20073fe6d584482dfad Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 10 Mar 2024 21:05:22 +0100 Subject: [PATCH 129/233] Revert "#863 increase compiler stack size on build pipeline" This reverts commit 6660e4091569fc48e582ab77c6626491f8bab8db. --- .github/workflows/pipeline.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 92b55283a..63540cc6c 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -52,8 +52,6 @@ jobs: java-version: ${{ matrix.java }} cache: 'maven' - name: Compile Java ${{ matrix.java }} - env: - MAVEN_OPTS: -Xss4m run: mvn clean compile -D maven.compiler.source=${{ matrix.java }} -D maven.compiler.target=${{ matrix.java }} -D maven.test.skip=true -D maven.site.skip=true -D maven.javadoc.skip=true - name: Run Tests ${{ matrix.java }} run: | From 045324ab42a0415f8cd65bc0a11dcbbc8626ba91 Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 10 Mar 2024 21:08:10 +0100 Subject: [PATCH 130/233] Revert "#863 replace short switch statements with if-else" This reverts commit c010033591c192a0821bdd8fe23bc61cdc6fe738. --- src/main/java/org/json/JSONObject.java | 7 ++++--- src/main/java/org/json/JSONTokener.java | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 38c6852b6..8b74607aa 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -216,11 +216,12 @@ public JSONObject(JSONTokener x) throws JSONException { } c = x.nextClean(); for (;;) { - if (c == 0) { + switch (c) { + case 0: throw x.syntaxError("A JSONObject text must end with '}'"); - } else if (c == '}') { + case '}': return; - } else { + default: key = x.nextSimpleValue(c).toString(); } diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 0e78909ef..96dc71c72 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -397,14 +397,15 @@ public String nextTo(String delimiters) throws JSONException { */ public Object nextValue() throws JSONException { char c = this.nextClean(); - if (c == '{') { + switch (c) { + case '{': this.back(); try { return new JSONObject(this); } catch (StackOverflowError e) { throw new JSONException("JSON Array or Object depth too large to process.", e); } - } else if (c == '[') { + case '[': this.back(); try { return new JSONArray(this); @@ -418,7 +419,9 @@ public Object nextValue() throws JSONException { Object nextSimpleValue(char c) { String string; - if (c == '"' || c == '\'') { + switch (c) { + case '"': + case '\'': return this.nextString(c); } From a3f15e588301c6d7b12352117e6d4dcb1fd17ebe Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 10 Mar 2024 21:08:31 +0100 Subject: [PATCH 131/233] Revert "#863 replace usage of back() method in JSONObject parsing" This reverts commit 5407423e439dfb4095e371666a65cf4f6603606d. --- src/main/java/org/json/JSONObject.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 8b74607aa..0f56c496e 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -214,8 +214,8 @@ public JSONObject(JSONTokener x) throws JSONException { if (x.nextClean() != '{') { throw x.syntaxError("A JSONObject text must begin with '{'"); } - c = x.nextClean(); for (;;) { + c = x.nextClean(); switch (c) { case 0: throw x.syntaxError("A JSONObject text must end with '}'"); @@ -252,13 +252,13 @@ public JSONObject(JSONTokener x) throws JSONException { switch (x.nextClean()) { case ';': case ',': - c = x.nextClean(); - if (c == '}') { + if (x.nextClean() == '}') { return; } if (x.end()) { throw x.syntaxError("A JSONObject text must end with '}'"); } + x.back(); break; case '}': return; From 0c5cf182552f0c2564c0d0d0b253829988d8a1e1 Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 10 Mar 2024 21:12:28 +0100 Subject: [PATCH 132/233] Revert "#863 improve performance of JSONTokener#nextString" This reverts commit 63625b3c622407273d2654660be7659ac5d74db7. --- src/main/java/org/json/JSONTokener.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java index 96dc71c72..0bc6dfb68 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -295,9 +295,12 @@ public String nextString(char quote) throws JSONException { StringBuilder sb = new StringBuilder(); for (;;) { c = this.next(); - if (c == quote) { - return sb.toString(); - } else if (c == '\\') { + switch (c) { + case 0: + case '\n': + case '\r': + throw this.syntaxError("Unterminated string"); + case '\\': c = this.next(); switch (c) { case 'b': @@ -331,9 +334,11 @@ public String nextString(char quote) throws JSONException { default: throw this.syntaxError("Illegal escape."); } - } else if (c == 0 || c == '\n' || c == '\r') { - throw this.syntaxError("Unterminated string"); - } else { + break; + default: + if (c == quote) { + return sb.toString(); + } sb.append(c); } } From 60090a7167bf86f3cd9af10863b42c18e7ee754d Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 25 Feb 2024 09:45:57 +0100 Subject: [PATCH 133/233] add a test case for an enum implementing JSONString (cherry picked from commit d17bbbd4174b3d84f70a6f0fdce9edc10d846a1a) --- src/test/java/org/json/junit/JSONStringTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java index b4fee3eb7..235df1806 100644 --- a/src/test/java/org/json/junit/JSONStringTest.java +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -319,6 +319,22 @@ public void testNullStringValue() throws Exception { } } + @Test + public void testEnumJSONString() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("key", MyEnum.MY_ENUM); + assertEquals("{\"key\":\"myJsonString\"}", jsonObject.toString()); + } + + private enum MyEnum implements JSONString { + MY_ENUM; + + @Override + public String toJSONString() { + return "\"myJsonString\""; + } + } + /** * A JSONString that returns a valid JSON string value. */ From 6c35b08ad64b65256b0ab2a9f932a84224bb10c9 Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 10 Mar 2024 23:20:09 +0100 Subject: [PATCH 134/233] #863 make StringBuilderWriter public and move test --- src/main/java/org/json/StringBuilderWriter.java | 6 +++--- .../java/org/json/{ => junit}/StringBuilderWriterTest.java | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) rename src/test/java/org/json/{ => junit}/StringBuilderWriterTest.java (95%) diff --git a/src/main/java/org/json/StringBuilderWriter.java b/src/main/java/org/json/StringBuilderWriter.java index b598482ef..4aaa4903f 100644 --- a/src/main/java/org/json/StringBuilderWriter.java +++ b/src/main/java/org/json/StringBuilderWriter.java @@ -7,13 +7,13 @@ * Performance optimised alternative for {@link java.io.StringWriter} * using internally a {@link StringBuilder} instead of a {@link StringBuffer}. */ -class StringBuilderWriter extends Writer { +public class StringBuilderWriter extends Writer { private final StringBuilder builder; /** * Create a new string builder writer using the default initial string-builder buffer size. */ - StringBuilderWriter() { + public StringBuilderWriter() { builder = new StringBuilder(); lock = builder; } @@ -26,7 +26,7 @@ class StringBuilderWriter extends Writer { * * @throws IllegalArgumentException If {@code initialSize} is negative */ - StringBuilderWriter(int initialSize) { + public StringBuilderWriter(int initialSize) { builder = new StringBuilder(initialSize); lock = builder; } diff --git a/src/test/java/org/json/StringBuilderWriterTest.java b/src/test/java/org/json/junit/StringBuilderWriterTest.java similarity index 95% rename from src/test/java/org/json/StringBuilderWriterTest.java rename to src/test/java/org/json/junit/StringBuilderWriterTest.java index 00f9d3c2c..b12f5db0c 100644 --- a/src/test/java/org/json/StringBuilderWriterTest.java +++ b/src/test/java/org/json/junit/StringBuilderWriterTest.java @@ -1,7 +1,8 @@ -package org.json; +package org.json.junit; import static org.junit.Assert.assertEquals; +import org.json.StringBuilderWriter; import org.junit.Before; import org.junit.Test; From b75da0754519194e20e84712eab24c71ac524d3d Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 10 Mar 2024 23:21:47 +0100 Subject: [PATCH 135/233] #863 move instanceof Enum check back to original position --- src/main/java/org/json/JSONObject.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index b8989297e..26a68c6dc 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2819,6 +2819,8 @@ static final Writer writeValue(Writer writer, Object value, } } else if (value instanceof Boolean) { writer.write(value.toString()); + } else if (value instanceof Enum) { + writer.write(quote(((Enum)value).name())); } else if (value instanceof JSONObject) { ((JSONObject) value).write(writer, indentFactor, indent); } else if (value instanceof JSONArray) { @@ -2829,8 +2831,6 @@ static final Writer writeValue(Writer writer, Object value, } else if (value instanceof Collection) { Collection coll = (Collection) value; new JSONArray(coll).write(writer, indentFactor, indent); - } else if (value instanceof Enum) { - writer.write(quote(((Enum)value).name())); } else if (value.getClass().isArray()) { new JSONArray(value).write(writer, indentFactor, indent); } else { From dcbbccc76cd2a822ac0069f4f65b18accd8a9306 Mon Sep 17 00:00:00 2001 From: rikkarth Date: Fri, 15 Mar 2024 00:19:25 +0000 Subject: [PATCH 136/233] feat(#871-strictMode): strictMode configuration add to JSONParserConfiguration docs(#871-strictMode): add javadoc --- .../org/json/JSONParserConfiguration.java | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 190daeb88..4aaf025a3 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -4,11 +4,19 @@ * Configuration object for the JSON parser. The configuration is immutable. */ public class JSONParserConfiguration extends ParserConfiguration { + /** * Used to indicate whether to overwrite duplicate key or not. */ private boolean overwriteDuplicateKey; + /** + * This flag, when set to true, instructs the parser to throw a JSONException if it encounters an invalid character + * immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the + * JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid. + */ + private boolean strictMode; + /** * Configuration with the default values. */ @@ -26,10 +34,9 @@ protected JSONParserConfiguration clone() { } /** - * Defines the maximum nesting depth that the parser will descend before throwing an exception - * when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into - * JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException - * if the maximum depth is reached. + * Defines the maximum nesting depth that the parser will descend before throwing an exception when parsing a map + * into JSONObject or parsing a {@link java.util.Collection} instance into JSONArray. The default max nesting depth + * is 512, which means the parser will throw a JsonException if the maximum depth is reached. * * @param maxNestingDepth the maximum nesting depth allowed to the JSON parser * @return The existing configuration will not be modified. A new configuration is returned. @@ -44,9 +51,8 @@ public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { } /** - * Controls the parser's behavior when meeting duplicate keys. - * If set to false, the parser will throw a JSONException when meeting a duplicate key. - * Or the duplicate key's value will be overwritten. + * Controls the parser's behavior when meeting duplicate keys. If set to false, the parser will throw a + * JSONException when meeting a duplicate key. Or the duplicate key's value will be overwritten. * * @param overwriteDuplicateKey defines should the parser overwrite duplicate keys. * @return The existing configuration will not be modified. A new configuration is returned. @@ -58,13 +64,45 @@ public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwrite return clone; } + /** - * The parser's behavior when meeting duplicate keys, controls whether the parser should - * overwrite duplicate keys or not. + * Sets the strict mode configuration for the JSON parser. + *

+ * When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character + * immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the + * JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid. + * + * @param mode a boolean value indicating whether strict mode should be enabled or not + * @return a new JSONParserConfiguration instance with the updated strict mode setting + */ + public JSONParserConfiguration withStrictMode(final boolean mode) { + JSONParserConfiguration clone = this.clone(); + clone.strictMode = mode; + + return clone; + } + + /** + * The parser's behavior when meeting duplicate keys, controls whether the parser should overwrite duplicate keys or + * not. * * @return The overwriteDuplicateKey configuration value. */ public boolean isOverwriteDuplicateKey() { return this.overwriteDuplicateKey; } + + + /** + * Retrieves the current strict mode setting of the JSON parser. + *

+ * Strict mode, when enabled, instructs the parser to throw a JSONException if it encounters an invalid character + * immediately following the final ']' character in the input. This ensures strict adherence to the JSON syntax, as + * any characters after the final closing bracket of a JSON array are considered invalid. + * + * @return the current strict mode setting. True if strict mode is enabled, false otherwise. + */ + public boolean isStrictMode() { + return this.strictMode; + } } From 63e8314debb80ab2a887b22523cff081d0a7c7e2 Mon Sep 17 00:00:00 2001 From: rikkarth Date: Fri, 15 Mar 2024 00:45:32 +0000 Subject: [PATCH 137/233] feat(#871-strictMode): strictMode JSONArray initial implementation test(#871-strictMode): initial test implementation --- src/main/java/org/json/JSONArray.java | 1143 +++++++---------- .../junit/JSONParserConfigurationTest.java | 29 +- 2 files changed, 484 insertions(+), 688 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index f86075e6b..a108a55dc 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -18,11 +18,10 @@ /** - * A JSONArray is an ordered sequence of values. Its external text form is a - * string wrapped in square brackets with commas separating the values. The - * internal form is an object having get and opt - * methods for accessing the values by index, and put methods for - * adding or replacing values. The values can be any of these types: + * A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with + * commas separating the values. The internal form is an object having get and opt methods for + * accessing the values by index, and put methods for adding or replacing values. The values can be any of + * these types: * Boolean, JSONArray, JSONObject, * Number, String, or the * JSONObject.NULL object. @@ -30,19 +29,17 @@ * The constructor can convert a JSON text into a Java object. The * toString method converts to JSON text. *

- * A get method returns a value if one can be found, and throws an - * exception if one cannot be found. An opt method returns a - * default value instead of throwing an exception, and so is useful for - * obtaining optional values. + * A get method returns a value if one can be found, and throws an exception if one cannot be found. An + * opt method returns a default value instead of throwing an exception, and so is useful for obtaining + * optional values. *

- * The generic get() and opt() methods return an - * object which you can cast or query for type. There are also typed + * The generic get() and opt() methods return an object which you can cast or query for type. + * There are also typed * get and opt methods that do type checking and type * coercion for you. *

- * The texts produced by the toString methods strictly conform to - * JSON syntax rules. The constructors are more forgiving in the texts they will - * accept: + * The texts produced by the toString methods strictly conform to JSON syntax rules. The constructors are + * more forgiving in the texts they will accept: *