Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67448d00f0 | ||
|
|
2cd0a8c211 | ||
|
|
bc90ee497b | ||
|
|
88b6be9f84 | ||
|
|
fea3a2ce32 | ||
|
|
9926542da1 | ||
|
|
293bbf65d3 | ||
|
|
69383f746d | ||
|
|
e5e97f87b1 | ||
|
|
4713079d13 | ||
|
|
406abf8b76 | ||
|
|
5e9b4f4e27 | ||
|
|
564acf4eb1 | ||
|
|
6527427aad | ||
|
|
23bc68f227 | ||
|
|
babc520380 | ||
|
|
3d2f7e07ec | ||
|
|
f3c83d8349 | ||
|
|
d38bff8957 | ||
|
|
5dfa60ba0a | ||
|
|
18e8ef6e61 | ||
|
|
f875ab2fc7 | ||
|
|
b55c69e650 | ||
|
|
4f822e0292 | ||
|
|
775c98ea41 | ||
|
|
9f3b527d9f | ||
|
|
0ae8d5ab4f | ||
|
|
8746735758 | ||
|
|
a25f83a0e4 | ||
|
|
359b43d8df | ||
|
|
bcbbc89568 | ||
|
|
4f859fdf3b | ||
|
|
3dd0ec02f2 | ||
|
|
6c14048079 | ||
|
|
34efda6423 | ||
|
|
22ab2fb724 | ||
|
|
392a352901 | ||
|
|
b594a3229c | ||
|
|
6b993e2e47 | ||
|
|
e2cfb5a641 | ||
|
|
94854a1dfa |
@@ -1,43 +0,0 @@
|
|||||||
name: "CodeQL"
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master ]
|
|
||||||
schedule:
|
|
||||||
- cron: '18 18 * * 1'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: Analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'java' ]
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: Initialize CodeQL
|
|
||||||
uses: github/codeql-action/init@v3
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
- run: "mvn clean compile -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true"
|
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
|
||||||
uses: github/codeql-action/analyze@v3
|
|
||||||
@@ -1,277 +0,0 @@
|
|||||||
# This workflow will build a Java project 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
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
# branches: [ master ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ master ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
# 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@v5
|
|
||||||
- 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@v5
|
|
||||||
with:
|
|
||||||
name: Create java 1.6 JAR
|
|
||||||
path: target/*.jar
|
|
||||||
|
|
||||||
build-8:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
max-parallel: 1
|
|
||||||
matrix:
|
|
||||||
# build against supported Java LTS versions:
|
|
||||||
java: [ 8 ]
|
|
||||||
name: Java ${{ matrix.java }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v5
|
|
||||||
- name: Set up JDK ${{ matrix.java }}
|
|
||||||
uses: actions/setup-java@v5
|
|
||||||
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@v5
|
|
||||||
with:
|
|
||||||
name: Test Results ${{ matrix.java }}
|
|
||||||
path: target/surefire-reports/
|
|
||||||
- name: Upload Test Report ${{ matrix.java }}
|
|
||||||
if: ${{ always() }}
|
|
||||||
uses: actions/upload-artifact@v5
|
|
||||||
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@v5
|
|
||||||
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@v5
|
|
||||||
- name: Set up JDK ${{ matrix.java }}
|
|
||||||
uses: actions/setup-java@v5
|
|
||||||
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@v5
|
|
||||||
with:
|
|
||||||
name: Test Results ${{ matrix.java }}
|
|
||||||
path: target/surefire-reports/
|
|
||||||
- name: Upload Test Report ${{ matrix.java }}
|
|
||||||
if: ${{ always() }}
|
|
||||||
uses: actions/upload-artifact@v5
|
|
||||||
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@v5
|
|
||||||
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@v5
|
|
||||||
- name: Set up JDK ${{ matrix.java }}
|
|
||||||
uses: actions/setup-java@v5
|
|
||||||
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@v5
|
|
||||||
with:
|
|
||||||
name: Test Results ${{ matrix.java }}
|
|
||||||
path: target/surefire-reports/
|
|
||||||
- name: Upload Test Report ${{ matrix.java }}
|
|
||||||
if: ${{ always() }}
|
|
||||||
uses: actions/upload-artifact@v5
|
|
||||||
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@v5
|
|
||||||
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@v5
|
|
||||||
- name: Set up JDK ${{ matrix.java }}
|
|
||||||
uses: actions/setup-java@v5
|
|
||||||
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@v5
|
|
||||||
with:
|
|
||||||
name: Test Results ${{ matrix.java }}
|
|
||||||
path: target/surefire-reports/
|
|
||||||
- name: Upload Test Report ${{ matrix.java }}
|
|
||||||
if: ${{ always() }}
|
|
||||||
uses: actions/upload-artifact@v5
|
|
||||||
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@v5
|
|
||||||
with:
|
|
||||||
name: Package Jar ${{ matrix.java }}
|
|
||||||
path: target/*.jar
|
|
||||||
|
|
||||||
build-25:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
max-parallel: 1
|
|
||||||
matrix:
|
|
||||||
# build against supported Java LTS versions:
|
|
||||||
java: [ 25 ]
|
|
||||||
name: Java ${{ matrix.java }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v5
|
|
||||||
- name: Set up JDK ${{ matrix.java }}
|
|
||||||
uses: actions/setup-java@v5
|
|
||||||
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@v5
|
|
||||||
with:
|
|
||||||
name: Test Results ${{ matrix.java }}
|
|
||||||
path: target/surefire-reports/
|
|
||||||
- name: Upload Test Report ${{ matrix.java }}
|
|
||||||
if: ${{ always() }}
|
|
||||||
uses: actions/upload-artifact@v5
|
|
||||||
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@v5
|
|
||||||
with:
|
|
||||||
name: Package Jar ${{ matrix.java }}
|
|
||||||
path: target/*.jar
|
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
# Build Instructions
|
||||||
|
|
||||||
|
## Building locally
|
||||||
|
|
||||||
|
```
|
||||||
|
mvn clean install
|
||||||
|
```
|
||||||
|
|
||||||
|
GPG signing and Maven Central publishing are only active in the `release` profile, so no GPG key is needed for normal builds.
|
||||||
|
|
||||||
|
## Deploying
|
||||||
|
|
||||||
|
- **Internal Artifactory** (`https://maven.duxtechnica.org/repository/...`, configured in `distributionManagement`; credentials come from the `internal-release` / `internal-snapshot` server ids in `settings.xml`):
|
||||||
|
```
|
||||||
|
mvn clean deploy
|
||||||
|
```
|
||||||
|
Versions ending in `-SNAPSHOT` go to the snapshot repo, all others to the release repo.
|
||||||
|
|
||||||
|
- **Maven Central** (signed; requires a GPG key and the `central` server id in `settings.xml`):
|
||||||
|
```
|
||||||
|
mvn clean deploy -Prelease
|
||||||
|
```
|
||||||
|
|
||||||
|
## GPG signing error (historical)
|
||||||
|
|
||||||
|
Before signing was moved to the `release` profile, `mvn clean install` without a GPG key failed with:
|
||||||
|
|
||||||
|
```
|
||||||
|
gpg: no default secret key: No secret key
|
||||||
|
gpg: signing failed: No secret key
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cause
|
||||||
|
|
||||||
|
`maven-gpg-plugin` ran the `sign` goal in the `verify` phase on every build. It now lives in the `release` profile in [pom.xml](./pom.xml), so it only runs with `-Prelease`.
|
||||||
|
|
||||||
|
### If you use `-Prelease`
|
||||||
|
|
||||||
|
- Check for existing keys: `gpg --list-secret-keys`
|
||||||
|
- Import a key (`gpg --import private.key`) or generate one (`gpg --gen-key`)
|
||||||
|
- With multiple keys, select one: `-Dgpg.keyname=<KEYID>`
|
||||||
|
- The pom passes `--pinentry-mode loopback`, so the passphrase can be supplied with `-Dgpg.passphrase=...`. Prefer `settings.xml` or an environment variable over the command line.
|
||||||
|
- To run the profile without signing: `-Dgpg.skip=true`
|
||||||
@@ -11,7 +11,7 @@ JSON in Java [package org.json]
|
|||||||
[](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml)
|
[](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml)
|
||||||
[](https://javadoc.io/doc/org.json/json)
|
[](https://javadoc.io/doc/org.json/json)
|
||||||
|
|
||||||
**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/202607119/json-20260719.jar)**
|
**[Click here if you just want the latest release jar file.](https://repo1.maven.org/maven2/org/json/json/20260814/json-20260814.jar)**
|
||||||
|
|
||||||
|
|
||||||
# Overview
|
# Overview
|
||||||
@@ -51,6 +51,24 @@ For more information on contributions, please see [CONTRIBUTING.md](https://gith
|
|||||||
|
|
||||||
Bug fixes, code improvements, and unit test coverage changes are welcome! Because this project is currently in the maintenance phase, the kinds of changes that can be accepted are limited. For more information, please read the [FAQ](https://github.com/stleary/JSON-java/wiki/FAQ).
|
Bug fixes, code improvements, and unit test coverage changes are welcome! Because this project is currently in the maintenance phase, the kinds of changes that can be accepted are limited. For more information, please read the [FAQ](https://github.com/stleary/JSON-java/wiki/FAQ).
|
||||||
|
|
||||||
|
The project has recently received a number of pull requests that each make a small change to existing behavior. Most come from reading the code or a specification, many apparently with the help of AI tools, and not from a problem that someone met in use. Each change is small, but together they put backward compatibility at risk. For that reason, the requirements below now apply.
|
||||||
|
|
||||||
|
# Requirements for code changes
|
||||||
|
|
||||||
|
These requirements apply to every pull request that changes code under src/main:
|
||||||
|
|
||||||
|
**1. File an issue first.** The issue must come from someone who hit the problem in real use. It should describe what you were doing, the input, the result you got, and the result you expected. A difference from a specification that was found by reading the code is not enough on its own.
|
||||||
|
|
||||||
|
**2. State which inputs change, and how.** The pull request description must link the issue and list the inputs that behave differently, with the result before and after the change. If no existing behavior changes, say so.
|
||||||
|
|
||||||
|
Pull requests that do not meet these requirements may be closed without review.
|
||||||
|
|
||||||
|
Pull requests that only change unit tests or documentation do not need an issue. Security vulnerabilities are handled separately; please report them as described in SECURITY.md.
|
||||||
|
|
||||||
|
**JSONPointer is frozen**
|
||||||
|
|
||||||
|
Several fixes that bring JSONPointer in line with RFC 6901 ship in the release that follows 20260814. Once that release is published, JSONPointer is frozen. It will be reopened only for bugs that users report, under the requirements above. Its behavior is pinned by JSONPointerConformanceTest (**TBD**).
|
||||||
|
|
||||||
# Build Instructions
|
# Build Instructions
|
||||||
|
|
||||||
The org.json package can be built from the command line, Maven, and Gradle. The unit tests can be executed from Maven, Gradle, or individually in an IDE e.g. Eclipse.
|
The org.json package can be built from the command line, Maven, and Gradle. The unit tests can be executed from Maven, Gradle, or individually in an IDE e.g. Eclipse.
|
||||||
|
|||||||
+9
-1
@@ -21,6 +21,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// To view the report open build/reports/jacoco/test/html/index.html
|
// To view the report open build/reports/jacoco/test/html/index.html
|
||||||
|
// Note: disable jacocoTestReport and test blocks, and the jacoco plugin if it interferes with stepping in the debugger
|
||||||
jacocoTestReport {
|
jacocoTestReport {
|
||||||
reports {
|
reports {
|
||||||
html.required = true
|
html.required = true
|
||||||
@@ -31,6 +32,13 @@ test {
|
|||||||
finalizedBy jacocoTestReport
|
finalizedBy jacocoTestReport
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// enable logging to stdout during test
|
||||||
|
tasks.withType(Test) {
|
||||||
|
testLogging {
|
||||||
|
showStandardStreams = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
testImplementation 'com.jayway.jsonpath:json-path:2.9.0'
|
testImplementation 'com.jayway.jsonpath:json-path:2.9.0'
|
||||||
@@ -42,7 +50,7 @@ subprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'org.json'
|
group = 'org.json'
|
||||||
version = 'v20260719-SNAPSHOT'
|
version = 'v20260814-SNAPSHOT'
|
||||||
description = 'JSON in Java'
|
description = 'JSON in Java'
|
||||||
sourceCompatibility = '1.8'
|
sourceCompatibility = '1.8'
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ and artifactId "json". For example:
|
|||||||
[https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav)
|
[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)
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
|
20260814 Fixes XML.unescape whitespace handling
|
||||||
|
|
||||||
20260719 Fixes CVE-2026-59171 very large BigInteger, BigDecimal
|
20260719 Fixes CVE-2026-59171 very large BigInteger, BigDecimal
|
||||||
|
|
||||||
20260522 Publish key data, recent commits for minor fixes
|
20260522 Publish key data, recent commits for minor fixes
|
||||||
|
|||||||
Vendored
BIN
Binary file not shown.
-5
@@ -1,5 +0,0 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
|
||||||
distributionPath=wrapper/dists
|
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
|
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
|
||||||
zipStorePath=wrapper/dists
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
|
|
||||||
#
|
|
||||||
# Copyright 2015 the original author or authors.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# You may obtain a copy of the License at
|
|
||||||
#
|
|
||||||
# https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
# See the License for the specific language governing permissions and
|
|
||||||
# limitations under the License.
|
|
||||||
#
|
|
||||||
|
|
||||||
##############################################################################
|
|
||||||
##
|
|
||||||
## Gradle start up script for UN*X
|
|
||||||
##
|
|
||||||
##############################################################################
|
|
||||||
|
|
||||||
# Attempt to set APP_HOME
|
|
||||||
# Resolve links: $0 may be a link
|
|
||||||
PRG="$0"
|
|
||||||
# Need this for relative symlinks.
|
|
||||||
while [ -h "$PRG" ] ; do
|
|
||||||
ls=`ls -ld "$PRG"`
|
|
||||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
|
||||||
if expr "$link" : '/.*' > /dev/null; then
|
|
||||||
PRG="$link"
|
|
||||||
else
|
|
||||||
PRG=`dirname "$PRG"`"/$link"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
SAVED="`pwd`"
|
|
||||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
|
||||||
APP_HOME="`pwd -P`"
|
|
||||||
cd "$SAVED" >/dev/null
|
|
||||||
|
|
||||||
APP_NAME="Gradle"
|
|
||||||
APP_BASE_NAME=`basename "$0"`
|
|
||||||
|
|
||||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
|
||||||
MAX_FD="maximum"
|
|
||||||
|
|
||||||
warn () {
|
|
||||||
echo "$*"
|
|
||||||
}
|
|
||||||
|
|
||||||
die () {
|
|
||||||
echo
|
|
||||||
echo "$*"
|
|
||||||
echo
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# OS specific support (must be 'true' or 'false').
|
|
||||||
cygwin=false
|
|
||||||
msys=false
|
|
||||||
darwin=false
|
|
||||||
nonstop=false
|
|
||||||
case "`uname`" in
|
|
||||||
CYGWIN* )
|
|
||||||
cygwin=true
|
|
||||||
;;
|
|
||||||
Darwin* )
|
|
||||||
darwin=true
|
|
||||||
;;
|
|
||||||
MINGW* )
|
|
||||||
msys=true
|
|
||||||
;;
|
|
||||||
NONSTOP* )
|
|
||||||
nonstop=true
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
|
||||||
|
|
||||||
# Determine the Java command to use to start the JVM.
|
|
||||||
if [ -n "$JAVA_HOME" ] ; then
|
|
||||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
|
||||||
# IBM's JDK on AIX uses strange locations for the executables
|
|
||||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
|
||||||
else
|
|
||||||
JAVACMD="$JAVA_HOME/bin/java"
|
|
||||||
fi
|
|
||||||
if [ ! -x "$JAVACMD" ] ; then
|
|
||||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
|
||||||
|
|
||||||
Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
location of your Java installation."
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
JAVACMD="java"
|
|
||||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
||||||
|
|
||||||
Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
location of your Java installation."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Increase the maximum file descriptors if we can.
|
|
||||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
|
||||||
MAX_FD_LIMIT=`ulimit -H -n`
|
|
||||||
if [ $? -eq 0 ] ; then
|
|
||||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
|
||||||
MAX_FD="$MAX_FD_LIMIT"
|
|
||||||
fi
|
|
||||||
ulimit -n $MAX_FD
|
|
||||||
if [ $? -ne 0 ] ; then
|
|
||||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# For Darwin, add options to specify how the application appears in the dock
|
|
||||||
if $darwin; then
|
|
||||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
|
||||||
fi
|
|
||||||
|
|
||||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
|
||||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
|
||||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
|
||||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
|
||||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
|
||||||
|
|
||||||
# We build the pattern for arguments to be converted via cygpath
|
|
||||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
|
||||||
SEP=""
|
|
||||||
for dir in $ROOTDIRSRAW ; do
|
|
||||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
|
||||||
SEP="|"
|
|
||||||
done
|
|
||||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
|
||||||
# Add a user-defined pattern to the cygpath arguments
|
|
||||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
|
||||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
|
||||||
fi
|
|
||||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
|
||||||
i=0
|
|
||||||
for arg in "$@" ; do
|
|
||||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
|
||||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
|
||||||
|
|
||||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
|
||||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
|
||||||
else
|
|
||||||
eval `echo args$i`="\"$arg\""
|
|
||||||
fi
|
|
||||||
i=`expr $i + 1`
|
|
||||||
done
|
|
||||||
case $i in
|
|
||||||
0) set -- ;;
|
|
||||||
1) set -- "$args0" ;;
|
|
||||||
2) set -- "$args0" "$args1" ;;
|
|
||||||
3) set -- "$args0" "$args1" "$args2" ;;
|
|
||||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
|
||||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
|
||||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
|
||||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
|
||||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
|
||||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
|
||||||
esac
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Escape application args
|
|
||||||
save () {
|
|
||||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
|
||||||
echo " "
|
|
||||||
}
|
|
||||||
APP_ARGS=`save "$@"`
|
|
||||||
|
|
||||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
|
||||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
|
||||||
|
|
||||||
exec "$JAVACMD" "$@"
|
|
||||||
Vendored
-103
@@ -1,103 +0,0 @@
|
|||||||
@rem
|
|
||||||
@rem Copyright 2015 the original author or authors.
|
|
||||||
@rem
|
|
||||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
@rem you may not use this file except in compliance with the License.
|
|
||||||
@rem You may obtain a copy of the License at
|
|
||||||
@rem
|
|
||||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
@rem
|
|
||||||
@rem Unless required by applicable law or agreed to in writing, software
|
|
||||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
@rem See the License for the specific language governing permissions and
|
|
||||||
@rem limitations under the License.
|
|
||||||
@rem
|
|
||||||
|
|
||||||
@if "%DEBUG%" == "" @echo off
|
|
||||||
@rem ##########################################################################
|
|
||||||
@rem
|
|
||||||
@rem Gradle startup script for Windows
|
|
||||||
@rem
|
|
||||||
@rem ##########################################################################
|
|
||||||
|
|
||||||
@rem Set local scope for the variables with windows NT shell
|
|
||||||
if "%OS%"=="Windows_NT" setlocal
|
|
||||||
|
|
||||||
set DIRNAME=%~dp0
|
|
||||||
if "%DIRNAME%" == "" set DIRNAME=.
|
|
||||||
set APP_BASE_NAME=%~n0
|
|
||||||
set APP_HOME=%DIRNAME%
|
|
||||||
|
|
||||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
|
||||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
|
||||||
|
|
||||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
||||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
|
||||||
|
|
||||||
@rem Find java.exe
|
|
||||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
|
||||||
|
|
||||||
set JAVA_EXE=java.exe
|
|
||||||
%JAVA_EXE% -version >NUL 2>&1
|
|
||||||
if "%ERRORLEVEL%" == "0" goto init
|
|
||||||
|
|
||||||
echo.
|
|
||||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
||||||
echo.
|
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
echo location of your Java installation.
|
|
||||||
|
|
||||||
goto fail
|
|
||||||
|
|
||||||
:findJavaFromJavaHome
|
|
||||||
set JAVA_HOME=%JAVA_HOME:"=%
|
|
||||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|
||||||
|
|
||||||
if exist "%JAVA_EXE%" goto init
|
|
||||||
|
|
||||||
echo.
|
|
||||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
|
||||||
echo.
|
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
echo location of your Java installation.
|
|
||||||
|
|
||||||
goto fail
|
|
||||||
|
|
||||||
:init
|
|
||||||
@rem Get command-line arguments, handling Windows variants
|
|
||||||
|
|
||||||
if not "%OS%" == "Windows_NT" goto win9xME_args
|
|
||||||
|
|
||||||
:win9xME_args
|
|
||||||
@rem Slurp the command line arguments.
|
|
||||||
set CMD_LINE_ARGS=
|
|
||||||
set _SKIP=2
|
|
||||||
|
|
||||||
:win9xME_args_slurp
|
|
||||||
if "x%~1" == "x" goto execute
|
|
||||||
|
|
||||||
set CMD_LINE_ARGS=%*
|
|
||||||
|
|
||||||
:execute
|
|
||||||
@rem Setup the command line
|
|
||||||
|
|
||||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
|
||||||
|
|
||||||
@rem Execute Gradle
|
|
||||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
|
|
||||||
|
|
||||||
:end
|
|
||||||
@rem End local scope for the variables with windows NT shell
|
|
||||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
|
||||||
|
|
||||||
:fail
|
|
||||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
|
||||||
rem the _cmd.exe /c_ return code!
|
|
||||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
|
||||||
exit /b 1
|
|
||||||
|
|
||||||
:mainEnd
|
|
||||||
if "%OS%"=="Windows_NT" endlocal
|
|
||||||
|
|
||||||
:omega
|
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
<groupId>org.json</groupId>
|
<groupId>org.json</groupId>
|
||||||
<artifactId>json</artifactId>
|
<artifactId>json</artifactId>
|
||||||
<version>20260719</version>
|
<version>20260814</version>
|
||||||
<packaging>bundle</packaging>
|
<packaging>bundle</packaging>
|
||||||
|
|
||||||
<name>JSON in Java</name>
|
<name>JSON in Java</name>
|
||||||
@@ -46,20 +46,6 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</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>
|
||||||
@@ -138,37 +124,6 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-gpg-plugin</artifactId>
|
|
||||||
<version>1.6</version>
|
|
||||||
<executions>
|
|
||||||
<execution>
|
|
||||||
<id>sign-artifacts</id>
|
|
||||||
<phase>verify</phase>
|
|
||||||
<goals>
|
|
||||||
<goal>sign</goal>
|
|
||||||
</goals>
|
|
||||||
<configuration>
|
|
||||||
<gpgArguments>
|
|
||||||
<arg>--pinentry-mode</arg>
|
|
||||||
<arg>loopback</arg>
|
|
||||||
</gpgArguments>
|
|
||||||
</configuration>
|
|
||||||
</execution>
|
|
||||||
</executions>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.sonatype.plugins</groupId>
|
|
||||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
|
||||||
<version>1.6.3</version>
|
|
||||||
<extensions>true</extensions>
|
|
||||||
<configuration>
|
|
||||||
<serverId>ossrh</serverId>
|
|
||||||
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
|
|
||||||
<autoReleaseAfterClose>false</autoReleaseAfterClose>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.moditect</groupId>
|
<groupId>org.moditect</groupId>
|
||||||
<artifactId>moditect-maven-plugin</artifactId>
|
<artifactId>moditect-maven-plugin</artifactId>
|
||||||
@@ -198,18 +153,46 @@
|
|||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>3.3.0</version>
|
<version>3.3.0</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
|
||||||
<groupId>org.sonatype.central</groupId>
|
|
||||||
<artifactId>central-publishing-maven-plugin</artifactId>
|
|
||||||
<version>0.9.0</version>
|
|
||||||
<extensions>true</extensions>
|
|
||||||
<configuration>
|
|
||||||
<publishingServerId>central</publishingServerId>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<profiles>
|
<profiles>
|
||||||
|
<!-- Publishes signed artifacts to Maven Central: mvn -Prelease deploy -->
|
||||||
|
<profile>
|
||||||
|
<id>release</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-gpg-plugin</artifactId>
|
||||||
|
<version>1.6</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>sign-artifacts</id>
|
||||||
|
<phase>verify</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>sign</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<gpgArguments>
|
||||||
|
<arg>--pinentry-mode</arg>
|
||||||
|
<arg>loopback</arg>
|
||||||
|
</gpgArguments>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.sonatype.central</groupId>
|
||||||
|
<artifactId>central-publishing-maven-plugin</artifactId>
|
||||||
|
<version>0.9.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<publishingServerId>central</publishingServerId>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
<profile>
|
<profile>
|
||||||
<id>test-strict-mode</id>
|
<id>test-strict-mode</id>
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* @param x
|
* @param x
|
||||||
* A JSONTokener
|
* A JSONTokener
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
* If there is a syntax error.
|
* If there is a syntax error.
|
||||||
*/
|
*/
|
||||||
public JSONArray(JSONTokener x) throws JSONException {
|
public JSONArray(JSONTokener x) throws JSONException {
|
||||||
this(x, x.getJsonParserConfiguration());
|
this(x, x.getJsonParserConfiguration());
|
||||||
@@ -88,42 +88,62 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
|
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
|
||||||
|
* The supplied configuration applies to all values during construction,
|
||||||
|
* including nested objects and arrays. The tokener's original configuration
|
||||||
|
* is restored afterward, even if parsing fails.
|
||||||
*
|
*
|
||||||
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
||||||
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||||
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
|
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
|
||||||
*/
|
*/
|
||||||
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||||
|
this(x, jsonParserConfiguration, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration, for internal use. <br>
|
||||||
|
* Never call this instead of using withStrictMode(boolean).
|
||||||
|
*
|
||||||
|
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
||||||
|
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||||
|
* @param isInitial A boolean that determines whether this array is the root.
|
||||||
|
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
|
||||||
|
*/
|
||||||
|
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
||||||
this();
|
this();
|
||||||
|
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
|
||||||
|
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
|
try {
|
||||||
|
if (x.nextClean() != '[') {
|
||||||
|
throw x.syntaxError("A JSONArray text must start with '['");
|
||||||
|
}
|
||||||
|
|
||||||
boolean isInitial = x.getPrevious() == 0;
|
char nextChar = x.nextClean();
|
||||||
if (x.nextClean() != '[') {
|
if (nextChar == 0) {
|
||||||
throw x.syntaxError("A JSONArray text must start with '['");
|
// array is unclosed. No ']' found, instead EOF
|
||||||
}
|
throw x.syntaxError("Expected a ',' or ']'");
|
||||||
|
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
|
||||||
char nextChar = x.nextClean();
|
throw x.syntaxError("Array content starts with a ','");
|
||||||
if (nextChar == 0) {
|
}
|
||||||
// array is unclosed. No ']' found, instead EOF
|
if (nextChar != ']') {
|
||||||
throw x.syntaxError("Expected a ',' or ']'");
|
x.back();
|
||||||
} else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
|
for (;;) {
|
||||||
throw x.syntaxError("Array content starts with a ','");
|
if (x.nextClean() == ',') {
|
||||||
}
|
x.back();
|
||||||
if (nextChar != ']') {
|
this.myArrayList.add(JSONObject.NULL);
|
||||||
x.back();
|
} else {
|
||||||
for (;;) {
|
x.back();
|
||||||
if (x.nextClean() == ',') {
|
this.myArrayList.add(x.nextValue());
|
||||||
x.back();
|
}
|
||||||
this.myArrayList.add(JSONObject.NULL);
|
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
|
||||||
} else {
|
}
|
||||||
x.back();
|
} else {
|
||||||
this.myArrayList.add(x.nextValue());
|
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
|
||||||
|
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
||||||
}
|
}
|
||||||
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
|
|
||||||
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
x.setJsonParserConfigurationInternal(originalConfiguration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ Public Domain.
|
|||||||
*/
|
*/
|
||||||
public class JSONML {
|
public class JSONML {
|
||||||
|
|
||||||
|
private static final String CHILD_NODES = "childNodes";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new JSONML object.
|
* Constructs a new JSONML object.
|
||||||
* @deprecated (Utility class cannot be instantiated)
|
* @deprecated (Utility class cannot be instantiated)
|
||||||
@@ -205,7 +207,7 @@ public class JSONML {
|
|||||||
// attribute = value
|
// attribute = value
|
||||||
|
|
||||||
attribute = (String)token;
|
attribute = (String)token;
|
||||||
if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
|
if (!arrayForm && ("tagName".equals(attribute) || CHILD_NODES.equals(attribute))) {
|
||||||
throw x.syntaxError("Reserved attribute.");
|
throw x.syntaxError("Reserved attribute.");
|
||||||
}
|
}
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
@@ -256,7 +258,7 @@ public class JSONML {
|
|||||||
}
|
}
|
||||||
tagName = null;
|
tagName = null;
|
||||||
if (!arrayForm && newja.length() > 0) {
|
if (!arrayForm && newja.length() > 0) {
|
||||||
newjo.put("childNodes", newja);
|
newjo.put(CHILD_NODES, newja);
|
||||||
}
|
}
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
if (arrayForm) {
|
if (arrayForm) {
|
||||||
@@ -274,7 +276,7 @@ public class JSONML {
|
|||||||
if (token instanceof String) {
|
if (token instanceof String) {
|
||||||
String strToken = (String) token;
|
String strToken = (String) token;
|
||||||
if (config.isKeepStrings()) {
|
if (config.isKeepStrings()) {
|
||||||
value = XML.unescape(strToken);
|
value = strToken;
|
||||||
} else {
|
} else {
|
||||||
value = XML.stringToValue(strToken);
|
value = XML.stringToValue(strToken);
|
||||||
}
|
}
|
||||||
@@ -648,7 +650,7 @@ public class JSONML {
|
|||||||
|
|
||||||
// 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()) {
|
||||||
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
|
if (!"tagName".equals(key) && !CHILD_NODES.equals(key)) {
|
||||||
XML.noSpace(key);
|
XML.noSpace(key);
|
||||||
value = jo.opt(key);
|
value = jo.opt(key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
@@ -664,7 +666,7 @@ public class JSONML {
|
|||||||
|
|
||||||
//Emit content in body
|
//Emit content in body
|
||||||
|
|
||||||
ja = jo.optJSONArray("childNodes");
|
ja = jo.optJSONArray(CHILD_NODES);
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
|
|||||||
@@ -195,8 +195,8 @@ public class JSONObject {
|
|||||||
* @param x
|
* @param x
|
||||||
* A JSONTokener object containing the source string.
|
* A JSONTokener object containing the source string.
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
* If there is a syntax error in the source string or a
|
* If there is a syntax error in the source string or a
|
||||||
* duplicated key.
|
* duplicated key.
|
||||||
*/
|
*/
|
||||||
public JSONObject(JSONTokener x) throws JSONException {
|
public JSONObject(JSONTokener x) throws JSONException {
|
||||||
this(x, x.getJsonParserConfiguration());
|
this(x, x.getJsonParserConfiguration());
|
||||||
@@ -204,26 +204,51 @@ public class JSONObject {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a JSONObject from a JSONTokener with custom json parse configurations.
|
* Construct a JSONObject from a JSONTokener with custom json parse configurations.
|
||||||
|
* The supplied configuration applies to all values during construction,
|
||||||
|
* including nested objects and arrays. The tokener's original configuration
|
||||||
|
* is restored afterward, even if parsing fails.
|
||||||
*
|
*
|
||||||
* @param x
|
* @param x
|
||||||
* A JSONTokener object containing the source string.
|
* A JSONTokener object containing the source string.
|
||||||
* @param jsonParserConfiguration
|
* @param jsonParserConfiguration
|
||||||
* Variable to pass parser custom configuration for json parsing.
|
* Variable to pass parser custom configuration for json parsing.
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
* If there is a syntax error in the source string or a
|
* If there is a syntax error in the source string or a
|
||||||
* duplicated key.
|
* duplicated key.
|
||||||
*/
|
*/
|
||||||
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||||
this();
|
this(x, jsonParserConfiguration, true);
|
||||||
boolean isInitial = x.getPrevious() == 0;
|
}
|
||||||
|
|
||||||
if (x.nextClean() != '{') {
|
/**
|
||||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
* Construct a JSONObject from a JSONTokener with custom json parse configurations, for internal use. <br>
|
||||||
}
|
* Never call this instead of using withStrictMode(boolean).
|
||||||
for (;;) {
|
*
|
||||||
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
|
* @param x
|
||||||
return;
|
* A JSONTokener object containing the source string.
|
||||||
|
* @param jsonParserConfiguration
|
||||||
|
* Variable to pass parser custom configuration for json parsing.
|
||||||
|
* @param isInitial
|
||||||
|
* A boolean that determines whether this object is the root.
|
||||||
|
* @throws JSONException
|
||||||
|
* If there is a syntax error in the source string or a
|
||||||
|
* duplicated key.
|
||||||
|
*/
|
||||||
|
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
|
||||||
|
this();
|
||||||
|
JSONParserConfiguration originalConfiguration = x.getJsonParserConfiguration();
|
||||||
|
x.setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
|
try {
|
||||||
|
if (x.nextClean() != '{') {
|
||||||
|
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||||
}
|
}
|
||||||
|
for (;;) {
|
||||||
|
if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
x.setJsonParserConfigurationInternal(originalConfiguration);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +276,7 @@ public class JSONObject {
|
|||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
obj = jsonTokener.nextSimpleValue(c);
|
obj = jsonTokener.nextSimpleValue(c);
|
||||||
key = obj.toString();
|
key = jsonTokener.shareKey(obj.toString(), jsonParserConfiguration.getMaxSharedKeys());
|
||||||
}
|
}
|
||||||
|
|
||||||
checkKeyForStrictMode(jsonTokener, jsonParserConfiguration, obj);
|
checkKeyForStrictMode(jsonTokener, jsonParserConfiguration, obj);
|
||||||
@@ -3447,12 +3472,15 @@ public class JSONObject {
|
|||||||
* of the given class. It supports basic data types including int, double, float,
|
* of the given class. It supports basic data types including int, double, float,
|
||||||
* long, and boolean (as well as their boxed counterparts). The class must have a
|
* long, and boolean (as well as their boxed counterparts). The class must have a
|
||||||
* no-argument constructor, and the field names in the class must match the keys
|
* no-argument constructor, and the field names in the class must match the keys
|
||||||
* in the JSON string.
|
* in the JSON string. Static fields are ignored; see {@link #fromJson(Class)} for
|
||||||
|
* how the remaining fields are bound.
|
||||||
*
|
*
|
||||||
* @param <T> the type of the object to return
|
* @param <T> the type of the object to return
|
||||||
* @param jsonString json in string format
|
* @param jsonString json in string format
|
||||||
* @param clazz the class of the object to be returned
|
* @param clazz the class of the object to be returned
|
||||||
* @return an instance of Object T with fields populated from the JSON string
|
* @return an instance of Object T with fields populated from the JSON string
|
||||||
|
* @throws JSONException if jsonString is not valid JSON, or if it cannot be bound to clazz
|
||||||
|
* as described in {@link #fromJson(Class)}
|
||||||
*/
|
*/
|
||||||
public static <T> T fromJson(String jsonString, Class<T> clazz) {
|
public static <T> T fromJson(String jsonString, Class<T> clazz) {
|
||||||
JSONObject jsonObject = new JSONObject(jsonString);
|
JSONObject jsonObject = new JSONObject(jsonString);
|
||||||
@@ -3468,15 +3496,20 @@ public class JSONObject {
|
|||||||
* The target class must have a no-argument constructor, and its field names must match
|
* The target class must have a no-argument constructor, and its field names must match
|
||||||
* the keys in the JSON string. Static fields are ignored.
|
* the keys in the JSON string. Static fields are ignored.
|
||||||
*
|
*
|
||||||
* <p><strong>Note:</strong> Only classes that are explicitly supported and registered within
|
* <p><strong>Note:</strong> binding is driven by the keys present in the JSON, not by
|
||||||
* the {@code JSONObject} context can be deserialized. If the provided class is not among those,
|
* how the fields are declared. Every non-static field declared by the class is written
|
||||||
* this method will not be able to deserialize it. This ensures that only a limited and
|
* when its name matches a key, including {@code private} and {@code transient} fields,
|
||||||
* controlled set of types can be instantiated from JSON for safety and predictability.
|
* and field access is widened with
|
||||||
|
* {@link java.lang.reflect.Field#setAccessible(boolean)}. {@link JSONPropertyIgnore}
|
||||||
|
* only suppresses the bean to JSONObject direction, so it does not keep a field from
|
||||||
|
* being populated here. JSON coming from an untrusted source should therefore be
|
||||||
|
* validated, or reduced to the expected keys, before it is handed to this method.
|
||||||
*
|
*
|
||||||
* @param clazz the class of the object to be returned
|
* @param clazz the class of the object to be returned
|
||||||
* @param <T> the type of the object
|
* @param <T> the type of the object
|
||||||
* @return an instance of type {@code T} with fields populated from the JSON string
|
* @return an instance of type {@code T} with fields populated from the JSON string
|
||||||
* @throws IllegalArgumentException if the class is not supported for deserialization
|
* @throws JSONException if the class has no accessible no-argument constructor, or if a
|
||||||
|
* field cannot be instantiated or set
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T fromJson(Class<T> clazz) {
|
public <T> T fromJson(Class<T> clazz) {
|
||||||
|
|||||||
@@ -4,6 +4,16 @@ package org.json;
|
|||||||
* Configuration object for the JSON parser. The configuration is immutable.
|
* Configuration object for the JSON parser. The configuration is immutable.
|
||||||
*/
|
*/
|
||||||
public class JSONParserConfiguration extends ParserConfiguration {
|
public class JSONParserConfiguration extends ParserConfiguration {
|
||||||
|
/**
|
||||||
|
* The default maximum number of distinct object keys shared within one parse.
|
||||||
|
*/
|
||||||
|
public static final int DEFAULT_MAXIMUM_SHARED_KEYS = 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to indicate that there is no limit on the number of distinct object keys shared within one parse.
|
||||||
|
*/
|
||||||
|
public static final int UNLIMITED_SHARED_KEYS = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to indicate whether to overwrite duplicate key or not.
|
* Used to indicate whether to overwrite duplicate key or not.
|
||||||
*/
|
*/
|
||||||
@@ -14,12 +24,18 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
*/
|
*/
|
||||||
private boolean useNativeNulls;
|
private boolean useNativeNulls;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of distinct object keys shared within one parse.
|
||||||
|
*/
|
||||||
|
private int maxSharedKeys;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration with the default values.
|
* Configuration with the default values.
|
||||||
*/
|
*/
|
||||||
public JSONParserConfiguration() {
|
public JSONParserConfiguration() {
|
||||||
super();
|
super();
|
||||||
this.overwriteDuplicateKey = false;
|
this.overwriteDuplicateKey = false;
|
||||||
|
this.maxSharedKeys = DEFAULT_MAXIMUM_SHARED_KEYS;
|
||||||
// DO NOT DELETE THE FOLLOWING LINE -- it is used for strictMode testing
|
// DO NOT DELETE THE FOLLOWING LINE -- it is used for strictMode testing
|
||||||
// this.strictMode = true;
|
// this.strictMode = true;
|
||||||
}
|
}
|
||||||
@@ -38,6 +54,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
clone.maxNestingDepth = maxNestingDepth;
|
clone.maxNestingDepth = maxNestingDepth;
|
||||||
clone.keepStrings = keepStrings;
|
clone.keepStrings = keepStrings;
|
||||||
clone.useNativeNulls = useNativeNulls;
|
clone.useNativeNulls = useNativeNulls;
|
||||||
|
clone.maxSharedKeys = maxSharedKeys;
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,6 +106,24 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines how many distinct object keys the parser shares within one parse. When a key repeats, for example
|
||||||
|
* in each element of an array of objects, the parser reuses the first String instance read for that key
|
||||||
|
* instead of keeping a separate copy in every object. Only the first <code>maxSharedKeys</code> distinct keys
|
||||||
|
* are shared, so input that begins with many unique keys may need a higher limit. The default is 1000.
|
||||||
|
* Parsed values are the same with any setting.
|
||||||
|
*
|
||||||
|
* @param maxSharedKeys the maximum number of distinct keys to share; 0 disables sharing, and
|
||||||
|
* {@link #UNLIMITED_SHARED_KEYS} (or any negative value) removes the limit
|
||||||
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
|
*/
|
||||||
|
public JSONParserConfiguration withMaxSharedKeys(final int maxSharedKeys) {
|
||||||
|
JSONParserConfiguration clone = this.clone();
|
||||||
|
clone.maxSharedKeys = Math.max(maxSharedKeys, UNLIMITED_SHARED_KEYS);
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the strict mode configuration for the JSON parser with default true value
|
* Sets the strict mode configuration for the JSON parser with default true value
|
||||||
* <p>
|
* <p>
|
||||||
@@ -140,6 +175,16 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of distinct object keys shared within one parse. 0 means keys are not shared,
|
||||||
|
* and {@link #UNLIMITED_SHARED_KEYS} means there is no limit.
|
||||||
|
*
|
||||||
|
* @return The <code>maxSharedKeys</code> configuration value.
|
||||||
|
*/
|
||||||
|
public int getMaxSharedKeys() {
|
||||||
|
return this.maxSharedKeys;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.
|
* The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.
|
||||||
* Otherwise, the parser is more relaxed and might tolerate some invalid characters.
|
* Otherwise, the parser is more relaxed and might tolerate some invalid characters.
|
||||||
|
|||||||
@@ -135,7 +135,8 @@ public class JSONPointer {
|
|||||||
if (pointer.startsWith("#/")) {
|
if (pointer.startsWith("#/")) {
|
||||||
refs = pointer.substring(2);
|
refs = pointer.substring(2);
|
||||||
try {
|
try {
|
||||||
refs = URLDecoder.decode(refs, ENCODING);
|
// URI fragments preserve plus signs rather than treating them as form-encoded spaces.
|
||||||
|
refs = URLDecoder.decode(refs.replace("+", "%2B"), ENCODING);
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -203,7 +204,7 @@ public class JSONPointer {
|
|||||||
Object current = document;
|
Object current = document;
|
||||||
for (String token : this.refTokens) {
|
for (String token : this.refTokens) {
|
||||||
if (current instanceof JSONObject) {
|
if (current instanceof JSONObject) {
|
||||||
current = ((JSONObject) current).opt(unescape(token));
|
current = ((JSONObject) current).opt(token);
|
||||||
} else if (current instanceof JSONArray) {
|
} else if (current instanceof JSONArray) {
|
||||||
current = readByIndexToken(current, token);
|
current = readByIndexToken(current, token);
|
||||||
} else {
|
} else {
|
||||||
@@ -276,7 +277,7 @@ public class JSONPointer {
|
|||||||
try {
|
try {
|
||||||
StringBuilder rval = new StringBuilder("#");
|
StringBuilder rval = new StringBuilder("#");
|
||||||
for (String token : this.refTokens) {
|
for (String token : this.refTokens) {
|
||||||
rval.append('/').append(URLEncoder.encode(token, ENCODING));
|
rval.append('/').append(URLEncoder.encode(escape(token), ENCODING).replace("+", "%20"));
|
||||||
}
|
}
|
||||||
return rval.toString();
|
return rval.toString();
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package org.json;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Public Domain.
|
Public Domain.
|
||||||
@@ -35,6 +37,9 @@ public class JSONTokener {
|
|||||||
// access to this object is required for strict mode checking
|
// access to this object is required for strict mode checking
|
||||||
private JSONParserConfiguration jsonParserConfiguration;
|
private JSONParserConfiguration jsonParserConfiguration;
|
||||||
|
|
||||||
|
/** object keys already read, so repeated keys can share one String instance. */
|
||||||
|
private Map<String, String> sharedKeys;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a JSONTokener from a Reader. The caller must close the Reader.
|
* Construct a JSONTokener from a Reader. The caller must close the Reader.
|
||||||
*
|
*
|
||||||
@@ -117,9 +122,38 @@ public class JSONTokener {
|
|||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
|
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
|
||||||
|
setJsonParserConfigurationInternal(jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setJsonParserConfigurationInternal(JSONParserConfiguration jsonParserConfiguration) {
|
||||||
this.jsonParserConfiguration = jsonParserConfiguration;
|
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an earlier String instance equal to the key if this tokener has read one, otherwise the key itself.
|
||||||
|
*
|
||||||
|
* @param key an object key that was just read
|
||||||
|
* @param maxSharedKeys the maximum number of distinct keys to remember; 0 disables sharing and a negative
|
||||||
|
* value means no limit
|
||||||
|
* @return a String equal to <code>key</code>
|
||||||
|
*/
|
||||||
|
String shareKey(String key, int maxSharedKeys) {
|
||||||
|
if (maxSharedKeys == 0) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
if (this.sharedKeys == null) {
|
||||||
|
this.sharedKeys = new HashMap<String, String>();
|
||||||
|
}
|
||||||
|
String shared = this.sharedKeys.get(key);
|
||||||
|
if (shared != null) {
|
||||||
|
return shared;
|
||||||
|
}
|
||||||
|
if (maxSharedKeys < 0 || this.sharedKeys.size() < maxSharedKeys) {
|
||||||
|
this.sharedKeys.put(key, key);
|
||||||
|
}
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Back up one character. This provides a sort of lookahead capability,
|
* Back up one character. This provides a sort of lookahead capability,
|
||||||
* so that you can test for a digit or letter before attempting to parse
|
* so that you can test for a digit or letter before attempting to parse
|
||||||
@@ -458,14 +492,14 @@ public class JSONTokener {
|
|||||||
case '{':
|
case '{':
|
||||||
this.back();
|
this.back();
|
||||||
try {
|
try {
|
||||||
return new JSONObject(this, jsonParserConfiguration);
|
return new JSONObject(this, jsonParserConfiguration, false);
|
||||||
} catch (StackOverflowError e) {
|
} catch (StackOverflowError e) {
|
||||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||||
}
|
}
|
||||||
case '[':
|
case '[':
|
||||||
this.back();
|
this.back();
|
||||||
try {
|
try {
|
||||||
return new JSONArray(this, jsonParserConfiguration);
|
return new JSONArray(this, jsonParserConfiguration, false);
|
||||||
} catch (StackOverflowError e) {
|
} catch (StackOverflowError e) {
|
||||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class Property {
|
|||||||
// can't use the new constructor for Android support
|
// can't use the new constructor for Android support
|
||||||
// JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
|
// JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
|
||||||
JSONObject jo = new JSONObject();
|
JSONObject jo = new JSONObject();
|
||||||
if (properties != null && !properties.isEmpty()) {
|
if (properties != null) {
|
||||||
Enumeration<?> enumProperties = properties.propertyNames();
|
Enumeration<?> enumProperties = properties.propertyNames();
|
||||||
while(enumProperties.hasMoreElements()) {
|
while(enumProperties.hasMoreElements()) {
|
||||||
String name = (String)enumProperties.nextElement();
|
String name = (String)enumProperties.nextElement();
|
||||||
|
|||||||
@@ -156,28 +156,45 @@ public class XML {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param cp code point to test
|
* @param cp code point to test
|
||||||
* @return true if the code point is not valid for an XML
|
* @return true if the code point must be escaped when serializing to XML
|
||||||
*/
|
*/
|
||||||
static boolean mustEscape(int cp) {
|
static boolean mustEscape(int cp) {
|
||||||
|
// Every ISO control character must be written as a numeric character
|
||||||
|
// reference when serializing. This includes #x9 (tab), #xA (LF) and
|
||||||
|
// #xD (CR): literal occurrences of these characters are subject to
|
||||||
|
// whitespace normalization by conforming XML parsers, so they can
|
||||||
|
// only survive a round trip as 	, and (see #1092).
|
||||||
|
//
|
||||||
|
// This method is only about the serialization path. Numeric character
|
||||||
|
// references found while parsing are validated with
|
||||||
|
// isValidXmlCharacter(int) (see #1059).
|
||||||
|
return Character.isISOControl(cp) || !isValidXmlCharacter(cp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param cp code point to test
|
||||||
|
* @return true if the code point can be written as a numeric character
|
||||||
|
* reference in a document generated by this class
|
||||||
|
*/
|
||||||
|
static boolean isValidXmlCharacter(int cp) {
|
||||||
/* Valid range from https://www.w3.org/TR/REC-xml/#charsets
|
/* Valid range from https://www.w3.org/TR/REC-xml/#charsets
|
||||||
*
|
*
|
||||||
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||||
*
|
*
|
||||||
* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
|
* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
|
||||||
*/
|
*/
|
||||||
// isISOControl is true when (cp >= 0 && cp <= 0x1F) || (cp >= 0x7F && cp <= 0x9F)
|
// The C1 control characters (#x7F-#x9F) fall inside [#x20-#xD7FF] and
|
||||||
// all ISO control characters are out of range except tabs and new lines
|
// are therefore legal XML 1.0 characters, but this class has always
|
||||||
return (Character.isISOControl(cp)
|
// rejected &#NN; references to them while parsing. Keeping them
|
||||||
&& cp != 0x9
|
// rejected here keeps the parse result of this fix identical to the
|
||||||
&& cp != 0xA
|
// previous release; accepting them would be a separate behaviour
|
||||||
&& cp != 0xD
|
// change (see #1093).
|
||||||
) || !(
|
if (Character.isISOControl(cp)) {
|
||||||
// valid the range of acceptable characters that aren't control
|
return cp == 0x9 || cp == 0xA || cp == 0xD;
|
||||||
(cp >= 0x20 && cp <= 0xD7FF)
|
}
|
||||||
|| (cp >= 0xE000 && cp <= 0xFFFD)
|
return (cp >= 0x20 && cp <= 0xD7FF)
|
||||||
|| (cp >= 0x10000 && cp <= 0x10FFFF)
|
|| (cp >= 0xE000 && cp <= 0xFFFD)
|
||||||
)
|
|| (cp >= 0x10000 && cp <= 0x10FFFF);
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -232,6 +249,28 @@ public class XML {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throw an exception if the string contains an XML metacharacter
|
||||||
|
* ({@code < > & " ' /}). Used by {@link #toString(Object)} to reject JSON
|
||||||
|
* keys that would otherwise be emitted verbatim between {@code <} and
|
||||||
|
* {@code >} and could break out of the tag context (element injection,
|
||||||
|
* CWE-91; see issue #1071).
|
||||||
|
*
|
||||||
|
* @param string the candidate element name
|
||||||
|
* @throws JSONException if {@code string} contains an XML metacharacter
|
||||||
|
*/
|
||||||
|
static void noXmlMetachars(String string) throws JSONException {
|
||||||
|
int length = string.length();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
char c = string.charAt(i);
|
||||||
|
if (c == '<' || c == '>' || c == '&'
|
||||||
|
|| c == '"' || c == '\'' || c == '/') {
|
||||||
|
throw new JSONException("'" + string
|
||||||
|
+ "' contains an XML metacharacter and may not be used as an element name.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scan the content following the named tag, attaching it to the context.
|
* Scan the content following the named tag, attaching it to the context.
|
||||||
*
|
*
|
||||||
@@ -968,6 +1007,10 @@ public class XML {
|
|||||||
JSONObject jo;
|
JSONObject jo;
|
||||||
String string;
|
String string;
|
||||||
|
|
||||||
|
if (tagName != null) {
|
||||||
|
noXmlMetachars(tagName);
|
||||||
|
}
|
||||||
|
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
|
|
||||||
// Emit <tagName>
|
// Emit <tagName>
|
||||||
@@ -986,6 +1029,9 @@ public class XML {
|
|||||||
// don't use the new entrySet accessor to maintain Android Support
|
// don't use the new entrySet accessor to maintain Android Support
|
||||||
jo = (JSONObject) object;
|
jo = (JSONObject) object;
|
||||||
for (final String key : jo.keySet()) {
|
for (final String key : jo.keySet()) {
|
||||||
|
if (!key.equals(config.getcDataTagName())) {
|
||||||
|
noXmlMetachars(key);
|
||||||
|
}
|
||||||
Object value = jo.opt(key);
|
Object value = jo.opt(key);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
value = "";
|
value = "";
|
||||||
|
|||||||
@@ -167,7 +167,11 @@ public class XMLTokener extends JSONTokener {
|
|||||||
int cp = (e.charAt(1) == 'x' || e.charAt(1) == 'X')
|
int cp = (e.charAt(1) == 'x' || e.charAt(1) == 'X')
|
||||||
? parseHexEntity(e)
|
? parseHexEntity(e)
|
||||||
: parseDecimalEntity(e);
|
: parseDecimalEntity(e);
|
||||||
if (XML.mustEscape(cp)) {
|
// Validate with isValidXmlCharacter, not with XML.mustEscape:
|
||||||
|
// mustEscape also escapes TAB/LF/CR for the serialization path
|
||||||
|
// (#1092), while the numeric references 	, and are
|
||||||
|
// valid XML and must keep being accepted here (see #1059).
|
||||||
|
if (!XML.isValidXmlCharacter(cp)) {
|
||||||
throw new JSONException("Invalid numeric character reference: &#" + e.substring(1) + ";");
|
throw new JSONException("Invalid numeric character reference: &#" + e.substring(1) + ";");
|
||||||
}
|
}
|
||||||
return new String(new int[] {cp}, 0, 1);
|
return new String(new int[] {cp}, 0, 1);
|
||||||
|
|||||||
@@ -1566,4 +1566,56 @@ public class JSONArrayTest {
|
|||||||
"[1,null,3]", jsonArray.toString());
|
"[1,null,3]", jsonArray.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
|
||||||
|
JSONParserConfiguration strict =
|
||||||
|
new JSONParserConfiguration().withStrictMode();
|
||||||
|
|
||||||
|
JSONTokener tok = new JSONTokener("[]xxx");
|
||||||
|
tok.next();
|
||||||
|
tok.back();
|
||||||
|
|
||||||
|
JSONException exception = assertThrows(
|
||||||
|
JSONException.class,
|
||||||
|
() -> new JSONArray(tok, strict));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(
|
||||||
|
"Unparsed characters found at end of input text"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void strictModeShouldCheckTrailingCharactersAfterConsumedWhitespace() {
|
||||||
|
JSONParserConfiguration strict =
|
||||||
|
new JSONParserConfiguration().withStrictMode();
|
||||||
|
|
||||||
|
JSONTokener tok = new JSONTokener(" []xxx");
|
||||||
|
tok.next();
|
||||||
|
tok.next();
|
||||||
|
tok.back();
|
||||||
|
|
||||||
|
JSONException exception = assertThrows(
|
||||||
|
JSONException.class,
|
||||||
|
() -> new JSONArray(tok, strict));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(
|
||||||
|
"Unparsed characters found at end of input text"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void strictModeShouldCheckTrailingCharactersAfterNextCleanAndBack() {
|
||||||
|
JSONParserConfiguration strict =
|
||||||
|
new JSONParserConfiguration().withStrictMode();
|
||||||
|
|
||||||
|
JSONTokener tok = new JSONTokener(" []xxx");
|
||||||
|
tok.nextClean();
|
||||||
|
tok.back();
|
||||||
|
|
||||||
|
JSONException exception = assertThrows(
|
||||||
|
JSONException.class,
|
||||||
|
() -> new JSONArray(tok, strict));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(
|
||||||
|
"Unparsed characters found at end of input text"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -724,6 +724,98 @@ public class JSONMLTest {
|
|||||||
assertNotEquals(revertedXml, originalXml);
|
assertNotEquals(revertedXml, originalXml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entity-looking text is decoded once, including when strings are kept.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldDecodeEntitiesOnceWhenKeepingStringsInJSONArray() {
|
||||||
|
final String[][] cases = {
|
||||||
|
{"&lt;", "<"},
|
||||||
|
{"&amp;", "&"},
|
||||||
|
{"&#65;", "A"},
|
||||||
|
{"&lt;", "<"},
|
||||||
|
{"<>&"'AB", "<>&\"'AB"}
|
||||||
|
};
|
||||||
|
for (String[] testCase : cases) {
|
||||||
|
final String xml = "<p title=\"" + testCase[0] + "\">" + testCase[0] + "</p>";
|
||||||
|
final JSONArray json = JSONML.toJSONArray(xml, true);
|
||||||
|
assertEquals(xml, testCase[1], json.getJSONObject(1).get("title"));
|
||||||
|
assertEquals(xml, testCase[1], json.get(2));
|
||||||
|
assertEquals(xml, testCase[1], JSONML.toJSONArray(xml, false).get(2));
|
||||||
|
assertTrue(xml, json.similar(JSONML.toJSONArray(new XMLTokener(JSONML.toString(json)),
|
||||||
|
JSONMLParserConfiguration.KEEP_STRINGS)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object form preserves the same decoded attribute and text values.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldDecodeEntitiesOnceWhenKeepingStringsInJSONObject() {
|
||||||
|
final String xml = "<p title=\"&lt;\">&lt;</p>";
|
||||||
|
final JSONObject json = JSONML.toJSONObject(xml,
|
||||||
|
JSONMLParserConfiguration.ORIGINAL.withKeepStrings(true));
|
||||||
|
assertEquals("<", json.get("title"));
|
||||||
|
assertEquals("<", json.getJSONArray("childNodes").get(0));
|
||||||
|
assertEquals("<", JSONML.toJSONObject(xml).getJSONArray("childNodes").get(0));
|
||||||
|
assertTrue(json.similar(JSONML.toJSONObject(new XMLTokener(JSONML.toString(json)), true)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keeping strings disables type conversion without disabling entity decoding.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldPreserveValueTypesWhenKeepingStrings() {
|
||||||
|
final String[] values = {"42", "true", "false", "null"};
|
||||||
|
final String[] content = {"42", "true", "false", "null"};
|
||||||
|
final Object[] typedValues = {Integer.valueOf(42), Boolean.TRUE, Boolean.FALSE, JSONObject.NULL};
|
||||||
|
for (int i = 0; i < values.length; i++) {
|
||||||
|
final String xml = "<p value=\"" + values[i] + "\">" + content[i] + "</p>";
|
||||||
|
final JSONArray array = JSONML.toJSONArray(new XMLTokener(xml), true);
|
||||||
|
final JSONObject object = JSONML.toJSONObject(xml, true);
|
||||||
|
assertEquals(values[i], array.getJSONObject(1).get("value"));
|
||||||
|
assertEquals(values[i], array.get(2));
|
||||||
|
assertEquals(values[i], object.get("value"));
|
||||||
|
assertEquals(values[i], object.getJSONArray("childNodes").get(0));
|
||||||
|
final JSONArray typedArray = JSONML.toJSONArray(xml);
|
||||||
|
final JSONObject typedObject = JSONML.toJSONObject(xml);
|
||||||
|
assertEquals(typedValues[i], typedArray.getJSONObject(1).get("value"));
|
||||||
|
assertEquals(typedValues[i], typedArray.get(2));
|
||||||
|
assertEquals(typedValues[i], typedObject.get("value"));
|
||||||
|
assertEquals(typedValues[i], typedObject.getJSONArray("childNodes").get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CDATA entity syntax remains literal content.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldPreserveCdataWhenKeepingStrings() {
|
||||||
|
final String content = "< A & 42 true null";
|
||||||
|
final String xml = "<p><![CDATA[" + content + "]]></p>";
|
||||||
|
assertEquals(content, JSONML.toJSONArray(xml, true).get(1));
|
||||||
|
assertEquals(content, JSONML.toJSONObject(xml, true).getJSONArray("childNodes").get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escaped invalid character references are text, while raw references remain invalid.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldPreserveEscapedInvalidCharacterReferences() {
|
||||||
|
final String[] references = {"#0", "#x110000"};
|
||||||
|
for (String reference : references) {
|
||||||
|
final String expected = "&" + reference + ";";
|
||||||
|
final String xml = "<p>&" + reference + ";</p>";
|
||||||
|
assertEquals(expected, JSONML.toJSONArray(xml, true).get(1));
|
||||||
|
assertEquals(expected, JSONML.toJSONObject(xml, true).getJSONArray("childNodes").get(0));
|
||||||
|
assertEquals(expected, JSONML.toJSONArray(xml).get(1));
|
||||||
|
assertEquals(expected, JSONML.toJSONObject(xml).getJSONArray("childNodes").get(0));
|
||||||
|
final String invalidXml = "<p>" + expected + "</p>";
|
||||||
|
assertThrows(JSONException.class, () -> JSONML.toJSONArray(invalidXml, true));
|
||||||
|
assertThrows(JSONException.class, () -> JSONML.toJSONObject(invalidXml, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON string cannot be reverted to original xml when type guessing is used.
|
* JSON string cannot be reverted to original xml when type guessing is used.
|
||||||
* When we force all the values as string, the original text comes back.
|
* When we force all the values as string, the original text comes back.
|
||||||
@@ -1052,4 +1144,61 @@ public class JSONMLTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that the childNodes attribute is reserved in object-form parsing.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testToJSONObjectRejectsReservedChildNodesAttribute() {
|
||||||
|
String xmlStr = "<p childNodes=\"metadata\">text</p>";
|
||||||
|
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||||
|
JSONException exception = assertThrows(JSONException.class,
|
||||||
|
() -> JSONML.toJSONObject(xmlStr, keepStrings));
|
||||||
|
assertTrue(exception.getMessage().startsWith("Reserved attribute."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that the ordinary childNode attribute is preserved in object-form parsing.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testToJSONObjectPreservesChildNodeAttribute() {
|
||||||
|
String xmlStr = "<p childNode=\"metadata\">text</p>";
|
||||||
|
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||||
|
JSONObject jsonObject = JSONML.toJSONObject(xmlStr, keepStrings);
|
||||||
|
assertEquals("p", jsonObject.getString("tagName"));
|
||||||
|
assertEquals("metadata", jsonObject.getString("childNode"));
|
||||||
|
assertEquals("text", jsonObject.getJSONArray("childNodes").getString(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that the tagName attribute remains reserved in object-form parsing.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testToJSONObjectRejectsReservedTagNameAttribute() {
|
||||||
|
String xmlStr = "<p tagName=\"metadata\">text</p>";
|
||||||
|
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||||
|
JSONException exception = assertThrows(JSONException.class,
|
||||||
|
() -> JSONML.toJSONObject(xmlStr, keepStrings));
|
||||||
|
assertTrue(exception.getMessage().startsWith("Reserved attribute."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that childNode and childNodes attributes are preserved in array-form parsing.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testToJSONArrayPreservesChildNodeAttributes() {
|
||||||
|
String xmlStr = "<p childNode=\"singular\" childNodes=\"plural\">text</p>";
|
||||||
|
for (boolean keepStrings : new boolean[] {false, true}) {
|
||||||
|
JSONArray jsonArray = JSONML.toJSONArray(xmlStr, keepStrings);
|
||||||
|
assertEquals(3, jsonArray.length());
|
||||||
|
assertEquals("p", jsonArray.getString(0));
|
||||||
|
JSONObject attributes = jsonArray.getJSONObject(1);
|
||||||
|
assertEquals("singular", attributes.getString("childNode"));
|
||||||
|
assertEquals("plural", attributes.getString("childNodes"));
|
||||||
|
assertEquals("text", jsonArray.getString(2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ import org.json.junit.data.CustomClassG;
|
|||||||
import org.json.junit.data.CustomClassH;
|
import org.json.junit.data.CustomClassH;
|
||||||
import org.json.junit.data.CustomClassI;
|
import org.json.junit.data.CustomClassI;
|
||||||
import org.json.junit.data.CustomClassJ;
|
import org.json.junit.data.CustomClassJ;
|
||||||
|
import org.json.junit.data.FromJsonKeyBindingExample;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -3883,6 +3884,31 @@ public class JSONObjectTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pins down the field binding done by {@code fromJson(Class)}: every non-static
|
||||||
|
* declared field whose name matches a JSON key is written, no matter how the
|
||||||
|
* field is declared. Private fields, transient fields and fields hidden behind
|
||||||
|
* a {@code @JSONPropertyIgnore} getter are all populated, because that
|
||||||
|
* annotation only affects the bean to JSONObject direction. See issue #1076.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void jsonObjectParseFromJson_10() {
|
||||||
|
JSONObject object = new JSONObject()
|
||||||
|
.put("displayName", "bob")
|
||||||
|
.put("privateBoolean", true)
|
||||||
|
.put("privateString", "private-value")
|
||||||
|
.put("transientString", "transient-value")
|
||||||
|
.put("ignoredGetterBoolean", true);
|
||||||
|
|
||||||
|
FromJsonKeyBindingExample bound = object.fromJson(FromJsonKeyBindingExample.class);
|
||||||
|
|
||||||
|
assertEquals("private field should be populated", true, bound.isPrivateBoolean());
|
||||||
|
assertEquals("private field should be populated", "private-value", bound.getPrivateString());
|
||||||
|
assertEquals("transient field should be populated", "transient-value", bound.getTransientString());
|
||||||
|
assertEquals("field behind a @JSONPropertyIgnore getter should be populated", true,
|
||||||
|
bound.isIgnoredGetterBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMaxNumberLength() {
|
public void testMaxNumberLength() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||||
@@ -4337,4 +4363,55 @@ public class JSONObjectTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
|
||||||
|
JSONParserConfiguration strict =
|
||||||
|
new JSONParserConfiguration().withStrictMode();
|
||||||
|
|
||||||
|
JSONTokener tok = new JSONTokener("{}xxx");
|
||||||
|
tok.next();
|
||||||
|
tok.back();
|
||||||
|
|
||||||
|
JSONException exception = assertThrows(
|
||||||
|
JSONException.class,
|
||||||
|
() -> new JSONObject(tok, strict));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(
|
||||||
|
"Unparsed characters found at end of input text"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void strictModeShouldCheckTrailingCharactersAfterConsumedWhitespace() {
|
||||||
|
JSONParserConfiguration strict =
|
||||||
|
new JSONParserConfiguration().withStrictMode();
|
||||||
|
|
||||||
|
JSONTokener tok = new JSONTokener(" {}xxx");
|
||||||
|
tok.next();
|
||||||
|
tok.next();
|
||||||
|
tok.back();
|
||||||
|
|
||||||
|
JSONException exception = assertThrows(
|
||||||
|
JSONException.class,
|
||||||
|
() -> new JSONObject(tok, strict));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(
|
||||||
|
"Unparsed characters found at end of input text"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void strictModeShouldCheckTrailingCharactersAfterNextCleanAndBack() {
|
||||||
|
JSONParserConfiguration strict =
|
||||||
|
new JSONParserConfiguration().withStrictMode();
|
||||||
|
|
||||||
|
JSONTokener tok = new JSONTokener(" {}xxx");
|
||||||
|
tok.nextClean();
|
||||||
|
tok.back();
|
||||||
|
|
||||||
|
JSONException exception = assertThrows(
|
||||||
|
JSONException.class,
|
||||||
|
() -> new JSONObject(tok, strict));
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(
|
||||||
|
"Unparsed characters found at end of input text"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,11 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotSame;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
import static org.junit.Assert.assertThrows;
|
import static org.junit.Assert.assertThrows;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public class JSONParserConfigurationTest {
|
public class JSONParserConfigurationTest {
|
||||||
@@ -36,6 +39,92 @@ public class JSONParserConfigurationTest {
|
|||||||
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void objectConstructorAppliesExplicitStrictModeToValues() {
|
||||||
|
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
|
||||||
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
for (String source : Arrays.asList(
|
||||||
|
"[]",
|
||||||
|
"{",
|
||||||
|
"{} trailing",
|
||||||
|
"{\"value\":'text'}",
|
||||||
|
"{\"value\":[1,]}",
|
||||||
|
"{\"value\":{\"nested\":1,}}")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, lenient);
|
||||||
|
assertThrows(JSONException.class, () -> new JSONObject(tokener, configuration));
|
||||||
|
assertSame(lenient, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void arrayConstructorAppliesExplicitStrictModeToValues() {
|
||||||
|
JSONParserConfiguration configuration = new JSONParserConfiguration().withStrictMode();
|
||||||
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
for (String source : Arrays.asList(
|
||||||
|
"{}",
|
||||||
|
"[",
|
||||||
|
"[] trailing",
|
||||||
|
"['text']",
|
||||||
|
"[[1,]]",
|
||||||
|
"[{\"nested\":1,}]")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, lenient);
|
||||||
|
assertThrows(JSONException.class, () -> new JSONArray(tokener, configuration));
|
||||||
|
assertSame(lenient, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void explicitConfigurationOverridesTokenerStrictMode() {
|
||||||
|
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
||||||
|
JSONParserConfiguration lenient = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
JSONTokener objectTokener = new JSONTokener("{\"value\":[{'nested':'text',}], 'other':'text'} 'next'", strict);
|
||||||
|
JSONTokener arrayTokener = new JSONTokener("[{\"value\":['text',]},'text'] 'next'", strict);
|
||||||
|
|
||||||
|
JSONObject object = new JSONObject(objectTokener, lenient);
|
||||||
|
JSONArray array = new JSONArray(arrayTokener, lenient);
|
||||||
|
|
||||||
|
assertEquals("text", object.getJSONArray("value").getJSONObject(0).getString("nested"));
|
||||||
|
assertEquals("text", array.getJSONObject(0).getJSONArray("value").getString(0));
|
||||||
|
assertEquals("text", object.getString("other"));
|
||||||
|
assertEquals("text", array.getString(1));
|
||||||
|
assertSame(strict, objectTokener.getJsonParserConfiguration());
|
||||||
|
assertSame(strict, arrayTokener.getJsonParserConfiguration());
|
||||||
|
assertThrows(JSONException.class, () -> objectTokener.nextValue());
|
||||||
|
assertThrows(JSONException.class, () -> arrayTokener.nextValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void objectConstructorRestoresTokenerConfiguration() {
|
||||||
|
JSONParserConfiguration original = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
||||||
|
for (String source : Arrays.asList("{}", "{\"value\":[{\"nested\":\"text\"}],\"other\":true}")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, original);
|
||||||
|
new JSONObject(tokener, strict);
|
||||||
|
assertSame(original, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void arrayConstructorRestoresTokenerConfiguration() {
|
||||||
|
JSONParserConfiguration original = new JSONParserConfiguration().withStrictMode(false);
|
||||||
|
JSONParserConfiguration strict = new JSONParserConfiguration().withStrictMode();
|
||||||
|
for (String source : Arrays.asList("[]", "[{\"value\":[\"text\"]},true]")) {
|
||||||
|
JSONTokener tokener = new JSONTokener(source, original);
|
||||||
|
new JSONArray(tokener, strict);
|
||||||
|
assertSame(original, tokener.getJsonParserConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void explicitConfigurationAppliesToNestedDuplicateKeys() {
|
||||||
|
JSONParserConfiguration configuration = new JSONParserConfiguration().withOverwriteDuplicateKey(true);
|
||||||
|
JSONObject object = new JSONObject(new JSONTokener("{\"value\":[{\"key\":1,\"key\":2}]}"), configuration);
|
||||||
|
JSONArray array = new JSONArray(new JSONTokener("[{\"value\":{\"key\":1,\"key\":2}}]"), configuration);
|
||||||
|
|
||||||
|
assertEquals(2, object.getJSONArray("value").getJSONObject(0).getInt("key"));
|
||||||
|
assertEquals(2, array.getJSONObject(0).getJSONObject("value").getInt("key"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void strictModeIsCloned(){
|
public void strictModeIsCloned(){
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
@@ -621,4 +710,140 @@ public class JSONParserConfigurationTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String SHARED_KEYS_SOURCE = "{\"rows\": ["
|
||||||
|
+ "{\"name\": \"a\", \"count\": 1, \"big\": 12345678901, \"ratio\": 1.5, \"flag\": true, \"missing\": null,"
|
||||||
|
+ " \"nested\": {\"name\": \"inner\", \"list\": [1, \"name\", null]}},"
|
||||||
|
+ "{\"name\": \"name\", \"count\": 2, \"nested\": {\"name\": \"inner2\", \"list\": []}},"
|
||||||
|
+ "{\"name\": \"third\", \"count\": -3}"
|
||||||
|
+ "]}";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMaxSharedKeysConfig() {
|
||||||
|
JSONParserConfiguration config = new JSONParserConfiguration();
|
||||||
|
assertEquals(JSONParserConfiguration.DEFAULT_MAXIMUM_SHARED_KEYS, config.getMaxSharedKeys());
|
||||||
|
assertEquals(1000, config.getMaxSharedKeys());
|
||||||
|
assertEquals(0, config.withMaxSharedKeys(0).getMaxSharedKeys());
|
||||||
|
assertEquals(JSONParserConfiguration.UNLIMITED_SHARED_KEYS, config.withMaxSharedKeys(-1).getMaxSharedKeys());
|
||||||
|
assertEquals(JSONParserConfiguration.UNLIMITED_SHARED_KEYS, config.withMaxSharedKeys(-5).getMaxSharedKeys());
|
||||||
|
|
||||||
|
JSONParserConfiguration chained = config.withMaxSharedKeys(7).withOverwriteDuplicateKey(true).withStrictMode(false);
|
||||||
|
assertEquals(7, chained.getMaxSharedKeys());
|
||||||
|
assertTrue(chained.isOverwriteDuplicateKey());
|
||||||
|
assertEquals(1000, config.getMaxSharedKeys());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysByDefault() {
|
||||||
|
JSONArray rows = new JSONObject(SHARED_KEYS_SOURCE).getJSONArray("rows");
|
||||||
|
String nameKey = key(rows.getJSONObject(0), "name");
|
||||||
|
for (int i = 1; i < rows.length(); i++) {
|
||||||
|
assertSame(nameKey, key(rows.getJSONObject(i), "name"));
|
||||||
|
}
|
||||||
|
assertSame(nameKey, key(rows.getJSONObject(0).getJSONObject("nested"), "name"));
|
||||||
|
assertSame(key(rows.getJSONObject(0), "count"), key(rows.getJSONObject(2), "count"));
|
||||||
|
assertSame(key(rows.getJSONObject(0).getJSONObject("nested"), "list"),
|
||||||
|
key(rows.getJSONObject(1).getJSONObject("nested"), "list"));
|
||||||
|
|
||||||
|
// string values equal to a key are not shared
|
||||||
|
assertNotSame(nameKey, rows.getJSONObject(1).get("name"));
|
||||||
|
assertNotSame(nameKey, rows.getJSONObject(0).getJSONObject("nested").getJSONArray("list").get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysDisabled() {
|
||||||
|
JSONArray rows = new JSONObject(SHARED_KEYS_SOURCE, new JSONParserConfiguration().withMaxSharedKeys(0))
|
||||||
|
.getJSONArray("rows");
|
||||||
|
assertNotSame(key(rows.getJSONObject(0), "name"), key(rows.getJSONObject(1), "name"));
|
||||||
|
assertNotSame(key(rows.getJSONObject(0), "count"), key(rows.getJSONObject(1), "count"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysLimit() {
|
||||||
|
JSONArray rows = new JSONArray("[{\"a\": 1, \"b\": 2, \"c\": 3}, {\"a\": 4, \"b\": 5, \"c\": 6}]",
|
||||||
|
new JSONParserConfiguration().withMaxSharedKeys(2));
|
||||||
|
assertSame(key(rows.getJSONObject(0), "a"), key(rows.getJSONObject(1), "a"));
|
||||||
|
assertSame(key(rows.getJSONObject(0), "b"), key(rows.getJSONObject(1), "b"));
|
||||||
|
assertNotSame(key(rows.getJSONObject(0), "c"), key(rows.getJSONObject(1), "c"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysUnlimited() {
|
||||||
|
int keyCount = JSONParserConfiguration.DEFAULT_MAXIMUM_SHARED_KEYS + 500;
|
||||||
|
StringBuilder row = new StringBuilder("{");
|
||||||
|
for (int i = 0; i < keyCount; i++) {
|
||||||
|
row.append(i == 0 ? "" : ",").append("\"k").append(i).append("\":").append(i);
|
||||||
|
}
|
||||||
|
row.append('}');
|
||||||
|
String source = "[" + row + "," + row + "]";
|
||||||
|
|
||||||
|
JSONArray unlimited = new JSONArray(source,
|
||||||
|
new JSONParserConfiguration().withMaxSharedKeys(JSONParserConfiguration.UNLIMITED_SHARED_KEYS));
|
||||||
|
for (int i = 0; i < keyCount; i++) {
|
||||||
|
assertSame(key(unlimited.getJSONObject(0), "k" + i), key(unlimited.getJSONObject(1), "k" + i));
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONArray limited = new JSONArray(source);
|
||||||
|
String lastKey = "k" + (keyCount - 1);
|
||||||
|
assertSame(key(limited.getJSONObject(0), "k0"), key(limited.getJSONObject(1), "k0"));
|
||||||
|
assertNotSame(key(limited.getJSONObject(0), lastKey), key(limited.getJSONObject(1), lastKey));
|
||||||
|
assertTrue(unlimited.similar(limited));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysNotSharedAcrossParses() {
|
||||||
|
JSONObject first = new JSONObject("{\"name\": 1}");
|
||||||
|
JSONObject second = new JSONObject("{\"name\": 2}");
|
||||||
|
assertNotSame(key(first, "name"), key(second, "name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysParseUnchanged() {
|
||||||
|
JSONObject unshared = new JSONObject(SHARED_KEYS_SOURCE, new JSONParserConfiguration().withMaxSharedKeys(0));
|
||||||
|
JSONObject shared = new JSONObject(SHARED_KEYS_SOURCE);
|
||||||
|
JSONObject unlimited = new JSONObject(SHARED_KEYS_SOURCE,
|
||||||
|
new JSONParserConfiguration().withMaxSharedKeys(JSONParserConfiguration.UNLIMITED_SHARED_KEYS));
|
||||||
|
assertTrue(unshared.similar(shared));
|
||||||
|
assertTrue(unshared.similar(unlimited));
|
||||||
|
assertEquals(unshared.toString(), shared.toString());
|
||||||
|
assertEquals(unshared.toString(), unlimited.toString());
|
||||||
|
|
||||||
|
JSONObject row = shared.getJSONArray("rows").getJSONObject(0);
|
||||||
|
assertEquals(Integer.class, row.get("count").getClass());
|
||||||
|
assertEquals(Long.class, row.get("big").getClass());
|
||||||
|
assertEquals(java.math.BigDecimal.class, row.get("ratio").getClass());
|
||||||
|
assertEquals(Boolean.TRUE, row.get("flag"));
|
||||||
|
assertSame(JSONObject.NULL, row.get("missing"));
|
||||||
|
assertSame(JSONObject.NULL, row.getJSONObject("nested").getJSONArray("list").get(2));
|
||||||
|
assertEquals("third", shared.getJSONArray("rows").getJSONObject(2).get("name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysLenientSyntax() {
|
||||||
|
JSONArray rows = new JSONArray("[{\"name\": 1, \"count\": 2}, {'name': 'single', count: 3}]",
|
||||||
|
new JSONParserConfiguration().withStrictMode(false));
|
||||||
|
assertSame(key(rows.getJSONObject(0), "name"), key(rows.getJSONObject(1), "name"));
|
||||||
|
assertSame(key(rows.getJSONObject(0), "count"), key(rows.getJSONObject(1), "count"));
|
||||||
|
assertEquals("single", rows.getJSONObject(1).get("name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSharedKeysStrictMode() {
|
||||||
|
JSONArray rows = new JSONArray("[{\"a\": 1}, {\"a\": 2}]", new JSONParserConfiguration().withStrictMode());
|
||||||
|
assertSame(key(rows.getJSONObject(0), "a"), key(rows.getJSONObject(1), "a"));
|
||||||
|
assertFalse(rows.getJSONObject(0).similar(rows.getJSONObject(1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the key instance a JSONObject holds, rather than the caller's equal String.
|
||||||
|
*/
|
||||||
|
private static String key(JSONObject jsonObject, String name) {
|
||||||
|
for (String key : jsonObject.keySet()) {
|
||||||
|
if (key.equals(name)) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fail("missing key " + name);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,6 +98,50 @@ public class JSONPointerTest {
|
|||||||
assertEquals(8, query("/m~0n"));
|
assertEquals(8, query("/m~0n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void escapeSequencesInObjectKeys() {
|
||||||
|
JSONObject object = new JSONObject().put("~1", "tilde one").put("/", "slash")
|
||||||
|
.put("~0", "tilde zero").put("~", "tilde");
|
||||||
|
assertEquals("tilde one", object.query("/~01"));
|
||||||
|
assertEquals("tilde zero", object.query("/~00"));
|
||||||
|
assertEquals("tilde one", object.query("#/~01"));
|
||||||
|
assertEquals("tilde zero", object.query("#/~00"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void builderPreservesLiteralTokens() {
|
||||||
|
JSONObject object = new JSONObject().put("~1", "tilde one").put("/", "slash")
|
||||||
|
.put("~0", "tilde zero").put("~", "tilde");
|
||||||
|
assertEquals("tilde one", JSONPointer.builder().append("~1").build().queryFrom(object));
|
||||||
|
assertEquals("tilde zero", JSONPointer.builder().append("~0").build().queryFrom(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tokenListPreservesLiteralTokens() {
|
||||||
|
JSONObject object = new JSONObject().put("~1", "tilde one").put("/", "slash")
|
||||||
|
.put("~0", "tilde zero").put("~", "tilde");
|
||||||
|
assertEquals("tilde one", new JSONPointer(java.util.Arrays.asList("~1")).queryFrom(object));
|
||||||
|
assertEquals("tilde zero", new JSONPointer(java.util.Arrays.asList("~0")).queryFrom(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void escapedTildeTokensSelectLiteralKeys() {
|
||||||
|
JSONObject object = new JSONObject().put("~1", "literal ~1").put("/", "slash")
|
||||||
|
.put("~0", "literal ~0").put("~", "tilde")
|
||||||
|
.put("a~1b", "literal a~1b").put("a/b", "a slash b")
|
||||||
|
.put("a~0b", "literal a~0b").put("a~b", "a tilde b");
|
||||||
|
|
||||||
|
assertEquals("literal ~1", object.query("/~01"));
|
||||||
|
assertEquals("literal ~1", object.query("#/~01"));
|
||||||
|
assertEquals("literal ~0", object.query("/~00"));
|
||||||
|
assertEquals("literal ~0", object.query("#/~00"));
|
||||||
|
assertEquals("literal a~1b", object.query("/a~01b"));
|
||||||
|
assertEquals("literal a~0b", object.query("/a~00b"));
|
||||||
|
assertEquals("literal ~1", JSONPointer.builder().append("~1").build().queryFrom(object));
|
||||||
|
assertEquals("literal ~0", JSONPointer.builder().append("~0").build().queryFrom(object));
|
||||||
|
assertEquals("literal ~1", new JSONPointer(java.util.Arrays.asList("~1")).queryFrom(object));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We pass backslashes as-is
|
* We pass backslashes as-is
|
||||||
*
|
*
|
||||||
@@ -141,6 +185,41 @@ public class JSONPointerTest {
|
|||||||
assertEquals(8, query("#/m~0n"));
|
assertEquals(8, query("#/m~0n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uriFragmentPreservesLiteralPlus() {
|
||||||
|
JSONObject object = new JSONObject().put("a+b", "plus").put("a b", "space")
|
||||||
|
.put("a%2Bb", "percent");
|
||||||
|
assertEquals("plus", object.query("#/a+b"));
|
||||||
|
assertEquals("plus", object.query("#/a%2Bb"));
|
||||||
|
assertEquals("space", object.query("#/a%20b"));
|
||||||
|
assertEquals("percent", object.query("#/a%252Bb"));
|
||||||
|
assertEquals("plus", object.query("/a+b"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uriFragmentEncodesSpaces() {
|
||||||
|
JSONObject object = new JSONObject().put("a+b", "plus").put("a b", "space");
|
||||||
|
JSONPointer plus = JSONPointer.builder().append("a+b").build();
|
||||||
|
JSONPointer space = JSONPointer.builder().append("a b").build();
|
||||||
|
assertEquals("#/a%2Bb", plus.toURIFragment());
|
||||||
|
assertEquals("#/a%20b", space.toURIFragment());
|
||||||
|
assertEquals("#/%20", new JSONPointer("/ ").toURIFragment());
|
||||||
|
assertEquals("plus", object.query(plus.toURIFragment()));
|
||||||
|
assertEquals("space", object.query(space.toURIFragment()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uriFragmentPreservesUtf8AndPointerEscapes() {
|
||||||
|
String key = "\u4e2d +/~1";
|
||||||
|
JSONObject object = new JSONObject().put(key, "expected")
|
||||||
|
.put("\u4e2d /~1", "wrong space");
|
||||||
|
assertEquals("expected", object.query("#/%E4%B8%AD%20+~1~01"));
|
||||||
|
assertEquals("expected", object.query("#/%E4%B8%AD%20%2B%7E1%7E01"));
|
||||||
|
JSONPointer pointer = JSONPointer.builder().append(key).build();
|
||||||
|
assertEquals("#/%E4%B8%AD%20%2B%7E1%7E01", pointer.toURIFragment());
|
||||||
|
assertEquals("expected", object.query(pointer.toURIFragment()));
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void syntaxError() {
|
public void syntaxError() {
|
||||||
@@ -193,9 +272,29 @@ public class JSONPointerTest {
|
|||||||
assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment());
|
assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment());
|
||||||
assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment());
|
assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment());
|
||||||
assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment());
|
assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment());
|
||||||
assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment());
|
assertEquals("#/m%7E0n", new JSONPointer("/m~n").toURIFragment());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toURIFragmentEscaping() {
|
||||||
|
JSONPointer pointer = JSONPointer.builder()
|
||||||
|
.append("obj")
|
||||||
|
.append("other~key").append("another/key")
|
||||||
|
.append(0)
|
||||||
|
.build();
|
||||||
|
assertEquals("#/obj/other%7E0key/another%7E1key/0", pointer.toURIFragment());
|
||||||
|
assertEquals("val", new JSONPointer(pointer.toURIFragment()).queryFrom(document));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toURIFragmentRoundTrip() {
|
||||||
|
JSONObject object = new JSONObject().put("~1", "tilde one").put("/", "slash");
|
||||||
|
JSONPointer tildeOne = JSONPointer.builder().append("~1").build();
|
||||||
|
JSONPointer slash = JSONPointer.builder().append("/").build();
|
||||||
|
assertEquals("tilde one", object.query(tildeOne.toURIFragment()));
|
||||||
|
assertEquals("slash", object.query(slash.toURIFragment()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tokenListIsCopiedInConstructor() {
|
public void tokenListIsCopiedInConstructor() {
|
||||||
JSONPointer.Builder b = JSONPointer.builder().append("key1");
|
JSONPointer.Builder b = JSONPointer.builder().append("key1");
|
||||||
|
|||||||
@@ -17,6 +17,33 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class PropertyTest {
|
public class PropertyTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldIncludeInheritedPropertiesWithoutLocalEntries() {
|
||||||
|
Properties defaults = new Properties();
|
||||||
|
defaults.setProperty("timeout", "30");
|
||||||
|
Properties properties = new Properties(new Properties(defaults));
|
||||||
|
|
||||||
|
JSONObject jsonObject = Property.toJSONObject(properties);
|
||||||
|
|
||||||
|
assertEquals(1, jsonObject.length());
|
||||||
|
assertEquals("30", jsonObject.getString("timeout"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldPreferLocalPropertiesOverDefaults() {
|
||||||
|
Properties defaults = new Properties();
|
||||||
|
defaults.setProperty("timeout", "30");
|
||||||
|
defaults.setProperty("host", "localhost");
|
||||||
|
Properties properties = new Properties(defaults);
|
||||||
|
properties.setProperty("timeout", "60");
|
||||||
|
|
||||||
|
JSONObject jsonObject = Property.toJSONObject(properties);
|
||||||
|
|
||||||
|
assertEquals(2, jsonObject.length());
|
||||||
|
assertEquals("60", jsonObject.getString("timeout"));
|
||||||
|
assertEquals("localhost", jsonObject.getString("host"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSONObject from null properties object should
|
* JSONObject from null properties object should
|
||||||
* result in an empty JSONObject.
|
* result in an empty JSONObject.
|
||||||
@@ -99,4 +126,4 @@ public class PropertyTest {
|
|||||||
assertTrue("property objects should match",
|
assertTrue("property objects should match",
|
||||||
properties.equals(jsonProperties));
|
properties.equals(jsonProperties));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ Public Domain.
|
|||||||
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;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
@@ -565,6 +566,42 @@ public class XMLTest {
|
|||||||
assertTrue("Illegal@node",result.contains("<Illegal@node>someValue2</Illegal@node>"));
|
assertTrue("Illegal@node",result.contains("<Illegal@node>someValue2</Illegal@node>"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A JSON key containing XML metacharacters must not be emitted as a raw
|
||||||
|
* tag name, since doing so allows the key to break out of its element and
|
||||||
|
* inject sibling structure into the output (CWE-91, issue #1071).
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void toStringRejectsElementInjectionInKey()
|
||||||
|
{
|
||||||
|
JSONObject jo = new JSONObject(
|
||||||
|
"{\"a/><injected>evil</injected><a\":\"\"}");
|
||||||
|
try {
|
||||||
|
XML.toString(jo, "root");
|
||||||
|
fail("expected JSONException for key containing XML metacharacters");
|
||||||
|
} catch (JSONException expected) {
|
||||||
|
// expected: '/', '<', '>' are rejected in element names
|
||||||
|
}
|
||||||
|
|
||||||
|
// caller-supplied tagName is checked too
|
||||||
|
try {
|
||||||
|
XML.toString(new JSONObject(), "bad<tag");
|
||||||
|
fail("expected JSONException for tagName containing '<'");
|
||||||
|
} catch (JSONException expected) {
|
||||||
|
// expected: '<' is rejected in element names
|
||||||
|
}
|
||||||
|
|
||||||
|
// each metacharacter is rejected individually
|
||||||
|
for (char c : new char[] {'<', '>', '&', '"', '\'', '/'}) {
|
||||||
|
try {
|
||||||
|
XML.toString(new JSONObject().put("a" + c + "b", "v"));
|
||||||
|
fail("expected JSONException for key containing '" + c + "'");
|
||||||
|
} catch (JSONException expected) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSONObject with NULL value, to XML.toString()
|
* JSONObject with NULL value, to XML.toString()
|
||||||
*/
|
*/
|
||||||
@@ -1537,6 +1574,113 @@ public class XMLTest {
|
|||||||
assertEquals("A", jsonObject.getString("a"));
|
assertEquals("A", jsonObject.getString("a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that valid XML numeric character references for whitespace
|
||||||
|
* control characters (TAB, LF, CR) are correctly unescaped. These
|
||||||
|
* codepoints are explicitly allowed by the XML 1.0 spec
|
||||||
|
* (https://www.w3.org/TR/REC-xml/#charsets) but were previously rejected
|
||||||
|
* as invalid. See issue #1059.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testValidWhitespaceNumericEntityUnescape() {
|
||||||
|
// decimal references for the three allowed control characters
|
||||||
|
assertEquals("\t", XML.unescape("	"));
|
||||||
|
assertEquals("\n", XML.unescape(" "));
|
||||||
|
assertEquals("\r", XML.unescape(" "));
|
||||||
|
// hex references for the same codepoints
|
||||||
|
assertEquals("\t", XML.unescape("	"));
|
||||||
|
assertEquals("\n", XML.unescape("
"));
|
||||||
|
assertEquals("\r", XML.unescape("
"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that {@code XML.toJSONObject} accepts numeric character references
|
||||||
|
* for the XML-allowed control characters (TAB, LF, CR) without throwing.
|
||||||
|
* Regression test for #1059, where {@code XML.toJSONObject("<a> </a>")}
|
||||||
|
* threw JSONException in versions after 20251224.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testValidWhitespaceNumericEntityToJSONObject() {
|
||||||
|
// LF reference should round-trip through toJSONObject without throwing
|
||||||
|
JSONObject jsonObject = XML.toJSONObject("<a> </a>");
|
||||||
|
// the value is the LF character (possibly trimmed by the JSON path,
|
||||||
|
// but the call must not throw)
|
||||||
|
assertTrue(jsonObject.has("a"));
|
||||||
|
// TAB and CR references also accepted
|
||||||
|
XML.toJSONObject("<a>	</a>");
|
||||||
|
XML.toJSONObject("<a> </a>");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that numeric character references to C1 control characters are
|
||||||
|
* still rejected. #x7F-#x9F is inside the XML 1.0 {@code Char} range, but
|
||||||
|
* this library has always refused those references while parsing
|
||||||
|
* ({@code …} is NEL, #x85), and fixing the serialization of TAB/LF/CR
|
||||||
|
* must not change what the parser accepts.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testRejectsC1ControlCharacterReference() {
|
||||||
|
assertThrows(JSONException.class,
|
||||||
|
() -> XML.toJSONObject("<name>Joe … Tester</name>"));
|
||||||
|
// the neighbouring boundary codepoints behave the same way
|
||||||
|
assertThrows(JSONException.class,
|
||||||
|
() -> XML.toJSONObject("<name>Joe  Tester</name>"));
|
||||||
|
assertThrows(JSONException.class,
|
||||||
|
() -> XML.toJSONObject("<name>Joe Ÿ Tester</name>"));
|
||||||
|
// ... while the characters the parser does accept still work
|
||||||
|
XML.toJSONObject("<name>Joe   Tester</name>");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that serializing a string value containing literal TAB, LF and CR
|
||||||
|
* characters writes numeric character references (&#9;, &#10;,
|
||||||
|
* &#13;) instead of the literal characters. Literal occurrences of
|
||||||
|
* these characters are subject to whitespace normalization by conforming
|
||||||
|
* XML parsers, so the original value would be lost when the generated XML
|
||||||
|
* is read back. Regression test for #1092, where #1070 stopped
|
||||||
|
* XML.toString() and JSONML.toString() from escaping these characters.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testEscapesLiteralWhitespaceControlCharsOnSerialization() {
|
||||||
|
// LF in a text node must survive as a numeric character reference
|
||||||
|
JSONObject jo = new JSONObject().put("value", "Lettuce\nTomato");
|
||||||
|
String xmlOutput = XML.toString(jo);
|
||||||
|
assertTrue("XML should escape literal LF to preserve it, got: " + xmlOutput,
|
||||||
|
xmlOutput.contains("
"));
|
||||||
|
|
||||||
|
// TAB and CR likewise
|
||||||
|
JSONObject tabJo = new JSONObject().put("value", "a\tb\rc");
|
||||||
|
String tabXml = XML.toString(tabJo);
|
||||||
|
assertTrue("XML should escape literal TAB, got: " + tabXml, tabXml.contains("	"));
|
||||||
|
assertTrue("XML should escape literal CR, got: " + tabXml, tabXml.contains("
"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that the generated XML round-trips: the numeric character
|
||||||
|
* references produced for TAB/LF/CR are read back by
|
||||||
|
* {@code XML.toJSONObject} as the original characters.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testWhitespaceControlCharsRoundTripThroughXml() {
|
||||||
|
String original = "Lettuce\nTomato\tMustard";
|
||||||
|
JSONObject jo = new JSONObject().put("value", original);
|
||||||
|
String xmlOutput = XML.toString(jo);
|
||||||
|
JSONObject reparsed = XML.toJSONObject(xmlOutput);
|
||||||
|
assertEquals("LF/TAB must survive the XML round trip",
|
||||||
|
original, reparsed.getString("value"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same escaping requirement for {@code JSONML.toString}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testJSONMLToStringEscapesLiteralNewlines() {
|
||||||
|
JSONObject jo = new JSONObject("{\"tagName\":\"MyTag\", \"value\":\"Lettuce\\nTomato\"}");
|
||||||
|
String xmlOutput = JSONML.toString(jo);
|
||||||
|
assertTrue("JSONML should escape literal LF to preserve it, got: " + xmlOutput,
|
||||||
|
xmlOutput.contains("
"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.json.junit.data;
|
||||||
|
|
||||||
|
import org.json.JSONPropertyIgnore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bean used to pin down how {@code JSONObject#fromJson(Class)} binds JSON keys
|
||||||
|
* to fields: {@code privateString} and {@code privateBoolean} are private,
|
||||||
|
* {@code transientString} is transient, and {@code ignoredGetterBoolean} is
|
||||||
|
* hidden behind a getter annotated with {@code @JSONPropertyIgnore} (which only
|
||||||
|
* suppresses the bean to JSONObject direction).
|
||||||
|
*/
|
||||||
|
public class FromJsonKeyBindingExample {
|
||||||
|
public String displayName;
|
||||||
|
private boolean privateBoolean;
|
||||||
|
private String privateString;
|
||||||
|
private transient String transientString;
|
||||||
|
private boolean ignoredGetterBoolean;
|
||||||
|
|
||||||
|
public FromJsonKeyBindingExample() {
|
||||||
|
// Required for JSONObject#fromJson(Class<T>) tests.
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPrivateBoolean() {
|
||||||
|
return privateBoolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrivateString() {
|
||||||
|
return privateString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTransientString() {
|
||||||
|
return transientString;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JSONPropertyIgnore
|
||||||
|
public boolean isIgnoredGetterBoolean() {
|
||||||
|
return ignoredGetterBoolean;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user