From 10514e48cb665a973e8c9b04db577fa6fdaddf07 Mon Sep 17 00:00:00 2001 From: XIAYM-gh Date: Tue, 13 Feb 2024 18:56:10 +0800 Subject: [PATCH 001/103] 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 0000000..4652dbc --- /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 039f136..e7d5cd5 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.Method; 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 class JSONObject { * 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 class JSONObject { 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 class JSONObject { /** * 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 class JSONObject { * 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 f95e244..f1ea2b2 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -4,23 +4,47 @@ package org.json; * 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 0000000..73dc70b --- /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 002/103] 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 4652dbc..954ac3a 100644 --- a/src/main/java/org/json/JSONDuplicateKeyStrategy.java +++ b/src/main/java/org/json/JSONDuplicateKeyStrategy.java @@ -6,7 +6,7 @@ package org.json; */ 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 e7d5cd5..1572a81 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -256,7 +256,7 @@ public class JSONObject { case MERGE_INTO_ARRAY: if (mergedKeys == null) { - mergedKeys = new ArrayList<>(); + mergedKeys = new ArrayList(); } Object current = this.get(key); From cb2c8d39629115e740fbd05d56669f44daa597e0 Mon Sep 17 00:00:00 2001 From: XIAYM-gh Date: Wed, 14 Feb 2024 17:53:58 +0800 Subject: [PATCH 003/103] 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 954ac3a..0000000 --- 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 1572a81..317fd3d 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -212,10 +212,6 @@ public class JSONObject { 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 class JSONObject { 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 && !jsonParserConfiguration.isOverwriteDuplicateKey()) { + throw x.syntaxError("Duplicate key \"" + key + "\""); } - 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); - } + 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 f1ea2b2..0d8706c 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -5,25 +5,27 @@ package org.json; */ 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 class JSONParserConfiguration extends ParserConfiguration { 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 73dc70b..1a3525b 100644 --- a/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java +++ b/src/test/java/org/json/junit/JSONObjectDuplicateKeyTest.java @@ -7,41 +7,17 @@ import static org.junit.Assert.*; 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 - )); + JSONObject jsonObject = new JSONObject(TEST_SOURCE, new JSONParserConfiguration(true)); - 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")); + assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key")); } } From b4b39bb441d903da0597e9b626ff18c046935309 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sun, 18 Feb 2024 15:29:44 -0600 Subject: [PATCH 004/103] 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 63540cc..c0d2c1a 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 005/103] 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 c0d2c1a..46c1592 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 006/103] 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 46c1592..edd0057 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 007/103] pipeline-updates: Java 11 intermittent fail - try separate build --- .github/workflows/pipeline.yml | 61 +++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index edd0057..7350f7a 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: | - 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 + 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: + 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 008/103] 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 0d8706c..fc16f61 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -30,22 +30,50 @@ public class JSONParserConfiguration extends ParserConfiguration { @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 5cdc10d..06cc443 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 @@ public class ParserConfiguration { // 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 class ParserConfiguration { * 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 class ParserConfiguration { /** * 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 1a3525b..0000000 --- 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 0000000..0e80d77 --- /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 009/103] #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 38b0b31..cda5694 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -5,7 +5,6 @@ Public Domain. */ 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 class JSONArray implements Iterable { */ @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 4c08b0b..36a7c7f 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -6,7 +6,6 @@ Public Domain. 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 class JSONObject { */ @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 class JSONObject { */ @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 0000000..25d2dbe --- /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 010/103] #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 25d2dbe..26b4c37 100644 --- a/src/main/java/org/json/StringBuilderWriter.java +++ b/src/main/java/org/json/StringBuilderWriter.java @@ -10,26 +10,21 @@ import java.io.Writer; 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 @@ class StringBuilderWriter extends Writer { @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 011/103] #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 63540cc..92b5528 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 06778bd2d9ffe761812cc1040e67f89603ead4ca Mon Sep 17 00:00:00 2001 From: Simulant Date: Sat, 24 Feb 2024 21:21:06 +0100 Subject: [PATCH 012/103] #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 cda5694..ba4c1d5 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -1694,7 +1694,10 @@ public class JSONArray implements Iterable { */ @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 36a7c7f..5980c87 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2226,7 +2226,10 @@ public class JSONObject { */ @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 class JSONObject { */ @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 @@ public class JSONObject { 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 @@ public class JSONObject { } 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 26b4c37..b598482 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 013/103] #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 0000000..00f9d3c --- /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 014/103] #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 5980c87..98105ef 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2716,7 +2716,7 @@ public class JSONObject { } 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 015/103] #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 98105ef..37c8a5e 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2706,17 +2706,9 @@ public class JSONObject { 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 @@ public class JSONObject { } } 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 @@ public class JSONObject { } 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 4f456d94324e661e6499a9bcf1b29c8db975a097 Mon Sep 17 00:00:00 2001 From: Simulant Date: Sun, 25 Feb 2024 09:42:06 +0100 Subject: [PATCH 016/103] #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 37c8a5e..d18429a 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2705,6 +2705,15 @@ public class JSONObject { 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 @@ public class JSONObject { 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 017/103] 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 d18429a..0f56c49 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2706,7 +2706,7 @@ public class JSONObject { 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 8de0628bd11912cbcaf11da566751f6396e096fe Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 2 Mar 2024 08:55:24 -0600 Subject: [PATCH 018/103] 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 e870644..8cda38e 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 019/103] pipeline-updates - do not build in parallel --- .github/workflows/pipeline.yml | 103 +++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 7350f7a..bb4cf07 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-matrix: + build-17: 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: [ 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-21: + runs-on: ubuntu-latest + strategy: + fail-fast: false + max-parallel: 1 + matrix: + # build against supported Java LTS versions: + 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 020/103] 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 8cda38e..35a74f5 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 021/103] 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 35a74f5..0000000 --- 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 022/103] 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 6d17373..e46d257 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 3308e6e..30b8af2 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 7196978..7b10243 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 023/103] #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 0bc6dfb..96dc71c 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -295,12 +295,9 @@ public class JSONTokener { 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 class JSONTokener { 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 024/103] #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 0f56c49..8b74607 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -214,8 +214,8 @@ public class JSONObject { 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 class JSONObject { 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 025/103] #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 8b74607..38c6852 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -216,12 +216,11 @@ public class JSONObject { } 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 96dc71c..0e78909 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -397,15 +397,14 @@ public class JSONTokener { */ 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 class JSONTokener { 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 026/103] 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 fc16f61..190daeb 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 0e80d77..509b988 100644 --- a/src/test/java/org/json/junit/JSONParserConfigurationTest.java +++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java @@ -18,7 +18,8 @@ public class JSONParserConfigurationTest { @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 027/103] 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 92b5528..63540cc 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 028/103] 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 38c6852..8b74607 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -216,11 +216,12 @@ public class JSONObject { } 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 0e78909..96dc71c 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -397,14 +397,15 @@ public class JSONTokener { */ 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 class JSONTokener { 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 029/103] 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 8b74607..0f56c49 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -214,8 +214,8 @@ public class JSONObject { 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 class JSONObject { 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 030/103] 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 96dc71c..0bc6dfb 100644 --- a/src/main/java/org/json/JSONTokener.java +++ b/src/main/java/org/json/JSONTokener.java @@ -295,9 +295,12 @@ public class JSONTokener { 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 class JSONTokener { 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 031/103] 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 b4fee3e..235df18 100644 --- a/src/test/java/org/json/junit/JSONStringTest.java +++ b/src/test/java/org/json/junit/JSONStringTest.java @@ -319,6 +319,22 @@ public class JSONStringTest { } } + @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 032/103] #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 b598482..4aaa490 100644 --- a/src/main/java/org/json/StringBuilderWriter.java +++ b/src/main/java/org/json/StringBuilderWriter.java @@ -7,13 +7,13 @@ 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 { +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 00f9d3c..b12f5db 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 033/103] #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 b898929..26a68c6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2819,6 +2819,8 @@ public class JSONObject { } } 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 @@ public class JSONObject { } 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 034/103] 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 190daeb..4aaf025 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -4,11 +4,19 @@ package org.json; * 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 @@ public class JSONParserConfiguration extends ParserConfiguration { } /** - * 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 class JSONParserConfiguration extends ParserConfiguration { } /** - * 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 class JSONParserConfiguration extends ParserConfiguration { 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 035/103] feat(#871-strictMode): strictMode JSONArray initial implementation test(#871-strictMode): initial test implementation --- src/main/java/org/json/JSONArray.java | 1211 +++++++---------- .../junit/JSONParserConfigurationTest.java | 29 +- 2 files changed, 518 insertions(+), 722 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index f86075e..a108a55 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -18,11 +18,10 @@ import java.util.Map; /** - * 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 @@ import java.util.Map; * 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: *