Compare commits

...

111 Commits

Author SHA1 Message Date
Sean Leary
3d69990ab5 20240303-pre-release-updates updates for release 2024-03-03 08:47:53 -06:00
Sean Leary
ba05e1a98c Merge pull request #874 from stleary/pipeline-updates
Deployment and Pipeline action updates
2024-03-03 08:02:03 -06:00
Sean Leary
390d442054 pipeline-updates - remove deployment.yml for now, will restore after setting up secrets 2024-03-02 10:00:13 -06:00
Sean Leary
3eb8a62af6 pipeline-updates - space after # char? 2024-03-02 09:57:40 -06:00
Sean Leary
989cdb61bc pipeline-updates - do not build in parallel 2024-03-02 09:15:32 -06:00
Sean Leary
8de0628bd1 pipeline-updates - disable deployment.yml workflow for now (it's not set up in secrets yet) 2024-03-02 08:55:24 -06:00
Sean Leary
569be9d19e Merge branch 'master' into pipeline-updates 2024-03-02 08:34:10 -06:00
Sean Leary
3f97826462 Merge pull request #869 from stleary/revert-recent-objLong-getLong-changes
Revert recent obj long get long changes
2024-02-28 09:52:04 -06:00
Sean Leary
d520210ea2 Added one more example to XMLTest clarifyCurrentBehavior() 2024-02-25 10:45:34 -06:00
Sean Leary
898288810f add unit tests to clarify current behavior for JSONObject and XML 2024-02-24 21:07:12 -06:00
Sean Leary
771c82c4eb backing out recent changes to optLong, getLong. See #868 2024-02-24 13:07:51 -06:00
Sean Leary
d36066cf82 Merge pull request #860 from jscrdev/fixed-javadocs2
Added missing Javadocs for Java 21
2024-02-22 20:18:34 -06:00
Sean Leary
c1107fa987 pipeline-updates: Java 11 intermittent fail - try separate build 2024-02-18 16:17:41 -06:00
Sean Leary
cd631d970e pipeline-updates: Java 11 intermittent fail - try an earlier release (there is no later release 2024-02-18 15:54:29 -06:00
Sean Leary
f0289413d6 pipeline-updates: Java 11 intermittent fail - try increasing stack size 2024-02-18 15:45:13 -06:00
Sean Leary
b4b39bb441 pipeline-updates: Java 11 intermittent test failures, try not running in parallel 2024-02-18 15:29:44 -06:00
Valentyn Kolesnikov
86253211c2 Added missing Javadocs for Java 21 2024-02-18 04:20:33 +02:00
Sean Leary
77c899d325 Merge pull request #858 from stleary/cleanup-after-commit
cleanup-after-commit for #854 and #856
2024-02-17 16:23:51 -06:00
Sean Leary
f164b8c597 cleanup-after-commit reverted pom.xml version 8 change and tabs in cdl. Updated JavaDocs in cdl 2024-02-13 20:08:54 -06:00
Sean Leary
6358b7f681 Merge pull request #854 from jscrdev/fixed-javadocs
Enhanced documentation for Java classes
2024-02-13 09:05:48 -06:00
Sean Leary
8550175556 Merge pull request #856 from michael-ameri/custom-delimiter
add ability for custom delimiters
2024-02-13 08:21:43 -06:00
mameri
72214f1b43 add ability for custom delimiters 2024-02-09 11:52:18 +01:00
Valentyn Kolesnikov
99c84fdf3a Enhanced documentation for Java classes 2024-02-07 14:43:44 +02:00
Sean Leary
010e83b925 Update RELEASES.md for release 20240205 2024-02-05 20:44:18 -06:00
Sean Leary
9865dbbebe Update pom.xml for release 20240205 2024-02-05 20:23:59 -06:00
Sean Leary
4548696c8d Update README.md for release 20240205 2024-02-05 20:22:23 -06:00
Sean Leary
f0308a3475 Merge pull request #855 from stleary/Fix-stack-overflow-failures
Fixing JSONArrayTest testRecursiveDepthArrayFor1000Levels()
2024-02-05 14:18:20 -06:00
Sean Leary
19dec1bb5f Fixing JSONArrayTest testRecursiveDepthArrayFor1000Levels() 2024-02-02 13:11:37 -06:00
Sean Leary
f2d20988de Merge pull request #832 from keatontaylor10/feature-disable-whitespace-trim
Add a config flag to disable whitespace trimming
2024-01-26 19:40:36 -06:00
Keaton
7915d8518f Merge branch 'stleary:master' into feature-disable-whitespace-trim 2024-01-18 09:17:26 +02:00
Sean Leary
55b824d4c4 Merge pull request #846 from stleary/cleanup-and-merge-tests
Cleanup warnings and merge new unit tests
2024-01-04 08:52:02 -06:00
Sean Leary
ac7806d060 Merge pull request #845 from seppl831/fix-annotation-search-performance
improved annotation search performance
2024-01-01 11:43:15 -06:00
Sean Leary
86bb0a1a02 cleanup-and-merge-tests: pull in unit tests from #809 2023-12-30 17:00:02 -06:00
Sean Leary
5ddb8c3d35 cleanup-and-merge-tests: fix warnings, set gradlew permissions, enable unchecked warnings in maven 2023-12-30 16:30:19 -06:00
Thomas Gress
23ac2e7bca improved annotation search performance 2023-12-29 12:28:24 +01:00
Sean Leary
d7819a4fa2 Merge pull request #823 from sk02241994/issue_743
JSON parsing self reference object and array
2023-12-27 05:28:17 -06:00
sk02241994
7701f21839 Adding comments 2023-12-24 11:39:26 +05:30
sk02241994
ffd48afa42 Review comments 2023-12-23 10:53:54 +05:30
sk02241994
abea194120 Adding JSONParserConfiguration for configuring the depth of nested maps 2023-12-22 15:47:55 +05:30
sk02241994
dcac3bc18e Adding test case for nested json with depth of 999, 1000, 1001 2023-12-22 15:47:54 +05:30
sk02241994
6d811607dd Resolving issue #743
- Recursive depth issue found in JSONObject
- Recursive depth issue found in JSONArray
2023-12-22 15:47:54 +05:30
Keaton Taylor
4d6de8c00a Remove unused constructor and add comment above other constructor 2023-12-13 14:04:05 +02:00
Sean Leary
6dba7220e1 Merge pull request #835 from LaFriska/branch1
deleted redundant .toString() call in README test method Sysout
2023-12-07 08:20:34 -06:00
Sean Leary
d4521696a9 Merge pull request #831 from adityap27/refactor
Refactor NumberConversionUtil and toString() of CookieList & XML Classes.
2023-12-03 10:18:22 -06:00
Keaton Taylor
e430db40aa Update XMLParserConfiguration to not be static and add a comment about the use of shouldTrimWhiteSpace 2023-11-30 10:05:54 +02:00
LaFriska
7cbeb35498 deleted redundant .toString() call in README test method Sysout 2023-11-28 17:39:46 -05:00
Aditya Purohit
aba82d9cc4 isNumericChar() - switch comparison order 2023-11-28 02:56:10 +00:00
Keaton Taylor
9ee10fdfc8 Merge remote-tracking branch 'origin/master' into feature-disable-whitespace-trim 2023-11-27 11:15:49 +02:00
Aditya Purohit
4a468d163a Merge branch 'stleary:master' into refactor 2023-11-26 20:16:53 -04:00
Sean Leary
92991770ca Merge pull request #830 from HappyHacker123/upgrade_json_path
Upgrade json-path's version to 2.4.0 to avoid dependency conflict.
2023-11-25 13:51:07 -06:00
Keaton Taylor
09f35372d4 Update clone() method so that default constructor does not need to be changed 2023-11-22 11:14:50 +02:00
Keaton Taylor
30f5b2de79 Add a config flag to disable whitespace trimming 2023-11-20 17:50:22 +02:00
Aditya Purohit
7f1cb8bf62 refactor: decompose condition of digit checks by using extra method 'isNumericChar(...)' in NumberConversionUtil. 2023-11-19 09:51:44 -04:00
Aditya Purohit
75419e3f25 refactor: introduce explaining variable 'indentationSuffix' in XML.toString() 2023-11-19 09:21:05 -04:00
Aditya Purohit
097a401f3f refactor: rename variable boolean 'b' to 'isEndOfPair' in CookieList.toString() 2023-11-19 09:11:32 -04:00
Sean Leary
5c4a7a1b1f Merge pull request #828 from harshith2000/Fix-flaky-tests
Fixed flaky tests in XMLTest.java
2023-11-17 11:51:00 -06:00
HappyHacker123
b5f9febfe9 Upgrade json-path's version to 2.4 to avoid dependency conflict. 2023-11-17 21:31:06 +08:00
Saiharshith Karuneegar Ramesh
1a61af8255 Fixed flaky tests in XMLTest.java 2023-11-13 13:25:30 -06:00
Sean Leary
11c29c366d Merge pull request #824 from johnjaylward/fix821
Ignore tests that fail due to resource differences
2023-11-10 11:27:18 -06:00
John J. Aylward
a3742acf74 Fixes #821
add ignore annotation to tests that may fail due to differences in machine resources and can't be controlled via the tests
2023-11-06 17:54:09 -05:00
Sean Leary
783d298f99 Merge pull request #814 from rudrajyotib/issue813
Refactor duplicate code for stringToNumber() in JSONObject, JSONArray, and XML
2023-10-31 17:40:17 -05:00
Sean Leary
6c1bc0660a Merge pull request #820 from rudrajyotib/issue748
Close XML tag explicitly for empty tags with configuration.
2023-10-31 17:38:45 -05:00
rudrajyoti biswas
8ec822c575 #748 - PR comments - follow convention of configuration builder. 2023-10-28 07:36:31 +05:30
rudrajyoti biswas
1ceb70b525 #813 - PR comments - alignments 2023-10-28 07:09:37 +05:30
rudrajyoti biswas
c05d7058ff #748 - javadoc updated for methods. 2023-10-27 17:17:20 +05:30
rudrajyoti biswas
07a358449a Merge branch 'master' into issue813 2023-10-27 17:09:01 +05:30
rudrajyoti biswas
7fe2fd95a5 Merge branch 'master' into issue748 2023-10-27 17:07:46 +05:30
Sean Leary
b5b9f636ff Merge pull request #815 from johnjaylward/ensure_1-6_compat
Ensure java 6 compatable
2023-10-25 13:53:23 -05:00
rudrajyoti biswas
c6ec2f0e4c #748 - close XML tag explicitly for empty tags with configuration. 2023-10-25 23:23:00 +05:30
Sean Leary
1a2108efa2 Merge pull request #812 from yeikel/patch-1
docs: use syntax highlighting
2023-10-25 09:58:30 -05:00
Sean Leary
caadcba30e Merge pull request #794 from rudrajyotib/master
XML optLong/getLong equivalent updates for string to number conversion.
2023-10-25 09:55:13 -05:00
John J. Aylward
ea842b437c remove unneeded matrix build typ for java 1.6 2023-10-23 17:13:07 -04:00
John J. Aylward
a2a8240d0d upload jar files to GitHub release 2023-10-23 17:07:10 -04:00
John J. Aylward
1ab11d0802 ensure java 6 compatable 2023-10-23 15:08:21 -04:00
rudrajyoti biswas
5539722c69 #813 - address PR review comment - brought down visibility. 2023-10-23 23:03:35 +05:30
rudrajyoti biswas
98b79ae7bf #813 - moved number conversion related common changes to utility static method. 2023-10-23 19:16:25 +05:30
Rudrajyoti Biswas
04a4c5a3ec Merge branch 'stleary:master' into master 2023-10-21 12:29:31 +00:00
Yeikel
6007165c17 docs: use syntax highlighting
use syntax highlighting to improve the format of the readme
2023-10-21 00:10:42 -04:00
Sean Leary
411f71137b Merge pull request #803 from yeikel/patch-2
ci: test with Java 21
2023-10-20 07:37:32 -05:00
Sean Leary
e9117dbe5c Merge branch 'master' into patch-2 2023-10-20 07:33:03 -05:00
Sean Leary
7a85b514a9 Merge pull request #808 from theKnightsOfRohan/fix-build-messages
Fix compiler warnings
2023-10-20 07:32:07 -05:00
Sean Leary
82c8f486c5 Merge pull request #806 from johnjaylward/deploymentPipeline
Add new deployment pipeline
2023-10-20 07:31:12 -05:00
Sean Leary
006b29bda3 Merge branch 'master' into deploymentPipeline 2023-10-20 07:28:44 -05:00
Sean Leary
996d3a5fad Merge pull request #801 from johnjaylward/updateActionsForDeploy
Updates the pipeline to validate that packaging a jar works properly
2023-10-20 07:26:41 -05:00
Yeikel
de745e9c81 ci: test with Java 21 2023-10-19 22:41:47 -04:00
rudrajyoti biswas
2374766018 #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.
2023-10-19 14:07:53 +05:30
rudrajyoti biswas
1d0775cce7 Revert changes with feature and refactor together. 2023-10-19 10:28:11 +05:30
Sean Leary
d677a99f4e Merge pull request #798 from hofi1/bugfix/fix-XML-flacky-test
fix: flakiness in org.json.junit.XMLTest#testIndentComplicatedJsonObjectWithArrayAndWithConfig
2023-10-17 13:52:29 -05:00
Sean Leary
e6d37c469d Merge pull request #788 from hofi1/bugfix/fix-JSON-flakiness
Fix string compare unit tests
2023-10-17 13:51:40 -05:00
theKnightsOfRohan
f074bed732 fix(ParserConfiguration): add <T> params to docs 2023-10-16 17:48:03 -07:00
John J. Aylward
e8f125fb6e update workflow to use GPG 2023-10-16 18:23:39 -04:00
John J. Aylward
ed183e6142 remove deprecated parent pom per Sonatype docs 2023-10-16 18:22:08 -04:00
John J. Aylward
a86786a5f5 Add snapshot repository 2023-10-16 18:03:39 -04:00
John J. Aylward
7c4f98c42c Add new deployment pipeline.
This should only trigger when a release is published
2023-10-16 17:30:57 -04:00
John J. Aylward
3894483560 Add build badges to README 2023-10-16 15:44:02 -04:00
John J. Aylward
be115059e9 Correct supported java versions 2023-10-16 15:44:02 -04:00
John J. Aylward
2b41cf44b5 include jar in job artifacts 2023-10-16 15:43:55 -04:00
John J. Aylward
9a9efac2af Correct moditect configuration to work on java8 2023-10-16 15:36:33 -04:00
John J. Aylward
134074aeaa Revert "Reverting #761"
This reverts commit b180dbedbc.
2023-10-16 15:36:33 -04:00
John J. Aylward
8540bb80c0 Validate that the mvn package step completes 2023-10-16 15:36:33 -04:00
simonh5
4dfd779b1c fix: flakiness in org.json.junit.XMLTest#testIndentComplicatedJsonObjectWithArrayAndWithConfig 2023-10-14 17:21:06 -05:00
rudrajyoti biswas
7b2677ac5a #790 - Update XML with changes for string to number conversion.
Moved the code logic to a common utility to de-duplicate.
2023-10-14 10:05:36 +05:30
simonh5
29a7f4622d remove JSONAssert 2023-10-13 21:23:09 -05:00
Sean Leary
f346203cd6 Merge pull request #793 from stleary/revert-761
Reverting #761
2023-10-13 16:10:17 -05:00
Sean Leary
b180dbedbc Reverting #761 2023-10-13 16:04:14 -05:00
Sean Leary
cca6d1020f Merge pull request #792 from stleary/pre-release-20231013
update the docs for release 20231013
2023-10-13 15:40:19 -05:00
simonh5
e4aa7f1308 fix: change from JSONAssert to checking the similarity of JSONObjects 2023-10-12 21:09:27 -05:00
Simon Hofbauer
228598ca84 Merge pull request #6 from hofi1/bugfix/fix-flakyness-testToJSONObject_reversibility
fix: flakiness in JSONMLTest#testToJSONObject_reversibility
2023-10-10 18:20:20 -05:00
Simon Hofbauer
0a6fb1d578 Merge pull request #5 from hofi1/bugfix/fix-flakyness-org.json.junit.JSONObjectTest#valueToString
fix: flakiness in org.json.junit.JSONObjectTest#valueToString
2023-10-10 18:19:28 -05:00
simonh5
ca88454f1c fix: flakiness in org.json.junit.JSONObjectTest#valueToString 2023-09-19 14:28:06 -05:00
simonh5
becc1631e6 fix: flakiness in JSONMLTest#testToJSONObject_reversibility 2023-09-18 20:20:13 -05:00
36 changed files with 1402 additions and 527 deletions

View File

@@ -1,5 +1,5 @@
# This workflow will build a Java project with Maven # 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 name: Java CI with Maven
@@ -10,12 +10,39 @@ on:
branches: [ master ] branches: [ master ]
jobs: jobs:
build: # old-school build and jar method. No tests run or compiled.
build-1_6:
name: Java 1.6
runs-on: ubuntu-latest
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 java 1.6 JAR
run: |
jar cvf target/org.json.jar -C target/classes .
- name: Upload JAR 1.6
if: ${{ always() }}
uses: actions/upload-artifact@v3
with:
name: Create java 1.6 JAR
path: target/*.jar
build-8:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
fail-fast: false
max-parallel: 1
matrix: matrix:
# build against supported Java LTS versions: # build against supported Java LTS versions:
java: [ 8, 11, 17 ] java: [ 8 ]
name: Java ${{ matrix.java }} name: Java ${{ matrix.java }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
@@ -26,24 +53,176 @@ jobs:
java-version: ${{ matrix.java }} java-version: ${{ matrix.java }}
cache: 'maven' cache: 'maven'
- name: Compile Java ${{ matrix.java }} - 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 }} - name: Run Tests ${{ matrix.java }}
run: | 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 }} - name: Build Test Report ${{ matrix.java }}
if: ${{ always() }} if: ${{ always() }}
run: | run: |
mvn surefire-report:report-only -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 -DgenerateReports=false -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.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 }} - name: Upload Test Results ${{ matrix.java }}
if: ${{ always() }} if: ${{ always() }}
uses: actions/upload-artifact@v1 uses: actions/upload-artifact@v3
with: with:
name: Test Results ${{ matrix.java }} name: Test Results ${{ matrix.java }}
path: target/surefire-reports/ path: target/surefire-reports/
- name: Upload Test Report ${{ matrix.java }} - name: Upload Test Report ${{ matrix.java }}
if: ${{ always() }} if: ${{ always() }}
uses: actions/upload-artifact@v1 uses: actions/upload-artifact@v3
with: with:
name: Test Report ${{ matrix.java }} name: Test Report ${{ matrix.java }}
path: target/site/ 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:
fail-fast: false
max-parallel: 1
matrix:
# build against supported Java LTS versions:
java: [ 11 ]
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-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-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
- 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

2
.gitignore vendored
View File

@@ -1,6 +1,8 @@
# ignore eclipse project files # ignore eclipse project files
.project .project
.classpath .classpath
# ignore vscode files
.vscode
# ignore Intellij Idea project files # ignore Intellij Idea project files
.idea .idea
*.iml *.iml

View File

@@ -7,8 +7,10 @@ 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) [![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)** **[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 # Overview
@@ -24,7 +26,8 @@ Project goals include:
* No external dependencies * No external dependencies
* Fast execution and low memory footprint * Fast execution and low memory footprint
* Maintain backward compatibility * Maintain backward compatibility
* Designed and tested to use on Java versions 1.6 - 1.11 * 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. The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL.
@@ -41,56 +44,56 @@ The org.json package can be built from the command line, Maven, and Gradle. The
**Building from the command line** **Building from the command line**
*Build the class files from the package root directory src/main/java* *Build the class files from the package root directory src/main/java*
```` ```shell
javac org/json/*.java javac org/json/*.java
```` ```
*Create the jar file in the current directory* *Create the jar file in the current directory*
```` ```shell
jar cf json-java.jar org/json/*.class jar cf json-java.jar org/json/*.class
```` ```
*Compile a program that uses the jar (see example code below)* *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 (Windows)
javac -cp .:json-java.jar Test.java (Unix Systems) javac -cp .:json-java.jar Test.java (Unix Systems)
```` ```
*Test file contents* *Test file contents*
```` ```java
import org.json.JSONObject; import org.json.JSONObject;
public class Test { public class Test {
public static void main(String args[]){ public static void main(String args[]){
JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }"); JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }");
System.out.println(jo.toString()); System.out.println(jo);
} }
} }
```` ```
*Execute the Test file* *Execute the Test file*
```` ```shell
java -cp .;json-java.jar Test (Windows) java -cp .;json-java.jar Test (Windows)
java -cp .:json-java.jar Test (Unix Systems) java -cp .:json-java.jar Test (Unix Systems)
```` ```
*Expected output* *Expected output*
```` ```json
{"abc":"def"} {"abc":"def"}
```` ```
**Tools to build the package and execute the unit tests** **Tools to build the package and execute the unit tests**
Execute the test suite with Maven: Execute the test suite with Maven:
``` ```shell
mvn clean test mvn clean test
``` ```
Execute the test suite with Gradlew: Execute the test suite with Gradlew:
``` ```shell
gradlew clean build test gradlew clean build test
``` ```

View File

@@ -21,7 +21,7 @@ repositories {
dependencies { dependencies {
testImplementation 'junit:junit:4.13.2' 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' testImplementation 'org.mockito:mockito-core:4.2.0'
} }

View File

@@ -5,6 +5,10 @@ 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) [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. 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. 20230618 Final release with Java 1.6 compatibility. Future releases will require Java 1.8 or greater.

0
gradlew vendored Normal file → Executable file
View File

37
pom.xml
View File

@@ -3,7 +3,7 @@
<groupId>org.json</groupId> <groupId>org.json</groupId>
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>20231013</version> <version>20240303</version>
<packaging>bundle</packaging> <packaging>bundle</packaging>
<name>JSON in Java</name> <name>JSON in Java</name>
@@ -21,12 +21,6 @@
</description> </description>
<url>https://github.com/douglascrockford/JSON-java</url> <url>https://github.com/douglascrockford/JSON-java</url>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</parent>
<scm> <scm>
<url>https://github.com/douglascrockford/JSON-java.git</url> <url>https://github.com/douglascrockford/JSON-java.git</url>
<connection>scm:git:git://github.com/douglascrockford/JSON-java.git</connection> <connection>scm:git:git://github.com/douglascrockford/JSON-java.git</connection>
@@ -53,6 +47,19 @@
</properties> </properties>
<distributionManagement>
<repository>
<id>ossrh</id>
<name>Central Repository OSSRH</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@@ -63,7 +70,7 @@
<dependency> <dependency>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId> <artifactId>json-path</artifactId>
<version>2.1.0</version> <version>2.4.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
@@ -97,6 +104,9 @@
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
</compilerArgs>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
@@ -173,12 +183,11 @@
<configuration> <configuration>
<jvmVersion>9</jvmVersion> <jvmVersion>9</jvmVersion>
<module> <module>
<moduleInfo> <moduleInfoSource>
<name>org.json</name> module org.json {
<exports> exports org.json;
org.json; }
</exports> </moduleInfoSource>
</moduleInfo>
</module> </module>
</configuration> </configuration>
</execution> </execution>

View File

@@ -5,15 +5,15 @@ Public Domain.
*/ */
/** /**
* This provides static methods to convert comma delimited text into a * This provides static methods to convert comma (or otherwise) delimited text into a
* JSONArray, and to convert a JSONArray into comma delimited text. Comma * 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 * delimited text is a very popular format for data interchange. It is
* understood by most database, spreadsheet, and organizer programs. * understood by most database, spreadsheet, and organizer programs.
* <p> * <p>
* Each row of text represents a row in a table or a data record. Each row * 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. * ends with a NEWLINE character. Each row contains one or more values.
* Values are separated by commas. A value can contain any character except * 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.
* <p> * <p>
* The first row usually contains the names of the columns. * The first row usually contains the names of the columns.
* <p> * <p>
@@ -25,25 +25,30 @@ Public Domain.
*/ */
public class CDL { public class CDL {
/**
* Constructs a new CDL object.
*/
public CDL() {
}
/** /**
* Get the next value. The value can be wrapped in quotes. The value can * Get the next value. The value can be wrapped in quotes. The value can
* be empty. * be empty.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @param delimiter used in the file
* @return The value string, or null if empty. * @return The value string, or null if empty.
* @throws JSONException if the quoted string is badly formed. * @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 c;
char q; char q;
StringBuilder sb; StringBuilder sb;
do { do {
c = x.next(); c = x.next();
} while (c == ' ' || c == '\t'); } while (c == ' ' || c == '\t');
switch (c) { if (c == 0) {
case 0:
return null; return null;
case '"': } else if (c == '"' || c == '\'') {
case '\'':
q = c; q = c;
sb = new StringBuilder(); sb = new StringBuilder();
for (;;) { for (;;) {
@@ -51,9 +56,9 @@ public class CDL {
if (c == q) { if (c == q) {
//Handle escaped double-quote //Handle escaped double-quote
char nextC = x.next(); char nextC = x.next();
if(nextC != '\"') { if (nextC != '\"') {
// if our quote was the end of the file, don't step // if our quote was the end of the file, don't step
if(nextC > 0) { if (nextC > 0) {
x.back(); x.back();
} }
break; break;
@@ -65,13 +70,12 @@ public class CDL {
sb.append(c); sb.append(c);
} }
return sb.toString(); return sb.toString();
case ',': } else if (c == delimiter) {
x.back(); x.back();
return ""; return "";
default:
x.back();
return x.nextTo(',');
} }
x.back();
return x.nextTo(delimiter);
} }
/** /**
@@ -81,17 +85,28 @@ public class CDL {
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
return rowToJSONArray(x, ',');
}
/**
* 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(); JSONArray ja = new JSONArray();
for (;;) { for (;;) {
String value = getValue(x); String value = getValue(x,delimiter);
char c = x.next(); char c = x.next();
if (value == null || if (value == null ||
(ja.length() == 0 && value.length() == 0 && c != ',')) { (ja.length() == 0 && value.length() == 0 && c != delimiter)) {
return null; return null;
} }
ja.put(value); ja.put(value);
for (;;) { for (;;) {
if (c == ',') { if (c == delimiter) {
break; break;
} }
if (c != ' ') { if (c != ' ') {
@@ -116,9 +131,23 @@ public class CDL {
* @return A JSONObject combining the names and values. * @return A JSONObject combining the names and values.
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws JSONException {
throws JSONException { return rowToJSONObject(names, x, ',');
JSONArray ja = rowToJSONArray(x); }
/**
* 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);
return ja != null ? ja.toJSONObject(names) : null; return ja != null ? ja.toJSONObject(names) : null;
} }
@@ -130,15 +159,27 @@ public class CDL {
* @return A string ending in NEWLINE. * @return A string ending in NEWLINE.
*/ */
public static String rowToString(JSONArray ja) { public static String rowToString(JSONArray ja) {
return rowToString(ja, ',');
}
/**
* 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(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < ja.length(); i += 1) { for (int i = 0; i < ja.length(); i += 1) {
if (i > 0) { if (i > 0) {
sb.append(','); sb.append(delimiter);
} }
Object object = ja.opt(i); Object object = ja.opt(i);
if (object != null) { if (object != null) {
String string = object.toString(); 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('\n') >= 0 || string.indexOf('\r') >= 0 ||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) { string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
sb.append('"'); sb.append('"');
@@ -167,7 +208,19 @@ public class CDL {
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static JSONArray toJSONArray(String string) throws JSONException { public static JSONArray toJSONArray(String string) throws JSONException {
return toJSONArray(new JSONTokener(string)); return 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);
} }
/** /**
@@ -178,7 +231,19 @@ public class CDL {
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static JSONArray toJSONArray(JSONTokener x) throws JSONException { public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
return toJSONArray(rowToJSONArray(x), x); return toJSONArray(x, ',');
}
/**
* 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);
} }
/** /**
@@ -189,9 +254,21 @@ public class CDL {
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static JSONArray toJSONArray(JSONArray names, String string) public static JSONArray toJSONArray(JSONArray names, String string) throws JSONException {
throws JSONException { return toJSONArray(names, string, ',');
return toJSONArray(names, new JSONTokener(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);
} }
/** /**
@@ -202,14 +279,26 @@ public class CDL {
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static JSONArray toJSONArray(JSONArray names, JSONTokener x) public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException {
throws JSONException { return toJSONArray(names, x, ',');
}
/**
* 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) { if (names == null || names.length() == 0) {
return null; return null;
} }
JSONArray ja = new JSONArray(); JSONArray ja = new JSONArray();
for (;;) { for (;;) {
JSONObject jo = rowToJSONObject(names, x); JSONObject jo = rowToJSONObject(names, x, delimiter);
if (jo == null) { if (jo == null) {
break; break;
} }
@@ -231,11 +320,24 @@ public class CDL {
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static String toString(JSONArray ja) throws JSONException { public static String toString(JSONArray ja) throws JSONException {
return toString(ja, ',');
}
/**
* 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); JSONObject jo = ja.optJSONObject(0);
if (jo != null) { if (jo != null) {
JSONArray names = jo.names(); JSONArray names = jo.names();
if (names != null) { if (names != null) {
return rowToString(names) + toString(names, ja); return rowToString(names, delimiter) + toString(names, ja, delimiter);
} }
} }
return null; return null;
@@ -250,8 +352,21 @@ public class CDL {
* @return A comma delimited text. * @return A comma delimited text.
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static String toString(JSONArray names, JSONArray ja) public static String toString(JSONArray names, JSONArray ja) throws JSONException {
throws JSONException { return toString(names, ja, ',');
}
/**
* 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) { if (names == null || names.length() == 0) {
return null; return null;
} }
@@ -259,7 +374,7 @@ public class CDL {
for (int i = 0; i < ja.length(); i += 1) { for (int i = 0; i < ja.length(); i += 1) {
JSONObject jo = ja.optJSONObject(i); JSONObject jo = ja.optJSONObject(i);
if (jo != null) { if (jo != null) {
sb.append(rowToString(jo.toJSONArray(names))); sb.append(rowToString(jo.toJSONArray(names), delimiter));
} }
} }
return sb.toString(); return sb.toString();

View File

@@ -15,6 +15,12 @@ Public Domain.
*/ */
public class Cookie { public class Cookie {
/**
* Constructs a new Cookie object.
*/
public Cookie() {
}
/** /**
* Produce a copy of a string in which the characters '+', '%', '=', ';' * Produce a copy of a string in which the characters '+', '%', '=', ';'
* and control characters are replaced with "%hh". This is a gentle form * and control characters are replaced with "%hh". This is a gentle form

View File

@@ -11,6 +11,12 @@ Public Domain.
*/ */
public class CookieList { public class CookieList {
/**
* Constructs a new CookieList object.
*/
public CookieList() {
}
/** /**
* Convert a cookie list into a JSONObject. A cookie list is a sequence * 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 '='. * of name/value pairs. The names are separated from the values by '='.
@@ -46,19 +52,19 @@ public class CookieList {
* @throws JSONException if a called function fails * @throws JSONException if a called function fails
*/ */
public static String toString(JSONObject jo) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
boolean b = false; boolean isEndOfPair = false;
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
// Don't use the new entrySet API to maintain Android support // Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) { for (final String key : jo.keySet()) {
final Object value = jo.opt(key); final Object value = jo.opt(key);
if (!JSONObject.NULL.equals(value)) { if (!JSONObject.NULL.equals(value)) {
if (b) { if (isEndOfPair) {
sb.append(';'); sb.append(';');
} }
sb.append(Cookie.escape(key)); sb.append(Cookie.escape(key));
sb.append("="); sb.append("=");
sb.append(Cookie.escape(value.toString())); sb.append(Cookie.escape(value.toString()));
b = true; isEndOfPair = true;
} }
} }
return sb.toString(); return sb.toString();

View File

@@ -13,6 +13,12 @@ import java.util.Locale;
*/ */
public class HTTP { public class HTTP {
/**
* Constructs a new HTTP object.
*/
public HTTP() {
}
/** Carriage return/line feed. */ /** Carriage return/line feed. */
public static final String CRLF = "\r\n"; public static final String CRLF = "\r\n";

View File

@@ -149,11 +149,40 @@ public class JSONArray implements Iterable<Object> {
* A Collection. * A Collection.
*/ */
public JSONArray(Collection<?> collection) { 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);
}
/**
* 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());
}
if (collection == null) { if (collection == null) {
this.myArrayList = new ArrayList<Object>(); this.myArrayList = new ArrayList<Object>();
} else { } else {
this.myArrayList = new ArrayList<Object>(collection.size()); this.myArrayList = new ArrayList<Object>(collection.size());
this.addAll(collection, true); this.addAll(collection, true, recursionDepth, jsonParserConfiguration);
} }
} }
@@ -205,7 +234,7 @@ public class JSONArray implements Iterable<Object> {
throw new JSONException( throw new JSONException(
"JSONArray initial value should be a string or collection or array."); "JSONArray initial value should be a string or collection or array.");
} }
this.addAll(array, true); this.addAll(array, true, 0);
} }
/** /**
@@ -1330,7 +1359,8 @@ public class JSONArray implements Iterable<Object> {
* The subscript. * The subscript.
* @param value * @param value
* The Map value. * The Map value.
* @return this. * @return
* reference to self
* @throws JSONException * @throws JSONException
* If the index is negative or if the value is an invalid * If the index is negative or if the value is an invalid
* number. * number.
@@ -1338,7 +1368,27 @@ public class JSONArray implements Iterable<Object> {
* If a key in the map is <code>null</code> * If a key in the map is <code>null</code>
*/ */
public JSONArray put(int index, Map<?, ?> value) throws JSONException { 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 object for the JSON parser
* @return reference to self
* @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; return this;
} }
@@ -1779,13 +1829,14 @@ public class JSONArray implements Iterable<Object> {
* @param wrap * @param wrap
* {@code true} to call {@link JSONObject#wrap(Object)} for each item, * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
* {@code false} to add the items directly * {@code false} to add the items directly
* * @param recursionDepth
* Variable for tracking the count of nested object creations.
*/ */
private void addAll(Collection<?> collection, boolean wrap) { private void addAll(Collection<?> collection, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size());
if (wrap) { if (wrap) {
for (Object o: collection){ for (Object o: collection){
this.put(JSONObject.wrap(o)); this.put(JSONObject.wrap(o, recursionDepth + 1, jsonParserConfiguration));
} }
} else { } else {
for (Object o: collection){ for (Object o: collection){
@@ -1814,7 +1865,24 @@ public class JSONArray implements Iterable<Object> {
} }
} }
} }
/**
* 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);
}
/** /**
* Add an array's elements to the JSONArray. * Add an array's elements to the JSONArray.
* *
@@ -1823,21 +1891,40 @@ public class JSONArray implements Iterable<Object> {
* JSONArray, Collection, or Iterable, an exception will be * JSONArray, Collection, or Iterable, an exception will be
* thrown. * thrown.
* @param wrap * @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());
}
/**
* 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 true} to call {@link JSONObject#wrap(Object)} for each item,
* {@code false} to add the items directly * {@code false} to add the items directly
* * @param recursionDepth
* Variable for tracking the count of nested object creations.
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
* @throws JSONException * @throws JSONException
* If not an array or if an array value is non-finite number. * If not an array or if an array value is non-finite number.
* @throws NullPointerException * @throws NullPointerException
* Thrown if the array parameter is null. * 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, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
if (array.getClass().isArray()) { if (array.getClass().isArray()) {
int length = Array.getLength(array); int length = Array.getLength(array);
this.myArrayList.ensureCapacity(this.myArrayList.size() + length); this.myArrayList.ensureCapacity(this.myArrayList.size() + length);
if (wrap) { if (wrap) {
for (int i = 0; i < length; i += 1) { 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, jsonParserConfiguration));
} }
} else { } else {
for (int i = 0; i < length; i += 1) { for (int i = 0; i < length; i += 1) {
@@ -1850,7 +1937,7 @@ public class JSONArray implements Iterable<Object> {
// JSONArray // JSONArray
this.myArrayList.addAll(((JSONArray)array).myArrayList); this.myArrayList.addAll(((JSONArray)array).myArrayList);
} else if (array instanceof Collection) { } else if (array instanceof Collection) {
this.addAll((Collection<?>)array, wrap); this.addAll((Collection<?>)array, wrap, recursionDepth);
} else if (array instanceof Iterable) { } else if (array instanceof Iterable) {
this.addAll((Iterable<?>)array, wrap); this.addAll((Iterable<?>)array, wrap);
} else { } else {

View File

@@ -13,6 +13,13 @@ Public Domain.
* @version 2016-01-30 * @version 2016-01-30
*/ */
public class JSONML { public class JSONML {
/**
* Constructs a new JSONML object.
*/
public JSONML() {
}
/** /**
* Parse XML values and store them in a JSONArray. * Parse XML values and store them in a JSONArray.
* @param x The XMLTokener containing the source string. * @param x The XMLTokener containing the source string.

View File

@@ -55,11 +55,13 @@ public class JSONMLParserConfiguration extends ParserConfiguration {
); );
} }
@SuppressWarnings("unchecked")
@Override @Override
public JSONMLParserConfiguration withKeepStrings(final boolean newVal) { public JSONMLParserConfiguration withKeepStrings(final boolean newVal) {
return super.withKeepStrings(newVal); return super.withKeepStrings(newVal);
} }
@SuppressWarnings("unchecked")
@Override @Override
public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
return super.withMaxNestingDepth(maxNestingDepth); return super.withMaxNestingDepth(maxNestingDepth);

View File

@@ -145,6 +145,11 @@ public class JSONObject {
*/ */
private final Map<String, Object> map; private final Map<String, Object> map;
/**
* Retrieves the type of the underlying Map in this class.
*
* @return The class object representing the type of the underlying Map.
*/
public Class<? extends Map> getMapType() { public Class<? extends Map> getMapType() {
return map.getClass(); return map.getClass();
} }
@@ -273,6 +278,30 @@ public class JSONObject {
* If a key in the map is <code>null</code> * If a key in the map is <code>null</code>
*/ */
public JSONObject(Map<?, ?> m) { 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);
}
/**
* Construct a JSONObject from a map with recursion depth.
*
*/
private 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) { if (m == null) {
this.map = new HashMap<String, Object>(); this.map = new HashMap<String, Object>();
} else { } else {
@@ -284,7 +313,7 @@ public class JSONObject {
final Object value = e.getValue(); final Object value = e.getValue();
if (value != null) { if (value != null) {
testValidity(value); testValidity(value);
this.map.put(String.valueOf(e.getKey()), wrap(value)); this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration));
} }
} }
} }
@@ -342,7 +371,6 @@ public class JSONObject {
* &#64;JSONPropertyIgnore * &#64;JSONPropertyIgnore
* public String getName() { return this.name; } * public String getName() { return this.name; }
* </pre> * </pre>
* <p>
* *
* @param bean * @param bean
* An object that has getter methods that should be used to make * An object that has getter methods that should be used to make
@@ -1838,6 +1866,10 @@ public class JSONObject {
} }
} }
//If the superclass is Object, no annotations will be found any more
if (c.getSuperclass().equals(Object.class))
return null;
try { try {
return getAnnotation( return getAnnotation(
c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()), c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
@@ -1892,6 +1924,10 @@ public class JSONObject {
} }
} }
//If the superclass is Object, no annotations will be found any more
if (c.getSuperclass().equals(Object.class))
return -1;
try { try {
int d = getAnnotationDepth( int d = getAnnotationDepth(
c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()), c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
@@ -2197,6 +2233,14 @@ public class JSONObject {
} }
} }
/**
* 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 { public static Writer quote(String string, Writer w) throws IOException {
if (string == null || string.isEmpty()) { if (string == null || string.isEmpty()) {
w.write("\"\""); w.write("\"\"");
@@ -2380,84 +2424,6 @@ public class JSONObject {
|| val.indexOf('E') > -1 || "-0".equals(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 * Try to convert a string into a number, boolean, or null. If the string
* can't be converted, return the string. * can't be converted, return the string.
@@ -2491,7 +2457,8 @@ public class JSONObject {
* produced, then the value will just be a 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 { try {
return stringToNumber(string); return stringToNumber(string);
} catch (Exception ignore) { } catch (Exception ignore) {
@@ -2500,26 +2467,74 @@ public class JSONObject {
return 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)
private static boolean potentialNumber(String value){ // BigInteger down conversion: We use a similar bitLength compare as
if (value == null || value.isEmpty()){ // BigInteger#intValueExact uses. Increases GC, but objects hold
return false; // 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;
} }
return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0)); throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
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';
} }
/** /**
@@ -2660,7 +2675,31 @@ public class JSONObject {
return wrap(object, null); return wrap(object, null);
} }
/**
* Wrap an object, if necessary. If the object is <code>null</code>, 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);
}
private static Object wrap(Object object, Set<Object> objectsRecord) { private static Object wrap(Object object, Set<Object> objectsRecord) {
return wrap(object, objectsRecord, 0, new JSONParserConfiguration());
}
private static Object wrap(Object object, Set<Object> objectsRecord, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
try { try {
if (NULL.equals(object)) { if (NULL.equals(object)) {
return NULL; return NULL;
@@ -2678,14 +2717,14 @@ public class JSONObject {
if (object instanceof Collection) { if (object instanceof Collection) {
Collection<?> coll = (Collection<?>) object; Collection<?> coll = (Collection<?>) object;
return new JSONArray(coll); return new JSONArray(coll, recursionDepth, jsonParserConfiguration);
} }
if (object.getClass().isArray()) { if (object.getClass().isArray()) {
return new JSONArray(object); return new JSONArray(object);
} }
if (object instanceof Map) { if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object; Map<?, ?> map = (Map<?, ?>) object;
return new JSONObject(map); return new JSONObject(map, recursionDepth, jsonParserConfiguration);
} }
Package objectPackage = object.getClass().getPackage(); Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? objectPackage String objectPackageName = objectPackage != null ? objectPackage

View File

@@ -0,0 +1,26 @@
package org.json;
/**
* Configuration object for the JSON parser. The configuration is immutable.
*/
public class JSONParserConfiguration extends ParserConfiguration {
/**
* Configuration with the default values.
*/
public JSONParserConfiguration() {
super();
}
@Override
protected JSONParserConfiguration clone() {
return new JSONParserConfiguration();
}
@SuppressWarnings("unchecked")
@Override
public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) {
return super.withMaxNestingDepth(maxNestingDepth);
}
}

View File

@@ -42,6 +42,12 @@ public class JSONPointer {
*/ */
public static class Builder { public static class Builder {
/**
* Constructs a new Builder object.
*/
public Builder() {
}
// Segments for the eventual JSONPointer string // Segments for the eventual JSONPointer string
private final List<String> refTokens = new ArrayList<String>(); private final List<String> refTokens = new ArrayList<String>();
@@ -163,6 +169,12 @@ public class JSONPointer {
//} //}
} }
/**
* 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<String> refTokens) { public JSONPointer(List<String> refTokens) {
this.refTokens = new ArrayList<String>(refTokens); this.refTokens = new ArrayList<String>(refTokens);
} }

View File

@@ -14,10 +14,21 @@ Public Domain.
public class JSONPointerException extends JSONException { public class JSONPointerException extends JSONException {
private static final long serialVersionUID = 8872944667561856751L; 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) { public JSONPointerException(String message) {
super(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) { public JSONPointerException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }

View File

@@ -21,6 +21,7 @@ import java.lang.annotation.Target;
@Target({METHOD}) @Target({METHOD})
public @interface JSONPropertyName { public @interface JSONPropertyName {
/** /**
* The value of the JSON property.
* @return The name of the property as to be used in the JSON Object. * @return The name of the property as to be used in the JSON Object.
*/ */
String value(); String value();

View File

@@ -525,6 +525,11 @@ public class JSONTokener {
this.line + "]"; 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 { public void close() throws IOException {
if(reader!=null){ if(reader!=null){
reader.close(); reader.close();

View File

@@ -29,11 +29,20 @@ public class ParserConfiguration {
*/ */
protected int maxNestingDepth; protected int maxNestingDepth;
/**
* Constructs a new ParserConfiguration with default settings.
*/
public ParserConfiguration() { public ParserConfiguration() {
this.keepStrings = false; this.keepStrings = false;
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; 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) { protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
this.keepStrings = keepStrings; this.keepStrings = keepStrings;
this.maxNestingDepth = maxNestingDepth; this.maxNestingDepth = maxNestingDepth;
@@ -71,9 +80,11 @@ public class ParserConfiguration {
* *
* @param newVal * @param newVal
* new value to use for the <code>keepStrings</code> configuration option. * new value to use for the <code>keepStrings</code> configuration option.
* * @param <T> the type of the configuration object
*
* @return The existing configuration will not be modified. A new configuration is returned. * @return The existing configuration will not be modified. A new configuration is returned.
*/ */
@SuppressWarnings("unchecked")
public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) { public <T extends ParserConfiguration> T withKeepStrings(final boolean newVal) {
T newConfig = (T)this.clone(); T newConfig = (T)this.clone();
newConfig.keepStrings = newVal; newConfig.keepStrings = newVal;
@@ -96,8 +107,11 @@ public class ParserConfiguration {
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth, * 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. * 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 maxNestingDepth the maximum nesting depth allowed to the XML parser
* @param <T> the type of the configuration object
*
* @return The existing configuration will not be modified. A new configuration is returned. * @return The existing configuration will not be modified. A new configuration is returned.
*/ */
@SuppressWarnings("unchecked")
public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) { public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth) {
T newConfig = (T)this.clone(); T newConfig = (T)this.clone();

View File

@@ -13,6 +13,13 @@ import java.util.Properties;
* @version 2015-05-05 * @version 2015-05-05
*/ */
public class Property { 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. * Converts a property file object into a JSONObject. The property file object is a table of name value pairs.
* @param properties java.util.Properties * @param properties java.util.Properties

View File

@@ -10,7 +10,6 @@ import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Iterator; import java.util.Iterator;
/** /**
* This provides static methods to convert an XML text into a JSONObject, and to * This provides static methods to convert an XML text into a JSONObject, and to
* covert a JSONObject into an XML text. * covert a JSONObject into an XML text.
@@ -21,6 +20,12 @@ import java.util.Iterator;
@SuppressWarnings("boxing") @SuppressWarnings("boxing")
public class XML { public class XML {
/**
* Constructs a new XML object.
*/
public XML() {
}
/** The Character '&amp;'. */ /** The Character '&amp;'. */
public static final Character AMP = '&'; public static final Character AMP = '&';
@@ -53,6 +58,9 @@ public class XML {
*/ */
public static final String NULL_ATTR = "xsi:nil"; 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"; public static final String TYPE_ATTR = "xsi:type";
/** /**
@@ -428,6 +436,9 @@ public class XML {
&& jsonObject.opt(config.getcDataTagName()) != null) { && jsonObject.opt(config.getcDataTagName()) != null) {
context.accumulate(tagName, jsonObject.opt(config.getcDataTagName())); context.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
} else { } else {
if (!config.shouldTrimWhiteSpace()) {
removeEmpty(jsonObject, config);
}
context.accumulate(tagName, jsonObject); context.accumulate(tagName, jsonObject);
} }
} }
@@ -442,58 +453,46 @@ public class XML {
} }
} }
} }
/** /**
* This method tries to convert the given string value to the target object * This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName
* @param string String to convert * and contains whitespace as this is caused by whitespace between tags. See test XMLTest.testNestedWithWhitespaceTrimmingDisabled.
* @param typeConverter value converter to convert string to integer, boolean e.t.c * @param jsonObject JSONObject which may require deletion
* @return JSON value of this string or the string * @param config The XMLParserConfiguration which includes the cDataTagName
*/ */
public static Object stringToValue(String string, XMLXsiTypeConverter<?> typeConverter) { private static void removeEmpty(final JSONObject jsonObject, final XMLParserConfiguration config) {
if(typeConverter != null) { if (jsonObject.has(config.getcDataTagName())) {
return typeConverter.convert(string); final Object s = jsonObject.get(config.getcDataTagName());
} if (s instanceof String) {
return stringToValue(string); if (isStringAllWhiteSpace(s.toString())) {
} jsonObject.remove(config.getcDataTagName());
}
/** }
* This method is the same as {@link JSONObject#stringToValue(String)}. else if (s instanceof JSONArray) {
* final JSONArray sArray = (JSONArray) s;
* @param string String to convert for (int k = sArray.length()-1; k >= 0; k--){
* @return JSON value of this string or the string final Object eachString = sArray.get(k);
*/ if (eachString instanceof String) {
// To maintain compatibility with the Android API, this method is a direct copy of String s1 = (String) eachString;
// the one in JSONObject. Changes made here should be reflected there. if (isStringAllWhiteSpace(s1)) {
// This method should not make calls out of the XML object. sArray.remove(k);
public static Object stringToValue(String string) { }
if ("".equals(string)) { }
return string; }
} if (sArray.isEmpty()) {
jsonObject.remove(config.getcDataTagName());
// check JSON key words true/false/null }
if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE;
}
if ("false".equalsIgnoreCase(string)) {
return Boolean.FALSE;
}
if ("null".equalsIgnoreCase(string)) {
return JSONObject.NULL;
}
/*
* If it might be a number, try converting it. If a number cannot be
* produced, then the value will just be a string.
*/
char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') {
try {
return stringToNumber(string);
} catch (Exception ignore) {
} }
} }
return string; }
private static boolean isStringAllWhiteSpace(final String s) {
for (int k = 0; k<s.length(); k++){
final char eachChar = s.charAt(k);
if (!Character.isWhitespace(eachChar)) {
return false;
}
}
return true;
} }
/** /**
@@ -567,6 +566,58 @@ public class XML {
|| val.indexOf('E') > -1 || "-0".equals(val); || val.indexOf('E') > -1 || "-0".equals(val);
} }
/**
* This method tries to convert the given string value to the target object
* @param string String to convert
* @param typeConverter value converter to convert string to integer, boolean e.t.c
* @return JSON value of this string or the string
*/
public static Object stringToValue(String string, XMLXsiTypeConverter<?> typeConverter) {
if(typeConverter != null) {
return typeConverter.convert(string);
}
return stringToValue(string);
}
/**
* This method is the same as {@link JSONObject#stringToValue(String)}.
*
* @param string String to convert
* @return JSON value of this string or the string
*/
// To maintain compatibility with the Android API, this method is a direct copy of
// the one in JSONObject. Changes made here should be reflected there.
// This method should not make calls out of the XML object.
public static Object stringToValue(String string) {
if ("".equals(string)) {
return string;
}
// check JSON key words true/false/null
if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE;
}
if ("false".equalsIgnoreCase(string)) {
return Boolean.FALSE;
}
if ("null".equalsIgnoreCase(string)) {
return JSONObject.NULL;
}
/*
* If it might be a number, try converting it. If a number cannot be
* produced, then the value will just be a string.
*/
char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') {
try {
return stringToNumber(string);
} catch (Exception ignore) {
}
}
return string;
}
/** /**
* Convert a well-formed (but not necessarily valid) XML string into a * Convert a well-formed (but not necessarily valid) XML string into a
@@ -659,7 +710,7 @@ public class XML {
*/ */
public static JSONObject toJSONObject(Reader reader, XMLParserConfiguration config) throws JSONException { public static JSONObject toJSONObject(Reader reader, XMLParserConfiguration config) throws JSONException {
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject();
XMLTokener x = new XMLTokener(reader); XMLTokener x = new XMLTokener(reader, config);
while (x.more()) { while (x.more()) {
x.skipPast("<"); x.skipPast("<");
if(x.more()) { if(x.more()) {
@@ -850,12 +901,25 @@ public class XML {
} }
} }
} else if ("".equals(value)) { } else if ("".equals(value)) {
sb.append(indent(indent)); if (config.isCloseEmptyTag()){
sb.append('<'); sb.append(indent(indent));
sb.append(key); sb.append('<');
sb.append("/>"); sb.append(key);
if(indentFactor > 0){ sb.append(">");
sb.append("\n"); sb.append("</");
sb.append(key);
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 <k> // Emit a new tag <k>
@@ -899,14 +963,14 @@ public class XML {
string = (object == null) ? "null" : escape(object.toString()); string = (object == null) ? "null" : escape(object.toString());
String indentationSuffix = (indentFactor > 0) ? "\n" : "";
if(tagName == null){ if(tagName == null){
return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : ""); return indent(indent) + "\"" + string + "\"" + indentationSuffix;
} else if(string.length() == 0){ } else if(string.length() == 0){
return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : ""); return indent(indent) + "<" + tagName + "/>" + indentationSuffix;
} else { } else {
return indent(indent) + "<" + tagName return indent(indent) + "<" + tagName
+ ">" + string + "</" + tagName + ">" + ((indentFactor > 0) ? "\n" : ""); + ">" + string + "</" + tagName + ">" + indentationSuffix;
} }
} }

View File

@@ -43,6 +43,13 @@ public class XMLParserConfiguration extends ParserConfiguration {
*/ */
private boolean convertNilAttributeToNull; 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 * This will allow type conversion for values in XML if xsi:type attribute is defined
*/ */
@@ -54,9 +61,18 @@ public class XMLParserConfiguration extends ParserConfiguration {
*/ */
private Set<String> forceList; private Set<String> 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;
/** /**
* Default parser configuration. Does not keep strings (tries to implicitly convert * 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 () { public XMLParserConfiguration () {
super(); super();
@@ -64,6 +80,7 @@ public class XMLParserConfiguration extends ParserConfiguration {
this.convertNilAttributeToNull = false; this.convertNilAttributeToNull = false;
this.xsiTypeMap = Collections.emptyMap(); this.xsiTypeMap = Collections.emptyMap();
this.forceList = Collections.emptySet(); this.forceList = Collections.emptySet();
this.shouldTrimWhiteSpace = true;
} }
/** /**
@@ -142,15 +159,17 @@ public class XMLParserConfiguration extends ParserConfiguration {
* xsi:type="integer" as integer, xsi:type="string" as string * xsi:type="integer" as integer, xsi:type="string" as string
* @param forceList <code>new HashSet<String>()</code> to parse the provided tags' values as arrays * @param forceList <code>new HashSet<String>()</code> to parse the provided tags' values as arrays
* @param maxNestingDepth <code>int</code> to limit the nesting depth * @param maxNestingDepth <code>int</code> to limit the nesting depth
* @param closeEmptyTag <code>boolean</code> to turn on explicit end tag for tag with empty value
*/ */
private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList, final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList,
final int maxNestingDepth) { final int maxNestingDepth, final boolean closeEmptyTag) {
super(keepStrings, maxNestingDepth); super(keepStrings, maxNestingDepth);
this.cDataTagName = cDataTagName; this.cDataTagName = cDataTagName;
this.convertNilAttributeToNull = convertNilAttributeToNull; this.convertNilAttributeToNull = convertNilAttributeToNull;
this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap); this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
this.forceList = Collections.unmodifiableSet(forceList); this.forceList = Collections.unmodifiableSet(forceList);
this.closeEmptyTag = closeEmptyTag;
} }
/** /**
@@ -163,14 +182,17 @@ public class XMLParserConfiguration extends ParserConfiguration {
// item, a new map instance should be created and if possible each value in the // 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 // 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. // be immutable, then a shallow clone of the map is acceptable.
return new XMLParserConfiguration( final XMLParserConfiguration config = new XMLParserConfiguration(
this.keepStrings, this.keepStrings,
this.cDataTagName, this.cDataTagName,
this.convertNilAttributeToNull, this.convertNilAttributeToNull,
this.xsiTypeMap, this.xsiTypeMap,
this.forceList, this.forceList,
this.maxNestingDepth this.maxNestingDepth,
this.closeEmptyTag
); );
config.shouldTrimWhiteSpace = this.shouldTrimWhiteSpace;
return config;
} }
/** /**
@@ -182,6 +204,7 @@ public class XMLParserConfiguration extends ParserConfiguration {
* *
* @return The existing configuration will not be modified. A new configuration is returned. * @return The existing configuration will not be modified. A new configuration is returned.
*/ */
@SuppressWarnings("unchecked")
@Override @Override
public XMLParserConfiguration withKeepStrings(final boolean newVal) { public XMLParserConfiguration withKeepStrings(final boolean newVal) {
return super.withKeepStrings(newVal); return super.withKeepStrings(newVal);
@@ -299,8 +322,51 @@ public class XMLParserConfiguration extends ParserConfiguration {
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser * @param maxNestingDepth the maximum nesting depth allowed to the XML parser
* @return The existing configuration will not be modified. A new configuration is returned. * @return The existing configuration will not be modified. A new configuration is returned.
*/ */
@SuppressWarnings("unchecked")
@Override @Override
public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
return super.withMaxNestingDepth(maxNestingDepth); return super.withMaxNestingDepth(maxNestingDepth);
} }
/**
* To enable explicit end tag with empty value.
* @param closeEmptyTag new value for the closeEmptyTag property
* @return same instance of configuration with empty tag config updated
*/
public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){
XMLParserConfiguration clonedConfiguration = this.clone();
clonedConfiguration.closeEmptyTag = 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;
}
/**
* 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;
}
} }

View File

@@ -20,6 +20,8 @@ public class XMLTokener extends JSONTokener {
*/ */
public static final java.util.HashMap<String, Character> entity; public static final java.util.HashMap<String, Character> entity;
private XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;
static { static {
entity = new java.util.HashMap<String, Character>(8); entity = new java.util.HashMap<String, Character>(8);
entity.put("amp", XML.AMP); entity.put("amp", XML.AMP);
@@ -45,6 +47,16 @@ public class XMLTokener extends JSONTokener {
super(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;
}
/** /**
* Get the text in the CDATA block. * Get the text in the CDATA block.
* @return The string up to the <code>]]&gt;</code>. * @return The string up to the <code>]]&gt;</code>.
@@ -83,7 +95,7 @@ public class XMLTokener extends JSONTokener {
StringBuilder sb; StringBuilder sb;
do { do {
c = next(); c = next();
} while (Character.isWhitespace(c)); } while (Character.isWhitespace(c) && configuration.shouldTrimWhiteSpace());
if (c == 0) { if (c == 0) {
return null; return null;
} }
@@ -97,7 +109,9 @@ public class XMLTokener extends JSONTokener {
} }
if (c == '<') { if (c == '<') {
back(); back();
return sb.toString().trim(); if (configuration.shouldTrimWhiteSpace()) {
return sb.toString().trim();
} else return sb.toString();
} }
if (c == '&') { if (c == '&') {
sb.append(nextEntity(c)); sb.append(nextEntity(c));

View File

@@ -42,5 +42,12 @@ Public Domain.
* @param <T> return type of convert method * @param <T> return type of convert method
*/ */
public interface XMLXsiTypeConverter<T> { public interface XMLXsiTypeConverter<T> {
/**
* 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); T convert(String value);
} }

View File

@@ -24,14 +24,13 @@ public class CDLTest {
* String of lines where the column names are in the first row, * 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. * and all subsequent rows are values. All keys and values should be legal.
*/ */
String lines = new String( private static final String LINES = "Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" +
"Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" + "val1, val2, val3, val4, val5, val6, val7\n" +
"val1, val2, val3, val4, val5, val6, val7\n" + "1, 2, 3, 4\t, 5, 6, 7\n" +
"1, 2, 3, 4\t, 5, 6, 7\n" + "true, false, true, true, false, false, false\n" +
"true, false, true, true, false, false, false\n" + "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\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";
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va\'l6, val7\n"
);
/** /**
* CDL.toJSONArray() adds all values as strings, with no filtering or * 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 * values all must be quoted in the cases where the JSONObject parsing
* might normally convert the value into a non-string. * might normally convert the value into a non-string.
*/ */
String expectedLines = new String( 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: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:\"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:\"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:\"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}]";
"{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. * Attempts to create a JSONArray from a null string.
@@ -194,8 +192,7 @@ public class CDLTest {
public void emptyString() { public void emptyString() {
String emptyStr = ""; String emptyStr = "";
JSONArray jsonArray = CDL.toJSONArray(emptyStr); JSONArray jsonArray = CDL.toJSONArray(emptyStr);
assertTrue("CDL should return null when the input string is empty", assertNull("CDL should return null when the input string is empty", jsonArray);
jsonArray == null);
} }
/** /**
@@ -254,7 +251,7 @@ public class CDLTest {
jsonObject.put("Col \r1", "V1"); jsonObject.put("Col \r1", "V1");
// \r will be filtered from value // \r will be filtered from value
jsonObject.put("Col 2", "V2\r"); 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); String cdlStr = CDL.toString(jsonArray);
jsonObject = jsonArray.getJSONObject(0); jsonObject = jsonArray.getJSONObject(0);
assertTrue(cdlStr.contains("\"Col 1\"")); assertTrue(cdlStr.contains("\"Col 1\""));
@@ -268,8 +265,15 @@ public class CDLTest {
*/ */
@Test @Test
public void textToJSONArray() { public void textToJSONArray() {
JSONArray jsonArray = CDL.toJSONArray(this.lines); JSONArray jsonArray = CDL.toJSONArray(LINES);
JSONArray expectedJsonArray = new JSONArray(this.expectedLines); 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); Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
} }
@@ -293,10 +297,24 @@ public class CDLTest {
*/ */
@Test @Test
public void textToJSONArrayAndBackToString() { public void textToJSONArrayAndBackToString() {
JSONArray jsonArray = CDL.toJSONArray(this.lines); JSONArray jsonArray = CDL.toJSONArray(LINES);
String jsonStr = CDL.toString(jsonArray); String jsonStr = CDL.toString(jsonArray);
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr); 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); Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
} }

View File

@@ -28,10 +28,13 @@ import java.util.Map;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONParserConfiguration;
import org.json.JSONPointerException; import org.json.JSONPointerException;
import org.json.JSONString; import org.json.JSONString;
import org.json.JSONTokener; import org.json.JSONTokener;
import org.json.ParserConfiguration;
import org.json.junit.data.MyJsonString; import org.json.junit.data.MyJsonString;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
@@ -1384,6 +1387,7 @@ public class JSONArrayTest {
/** /**
* Tests for stack overflow. See https://github.com/stleary/JSON-java/issues/654 * 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) @Test(expected = JSONException.class)
public void issue654StackOverflowInputWellFormed() { public void issue654StackOverflowInputWellFormed() {
//String input = new String(java.util.Base64.getDecoder().decode(base64Bytes)); //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes));
@@ -1391,7 +1395,7 @@ public class JSONArrayTest {
JSONTokener tokener = new JSONTokener(resourceAsStream); JSONTokener tokener = new JSONTokener(resourceAsStream);
JSONArray json_input = new JSONArray(tokener); JSONArray json_input = new JSONArray(tokener);
assertNotNull(json_input); assertNotNull(json_input);
fail("Excepected Exception."); fail("Excepected Exception due to stack overflow.");
Util.checkJSONArrayMaps(json_input); Util.checkJSONArrayMaps(json_input);
} }
@@ -1415,4 +1419,92 @@ public class JSONArrayTest {
.put(2); .put(2);
assertFalse(ja1.similar(ja3)); assertFalse(ja1.similar(ja3));
} }
@Test(expected = JSONException.class)
public void testRecursiveDepth() {
HashMap<String, Object> map = new HashMap<>();
map.put("t", map);
new JSONArray().put(map);
}
@Test(expected = JSONException.class)
public void testRecursiveDepthAtPosition() {
HashMap<String, Object> map = new HashMap<>();
map.put("t", map);
new JSONArray().put(0, map);
}
@Test(expected = JSONException.class)
public void testRecursiveDepthArray() {
ArrayList<Object> array = new ArrayList<>();
array.add(array);
new JSONArray(array);
}
@Test
public void testRecursiveDepthAtPositionDefaultObject() {
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
new JSONArray().put(0, map);
}
@Test
public void testRecursiveDepthAtPosition1000Object() {
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(1000);
new JSONArray().put(0, map, new JSONParserConfiguration().withMaxNestingDepth(1000));
}
@Test(expected = JSONException.class)
public void testRecursiveDepthAtPosition1001Object() {
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(1001);
new JSONArray().put(0, map);
}
@Test(expected = JSONException.class)
public void testRecursiveDepthArrayLimitedMaps() {
ArrayList<Object> array = new ArrayList<>();
array.add(array);
new JSONArray(array);
}
@Test
public void testRecursiveDepthArrayForDefaultLevels() {
ArrayList<Object> array = buildNestedArray(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
new JSONArray(array, new JSONParserConfiguration());
}
@Test
public void testRecursiveDepthArrayFor1000Levels() {
try {
ArrayList<Object> 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)
public void testRecursiveDepthArrayFor1001Levels() {
ArrayList<Object> array = buildNestedArray(1001);
new JSONArray(array);
}
public static ArrayList<Object> buildNestedArray(int maxDepth) {
if (maxDepth <= 0) {
return new ArrayList<>();
}
ArrayList<Object> nestedArray = new ArrayList<>();
nestedArray.add(buildNestedArray(maxDepth - 1));
return nestedArray;
}
} }

View File

@@ -762,8 +762,8 @@ public class JSONMLTest {
final String xml = JSONML.toString(originalObject); final String xml = JSONML.toString(originalObject);
final JSONObject revertedObject = JSONML.toJSONObject(xml, false); final JSONObject revertedObject = JSONML.toJSONObject(xml, false);
final String newJson = revertedObject.toString(); final String newJson = revertedObject.toString();
assertTrue("JSON Objects are not similar",originalObject.similar(revertedObject)); assertTrue("JSON Objects are not similar", originalObject.similar(revertedObject));
assertEquals("original JSON does not equal the new JSON",originalJson, newJson); assertTrue("JSON Strings are not similar", new JSONObject(originalJson).similar(new JSONObject(newJson)));
} }
// these tests do not pass for the following reasons: // these tests do not pass for the following reasons:

View File

@@ -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")));
}
}

View File

@@ -23,10 +23,7 @@ public class JSONObjectNumberTest {
@Parameters(name = "{index}: {0}") @Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{ return Arrays.asList(new Object[][]{
{"{value:0050}", 1}, {"{value:50}", 1},
{"{value:0050.0000}", 1},
{"{value:-0050}", -1},
{"{value:-0050.0000}", -1},
{"{value:50.0}", 1}, {"{value:50.0}", 1},
{"{value:5e1}", 1}, {"{value:5e1}", 1},
{"{value:5E1}", 1}, {"{value:5E1}", 1},
@@ -35,7 +32,6 @@ public class JSONObjectNumberTest {
{"{value:-50}", -1}, {"{value:-50}", -1},
{"{value:-50.0}", -1}, {"{value:-50.0}", -1},
{"{value:-5e1}", -1}, {"{value:-5e1}", -1},
{"{value:-0005e1}", -1},
{"{value:-5E1}", -1}, {"{value:-5E1}", -1},
{"{value:-5e1}", -1}, {"{value:-5e1}", -1},
{"{value:'-50'}", -1} {"{value:'-50'}", -1}

View File

@@ -4,7 +4,6 @@ package org.json.junit;
Public Domain. Public Domain.
*/ */
import static java.lang.Double.NaN;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
@@ -32,8 +31,10 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONPointerException; import org.json.JSONPointerException;
import org.json.JSONParserConfiguration;
import org.json.JSONString; import org.json.JSONString;
import org.json.JSONTokener; import org.json.JSONTokener;
import org.json.ParserConfiguration;
import org.json.XML; import org.json.XML;
import org.json.junit.data.BrokenToString; import org.json.junit.data.BrokenToString;
import org.json.junit.data.ExceptionalBean; import org.json.junit.data.ExceptionalBean;
@@ -55,6 +56,7 @@ import org.json.junit.data.RecursiveBeanEquals;
import org.json.junit.data.Singleton; import org.json.junit.data.Singleton;
import org.json.junit.data.SingletonEnum; import org.json.junit.data.SingletonEnum;
import org.json.junit.data.WeirdList; import org.json.junit.data.WeirdList;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
@@ -783,7 +785,7 @@ public class JSONObjectTest {
jsonObject.accumulate("myArray", -23.45e7); jsonObject.accumulate("myArray", -23.45e7);
// include an unsupported object for coverage // include an unsupported object for coverage
try { try {
jsonObject.accumulate("myArray", NaN); jsonObject.accumulate("myArray", Double.NaN);
fail("Expected exception"); fail("Expected exception");
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
@@ -815,7 +817,7 @@ public class JSONObjectTest {
jsonObject.append("myArray", -23.45e7); jsonObject.append("myArray", -23.45e7);
// include an unsupported object for coverage // include an unsupported object for coverage
try { try {
jsonObject.append("myArray", NaN); jsonObject.append("myArray", Double.NaN);
fail("Expected exception"); fail("Expected exception");
} catch (JSONException ignored) {} } catch (JSONException ignored) {}
@@ -840,7 +842,7 @@ public class JSONObjectTest {
public void jsonObjectDoubleToString() { public void jsonObjectDoubleToString() {
String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" }; String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" };
Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67, 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) { for (int i = 0; i < expectedStrs.length; ++i) {
String actualStr = JSONObject.doubleToString(doubles[i]); String actualStr = JSONObject.doubleToString(doubles[i]);
assertTrue("value expected ["+expectedStrs[i]+ assertTrue("value expected ["+expectedStrs[i]+
@@ -895,11 +897,11 @@ public class JSONObjectTest {
assertTrue("opt doubleKey should be double", assertTrue("opt doubleKey should be double",
jsonObject.optDouble("doubleKey") == -23.45e7); jsonObject.optDouble("doubleKey") == -23.45e7);
assertTrue("opt doubleKey with Default should be double", 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", assertTrue("opt doubleKey should be Double",
Double.valueOf(-23.45e7).equals(jsonObject.optDoubleObject("doubleKey"))); Double.valueOf(-23.45e7).equals(jsonObject.optDoubleObject("doubleKey")));
assertTrue("opt doubleKey with Default should be Double", 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", assertTrue("opt negZeroKey should be a Double",
jsonObject.opt("negZeroKey") instanceof Double); jsonObject.opt("negZeroKey") instanceof Double);
assertTrue("get negZeroKey should be a Double", assertTrue("get negZeroKey should be a Double",
@@ -1065,21 +1067,12 @@ public class JSONObjectTest {
"\"tooManyZeros\":00,"+ "\"tooManyZeros\":00,"+
"\"negativeInfinite\":-Infinity,"+ "\"negativeInfinite\":-Infinity,"+
"\"negativeNaN\":-NaN,"+ "\"negativeNaN\":-NaN,"+
"\"negativeNaNWithLeadingZeros\":-00NaN,"+
"\"negativeFraction\":-.01,"+ "\"negativeFraction\":-.01,"+
"\"tooManyZerosFraction\":00.001,"+ "\"tooManyZerosFraction\":00.001,"+
"\"negativeHexFloat\":-0x1.fffp1,"+ "\"negativeHexFloat\":-0x1.fffp1,"+
"\"hexFloat\":0x1.0P-1074,"+ "\"hexFloat\":0x1.0P-1074,"+
"\"floatIdentifier\":0.1f,"+ "\"floatIdentifier\":0.1f,"+
"\"doubleIdentifier\":0.1d,"+ "\"doubleIdentifier\":0.1d"+
"\"doubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":0000000.1d,"+
"\"negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":-0000000.1d,"+
"\"doubleIdentifierWithMultipleLeadingZerosAfterDecimal\":0000000.0001d,"+
"\"negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal\":-0000000.0001d,"+
"\"integerWithLeadingZeros\":000900,"+
"\"integerWithAllZeros\":00000,"+
"\"compositeWithLeadingZeros\":00800.90d,"+
"\"decimalPositiveWithoutNumberBeforeDecimalPoint\":.90,"+
"}"; "}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
Object obj; Object obj;
@@ -1089,22 +1082,15 @@ public class JSONObjectTest {
assertTrue("hexNumber currently evaluates to string", assertTrue("hexNumber currently evaluates to string",
obj.equals("-0x123")); obj.equals("-0x123"));
assertTrue( "tooManyZeros currently evaluates to string", assertTrue( "tooManyZeros currently evaluates to string",
jsonObject.get( "tooManyZeros" ).equals(0)); jsonObject.get( "tooManyZeros" ).equals("00"));
obj = jsonObject.get("negativeInfinite"); obj = jsonObject.get("negativeInfinite");
assertTrue( "negativeInfinite currently evaluates to string", assertTrue( "negativeInfinite currently evaluates to string",
obj.equals("-Infinity")); obj.equals("-Infinity"));
obj = jsonObject.get("negativeNaN"); obj = jsonObject.get("negativeNaN");
assertTrue( "negativeNaN currently evaluates to string", assertTrue( "negativeNaN currently evaluates to string",
obj.equals("-NaN")); obj.equals("-NaN"));
obj = jsonObject.get("negativeNaNWithLeadingZeros");
assertTrue( "negativeNaNWithLeadingZeros currently evaluates to string",
obj.equals("-00NaN"));
assertTrue( "negativeFraction currently evaluates to double -0.01", assertTrue( "negativeFraction currently evaluates to double -0.01",
jsonObject.get( "negativeFraction" ).equals(BigDecimal.valueOf(-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", assertTrue( "tooManyZerosFraction currently evaluates to double 0.001",
jsonObject.optLong( "tooManyZerosFraction" )==0); jsonObject.optLong( "tooManyZerosFraction" )==0);
assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875",
@@ -1115,53 +1101,6 @@ public class JSONObjectTest {
jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1))); jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1)));
assertTrue("doubleIdentifier currently evaluates to double 0.1", assertTrue("doubleIdentifier currently evaluates to double 0.1",
jsonObject.get("doubleIdentifier").equals(Double.valueOf(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); Util.checkJSONObjectMaps(jsonObject);
} }
@@ -2095,7 +2034,9 @@ public class JSONObjectTest {
"}"; "}";
JSONObject jsonObject = new JSONObject(jsonObjectStr); JSONObject jsonObject = new JSONObject(jsonObjectStr);
assertTrue("jsonObject valueToString() incorrect", assertTrue("jsonObject valueToString() incorrect",
JSONObject.valueToString(jsonObject).equals(jsonObject.toString())); new JSONObject(JSONObject.valueToString(jsonObject))
.similar(new JSONObject(jsonObject.toString()))
);
String jsonArrayStr = String jsonArrayStr =
"[1,2,3]"; "[1,2,3]";
JSONArray jsonArray = new JSONArray(jsonArrayStr); JSONArray jsonArray = new JSONArray(jsonArrayStr);
@@ -2106,7 +2047,8 @@ public class JSONObjectTest {
map.put("key2", "val2"); map.put("key2", "val2");
map.put("key3", "val3"); map.put("key3", "val3");
assertTrue("map valueToString() incorrect", assertTrue("map valueToString() incorrect",
jsonObject.toString().equals(JSONObject.valueToString(map))); new JSONObject(jsonObject.toString())
.similar(new JSONObject(JSONObject.valueToString(map))));
Collection<Integer> collection = new ArrayList<Integer>(); Collection<Integer> collection = new ArrayList<Integer>();
collection.add(Integer.valueOf(1)); collection.add(Integer.valueOf(1));
collection.add(Integer.valueOf(2)); collection.add(Integer.valueOf(2));
@@ -2392,7 +2334,7 @@ public class JSONObjectTest {
} }
try { try {
// test validity of invalid double // test validity of invalid double
JSONObject.testValidity(NaN); JSONObject.testValidity(Double.NaN);
fail("Expected an exception"); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("", true); assertTrue("", true);
@@ -3662,6 +3604,7 @@ public class JSONObjectTest {
/** /**
* Tests for stack overflow. See https://github.com/stleary/JSON-java/issues/654 * 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) @Test(expected = JSONException.class)
public void issue654StackOverflowInputWellFormed() { public void issue654StackOverflowInputWellFormed() {
//String input = new String(java.util.Base64.getDecoder().decode(base64Bytes)); //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes));
@@ -3669,7 +3612,7 @@ public class JSONObjectTest {
JSONTokener tokener = new JSONTokener(resourceAsStream); JSONTokener tokener = new JSONTokener(resourceAsStream);
JSONObject json_input = new JSONObject(tokener); JSONObject json_input = new JSONObject(tokener);
assertNotNull(json_input); assertNotNull(json_input);
fail("Excepected Exception."); fail("Excepected Exception due to stack overflow.");
} }
@Test @Test
@@ -3713,4 +3656,138 @@ public class JSONObjectTest {
assertThrows(JSONException.class, () -> new JSONObject(bean)); assertThrows(JSONException.class, () -> new JSONObject(bean));
} }
} }
@Test(expected = JSONException.class)
public void issue743SerializationMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("t", map);
JSONObject object = new JSONObject(map);
String jsonString = object.toString();
}
@Test(expected = JSONException.class)
public void testCircularReferenceMultipleLevel() {
HashMap<String, Object> inside = new HashMap<>();
HashMap<String, Object> jsonObject = new HashMap<>();
inside.put("inside", jsonObject);
jsonObject.put("test", inside);
new JSONObject(jsonObject);
}
@Test
public void issue743SerializationMapWith512Objects() {
HashMap<String, Object> map = buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
JSONObject object = new JSONObject(map);
String jsonString = object.toString();
}
@Test
public void issue743SerializationMapWith1000Objects() {
HashMap<String, Object> map = buildNestedMap(1000);
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000);
JSONObject object = new JSONObject(map, parserConfiguration);
String jsonString = object.toString();
}
@Test(expected = JSONException.class)
public void issue743SerializationMapWith1001Objects() {
HashMap<String, Object> map = buildNestedMap(1001);
JSONObject object = new JSONObject(map);
String jsonString = object.toString();
}
@Test(expected = JSONException.class)
public void testCircleReferenceFirstLevel() {
Map<Object, Object> jsonObject = new HashMap<>();
jsonObject.put("test", jsonObject);
new JSONObject(jsonObject, new JSONParserConfiguration());
}
@Test(expected = StackOverflowError.class)
public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() {
Map<Object, Object> inside = new HashMap<>();
Map<Object, Object> 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<Object, Object> inside = new HashMap<>();
Map<Object, Object> jsonObject = new HashMap<>();
inside.put("test", jsonObject);
jsonObject.put("test", inside);
new JSONObject(jsonObject, new JSONParserConfiguration());
}
@Test
public void testDifferentKeySameInstanceNotACircleReference() {
Map<Object, Object> map1 = new HashMap<>();
Map<Object, Object> map2 = new HashMap<>();
map1.put("test1", map2);
map1.put("test2", map2);
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
*
* @param maxDepth
* @return
*/
public static HashMap<String, Object> buildNestedMap(int maxDepth) {
if (maxDepth <= 0) {
return new HashMap<>();
}
HashMap<String, Object> nestedMap = new HashMap<>();
nestedMap.put("t", buildNestedMap(maxDepth - 1));
return nestedMap;
}
} }

View File

@@ -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")));
}
}

View File

@@ -4,11 +4,6 @@ package org.json.junit;
Public Domain. 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.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
@@ -27,6 +22,8 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.*;
/** /**
* Tests for JSON-Java XML.java with XMLParserConfiguration.java * Tests for JSON-Java XML.java with XMLParserConfiguration.java
@@ -557,6 +554,37 @@ public class XMLConfigurationTest {
assertEquals(actualXML, resultXML); assertEquals(actualXML, resultXML);
} }
@Test
public void shouldHandleEmptyNodeValue()
{
JSONObject inputJSON = new JSONObject();
inputJSON.put("Emptyness", "");
String expectedXmlWithoutExplicitEndTag = "<Emptyness/>";
String expectedXmlWithExplicitEndTag = "<Emptyness></Emptyness>";
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());
}
/** /**
* Investigate exactly how the "content" keyword works * Investigate exactly how the "content" keyword works
*/ */
@@ -1153,4 +1181,4 @@ public class XMLConfigurationTest {
assertTrue("Error: " +e.getMessage(), false); assertTrue("Error: " +e.getMessage(), false);
} }
} }
} }

View File

@@ -1177,6 +1177,30 @@ public class XMLTest {
} }
@Test
public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
JSONObject jsonObject = new JSONObject(jsonString);
String expectedXmlString = "<encloser><outer><innerOne></innerOne><innerTwo>two</innerTwo></outer></encloser>";
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true));
JSONObject actualJsonObject = XML.toJSONObject(xmlForm);
JSONObject expectedJsonObject = XML.toJSONObject(expectedXmlString);
assertTrue(expectedJsonObject.similar(actualJsonObject));
}
@Test
public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
JSONObject jsonObject = new JSONObject(jsonString);
String expectedXmlString = "<encloser><outer><innerOne/><innerTwo>two</innerTwo></outer></encloser>";
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false));
JSONObject actualJsonObject = XML.toJSONObject(xmlForm);
JSONObject expectedJsonObject = XML.toJSONObject(expectedXmlString);
assertTrue(expectedJsonObject.similar(actualJsonObject));
}
@Test @Test
public void testIndentSimpleJsonObject(){ public void testIndentSimpleJsonObject(){
String str = "{ \"employee\": { \n" + String str = "{ \"employee\": { \n" +
@@ -1234,7 +1258,7 @@ public class XMLTest {
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) { for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
expected.append(buffer, 0, numRead); expected.append(buffer, 0, numRead);
} }
assertEquals(expected.toString(), actualString); assertTrue(XML.toJSONObject(expected.toString()).similar(XML.toJSONObject(actualString)));
} }
} catch (IOException e) { } catch (IOException e) {
fail("file writer error: " +e.getMessage()); fail("file writer error: " +e.getMessage());
@@ -1299,6 +1323,109 @@ public class XMLTest {
"parameter of the XMLParserConfiguration used"); "parameter of the XMLParserConfiguration used");
} }
} }
@Test
public void testWithWhitespaceTrimmingDisabled() {
String originalXml = "<testXml> Test Whitespace String \t </testXml>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses>\n"+
" <address>\n"+
" <name> Sherlock Holmes </name>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses>\n"+
" <address>\n"+
" <content> Sherlock Holmes </content>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses>\n"+
" <address>\n"+
" <content> Sherlock Holmes </content>\n"+
" </address>\n"+
"</addresses>";
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 = "<testXml> Test Whitespace String \t </testXml>";
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 = "<testXml> Test Whitespace String \t </testXml>";
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration());
String expectedJsonString = "{\"testXml\":\"Test Whitespace String\"}";
JSONObject expectedJson = new JSONObject(expectedJsonString);
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
}
@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 =
" <datatypes>\n" +
" <telephone>0123456789</telephone>\n" +
" <amount>0.1230</amount>\n" +
" <boolean>true</boolean>\n" +
" </datatypes>";
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 str2 = "<color> <color_type>primary</color_type> <value>008E97</value> </color>";
JSONObject jsonObject2 = XML.toJSONObject(str2);
assertEquals(jsonObject2.getJSONObject("color").getLong("value"), 0e897, .1);
// Workaround for now is to use keepStrings
JSONObject jsonObject3 = XML.toJSONObject(str2, new XMLParserConfiguration().withKeepStrings(true));
assertEquals(jsonObject3.getJSONObject("color").getString("value"), "008E97");
}
} }

View File

@@ -12,7 +12,7 @@ import java.util.List;
*/ */
public class WeirdList { public class WeirdList {
/** */ /** */
private final List<Integer> list = new ArrayList(); private final List<Integer> list = new ArrayList<>();
/** /**
* @param vals * @param vals
@@ -25,14 +25,14 @@ public class WeirdList {
* @return a copy of the list * @return a copy of the list
*/ */
public List<Integer> get() { public List<Integer> get() {
return new ArrayList(this.list); return new ArrayList<>(this.list);
} }
/** /**
* @return a copy of the list * @return a copy of the list
*/ */
public List<Integer> getALL() { public List<Integer> getALL() {
return new ArrayList(this.list); return new ArrayList<>(this.list);
} }
/** /**