├── .github └── workflows │ └── pr.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.md ├── NOTICE.md ├── README.md ├── bundles ├── dist │ ├── pom.xml │ └── src │ │ └── main │ │ ├── assembly │ │ └── archive.xml │ │ └── resources │ │ ├── LICENSE.md │ │ └── README.txt ├── jakarta.json │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── module-info.java │ │ └── javadoc │ │ ├── doc-files │ │ └── speclicense.html │ │ └── overview.html └── pom.xml ├── demos ├── LICENSE.md ├── NOTICE.md ├── customprovider-jdk9 │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ ├── module-info.java │ │ │ └── org │ │ │ └── eclipse │ │ │ └── parsson │ │ │ └── demos │ │ │ └── customprovider │ │ │ ├── TestGenerator.java │ │ │ └── TestProvider.java │ │ └── test │ │ └── java │ │ └── customprovider │ │ └── test │ │ └── TestProviderTest.java ├── facebook │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ ├── module-info.java │ │ └── org │ │ │ └── eclipse │ │ │ └── parsson │ │ │ └── demos │ │ │ └── facebook │ │ │ ├── FacebookObjectSearch.java │ │ │ └── FacebookStreamSearch.java │ │ └── resources │ │ └── facebookconfig.properties ├── jsonpointer │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ ├── module-info.java │ │ └── org │ │ │ └── eclipse │ │ │ └── parsson │ │ │ └── demos │ │ │ └── jsonpointer │ │ │ └── JsonpointerDemo.java │ │ └── resources │ │ ├── jsonpointer.json │ │ └── wiki.json ├── pom.xml ├── rest │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── eclipse │ │ └── parsson │ │ └── demos │ │ └── rest │ │ ├── ArrayResource.java │ │ ├── DemoApplication.java │ │ ├── GeneratorResource.java │ │ ├── ObjectResource.java │ │ ├── ParserResource.java │ │ └── StructureResource.java ├── servlet │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── eclipse │ │ └── parsson │ │ └── demos │ │ └── servlet │ │ └── ArrayServlet.java └── twitter │ ├── pom.xml │ └── src │ └── main │ ├── java │ ├── module-info.java │ └── org │ │ └── eclipse │ │ └── parsson │ │ └── demos │ │ └── twitter │ │ ├── TwitterObjectSearch.java │ │ └── TwitterStreamSearch.java │ └── resources │ └── twitterconfig.properties ├── etc └── config │ ├── copyright-exclude │ ├── copyright.txt │ └── exclude.xml ├── impl ├── pom.xml └── src │ ├── main │ ├── java │ │ ├── module-info.java │ │ └── org │ │ │ └── eclipse │ │ │ └── parsson │ │ │ ├── BufferPoolImpl.java │ │ │ ├── JsonArrayBuilderImpl.java │ │ │ ├── JsonBuilderFactoryImpl.java │ │ │ ├── JsonContext.java │ │ │ ├── JsonGeneratorFactoryImpl.java │ │ │ ├── JsonGeneratorImpl.java │ │ │ ├── JsonLocationImpl.java │ │ │ ├── JsonMergePatchImpl.java │ │ │ ├── JsonMessages.java │ │ │ ├── JsonNumberImpl.java │ │ │ ├── JsonObjectBuilderImpl.java │ │ │ ├── JsonParserFactoryImpl.java │ │ │ ├── JsonParserImpl.java │ │ │ ├── JsonPatchBuilderImpl.java │ │ │ ├── JsonPatchImpl.java │ │ │ ├── JsonPointerImpl.java │ │ │ ├── JsonPrettyGeneratorImpl.java │ │ │ ├── JsonProviderImpl.java │ │ │ ├── JsonReaderFactoryImpl.java │ │ │ ├── JsonReaderImpl.java │ │ │ ├── JsonStringImpl.java │ │ │ ├── JsonStructureParser.java │ │ │ ├── JsonTokenizer.java │ │ │ ├── JsonWriterFactoryImpl.java │ │ │ ├── JsonWriterImpl.java │ │ │ ├── MapUtil.java │ │ │ ├── NodeReference.java │ │ │ ├── UnicodeDetectingInputStream.java │ │ │ └── api │ │ │ ├── BufferPool.java │ │ │ └── JsonConfig.java │ └── resources │ │ ├── META-INF │ │ └── services │ │ │ └── jakarta.json.spi.JsonProvider │ │ └── org │ │ └── eclipse │ │ └── parsson │ │ └── messages.properties │ └── test │ ├── java │ └── org │ │ └── eclipse │ │ └── parsson │ │ ├── JsonParserFixture.java │ │ ├── TestUtils.java │ │ └── tests │ │ ├── Issue25Test.java │ │ ├── JsonArrayTest.java │ │ ├── JsonBigDecimalLengthLimitTest.java │ │ ├── JsonBigDecimalScaleLimitTest.java │ │ ├── JsonBuilderFactoryTest.java │ │ ├── JsonBuilderTest.java │ │ ├── JsonCollectorTest.java │ │ ├── JsonDuplicateKeyTest.java │ │ ├── JsonFieldTest.java │ │ ├── JsonGeneratorFactoryTest.java │ │ ├── JsonGeneratorTest.java │ │ ├── JsonMergePatch2Test.java │ │ ├── JsonMergePatchDiffTest.java │ │ ├── JsonMergePatchTest.java │ │ ├── JsonNestingTest.java │ │ ├── JsonNumberTest.java │ │ ├── JsonObjectTest.java │ │ ├── JsonParserFactoryTest.java │ │ ├── JsonParserSkipTest.java │ │ ├── JsonParserTest.java │ │ ├── JsonParsingExceptionTest.java │ │ ├── JsonPatchBugsTest.java │ │ ├── JsonPatchBuilderTest.java │ │ ├── JsonPatchDiffTest.java │ │ ├── JsonPatchOperationTest.java │ │ ├── JsonPatchTest.java │ │ ├── JsonPointerAddOperationTest.java │ │ ├── JsonPointerEscapeTest.java │ │ ├── JsonPointerRemoveOperationTest.java │ │ ├── JsonPointerReplaceOperationTest.java │ │ ├── JsonPointerTest.java │ │ ├── JsonPointerToStringTest.java │ │ ├── JsonReaderTest.java │ │ ├── JsonSamplesParsingTest.java │ │ ├── JsonStringTest.java │ │ ├── JsonValueTest.java │ │ ├── JsonWriterTest.java │ │ ├── RFC7159Test.java │ │ ├── ToJsonTest.java │ │ └── TwitterSearchTest.java │ └── resources │ ├── comments.json │ ├── facebook.json │ ├── facebook1.json │ ├── facebook2.json │ ├── jsonmergepatch.json │ ├── jsonmergepatchdiff.json │ ├── jsonpatch.json │ ├── jsonpatchdiff.json │ ├── rfc6901.json │ ├── twitter.json │ └── wiki.json ├── pom.xml ├── providers ├── customprovider │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── org │ │ │ └── eclipse │ │ │ └── parsson │ │ │ └── customprovider │ │ │ ├── TestGenerator.java │ │ │ ├── TestProvider.java │ │ │ └── TestServlet.java │ │ └── resources │ │ └── META-INF │ │ └── services │ │ └── jakarta.json.spi.JsonProvider ├── defaultprovider │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── org │ │ └── eclipse │ │ └── parsson │ │ └── defaultprovider │ │ └── TestServlet.java └── pom.xml ├── rest ├── pom.xml └── src │ └── main │ └── java │ ├── module-info.java │ └── org │ └── eclipse │ └── parsson │ └── media │ ├── JsonValueBodyReader.java │ └── JsonValueBodyWriter.java ├── src └── main │ └── assembly │ └── resources.xml └── tck-impl ├── build.gradle └── settings.gradle /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2021, 2024 Contributors to the Eclipse Foundation 3 | # 4 | # This program and the accompanying materials are made available under the 5 | # terms of the Eclipse Public License v. 2.0 which is available at 6 | # http://www.eclipse.org/legal/epl-2.0, 7 | # or the Eclipse Distribution License v. 1.0 which is available at 8 | # http://www.eclipse.org/org/documents/edl-v10.php. 9 | # 10 | # SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause 11 | # 12 | 13 | name: Parsson build 14 | 15 | on: 16 | pull_request: 17 | push: 18 | 19 | concurrency: 20 | group: ${{ github.ref }} 21 | cancel-in-progress: true 22 | 23 | jobs: 24 | build: 25 | name: Test on JDK ${{ matrix.java_version }} 26 | runs-on: ubuntu-latest 27 | outputs: 28 | jdk: ${{ steps.build.outputs.jdk }} 29 | 30 | strategy: 31 | matrix: 32 | java_version: [ 11, 17, 21 ] 33 | 34 | steps: 35 | - name: Checkout for build 36 | uses: actions/checkout@v3 37 | - name: Set up JDK 38 | uses: actions/setup-java@v3 39 | with: 40 | distribution: 'zulu' 41 | java-version: ${{ matrix.java_version }} 42 | cache: 'maven' 43 | - name: Verify 44 | id: build 45 | # GH action creates a merge commit which modifies files in the current year, to get over it, we have to exclude the check for the year of modification 46 | run: | 47 | mvn -B -V -U -C -Poss-release,staging clean verify org.glassfish.copyright:glassfish-copyright-maven-plugin:check -Dgpg.skip=true -Dcopyright.ignoreyear=true 48 | echo "::set-output name=jdk::${{ matrix.java_version }}" 49 | - name: Upload binary image 50 | uses: actions/upload-artifact@v4 51 | if: success() 52 | with: 53 | name: parsson-dist 54 | path: bundles/dist/target/parsson-dist.zip 55 | overwrite: true 56 | 57 | test: 58 | needs: build 59 | strategy: 60 | fail-fast: false 61 | matrix: 62 | test_suite: 63 | - mods 64 | - standalone 65 | name: TCK ${{ matrix.test_suite }} 66 | runs-on: ubuntu-latest 67 | steps: 68 | - name: Set up JDK 69 | uses: actions/setup-java@v3 70 | with: 71 | distribution: 'zulu' 72 | java-version: ${{ needs.build.outputs.jdk }} 73 | - name: Checkout tests 74 | uses: actions/checkout@v3 75 | with: 76 | fetch-depth: 0 77 | - name: Download binaries 78 | uses: actions/download-artifact@v4 79 | with: 80 | name: parsson-dist 81 | - name: Prepare distribution 82 | run: unzip -q -d image parsson-dist.zip 83 | - name: Test 84 | uses: gradle/gradle-build-action@v2 85 | with: 86 | gradle-version: current 87 | build-root-directory: tck-impl 88 | arguments: check -Pparsson.home=image/parsson-dist -Pparsson.impl=${{ matrix.test_suite }} 89 | - name: Upload test results 90 | uses: actions/upload-artifact@v4 91 | with: 92 | name: test-results 93 | path: tck-impl/build/distributions/tck-test-results.zip 94 | overwrite: true 95 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Maven noise 2 | target/ 3 | 4 | # OSX noise 5 | .DS_Store 6 | 7 | # IntelliJ Idea noise 8 | .idea 9 | *.iws 10 | *.ipr 11 | *.iml 12 | 13 | # Eclipse noise 14 | .settings/ 15 | .classpath 16 | .project 17 | 18 | # NetBeans noise 19 | nbproject/ 20 | 21 | # VS Code noise 22 | .vscode/ 23 | 24 | # Java noise 25 | *.class 26 | *err_pid*.log 27 | /tck-impl/.gradle/ 28 | /tck-impl/build/ 29 | /.sdkmanrc 30 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | [//]: # " Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved. " 2 | [//]: # " " 3 | [//]: # " This program and the accompanying materials are made available under the " 4 | [//]: # " terms of the Eclipse Public License v. 2.0, which is available at " 5 | [//]: # " http://www.eclipse.org/legal/epl-2.0. " 6 | [//]: # " " 7 | [//]: # " This Source Code may also be made available under the following Secondary " 8 | [//]: # " Licenses when the conditions for such availability set forth in the " 9 | [//]: # " Eclipse Public License v. 2.0 are satisfied: GNU General Public License, " 10 | [//]: # " version 2 with the GNU Classpath Exception, which is available at " 11 | [//]: # " https://www.gnu.org/software/classpath/license.html. " 12 | [//]: # " " 13 | [//]: # " SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 " 14 | 15 | # Contributing to Eclipse Parsson 16 | 17 | Thanks for your interest in this project. 18 | 19 | ## Project description 20 | 21 | Eclipse Parsson provides an implementation of Jakarta JSON Processing 22 | Specification. It allows processing (e.g. parse, generate, transform and query) 23 | JSON documents. It produces and consumes JSON in a streaming fashion (similar to 24 | StAX API for XML) and allows to build a Java object model for JSON using API 25 | classes (similar to DOM API for XML). 26 | 27 | * https://projects.eclipse.org/projects/ee4j.parsson 28 | 29 | ## Developer resources 30 | 31 | Information regarding source code management, builds, coding standards, and 32 | more. 33 | 34 | * https://projects.eclipse.org/projects/ee4j.parsson/developer 35 | 36 | The project maintains the following source code repositories 37 | 38 | * https://github.com/eclipse-ee4j/parsson 39 | 40 | ## Eclipse Development Process 41 | 42 | This Eclipse Foundation open project is governed by the Eclipse Foundation 43 | Development Process and operates under the terms of the Eclipse IP Policy. 44 | 45 | * https://eclipse.org/projects/dev_process 46 | * https://www.eclipse.org/org/documents/Eclipse_IP_Policy.pdf 47 | 48 | ## Eclipse Contributor Agreement 49 | 50 | In order to be able to contribute to Eclipse Foundation projects you must 51 | electronically sign the Eclipse Contributor Agreement (ECA). 52 | 53 | * http://www.eclipse.org/legal/ECA.php 54 | 55 | The ECA provides the Eclipse Foundation with a permanent record that you agree 56 | that each of your contributions will comply with the commitments documented in 57 | the Developer Certificate of Origin (DCO). Having an ECA on file associated with 58 | the email address matching the "Author" field of your contribution's Git commits 59 | fulfills the DCO's requirement that you sign-off on your contributions. 60 | 61 | For more information, please see the Eclipse Committer Handbook: 62 | https://www.eclipse.org/projects/handbook/#resources-commit 63 | 64 | ## Contact 65 | 66 | Contact the project developers via the project's "dev" list. 67 | 68 | * https://accounts.eclipse.org/mailing-list/parsson-dev -------------------------------------------------------------------------------- /NOTICE.md: -------------------------------------------------------------------------------- 1 | [//]: # " Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved. " 2 | [//]: # " " 3 | [//]: # " This program and the accompanying materials are made available under the " 4 | [//]: # " terms of the Eclipse Public License v. 2.0, which is available at " 5 | [//]: # " http://www.eclipse.org/legal/epl-2.0. " 6 | [//]: # " " 7 | [//]: # " This Source Code may also be made available under the following Secondary " 8 | [//]: # " Licenses when the conditions for such availability set forth in the " 9 | [//]: # " Eclipse Public License v. 2.0 are satisfied: GNU General Public License, " 10 | [//]: # " version 2 with the GNU Classpath Exception, which is available at " 11 | [//]: # " https://www.gnu.org/software/classpath/license.html. " 12 | [//]: # " " 13 | [//]: # " SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 " 14 | 15 | # Notices for Eclipse Parsson 16 | 17 | This content is produced and maintained by the Eclipse Parsson project. 18 | 19 | * Project home: https://projects.eclipse.org/projects/ee4j.parsson 20 | 21 | ## Trademarks 22 | 23 | Eclipse Parsson is a trademark of the Eclipse Foundation. 24 | 25 | ## Copyright 26 | 27 | All content is the property of the respective authors or their employers. For 28 | more information regarding authorship of content, please consult the listed 29 | source code repository logs. 30 | 31 | ## Declared Project Licenses 32 | 33 | This program and the accompanying materials are made available under the terms 34 | of the Eclipse Public License v. 2.0 which is available at 35 | https://www.eclipse.org/legal/epl-2.0. 36 | 37 | SPDX-License-Identifier: EPL-2.0 38 | 39 | ## Source Code 40 | 41 | The project maintains the following source code repositories: 42 | 43 | * https://github.com/eclipse-ee4j/parsson 44 | 45 | ## Third-party Content 46 | 47 | This project leverages the following third party content. 48 | 49 | None 50 | 51 | ## Cryptography 52 | 53 | Content may contain encryption software. The country in which you are currently 54 | may have restrictions on the import, possession, and use, and/or re-export to 55 | another country, of encryption software. BEFORE using any encryption software, 56 | please check the country's laws, regulations and policies concerning the import, 57 | possession, or use, and re-export of encryption software, to see if this is 58 | permitted. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [//]: # " Copyright (c) 2018, 2022 Oracle and/or its affiliates. All rights reserved. " 2 | [//]: # " " 3 | [//]: # " This program and the accompanying materials are made available under the " 4 | [//]: # " terms of the Eclipse Public License v. 2.0, which is available at " 5 | [//]: # " http://www.eclipse.org/legal/epl-2.0. " 6 | [//]: # " " 7 | [//]: # " This Source Code may also be made available under the following Secondary " 8 | [//]: # " Licenses when the conditions for such availability set forth in the " 9 | [//]: # " Eclipse Public License v. 2.0 are satisfied: GNU General Public License, " 10 | [//]: # " version 2 with the GNU Classpath Exception, which is available at " 11 | [//]: # " https://www.gnu.org/software/classpath/license.html. " 12 | [//]: # " " 13 | [//]: # " SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 " 14 | 15 | # Eclipse Parsson 16 | 17 | Eclipse Parsson is an implementation of Jakarta JSON Processing specification. 18 | 19 | ## Build 20 | 21 | Use the following command: 22 | ```bash 23 | mvn -U -C clean install 24 | ``` 25 | 26 | ## License 27 | 28 | * Most of the Eclipse Parsson project source code is licensed 29 | under the [Eclipse Public License (EPL) v2.0](https://projects.eclipse.org/license/epl-2.0) 30 | and [GNU General Public License (GPL) v2 with Classpath Exception](https://www.gnu.org/software/classpath/license.html); 31 | see the license information at the top of each source file. 32 | * The source code for the demo programs is licensed 33 | under the [Eclipse Distribution License (EDL) v1.0.](https://www.eclipse.org/org/documents/edl-v10.php). 34 | * The binary jar files published to the Maven repository are licensed 35 | under the same licenses as the corresponding source code; 36 | see the file `META-INF/LICENSE.txt` in each jar file. 37 | 38 | You’ll find the text of the licenses in the workspace in various `LICENSE.txt` or `LICENSE.md` files. 39 | Don’t let the presence of these license files in the workspace confuse you into thinking 40 | that they apply to all files in the workspace. 41 | 42 | You should always read the license file included with every download, and read 43 | the license text included in every source file. 44 | 45 | ## Links 46 | 47 | - [Eclipse Parsson @ Eclipse](https://projects.eclipse.org/projects/ee4j.parsson) 48 | - [Jakarta JSON Processing official web site](https://jakartaee.github.io/jsonp-api) 49 | - [Jakarta JSON Processing @ Eclipse](https://projects.eclipse.org/projects/ee4j.jsonp) 50 | - [README.txt](https://github.com/eclipse-ee4j/parsson/blob/master/bundles/dist/src/main/resources/README.txt) 51 | 52 | ## Contributing 53 | 54 | We use [contribution policy](CONTRIBUTING.md), which means we can only accept contributions under 55 | the terms of [Eclipse Contributor Agreement](http://www.eclipse.org/legal/ECA.php). 56 | -------------------------------------------------------------------------------- /bundles/dist/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 4.0.0 22 | 23 | org.eclipse.parsson 24 | parsson-bundles 25 | 1.1.7-SNAPSHOT 26 | 27 | 28 | parsson-dist 29 | Eclipse Parsson Distribution zip 30 | pom 31 | 32 | 33 | 34 | jakarta.json 35 | jakarta.json-api 36 | 37 | 38 | org.eclipse.parsson 39 | parsson 40 | 41 | 42 | org.eclipse.parsson 43 | jakarta.json 44 | 45 | 46 | org.eclipse.parsson 47 | parsson-media 48 | 49 | 50 | 51 | 52 | 53 | 54 | maven-assembly-plugin 55 | 56 | 57 | make-assembly 58 | package 59 | 60 | single 61 | 62 | 63 | 64 | src/main/assembly/archive.xml 65 | 66 | parsson-dist 67 | false 68 | 69 | 70 | 71 | 72 | 73 | org.apache.maven.plugins 74 | maven-dependency-plugin 75 | 76 | 77 | stage 78 | package 79 | 80 | unpack 81 | 82 | 83 | **/module-info.java 84 | 85 | 86 | ${project.groupId} 87 | ${project.artifactId} 88 | ${project.version} 89 | zip 90 | ${project.build.directory}/stage 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /bundles/dist/src/main/assembly/archive.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | dist 21 | 22 | zip 23 | 24 | 25 | 26 | src/main/resources/README.txt 27 | 28 | true 29 | 30 | 31 | src/main/resources/LICENSE.md 32 | 33 | 34 | 35 | 36 | 37 | false 38 | mods 39 | 40 | jakarta.json:jakarta.json-api:* 41 | org.eclipse.parsson:parsson:* 42 | 43 | 44 | 45 | false 46 | standalone 47 | 48 | org.eclipse.parsson:jakarta.json:* 49 | 50 | 51 | 52 | false 53 | media 54 | 55 | org.eclipse.parsson:parsson-media* 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /bundles/dist/src/main/resources/README.txt: -------------------------------------------------------------------------------- 1 | For running on JPMS, following modules are provided: 2 | * mods/jakarta.json-api-${project.version}.jar - 'jakarta.json' module containing only API classes 3 | * mods/parsson-${project.version}.jar - 'org.eclipse.parsson' module containing implementation 4 | 5 | Integration with Jakarta RESTful Web Services: 6 | * media/parsson-media-${project.version}.jar 7 | 8 | * standalone/jakarta.json-${project.version}.jar - 'jakarta.json' module containing API classes and implementation, which serves as a fallback for environments 9 | willing to support different implementations of Jakarta JSON Processing API while also providing some default implementation, such as application servers. 10 | When this library is used, then Eclipse Parsson is the default implementation being provided. 11 | 12 | NOTE: jakarta.json-api.jar and jakarta.json.jar CAN NOT co-exist together when running on JPMS. One can be used as a drop-in 13 | replacement of the other wrt Jakarta JSON Processing API. 14 | 15 | IMPORTANT NOTE: module names are not yet final and may change in the future releases 16 | 17 | 18 | * If you are running with maven, you can use the following maven coordinates: 19 | 20 | for the implementation: 21 | 22 | org.eclipse.parsson 23 | parsson 24 | ${project.version} 25 | 26 | 27 | for APIs: 28 | 29 | jakarta.json 30 | jakarta.json-api 31 | ${api.version} 32 | 33 | 34 | for Jakarta RESTful Web Services integration module: 35 | 36 | org.eclipse.parsson 37 | parsson-media 38 | ${project.version} 39 | 40 | 41 | for Jakarta JSON Processing API which uses Eclipse JSONP as a default provider: 42 | 43 | org.eclipse.parsson 44 | jakarta.json 45 | ${project.version} 46 | 47 | 48 | * GlassFish 6.x already bundles latest Jakarta JSON Processing implementation and Jakarta RESTful Web Services integration module. 49 | If you deploy an application with GlassFish 6.x, your application (war/ear) doesn't have to bundle APIs nor the implementation. 50 | 51 | * Samples can be run from https://github.com/eclipse-ee4j/glassfish-samples 52 | -------------------------------------------------------------------------------- /bundles/jakarta.json/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | module jakarta.json { 18 | 19 | requires java.logging; 20 | 21 | exports jakarta.json; 22 | exports jakarta.json.spi; 23 | exports jakarta.json.stream; 24 | exports org.eclipse.parsson.api; 25 | 26 | uses jakarta.json.spi.JsonProvider; 27 | } 28 | -------------------------------------------------------------------------------- /bundles/jakarta.json/src/main/javadoc/doc-files/speclicense.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Eclipse Foundation Specification License - v1.0 4 | 5 | 6 |

Eclipse Foundation Specification License - v1.0

7 |

By using and/or copying this document, or the Eclipse Foundation 8 | document from which this statement is linked, you (the licensee) agree 9 | that you have read, understood, and will comply with the following 10 | terms and conditions:

11 | 12 |

Permission to copy, and distribute the contents of this document, or 13 | the Eclipse Foundation document from which this statement is linked, in 14 | any medium for any purpose and without fee or royalty is hereby 15 | granted, provided that you include the following on ALL copies of the 16 | document, or portions thereof, that you use:

17 | 18 | 27 | 28 |

Inclusion of the full text of this NOTICE must be provided. We 29 | request that authorship attribution be provided in any software, 30 | documents, or other items or products that you create pursuant to the 31 | implementation of the contents of this document, or any portion 32 | thereof.

33 | 34 |

No right to create modifications or derivatives of Eclipse Foundation 35 | documents is granted pursuant to this license, except anyone may 36 | prepare and distribute derivative works and portions of this document 37 | in software that implements the specification, in supporting materials 38 | accompanying such software, and in documentation of such software, 39 | PROVIDED that all such works include the notice below. HOWEVER, the 40 | publication of derivative works of this document for use as a technical 41 | specification is expressly prohibited.

42 | 43 |

The notice is:

44 | 45 |

"Copyright © 2018 Eclipse Foundation. This software or 46 | document includes material copied from or derived from [title and URI 47 | of the Eclipse Foundation specification document]."

48 | 49 |

Disclaimers

50 | 51 |

THIS DOCUMENT IS PROVIDED "AS IS," AND THE COPYRIGHT 52 | HOLDERS AND THE ECLIPSE FOUNDATION MAKE NO REPRESENTATIONS OR 53 | WARRANTIES, EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 54 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 55 | NON-INFRINGEMENT, OR TITLE; THAT THE CONTENTS OF THE DOCUMENT ARE 56 | SUITABLE FOR ANY PURPOSE; NOR THAT THE IMPLEMENTATION OF SUCH CONTENTS 57 | WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR 58 | OTHER RIGHTS.

59 | 60 |

THE COPYRIGHT HOLDERS AND THE ECLIPSE FOUNDATION WILL NOT BE LIABLE 61 | FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT 62 | OF ANY USE OF THE DOCUMENT OR THE PERFORMANCE OR IMPLEMENTATION OF THE 63 | CONTENTS THEREOF.

64 | 65 |

The name and trademarks of the copyright holders or the Eclipse 66 | Foundation may NOT be used in advertising or publicity pertaining to 67 | this document or its contents without specific, written prior 68 | permission. Title to copyright in this document will at all times 69 | remain with copyright holders.

70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /bundles/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 4.0.0 22 | 23 | 24 | org.eclipse.parsson 25 | project 26 | 1.1.7-SNAPSHOT 27 | ../pom.xml 28 | 29 | 30 | org.eclipse.parsson 31 | parsson-bundles 32 | pom 33 | Eclipse Parsson bundles 34 | 35 | 36 | jakarta.json 37 | dist 38 | 39 | 40 | -------------------------------------------------------------------------------- /demos/LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | - Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | - Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 15 | - Neither the name of the Eclipse Foundation, Inc. nor the names of its 16 | contributors may be used to endorse or promote products derived 17 | from this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /demos/NOTICE.md: -------------------------------------------------------------------------------- 1 | # Notices for Eclipse Project for JSON Processing 2 | 3 | This content is produced and maintained by the Eclipse Project for JSON 4 | Processing project. 5 | 6 | * Project home: https://projects.eclipse.org/projects/ee4j.jsonp 7 | 8 | ## Trademarks 9 | 10 | Eclipse Project for JSON Processing is a trademark of the Eclipse Foundation. 11 | 12 | ## Copyright 13 | 14 | All content is the property of the respective authors or their employers. For 15 | more information regarding authorship of content, please consult the listed 16 | source code repository logs. 17 | 18 | ## Declared Project Licenses 19 | 20 | This program and the accompanying materials are made available under the terms 21 | of the Eclipse Public License v. 2.0 which is available at 22 | http://www.eclipse.org/legal/epl-2.0. This Source Code may also be made 23 | available under the following Secondary Licenses when the conditions for such 24 | availability set forth in the Eclipse Public License v. 2.0 are satisfied: GNU 25 | General Public License, version 2 with the GNU Classpath Exception which is 26 | available at https://www.gnu.org/software/classpath/license.html. 27 | 28 | SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 29 | 30 | ## Source Code 31 | 32 | The project maintains the following source code repositories: 33 | 34 | * https://github.com/eclipse-ee4j/jsonp 35 | 36 | ## Third-party Content 37 | 38 | JUnit (4.12) 39 | 40 | * License: Eclipse Public License 41 | 42 | ## Cryptography 43 | 44 | Content may contain encryption software. The country in which you are currently 45 | may have restrictions on the import, possession, and use, and/or re-export to 46 | another country, of encryption software. BEFORE using any encryption software, 47 | please check the country's laws, regulations and policies concerning the import, 48 | possession, or use, and re-export of encryption software, to see if this is 49 | permitted. 50 | -------------------------------------------------------------------------------- /demos/customprovider-jdk9/pom.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 4.0.0 15 | 16 | 17 | org.eclipse.parsson 18 | demos 19 | 1.1.7-SNAPSHOT 20 | ../pom.xml 21 | 22 | 23 | customprovider-jdk9 24 | 25 | customprovider-jdk9 26 | 27 | 28 | ${project.build.directory}/mods 29 | 30 | 31 | customprovider-jdk9 32 | 33 | 34 | org.apache.maven.plugins 35 | maven-dependency-plugin 36 | 37 | 38 | prepare-endorsed 39 | validate 40 | 41 | copy-dependencies 42 | 43 | 44 | ${mod.dir} 45 | false 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.apache.maven.plugins 53 | maven-surefire-plugin 54 | 55 | 56 | 57 | test 58 | 59 | test 60 | 61 | 62 | 63 | 64 | --module-path ${project.build.directory}/classes:${mod.dir}:${project.build.directory}/test-classes 65 | --add-modules org.eclipse.parsson.demos.customprovider 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | jakarta.json 74 | jakarta.json-api 75 | 76 | 77 | org.junit.jupiter 78 | junit-jupiter 79 | test 80 | 81 | 82 | org.hamcrest 83 | hamcrest-core 84 | test 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /demos/customprovider-jdk9/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | module org.eclipse.parsson.demos.customprovider { 12 | requires transitive jakarta.json; 13 | exports org.eclipse.parsson.demos.customprovider; 14 | provides jakarta.json.spi.JsonProvider with org.eclipse.parsson.demos.customprovider.TestProvider; 15 | } 16 | -------------------------------------------------------------------------------- /demos/customprovider-jdk9/src/main/java/org/eclipse/parsson/demos/customprovider/TestProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.customprovider; 12 | 13 | import jakarta.json.spi.JsonProvider; 14 | import jakarta.json.stream.JsonGenerator; 15 | import jakarta.json.stream.JsonGeneratorFactory; 16 | import jakarta.json.stream.JsonParser; 17 | import jakarta.json.stream.JsonParserFactory; 18 | import java.io.InputStream; 19 | import java.io.OutputStream; 20 | import java.io.OutputStreamWriter; 21 | import java.io.Reader; 22 | import java.io.Writer; 23 | import java.util.Map; 24 | import jakarta.json.JsonArrayBuilder; 25 | import jakarta.json.JsonBuilderFactory; 26 | import jakarta.json.JsonObjectBuilder; 27 | import jakarta.json.JsonReader; 28 | import jakarta.json.JsonReaderFactory; 29 | import jakarta.json.JsonWriter; 30 | import jakarta.json.JsonWriterFactory; 31 | 32 | /** 33 | * @author Jitendra Kotamraju 34 | */ 35 | public class TestProvider extends JsonProvider { 36 | 37 | @Override 38 | public JsonGenerator createGenerator(Writer writer) { 39 | return new TestGenerator(writer); 40 | } 41 | 42 | @Override 43 | public JsonGenerator createGenerator(OutputStream out) { 44 | return new TestGenerator(new OutputStreamWriter(out)); 45 | } 46 | 47 | @Override 48 | public JsonGeneratorFactory createGeneratorFactory(Map config) { 49 | return null; 50 | } 51 | 52 | @Override 53 | public JsonReader createReader(Reader reader) { 54 | return null; 55 | } 56 | 57 | @Override 58 | public JsonReader createReader(InputStream in) { 59 | return null; 60 | } 61 | 62 | @Override 63 | public JsonWriter createWriter(Writer writer) { 64 | return null; 65 | } 66 | 67 | @Override 68 | public JsonWriter createWriter(OutputStream out) { 69 | return null; 70 | } 71 | 72 | @Override 73 | public JsonWriterFactory createWriterFactory(Map config) { 74 | return null; 75 | } 76 | 77 | @Override 78 | public JsonReaderFactory createReaderFactory(Map config) { 79 | return null; 80 | } 81 | 82 | @Override 83 | public JsonObjectBuilder createObjectBuilder() { 84 | return null; 85 | } 86 | 87 | @Override 88 | public JsonArrayBuilder createArrayBuilder() { 89 | return null; 90 | } 91 | 92 | @Override 93 | public JsonBuilderFactory createBuilderFactory(Map config) { 94 | return null; 95 | } 96 | 97 | @Override 98 | public JsonParser createParser(Reader reader) { 99 | return null; 100 | } 101 | 102 | @Override 103 | public JsonParser createParser(InputStream in) { 104 | return null; 105 | } 106 | 107 | @Override 108 | public JsonParserFactory createParserFactory(Map config) { 109 | return null; 110 | } 111 | 112 | 113 | } 114 | -------------------------------------------------------------------------------- /demos/customprovider-jdk9/src/test/java/customprovider/test/TestProviderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package customprovider.test; 12 | 13 | import jakarta.json.Json; 14 | import jakarta.json.stream.JsonGenerator; 15 | 16 | import org.eclipse.parsson.demos.customprovider.TestGenerator; 17 | import org.junit.jupiter.api.Assertions; 18 | import org.junit.jupiter.api.Test; 19 | 20 | /** 21 | * 22 | * @author lukas 23 | */ 24 | public class TestProviderTest { 25 | 26 | @Test 27 | void hello() { 28 | try (JsonGenerator generator = Json.createGenerator(System.out)) { 29 | Assertions.assertInstanceOf(TestGenerator.class, generator, "TestGenerator is not picked up"); 30 | generator.writeStartArray().writeEnd(); 31 | } 32 | System.out.println(); 33 | System.out.println("Hurray!!!"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /demos/facebook/pom.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 4.0.0 15 | 16 | 17 | org.eclipse.parsson 18 | demos 19 | 1.1.7-SNAPSHOT 20 | ../pom.xml 21 | 22 | 23 | jar 24 | jsondemos-facebook 25 | 26 | 27 | org.eclipse.parsson.demos.facebook.FacebookObjectSearch 28 | 31 | 32 | 33 | 34 | 35 | org.eclipse.parsson 36 | jakarta.json 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.codehaus.mojo 44 | exec-maven-plugin 45 | 46 | java 47 | 48 | --module-path 49 | 50 | -m 51 | org.eclipse.parsson.demos.facebook/${main.class} 52 | 53 | 54 | 55 | 56 | example-facebook 57 | 58 | exec 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /demos/facebook/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | module org.eclipse.parsson.demos.facebook { 12 | requires jakarta.json; 13 | } 14 | -------------------------------------------------------------------------------- /demos/facebook/src/main/java/org/eclipse/parsson/demos/facebook/FacebookObjectSearch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.facebook; 12 | 13 | import jakarta.json.*; 14 | import java.io.*; 15 | import java.net.HttpURLConnection; 16 | import java.net.URL; 17 | import java.util.Properties; 18 | 19 | /** 20 | * Parses JSON from facebook graph API using object model API. 21 | * JSON would like : 22 | * 23 | * { 24 | * data: [ 25 | * { "from" : { "name" : "xxx", ... }, "message: "yyy", ... }, 26 | * { "from" : { "name" : "ppp", ... }, "message: "qqq", ... }, 27 | * ... 28 | * ], 29 | * ... 30 | * } 31 | * 32 | * This codes writes the facebook posts to output as follows: 33 | * xxx: yyy 34 | * -------- 35 | * ppp: qqq 36 | * -------- 37 | * 38 | * @author Jitendra Kotamraju 39 | */ 40 | public class FacebookObjectSearch { 41 | 42 | public static void main(String... args) throws Exception { 43 | try (InputStream is = getSearchStream(); 44 | JsonReader rdr = Json.createReader(is)) { 45 | 46 | JsonObject obj = rdr.readObject(); 47 | JsonArray results = obj.getJsonArray("data"); 48 | for (JsonObject result : results.getValuesAs(JsonObject.class)) { 49 | JsonValue value = result.get("from"); 50 | if (value != null && value instanceof JsonObject) { 51 | System.out.print(((JsonObject)value).getString("name", "anon")); 52 | } 53 | System.out.print(": "); 54 | System.out.println(result.getString("message", "")); 55 | System.out.println("-----------"); 56 | } 57 | } 58 | } 59 | 60 | static InputStream getSearchStream() throws Exception { 61 | Properties config = new Properties(); 62 | config.load(FacebookObjectSearch.class.getResourceAsStream( 63 | "/facebookconfig.properties")); 64 | final String accessToken = (String)config.get("access_token"); 65 | 66 | // Gets the search stream 67 | String searchUrl = "https://graph.facebook.com/search?q=tamil&type=post&access_token="; 68 | URL url = new URL(searchUrl+accessToken); 69 | HttpURLConnection con = (HttpURLConnection)url.openConnection(); 70 | return con.getInputStream(); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /demos/facebook/src/main/java/org/eclipse/parsson/demos/facebook/FacebookStreamSearch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.facebook; 12 | 13 | import jakarta.json.*; 14 | import jakarta.json.stream.JsonParser; 15 | import jakarta.json.stream.JsonParser.Event; 16 | import java.io.*; 17 | 18 | /** 19 | * Parses JSON from facebook graph API using streaming API. 20 | * JSON would like : 21 | * 22 | * { 23 | * data: [ 24 | * { "from" : { "name" : "xxx", ... }, "message: "yyy", ... }, 25 | * { "from" : { "name" : "ppp", ... }, "message: "qqq", ... }, 26 | * ... 27 | * ], 28 | * ... 29 | * } 30 | * 31 | * This codes writes the facebook posts to output as follows: 32 | * xxx: yyy 33 | * -------- 34 | * ppp: qqq 35 | * -------- 36 | * 37 | * @author Jitendra Kotamraju 38 | */ 39 | public class FacebookStreamSearch { 40 | 41 | public static void main(String... args) throws Exception { 42 | try (InputStream is = FacebookObjectSearch.getSearchStream(); 43 | JsonParser parser = Json.createParser(is)) { 44 | while (parser.hasNext()) { 45 | Event e = parser.next(); 46 | if (e == Event.KEY_NAME) { 47 | switch (parser.getString()) { 48 | case "name": 49 | parser.next(); 50 | System.out.print(parser.getString()); 51 | System.out.print(": "); 52 | break; 53 | case "message": 54 | parser.next(); 55 | System.out.println(parser.getString()); 56 | System.out.println("---------"); 57 | break; 58 | } 59 | } 60 | } 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /demos/facebook/src/main/resources/facebookconfig.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # This program and the accompanying materials are made available under the 5 | # terms of the Eclipse Distribution License v. 1.0, which is available at 6 | # http://www.eclipse.org/org/documents/edl-v10.php. 7 | # 8 | # SPDX-License-Identifier: BSD-3-Clause 9 | # 10 | 11 | 12 | access_token= 13 | -------------------------------------------------------------------------------- /demos/jsonpointer/pom.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 4.0.0 15 | 16 | 17 | org.eclipse.parsson 18 | demos 19 | 1.1.7-SNAPSHOT 20 | ../pom.xml 21 | 22 | 23 | jar 24 | jsondemos-jsonpointer 25 | 26 | 27 | org.eclipse.parsson.demos.jsonpointer.JsonpointerDemo 28 | 29 | 30 | 31 | 32 | jakarta.json 33 | jakarta.json-api 34 | 35 | 36 | org.eclipse.parsson 37 | parsson 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.apache.maven.plugins 45 | maven-javadoc-plugin 46 | 47 | 48 | false 49 | 50 | 51 | 52 | org.codehaus.mojo 53 | exec-maven-plugin 54 | 55 | java 56 | 57 | --module-path 58 | 59 | -m 60 | org.eclipse.parsson.demos.jsonpointer/${main.class} 61 | 62 | 63 | 64 | 65 | example-jsonpointer 66 | verify 67 | 68 | exec 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /demos/jsonpointer/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | module org.eclipse.parsson.demos.jsonpointer { 12 | requires jakarta.json; 13 | } 14 | -------------------------------------------------------------------------------- /demos/jsonpointer/src/main/resources/jsonpointer.json: -------------------------------------------------------------------------------- 1 | { 2 | "foo": ["bar", "baz"], 3 | "": 0, 4 | "a/b": 1, 5 | "c%d": 2, 6 | "e^f": 3, 7 | "g|h": 4, 8 | "i\\j": 5, 9 | "k\"l": 6, 10 | " ": 7, 11 | "m~n": 8 12 | } 13 | -------------------------------------------------------------------------------- /demos/jsonpointer/src/main/resources/wiki.json: -------------------------------------------------------------------------------- 1 | { 2 | "firstName": "John", 3 | "lastName": "Smith", 4 | "age": 25, 5 | "address": { 6 | "streetAddress": "21 2nd Street", 7 | "city": "New York", 8 | "state": "NY", 9 | "postalCode": "10021" 10 | }, 11 | "phoneNumber": [ 12 | { 13 | "type": "home", 14 | "number": "212 555-1234" 15 | }, 16 | { 17 | "type": "fax", 18 | "number": "646 555-4567" 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /demos/pom.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 4.0.0 15 | 16 | 17 | org.eclipse.parsson 18 | project 19 | 1.1.7-SNAPSHOT 20 | ../pom.xml 21 | 22 | 23 | pom 24 | org.eclipse.parsson 25 | demos 26 | 27 | 28 | rest 29 | twitter 30 | facebook 31 | jsonpointer 32 | servlet 33 | customprovider-jdk9 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /demos/rest/pom.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 4.0.0 15 | 16 | 17 | org.eclipse.parsson 18 | demos 19 | 1.1.7-SNAPSHOT 20 | ../pom.xml 21 | 22 | 23 | war 24 | jsondemos-media 25 | 26 | jsondemos-media 27 | 28 | 29 | 30 | jakarta.ws.rs 31 | jakarta.ws.rs-api 32 | provided 33 | 34 | 35 | jakarta.json 36 | jakarta.json-api 37 | provided 38 | 39 | 40 | 41 | 42 | 43 | org.apache.maven.plugins 44 | maven-compiler-plugin 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-war-plugin 49 | 50 | 51 | jsondemos-media 52 | 53 | 54 | -------------------------------------------------------------------------------- /demos/rest/src/main/java/org/eclipse/parsson/demos/rest/ArrayResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.rest; 12 | 13 | import jakarta.json.*; 14 | import jakarta.ws.rs.*; 15 | import jakarta.ws.rs.core.MediaType; 16 | 17 | /** 18 | * JsonArray as parameter and return type for a Jakarta RESTful Web Services resource 19 | * 20 | * @author Jitendra Kotamraju 21 | */ 22 | @Path("/array") 23 | public class ArrayResource { 24 | private static final JsonBuilderFactory bf = Json.createBuilderFactory(null); 25 | 26 | @GET 27 | @Produces(MediaType.APPLICATION_JSON) 28 | public JsonArray doGet() { 29 | return bf.createArrayBuilder() 30 | .add(bf.createObjectBuilder() 31 | .add("type", "home") 32 | .add("number", "212 555-1234")) 33 | .add(bf.createObjectBuilder() 34 | .add("type", "fax") 35 | .add("number", "646 555-4567")) 36 | .build(); 37 | } 38 | 39 | @POST 40 | @Consumes(MediaType.APPLICATION_JSON) 41 | public void doPost(JsonArray structure) { 42 | System.out.println(structure); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /demos/rest/src/main/java/org/eclipse/parsson/demos/rest/DemoApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.rest; 12 | 13 | import jakarta.json.stream.JsonGenerator; 14 | import jakarta.ws.rs.ApplicationPath; 15 | import jakarta.ws.rs.core.Application; 16 | import java.util.HashMap; 17 | import java.util.HashSet; 18 | import java.util.Map; 19 | import java.util.Set; 20 | 21 | /** 22 | * A Jakarta RESTful Web Services Demo Application 23 | * 24 | * @author Jitendra Kotamraju 25 | */ 26 | @ApplicationPath("/") 27 | public class DemoApplication extends Application { 28 | 29 | @Override 30 | public Set> getClasses() { 31 | Set> set = new HashSet<>(); 32 | set.add(ParserResource.class); 33 | set.add(GeneratorResource.class); 34 | set.add(ObjectResource.class); 35 | set.add(ArrayResource.class); 36 | set.add(StructureResource.class); 37 | 38 | return set; 39 | } 40 | 41 | @Override 42 | public Map getProperties() { 43 | return new HashMap() {{ 44 | put(JsonGenerator.PRETTY_PRINTING, true); 45 | }}; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /demos/rest/src/main/java/org/eclipse/parsson/demos/rest/GeneratorResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.rest; 12 | 13 | import jakarta.json.Json; 14 | import jakarta.json.stream.JsonGenerator; 15 | import jakarta.ws.rs.GET; 16 | import jakarta.ws.rs.Path; 17 | import jakarta.ws.rs.Produces; 18 | import jakarta.ws.rs.core.MediaType; 19 | import jakarta.ws.rs.core.StreamingOutput; 20 | import java.io.OutputStream; 21 | 22 | /** 23 | * Writes wiki's JSON example in a streaming fashion using JsonGenerator 24 | * 25 | * @author Jitendra Kotamraju 26 | */ 27 | @Path("/generator") 28 | public class GeneratorResource { 29 | 30 | @GET 31 | @Produces(MediaType.APPLICATION_JSON) 32 | public StreamingOutput doGet() { 33 | return new StreamingOutput() { 34 | public void write(OutputStream os) { 35 | writeWikiExample(os); 36 | } 37 | }; 38 | } 39 | 40 | // Writes wiki example JSON in a streaming fashion 41 | private void writeWikiExample(OutputStream os) { 42 | try(JsonGenerator gene = Json.createGenerator(os)) { 43 | gene.writeStartObject() 44 | .write("firstName", "John") 45 | .write("lastName", "Smith") 46 | .write("age", 25) 47 | .writeStartObject("address") 48 | .write("streetAddress", "21 2nd Street") 49 | .write("city", "New York") 50 | .write("state", "NY") 51 | .write("postalCode", "10021") 52 | .writeEnd() 53 | .writeStartArray("phoneNumber") 54 | .writeStartObject() 55 | .write("type", "home") 56 | .write("number", "212 555-1234") 57 | .writeEnd() 58 | .writeStartObject() 59 | .write("type", "fax") 60 | .write("number", "646 555-4567") 61 | .writeEnd() 62 | .writeEnd() 63 | .writeEnd(); 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /demos/rest/src/main/java/org/eclipse/parsson/demos/rest/ObjectResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.rest; 12 | 13 | import jakarta.json.*; 14 | import jakarta.ws.rs.*; 15 | import jakarta.ws.rs.core.MediaType; 16 | 17 | /** 18 | * JsonObject as parameter and return type for a 19 | * Jakarta RESTful Web Services resource 20 | * Writes a person's representation as JSON using JsonObject 21 | * 22 | * @author Jitendra Kotamraju 23 | */ 24 | @Path("/object") 25 | public class ObjectResource { 26 | private static final JsonBuilderFactory bf = Json.createBuilderFactory(null); 27 | 28 | @GET 29 | @Produces(MediaType.APPLICATION_JSON) 30 | public JsonObject doGet() { 31 | return bf.createObjectBuilder() 32 | .add("firstName", "John") 33 | .add("lastName", "Smith") 34 | .add("age", 25) 35 | .add("address", bf.createObjectBuilder() 36 | .add("streetAddress", "21 2nd Street") 37 | .add("city", "New York") 38 | .add("state", "NY") 39 | .add("postalCode", "10021")) 40 | .add("phoneNumber", bf.createArrayBuilder() 41 | .add(bf.createObjectBuilder() 42 | .add("type", "home") 43 | .add("number", "212 555-1234")) 44 | .add(bf.createObjectBuilder() 45 | .add("type", "fax") 46 | .add("number", "646 555-4567"))) 47 | .build(); 48 | } 49 | 50 | @POST 51 | @Consumes(MediaType.APPLICATION_JSON) 52 | public void doPost(JsonObject structure) { 53 | System.out.println(structure); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /demos/rest/src/main/java/org/eclipse/parsson/demos/rest/ParserResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.rest; 12 | 13 | import jakarta.json.Json; 14 | import jakarta.json.stream.JsonParser; 15 | import jakarta.json.stream.JsonParser.Event; 16 | import jakarta.ws.rs.GET; 17 | import jakarta.ws.rs.Path; 18 | import jakarta.ws.rs.Produces; 19 | import jakarta.ws.rs.core.StreamingOutput; 20 | import java.io.*; 21 | import java.net.URL; 22 | 23 | /** 24 | * Filters JSON from flicker photo search REST API 25 | * 26 | * { 27 | * photos : { 28 | * photo: [ 29 | * { id: "9889087315", secret: "40aeb70c83", server: "3818",farm: 4, ..}, 30 | * { id: "9889087315", secret: "40aeb70c83", server: "3818",farm: 4, ..} 31 | * ... 32 | * ], 33 | * ... 34 | * } 35 | * } 36 | * 37 | * @author Jitendra Kotamraju 38 | */ 39 | @Path("/parser") 40 | public class ParserResource { 41 | 42 | @GET 43 | @Produces("text/html") 44 | public StreamingOutput doGet() { 45 | return new StreamingOutput() { 46 | public void write(OutputStream os) throws IOException { 47 | writeFlickerFeed(os); 48 | } 49 | }; 50 | } 51 | 52 | private void writeFlickerFeed(OutputStream os) throws IOException { 53 | URL url = new URL("http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=221160312e1c22ec60ecf336951b0e77&format=json&nojsoncallback=1&per_page=20"); 54 | try(InputStream is = url.openStream(); 55 | JsonParser parser = Json.createParser(is); 56 | PrintWriter ps = new PrintWriter(new OutputStreamWriter(os, "UTF-8"))) { 57 | String id = null; 58 | String server = null; 59 | String secret = null; 60 | 61 | ps.println(""); 62 | while(parser.hasNext()) { 63 | Event e = parser.next(); 64 | if (e == Event.KEY_NAME) { 65 | String str = parser.getString(); 66 | switch (str) { 67 | case "id" : 68 | parser.next(); 69 | id = parser.getString(); 70 | break; 71 | case "farm" : 72 | parser.next(); 73 | String farm = parser.getString(); 74 | ps.println(""); 75 | break; 76 | case "server" : 77 | parser.next(); 78 | server = parser.getString(); 79 | break; 80 | case "secret" : 81 | parser.next(); 82 | secret = parser.getString(); 83 | break; 84 | } 85 | } 86 | } 87 | ps.println(""); 88 | ps.flush(); 89 | } 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /demos/rest/src/main/java/org/eclipse/parsson/demos/rest/StructureResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.rest; 12 | 13 | import jakarta.json.*; 14 | import jakarta.ws.rs.*; 15 | import jakarta.ws.rs.core.MediaType; 16 | 17 | /** 18 | * JsonStructure as parameter and return type for a 19 | * Jakarta RESTful Web Services resource 20 | * 21 | * @author Jitendra Kotamraju 22 | */ 23 | @Path("/structure") 24 | public class StructureResource { 25 | private static final JsonBuilderFactory bf = Json.createBuilderFactory(null); 26 | 27 | @GET 28 | @Produces(MediaType.APPLICATION_JSON) 29 | public JsonStructure doGet() { 30 | return bf.createObjectBuilder() 31 | .add("firstName", "John") 32 | .add("lastName", "Smith") 33 | .add("age", 25) 34 | .add("address", bf.createObjectBuilder() 35 | .add("streetAddress", "21 2nd Street") 36 | .add("city", "New York") 37 | .add("state", "NY") 38 | .add("postalCode", "10021")) 39 | .add("phoneNumber", bf.createArrayBuilder() 40 | .add(bf.createObjectBuilder() 41 | .add("type", "home") 42 | .add("number", "212 555-1234")) 43 | .add(bf.createObjectBuilder() 44 | .add("type", "fax") 45 | .add("number", "646 555-4567"))) 46 | .build(); 47 | } 48 | 49 | @POST 50 | @Consumes(MediaType.APPLICATION_JSON) 51 | public void doPost(JsonStructure structure) { 52 | System.out.println(structure); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /demos/servlet/pom.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 4.0.0 15 | 16 | 17 | org.eclipse.parsson 18 | demos 19 | 1.1.7-SNAPSHOT 20 | ../pom.xml 21 | 22 | 23 | war 24 | jsondemos-servlet 25 | 26 | jsondemos-servlet 27 | 28 | 29 | 30 | jakarta.servlet 31 | jakarta.servlet-api 32 | provided 33 | 34 | 35 | jakarta.json 36 | jakarta.json-api 37 | provided 38 | 39 | 40 | 41 | 42 | 43 | org.apache.maven.plugins 44 | maven-compiler-plugin 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-war-plugin 49 | 50 | 51 | jsondemos-servlet 52 | 53 | 54 | -------------------------------------------------------------------------------- /demos/servlet/src/main/java/org/eclipse/parsson/demos/servlet/ArrayServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.servlet; 12 | 13 | import java.io.IOException; 14 | 15 | import jakarta.json.Json; 16 | import jakarta.json.JsonArray; 17 | import jakarta.json.JsonBuilderFactory; 18 | import jakarta.json.JsonWriter; 19 | import jakarta.json.JsonWriterFactory; 20 | 21 | import jakarta.servlet.annotation.WebServlet; 22 | import jakarta.servlet.http.HttpServlet; 23 | import jakarta.servlet.http.HttpServletRequest; 24 | import jakarta.servlet.http.HttpServletResponse; 25 | 26 | /** 27 | * Writes a JsonArray using HttpServletResponse#getWriter 28 | * http://localhost:8080/jsondemos-servlet/array 29 | * 30 | * Writes a JsonArray using HttpServletResponse#getOutputStream 31 | * http://localhost:8080/jsondemos-servlet/array?stream 32 | * 33 | * 34 | * @author Jitendra Kotamraju 35 | */ 36 | @WebServlet("/array") 37 | public class ArrayServlet extends HttpServlet { 38 | private static final JsonBuilderFactory bf = Json.createBuilderFactory(null); 39 | private static final JsonWriterFactory wf = Json.createWriterFactory(null); 40 | 41 | @Override 42 | public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { 43 | JsonArray array = bf.createArrayBuilder() 44 | .add(bf.createObjectBuilder() 45 | .add("type", "home") 46 | .add("number", "212 555-1234")) 47 | .add(bf.createObjectBuilder() 48 | .add("type", "fax") 49 | .add("number", "646 555-4567")) 50 | .build(); 51 | res.setStatus(HttpServletResponse.SC_OK); 52 | res.setContentType("application/json"); 53 | res.setCharacterEncoding("UTF-8"); 54 | 55 | String q = req.getQueryString(); 56 | boolean isStream = q != null && q.equals("stream"); 57 | JsonWriter writer = isStream 58 | ? wf.createWriter(res.getOutputStream()) 59 | : wf.createWriter(res.getWriter()); 60 | writer.write(array); 61 | // not closing writer intentionally 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /demos/twitter/pom.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 4.0.0 15 | 16 | 17 | org.eclipse.parsson 18 | demos 19 | 1.1.7-SNAPSHOT 20 | ../pom.xml 21 | 22 | 23 | jar 24 | jsondemos-twitter 25 | 26 | 27 | org.eclipse.parsson.demos.twitter.TwitterObjectSearch 28 | 31 | ${project.build.directory}/modules 32 | 33 | 34 | 35 | 36 | jakarta.json 37 | jakarta.json-api 38 | 39 | 40 | org.eclipse.parsson 41 | parsson 42 | runtime 43 | 44 | 45 | 46 | jakarta.xml.bind 47 | jakarta.xml.bind-api 48 | 49 | 50 | com.sun.xml.bind 51 | jaxb-impl 52 | runtime 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.codehaus.mojo 60 | exec-maven-plugin 61 | 62 | java 63 | 64 | --module-path 65 | 66 | -m 67 | org.eclipse.parsson.demos.twitter/${main.class} 68 | 69 | 70 | 71 | 72 | example-twitter 73 | 74 | exec 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /demos/twitter/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | module org.eclipse.parsson.demos.twitter { 12 | requires jakarta.xml.bind; 13 | requires jakarta.json; 14 | } 15 | -------------------------------------------------------------------------------- /demos/twitter/src/main/java/org/eclipse/parsson/demos/twitter/TwitterStreamSearch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Distribution License v. 1.0, which is available at 6 | * http://www.eclipse.org/org/documents/edl-v10.php. 7 | * 8 | * SPDX-License-Identifier: BSD-3-Clause 9 | */ 10 | 11 | package org.eclipse.parsson.demos.twitter; 12 | 13 | import jakarta.json.*; 14 | import jakarta.json.stream.JsonParser; 15 | import jakarta.json.stream.JsonParser.Event; 16 | import java.io.*; 17 | 18 | /** 19 | * Parses JSON from twitter search REST API using streaming API. 20 | * JSON would like : 21 | * 22 | * { 23 | " statuses": [ 24 | * { ..., "user" : { "name" : "xxx", ...}, "text: "yyy", ... }, 25 | * { ..., "user" : { "name" : "ppp", ...}, "text: "qqq", ... }, 26 | * ... 27 | * ], 28 | * ... 29 | * } 30 | * 31 | * This codes writes the tweets to output as follows: 32 | * xxx: yyy 33 | * -------- 34 | * ppp: qqq 35 | * -------- 36 | * 37 | * TODO need to do better, also the last tweet is repeated ! 38 | * 39 | * @author Jitendra Kotamraju 40 | */ 41 | public class TwitterStreamSearch { 42 | 43 | public static void main(String... args) throws Exception { 44 | try (InputStream is = TwitterObjectSearch.getSearchStream(); 45 | JsonParser parser = Json.createParser(is)) { 46 | int depth = 0; 47 | String name = null; 48 | String text = null; 49 | while (parser.hasNext()) { 50 | Event e = parser.next(); 51 | if (e == Event.KEY_NAME) { 52 | switch (parser.getString()) { 53 | case "name": 54 | if (depth == 3) { 55 | parser.next(); 56 | name = parser.getString(); 57 | } 58 | break; 59 | case "text": 60 | if (depth == 2) { 61 | parser.next(); 62 | text = parser.getString(); 63 | } 64 | break; 65 | } 66 | } else if (e == Event.START_OBJECT) { 67 | ++depth; 68 | } else if (e == Event.END_OBJECT) { 69 | --depth; 70 | if (depth == 1) { 71 | System.out.println(name+": "+text); 72 | System.out.println("-----------"); 73 | 74 | } 75 | } 76 | } 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /demos/twitter/src/main/resources/twitterconfig.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2017, 2021 Oracle and/or its affiliates. All rights reserved. 3 | # 4 | # This program and the accompanying materials are made available under the 5 | # terms of the Eclipse Distribution License v. 1.0, which is available at 6 | # http://www.eclipse.org/org/documents/edl-v10.php. 7 | # 8 | # SPDX-License-Identifier: BSD-3-Clause 9 | # 10 | 11 | 12 | consumer-key= 13 | consumer-secret= 14 | access-token= 15 | access-token-secret= 16 | 17 | -------------------------------------------------------------------------------- /etc/config/copyright-exclude: -------------------------------------------------------------------------------- 1 | .json 2 | .md 3 | speclicense.html 4 | MANIFEST.MF 5 | /META-INF/services/ 6 | /etc/config/copyright-exclude 7 | /etc/config/copyright.txt 8 | /bundles/dist/src/main/resources/README.txt 9 | -------------------------------------------------------------------------------- /etc/config/copyright.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) YYYY Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | -------------------------------------------------------------------------------- /etc/config/exclude.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /impl/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | /** 18 | * Jakarta JSON API with Eclipse Parsson implementation. 19 | */ 20 | module org.eclipse.parsson { 21 | requires transitive jakarta.json; 22 | exports org.eclipse.parsson.api; 23 | opens org.eclipse.parsson to jakarta.json; 24 | provides jakarta.json.spi.JsonProvider with org.eclipse.parsson.JsonProviderImpl; 25 | } 26 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/BufferPoolImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import org.eclipse.parsson.api.BufferPool; 20 | 21 | import java.lang.ref.WeakReference; 22 | import java.util.concurrent.ConcurrentLinkedQueue; 23 | 24 | /** 25 | * char[] pool that pool instances of char[] which are expensive to create. 26 | * 27 | * @author Jitendra Kotamraju 28 | */ 29 | class BufferPoolImpl implements BufferPool { 30 | 31 | // volatile since multiple threads may access queue reference 32 | private volatile WeakReference> queue; 33 | 34 | /** 35 | * Gets a new object from the pool. 36 | * 37 | *

38 | * If no object is available in the pool, this method creates a new one. 39 | * 40 | * @return 41 | * always non-null. 42 | */ 43 | @Override 44 | public final char[] take() { 45 | char[] t = getQueue().poll(); 46 | if (t==null) 47 | return new char[4096]; 48 | return t; 49 | } 50 | 51 | private ConcurrentLinkedQueue getQueue() { 52 | WeakReference> q = queue; 53 | if (q != null) { 54 | ConcurrentLinkedQueue d = q.get(); 55 | if (d != null) 56 | return d; 57 | } 58 | 59 | // overwrite the queue 60 | ConcurrentLinkedQueue d = new ConcurrentLinkedQueue<>(); 61 | queue = new WeakReference<>(d); 62 | 63 | return d; 64 | } 65 | 66 | /** 67 | * Returns an object back to the pool. 68 | */ 69 | @Override 70 | public final void recycle(char[] t) { 71 | getQueue().offer(t); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonBuilderFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import java.util.Collection; 20 | 21 | import jakarta.json.JsonObject; 22 | import jakarta.json.JsonArray; 23 | import jakarta.json.JsonArrayBuilder; 24 | import jakarta.json.JsonBuilderFactory; 25 | import jakarta.json.JsonObjectBuilder; 26 | import java.util.Map; 27 | 28 | /** 29 | * @author Jitendra Kotamraju 30 | */ 31 | class JsonBuilderFactoryImpl implements JsonBuilderFactory { 32 | 33 | private final JsonContext jsonContext; 34 | 35 | JsonBuilderFactoryImpl(JsonContext jsonContext) { 36 | this.jsonContext = jsonContext; 37 | } 38 | 39 | @Override 40 | public JsonObjectBuilder createObjectBuilder() { 41 | return new JsonObjectBuilderImpl(jsonContext); 42 | } 43 | 44 | @Override 45 | public JsonObjectBuilder createObjectBuilder(JsonObject object) { 46 | return new JsonObjectBuilderImpl(object, jsonContext); 47 | } 48 | 49 | @Override 50 | public JsonObjectBuilder createObjectBuilder(Map object) { 51 | return new JsonObjectBuilderImpl(object, jsonContext); 52 | } 53 | 54 | @Override 55 | public JsonArrayBuilder createArrayBuilder() { 56 | return new JsonArrayBuilderImpl(jsonContext); 57 | } 58 | 59 | @Override 60 | public JsonArrayBuilder createArrayBuilder(JsonArray array) { 61 | return new JsonArrayBuilderImpl(array, jsonContext); 62 | } 63 | 64 | @Override 65 | public JsonArrayBuilder createArrayBuilder(Collection collection) { 66 | return new JsonArrayBuilderImpl(collection, jsonContext); 67 | } 68 | 69 | @Override 70 | public Map getConfigInUse() { 71 | return jsonContext.config(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonGeneratorFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import jakarta.json.stream.JsonGenerator; 20 | import jakarta.json.stream.JsonGeneratorFactory; 21 | import java.io.OutputStream; 22 | import java.io.Writer; 23 | import java.nio.charset.Charset; 24 | import java.util.Map; 25 | 26 | /** 27 | * @author Jitendra Kotamraju 28 | */ 29 | class JsonGeneratorFactoryImpl implements JsonGeneratorFactory { 30 | 31 | private final JsonContext jsonContext; 32 | 33 | JsonGeneratorFactoryImpl(JsonContext jsonContext) { 34 | this.jsonContext = jsonContext; 35 | } 36 | 37 | @Override 38 | public JsonGenerator createGenerator(Writer writer) { 39 | return jsonContext.prettyPrinting() 40 | ? new JsonPrettyGeneratorImpl(writer, jsonContext) 41 | : new JsonGeneratorImpl(writer, jsonContext); 42 | } 43 | 44 | @Override 45 | public JsonGenerator createGenerator(OutputStream out) { 46 | return jsonContext.prettyPrinting() 47 | ? new JsonPrettyGeneratorImpl(out, jsonContext) 48 | : new JsonGeneratorImpl(out, jsonContext); 49 | } 50 | 51 | @Override 52 | public JsonGenerator createGenerator(OutputStream out, Charset charset) { 53 | return jsonContext.prettyPrinting() 54 | ? new JsonPrettyGeneratorImpl(out, charset, jsonContext) 55 | : new JsonGeneratorImpl(out, charset, jsonContext); 56 | } 57 | 58 | @Override 59 | public Map getConfigInUse() { 60 | return jsonContext.config(); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonLocationImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import jakarta.json.stream.JsonLocation; 20 | 21 | /** 22 | * @author Jitendra Kotamraju 23 | */ 24 | class JsonLocationImpl implements JsonLocation { 25 | static final JsonLocation UNKNOWN = new JsonLocationImpl(-1, -1, -1); 26 | 27 | private final long columnNo; 28 | private final long lineNo; 29 | private final long offset; 30 | 31 | JsonLocationImpl(long lineNo, long columnNo, long streamOffset) { 32 | this.lineNo = lineNo; 33 | this.columnNo = columnNo; 34 | this.offset = streamOffset; 35 | } 36 | 37 | @Override 38 | public long getLineNumber() { 39 | return lineNo; 40 | } 41 | 42 | @Override 43 | public long getColumnNumber() { 44 | return columnNo; 45 | } 46 | 47 | @Override 48 | public long getStreamOffset() { 49 | return offset; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "(line no="+lineNo+", column no="+columnNo+", offset="+ offset +")"; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonParserFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import jakarta.json.JsonArray; 20 | import jakarta.json.JsonObject; 21 | import jakarta.json.stream.JsonParserFactory; 22 | import jakarta.json.stream.JsonParser; 23 | import java.io.InputStream; 24 | import java.io.Reader; 25 | import java.nio.charset.Charset; 26 | import java.util.Map; 27 | 28 | /** 29 | * @author Jitendra Kotamraju 30 | */ 31 | class JsonParserFactoryImpl implements JsonParserFactory { 32 | 33 | private final JsonContext jsonContext; 34 | 35 | JsonParserFactoryImpl(JsonContext jsonContext) { 36 | this.jsonContext = jsonContext; 37 | } 38 | 39 | @Override 40 | public JsonParser createParser(Reader reader) { 41 | return new JsonParserImpl(reader, jsonContext); 42 | } 43 | 44 | @Override 45 | public JsonParser createParser(InputStream in) { 46 | return new JsonParserImpl(in, jsonContext); 47 | } 48 | 49 | @Override 50 | public JsonParser createParser(InputStream in, Charset charset) { 51 | return new JsonParserImpl(in, charset, jsonContext); 52 | } 53 | 54 | @Override 55 | public JsonParser createParser(JsonArray array) { 56 | return new JsonStructureParser(array); 57 | } 58 | 59 | @Override 60 | public Map getConfigInUse() { 61 | return jsonContext.config(); 62 | } 63 | 64 | @Override 65 | public JsonParser createParser(JsonObject object) { 66 | return new JsonStructureParser(object); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonPrettyGeneratorImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import jakarta.json.stream.JsonGenerator; 20 | import java.io.OutputStream; 21 | import java.io.Writer; 22 | import java.nio.charset.Charset; 23 | 24 | /** 25 | * @author Jitendra Kotamraju 26 | */ 27 | public class JsonPrettyGeneratorImpl extends JsonGeneratorImpl { 28 | 29 | private int indentLevel; 30 | private static final String INDENT = " "; 31 | 32 | public JsonPrettyGeneratorImpl(Writer writer, JsonContext jsonContext) { 33 | super(writer, jsonContext); 34 | } 35 | 36 | public JsonPrettyGeneratorImpl(OutputStream out, JsonContext jsonContext) { 37 | super(out, jsonContext); 38 | } 39 | 40 | public JsonPrettyGeneratorImpl(OutputStream out, Charset encoding, JsonContext jsonContext) { 41 | super(out, encoding, jsonContext); 42 | } 43 | 44 | @Override 45 | public JsonGenerator writeStartObject() { 46 | super.writeStartObject(); 47 | indentLevel++; 48 | return this; 49 | } 50 | 51 | @Override 52 | public JsonGenerator writeStartObject(String name) { 53 | super.writeStartObject(name); 54 | indentLevel++; 55 | return this; 56 | } 57 | 58 | @Override 59 | public JsonGenerator writeStartArray() { 60 | super.writeStartArray(); 61 | indentLevel++; 62 | return this; 63 | } 64 | 65 | @Override 66 | public JsonGenerator writeStartArray(String name) { 67 | super.writeStartArray(name); 68 | indentLevel++; 69 | return this; 70 | } 71 | 72 | @Override 73 | public JsonGenerator writeEnd() { 74 | writeNewLine(); 75 | indentLevel--; 76 | writeIndent(); 77 | super.writeEnd(); 78 | return this; 79 | } 80 | 81 | private void writeIndent() { 82 | for(int i=0; i < indentLevel; i++) { 83 | writeString(INDENT); 84 | } 85 | } 86 | 87 | @Override 88 | protected void writeComma() { 89 | super.writeComma(); 90 | if (isCommaAllowed() && !inNone()) { 91 | writeChar('\n'); 92 | writeIndent(); 93 | } 94 | } 95 | 96 | @Override 97 | protected void writeColon() { 98 | super.writeColon(); 99 | writeChar(' '); 100 | } 101 | 102 | private void writeNewLine() { 103 | writeChar('\n'); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonReaderFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import jakarta.json.JsonReader; 20 | import jakarta.json.JsonReaderFactory; 21 | import java.io.InputStream; 22 | import java.io.Reader; 23 | import java.nio.charset.Charset; 24 | import java.util.Map; 25 | 26 | /** 27 | * @author Jitendra Kotamraju 28 | */ 29 | class JsonReaderFactoryImpl implements JsonReaderFactory { 30 | 31 | private final JsonContext jsonContext; 32 | 33 | JsonReaderFactoryImpl(JsonContext jsonContext) { 34 | this.jsonContext = jsonContext; 35 | } 36 | 37 | @Override 38 | public JsonReader createReader(Reader reader) { 39 | return new JsonReaderImpl(reader, jsonContext); 40 | } 41 | 42 | @Override 43 | public JsonReader createReader(InputStream in) { 44 | return new JsonReaderImpl(in, jsonContext); 45 | } 46 | 47 | @Override 48 | public JsonReader createReader(InputStream in, Charset charset) { 49 | return new JsonReaderImpl(in, charset, jsonContext); 50 | } 51 | 52 | @Override 53 | public Map getConfigInUse() { 54 | return jsonContext.config(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonStringImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import jakarta.json.JsonString; 20 | 21 | /** 22 | * JsonString impl 23 | * 24 | * @author Jitendra Kotamraju 25 | */ 26 | final class JsonStringImpl implements JsonString { 27 | 28 | private final CharSequence value; 29 | 30 | JsonStringImpl(CharSequence value) { 31 | this.value = value; 32 | } 33 | 34 | @Override 35 | public String getString() { 36 | return value.toString(); 37 | } 38 | 39 | @Override 40 | public CharSequence getChars() { 41 | return value; 42 | } 43 | 44 | @Override 45 | public ValueType getValueType() { 46 | return ValueType.STRING; 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return getString().hashCode(); 52 | } 53 | 54 | @Override 55 | public boolean equals(Object obj) { 56 | if (this == obj){ 57 | return true; 58 | } 59 | if (!(obj instanceof JsonString)) { 60 | return false; 61 | } 62 | JsonString other = (JsonString)obj; 63 | return getString().equals(other.getString()); 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | StringBuilder sb = new StringBuilder(); 69 | sb.append('"'); 70 | 71 | for(int i = 0; i < value.length(); i++) { 72 | char c = value.charAt(i); 73 | // unescaped = %x20-21 | %x23-5B | %x5D-10FFFF 74 | if (c >= 0x20 && c <= 0x10ffff && c != 0x22 && c != 0x5c) { 75 | sb.append(c); 76 | } else { 77 | switch (c) { 78 | case '"': 79 | case '\\': 80 | sb.append('\\'); sb.append(c); 81 | break; 82 | case '\b': 83 | sb.append('\\'); sb.append('b'); 84 | break; 85 | case '\f': 86 | sb.append('\\'); sb.append('f'); 87 | break; 88 | case '\n': 89 | sb.append('\\'); sb.append('n'); 90 | break; 91 | case '\r': 92 | sb.append('\\'); sb.append('r'); 93 | break; 94 | case '\t': 95 | sb.append('\\'); sb.append('t'); 96 | break; 97 | default: 98 | String hex = "000" + Integer.toHexString(c); 99 | sb.append("\\u").append(hex.substring(hex.length() - 4)); 100 | } 101 | } 102 | } 103 | 104 | sb.append('"'); 105 | return sb.toString(); 106 | } 107 | } 108 | 109 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/JsonWriterFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import jakarta.json.JsonWriter; 20 | import jakarta.json.JsonWriterFactory; 21 | import java.io.OutputStream; 22 | import java.io.Writer; 23 | import java.nio.charset.Charset; 24 | import java.util.Map; 25 | 26 | /** 27 | * @author Jitendra Kotamraju 28 | */ 29 | class JsonWriterFactoryImpl implements JsonWriterFactory { 30 | 31 | private final JsonContext jsonContext; 32 | 33 | JsonWriterFactoryImpl(JsonContext jsonContext) { 34 | this.jsonContext = jsonContext; 35 | } 36 | 37 | @Override 38 | public JsonWriter createWriter(Writer writer) { 39 | return new JsonWriterImpl(writer, jsonContext); 40 | } 41 | 42 | @Override 43 | public JsonWriter createWriter(OutputStream out) { 44 | return new JsonWriterImpl(out, jsonContext); 45 | } 46 | 47 | @Override 48 | public JsonWriter createWriter(OutputStream out, Charset charset) { 49 | return new JsonWriterImpl(out, charset, jsonContext); 50 | } 51 | 52 | @Override 53 | public Map getConfigInUse() { 54 | return jsonContext.config(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/api/BufferPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.api; 18 | 19 | /** 20 | * char[] pool that pool instances of char[] which are expensive to create. 21 | * 22 | * @author Jitendra Kotamraju 23 | */ 24 | public interface BufferPool { 25 | 26 | /** 27 | * Gets a new char[] object from the pool. 28 | * 29 | *

30 | * If no object is available in the pool, this method creates a new one. 31 | * 32 | * @return 33 | * always non-null. 34 | */ 35 | char[] take(); 36 | 37 | /** 38 | * Returns an object back to the pool. 39 | * 40 | * @param buf object to return back to the pool 41 | */ 42 | void recycle(char[] buf); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /impl/src/main/java/org/eclipse/parsson/api/JsonConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.api; 18 | 19 | public interface JsonConfig { 20 | 21 | /** 22 | * Configuration property to limit maximum absolute value of BigInteger scale. 23 | * This property limits maximum absolute value of scale to be allowed 24 | * in {@link jakarta.json.JsonNumber#bigIntegerValue()} 25 | * and {@link jakarta.json.JsonNumber#bigIntegerValueExact()} implemented methods. 26 | * Default value is set to {@code 100000}. 27 | */ 28 | String MAX_BIGINTEGER_SCALE = "org.eclipse.parsson.maxBigIntegerScale"; 29 | 30 | /** 31 | * Configuration property to limit maximum value of BigDecimal length when being parsed. 32 | * This property limits maximum number of characters of BigDecimal source being parsed. 33 | * Default value is set to {@code 1100}. 34 | */ 35 | String MAX_BIGDECIMAL_LEN = "org.eclipse.parsson.maxBigDecimalLength"; 36 | 37 | /** 38 | * Configuration property to limit maximum level of nesting when being parsing JSON string. 39 | * Default value is set to {@code 1000}. 40 | */ 41 | String MAX_DEPTH = "org.eclipse.parsson.maxDepth"; 42 | 43 | /** 44 | * Configuration property to reject duplicate keys. 45 | * The value of the property could be anything. 46 | * 47 | * @deprecated in favor of {@link jakarta.json.JsonConfig#KEY_STRATEGY} 48 | */ 49 | @Deprecated 50 | String REJECT_DUPLICATE_KEYS = "org.eclipse.parsson.rejectDuplicateKeys"; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /impl/src/main/resources/META-INF/services/jakarta.json.spi.JsonProvider: -------------------------------------------------------------------------------- 1 | org.eclipse.parsson.JsonProviderImpl 2 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/JsonParserFixture.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import java.io.StringReader; 20 | import java.util.function.Consumer; 21 | import java.util.function.Supplier; 22 | 23 | import jakarta.json.Json; 24 | import jakarta.json.JsonArray; 25 | import jakarta.json.JsonObject; 26 | import jakarta.json.stream.JsonParser; 27 | 28 | /** 29 | * Class with methods that creates JsonParser with different configuration from different sources and runs the test code with this parser 30 | */ 31 | public class JsonParserFixture { 32 | /** 33 | * Runs the test code with JsonParser created from the given JsonObject 34 | * 35 | * @param object JsonObject to create JsonParser from 36 | * @param parserConsumer test code to run with the created JsonParser 37 | */ 38 | public static void testWithCreateParserFromObject(JsonObject object, Consumer parserConsumer) { 39 | testWithParser(() -> Json.createParserFactory(null).createParser(object), parserConsumer); 40 | } 41 | 42 | /** 43 | * Runs the test code with JsonParser created from the given JsonArray 44 | * 45 | * @param array JsonArray to create JsonParser from 46 | * @param parserConsumer test code to run with the created JsonParser 47 | */ 48 | public static void testWithCreateParserFromArray(JsonArray array, Consumer parserConsumer) { 49 | testWithParser(() -> Json.createParserFactory(null).createParser(array), parserConsumer); 50 | } 51 | 52 | /** 53 | * Runs the test code with JsonParser created from the given String 54 | * 55 | * @param string String with JSON to create JsonParser from 56 | * @param parserConsumer test code to run with the created JsonParser 57 | */ 58 | public static void testWithCreateParserFromString(String string, Consumer parserConsumer) { 59 | testWithParser(() -> Json.createParser(new StringReader(string)), parserConsumer); 60 | } 61 | 62 | /** 63 | * Runs the test code with JsonParser created from the given String 64 | * 65 | * @param parserSupplier Supplier of JsonParser to create JsonParser from 66 | * @param parserConsumer test code to run with the created JsonParser 67 | */ 68 | private static void testWithParser(Supplier parserSupplier, Consumer parserConsumer) { 69 | try (JsonParser parser = parserSupplier.get()) { 70 | parserConsumer.accept(parser); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/TestUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson; 18 | 19 | import java.io.StringReader; 20 | import java.util.Collections; 21 | 22 | import jakarta.json.JsonReader; 23 | import jakarta.json.JsonValue; 24 | 25 | /** 26 | * Local test utils. 27 | */ 28 | public class TestUtils { 29 | 30 | /** 31 | * Creates an instance of JSON Merge Patch with empty context. 32 | * 33 | * @param patch JSON Merge Patch 34 | * @return new JSON Merge Patch instance 35 | */ 36 | public static JsonMergePatchImpl createJsonMergePatchImpl(JsonValue patch) { 37 | return new JsonMergePatchImpl(patch, new JsonContext(null, new BufferPoolImpl())); 38 | } 39 | 40 | /** 41 | * Reads the input JSON text and returns a JsonValue. 42 | *

For convenience, single quotes as well as double quotes 43 | * are allowed to delimit JSON strings. If single quotes are 44 | * used, any quotes, single or double, in the JSON string must be 45 | * escaped (prepend with a '\'). 46 | * 47 | * @param jsonString the input JSON data 48 | * @return the object model for {@code jsonString} 49 | * @throws jakarta.json.stream.JsonParsingException if the input is not legal JSON text 50 | */ 51 | public static JsonValue toJson(String jsonString) { 52 | StringBuilder builder = new StringBuilder(); 53 | boolean single_context = false; 54 | for (int i = 0; i < jsonString.length(); i++) { 55 | char ch = jsonString.charAt(i); 56 | if (ch == '\\') { 57 | i = i + 1; 58 | if (i < jsonString.length()) { 59 | ch = jsonString.charAt(i); 60 | if (!(single_context && ch == '\'')) { 61 | // unescape ' inside single quotes 62 | builder.append('\\'); 63 | } 64 | } 65 | } else if (ch == '\'') { 66 | // Turn ' into ", for proper JSON string 67 | ch = '"'; 68 | single_context = ! single_context; 69 | } 70 | builder.append(ch); 71 | } 72 | 73 | JsonReader reader = new JsonReaderImpl( 74 | new StringReader(builder.toString()), 75 | new JsonContext(Collections.emptyMap(), new BufferPoolImpl())); 76 | JsonValue value = reader.readValue(); 77 | reader.close(); 78 | return value; 79 | } 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/Issue25Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.ByteArrayInputStream; 20 | import java.io.FileNotFoundException; 21 | import java.io.IOException; 22 | import java.io.InputStream; 23 | 24 | import org.junit.jupiter.api.Assertions; 25 | import org.junit.jupiter.api.Test; 26 | 27 | import jakarta.json.JsonObject; 28 | import jakarta.json.spi.JsonProvider; 29 | import jakarta.json.stream.JsonParser; 30 | 31 | public class Issue25Test { 32 | 33 | @Test 34 | void doubleClose() throws IOException { 35 | byte[] content = "[\"test\"]".getBytes(); 36 | JsonProvider json = JsonProvider.provider(); 37 | for (int i = 0; i < 3; i++) { 38 | try (InputStream in = new ByteArrayInputStream(content)) { 39 | try (JsonParser parser = json.createParser(in)) { 40 | JsonParser.Event firstEvent = parser.next(); 41 | Assertions.assertEquals(JsonParser.Event.START_ARRAY, firstEvent); 42 | while (parser.hasNext()) { 43 | JsonParser.Event event = parser.next(); 44 | if (event == JsonParser.Event.START_OBJECT) { 45 | JsonObject object = parser.getObject(); 46 | object.toString(); 47 | } 48 | } 49 | parser.close(); 50 | } 51 | } 52 | } 53 | } 54 | 55 | @Test 56 | void doubleCloseWithMoreContent() throws IOException { 57 | byte[] content = loadResource("/comments.json"); 58 | JsonProvider json = JsonProvider.provider(); 59 | for (int i = 0; i < 3; i++) { 60 | try (InputStream in = new ByteArrayInputStream(content)) { 61 | try (JsonParser parser = json.createParser(in)) { 62 | JsonParser.Event firstEvent = parser.next(); 63 | Assertions.assertEquals(JsonParser.Event.START_ARRAY, firstEvent); 64 | while (parser.hasNext()) { 65 | JsonParser.Event event = parser.next(); 66 | if (event == JsonParser.Event.START_OBJECT) { 67 | JsonObject object = parser.getObject(); 68 | object.toString(); 69 | } 70 | } 71 | // Closing 72 | parser.close(); 73 | } 74 | } 75 | } 76 | } 77 | 78 | private byte[] loadResource(String name) throws IOException { 79 | try (InputStream in = openResource(name)) { 80 | return in.readAllBytes(); 81 | } 82 | } 83 | 84 | private InputStream openResource(String name) throws FileNotFoundException { 85 | InputStream in = Issue25Test.class.getResourceAsStream(name); 86 | if (in == null) throw new FileNotFoundException(name); 87 | return in; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalLengthLimitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.StringReader; 20 | import java.math.BigDecimal; 21 | 22 | import jakarta.json.Json; 23 | import jakarta.json.JsonNumber; 24 | import jakarta.json.JsonReader; 25 | import jakarta.json.JsonValue; 26 | 27 | import org.eclipse.parsson.api.JsonConfig; 28 | import org.junit.jupiter.api.AfterEach; 29 | import org.junit.jupiter.api.Assertions; 30 | import org.junit.jupiter.api.BeforeEach; 31 | import org.junit.jupiter.api.Test; 32 | 33 | /** 34 | * Test maxBigDecimalLength limit set from System property. 35 | */ 36 | public class JsonBigDecimalLengthLimitTest { 37 | 38 | @BeforeEach 39 | void setUp() { 40 | System.setProperty(JsonConfig.MAX_BIGDECIMAL_LEN, "500"); 41 | } 42 | 43 | @AfterEach 44 | void tearDown() { 45 | System.clearProperty(JsonConfig.MAX_BIGDECIMAL_LEN); 46 | } 47 | 48 | // Test BigDecimal max source characters array length using length equal to system property limit of 500. 49 | // Parsing shall pass and return value equal to source String. 50 | @Test 51 | void testLargeBigDecimalBellowLimit() { 52 | JsonReader reader = Json.createReader(new StringReader(JsonNumberTest.Π_500)); 53 | JsonNumber check = Json.createValue(new BigDecimal(JsonNumberTest.Π_500)); 54 | JsonValue value = reader.readValue(); 55 | Assertions.assertEquals(value.getValueType(), JsonValue.ValueType.NUMBER); 56 | Assertions.assertEquals(value, check); 57 | } 58 | 59 | // Test BigDecimal max source characters array length using length above system property limit of 500. 60 | // Parsing shall pass and return value equal to source String. 61 | @Test 62 | void testLargeBigDecimalAboveLimit() { 63 | JsonReader reader = Json.createReader(new StringReader(JsonNumberTest.Π_501)); 64 | UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, reader::readValue, 65 | "No exception was thrown from BigDecimal parsing with source characters array length over limit"); 66 | Assertions.assertEquals( 67 | "Number of BigDecimal source characters 501 exceeded maximal allowed value of 500", 68 | e.getMessage()); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonBigDecimalScaleLimitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.math.BigDecimal; 20 | import java.math.RoundingMode; 21 | 22 | import jakarta.json.Json; 23 | 24 | import org.eclipse.parsson.api.JsonConfig; 25 | 26 | import org.junit.jupiter.api.AfterEach; 27 | import org.junit.jupiter.api.Assertions; 28 | import org.junit.jupiter.api.BeforeEach; 29 | import org.junit.jupiter.api.Test; 30 | 31 | 32 | /** 33 | * Test maxBigIntegerScale limit set from System property. 34 | */ 35 | public class JsonBigDecimalScaleLimitTest { 36 | 37 | private static final int MAX_BIGINTEGER_SCALE = 50000; 38 | 39 | @BeforeEach 40 | void setUp() { 41 | System.setProperty(JsonConfig.MAX_BIGINTEGER_SCALE, Integer.toString(MAX_BIGINTEGER_SCALE)); 42 | } 43 | 44 | @AfterEach 45 | void tearDown() { 46 | System.clearProperty(JsonConfig.MAX_BIGINTEGER_SCALE); 47 | } 48 | 49 | // Test BigInteger scale value limit set from system property using value bellow limit. 50 | // Call shall return value. 51 | @Test 52 | void testSystemPropertyBigIntegerScaleBellowLimit() { 53 | BigDecimal value = new BigDecimal("3.1415926535897932384626433"); 54 | Json.createValue(value).bigIntegerValue(); 55 | } 56 | 57 | // Test BigInteger scale value limit set from system property using value above limit. 58 | // Call shall throw specific UnsupportedOperationException exception. 59 | // Default value is 100000 and system property lowered it to 50000 so value with scale 50001 60 | // test shall fail with exception message matching modified limits. 61 | @Test 62 | void testSystemPropertyBigIntegerScaleAboveLimit() { 63 | BigDecimal value = new BigDecimal("3.1415926535897932384626433") 64 | .setScale(50001, RoundingMode.HALF_UP); 65 | UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, 66 | () -> Json.createValue(value).bigIntegerValue(), 67 | "No exception was thrown from bigIntegerValue with scale over limit"); 68 | JsonNumberTest.assertExceptionMessageContainsNumber(e, 50001); 69 | JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE); 70 | System.clearProperty("org.eclipse.parsson.maxBigIntegerScale"); 71 | } 72 | 73 | // Test BigInteger scale value limit set from system property using value above limit. 74 | // Call shall throw specific UnsupportedOperationException exception. 75 | // Default value is 100000 and system property lowered it to 50000 so value with scale -50001 76 | // test shall fail with exception message matching modified limits. 77 | @Test 78 | void testSystemPropertyBigIntegerNegScaleAboveLimit() { 79 | BigDecimal value = new BigDecimal("3.1415926535897932384626433") 80 | .setScale(-50001, RoundingMode.HALF_UP); 81 | UnsupportedOperationException e = Assertions.assertThrows(UnsupportedOperationException.class, 82 | () -> Json.createValue(value).bigIntegerValue(), 83 | "No exception was thrown from bigIntegerValue with scale over limit"); 84 | JsonNumberTest.assertExceptionMessageContainsNumber(e, -50001); 85 | JsonNumberTest.assertExceptionMessageContainsNumber(e, MAX_BIGINTEGER_SCALE); 86 | System.clearProperty("org.eclipse.parsson.maxBigIntegerScale"); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonBuilderFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.util.Map; 20 | 21 | import jakarta.json.Json; 22 | import jakarta.json.JsonArrayBuilder; 23 | import jakarta.json.JsonBuilderFactory; 24 | import jakarta.json.JsonObject; 25 | import jakarta.json.JsonObjectBuilder; 26 | 27 | import org.junit.jupiter.api.Assertions; 28 | import org.junit.jupiter.api.Test; 29 | 30 | /** 31 | * 32 | * @author lukas 33 | */ 34 | public class JsonBuilderFactoryTest { 35 | 36 | @Test 37 | void testArrayBuilder() { 38 | JsonBuilderFactory builderFactory = Json.createBuilderFactory(null); 39 | Assertions.assertNotNull(builderFactory.createArrayBuilder()); 40 | } 41 | 42 | @Test 43 | void testArrayBuilderNPE() { 44 | Assertions.assertThrows(NullPointerException.class, () -> { 45 | JsonBuilderFactory builderFactory = Json.createBuilderFactory(null); 46 | builderFactory.createArrayBuilder(null); 47 | }); 48 | } 49 | 50 | @Test 51 | void testArrayBuilderFromArray() { 52 | JsonBuilderFactory builderFactory = Json.createBuilderFactory(null); 53 | JsonArrayBuilder builder = builderFactory.createArrayBuilder(JsonBuilderTest.buildPhone()); 54 | Assertions.assertEquals(JsonBuilderTest.buildPhone(), builder.build()); 55 | } 56 | 57 | @Test 58 | void testObjectBuilder() { 59 | JsonBuilderFactory builderFactory = Json.createBuilderFactory(null); 60 | Assertions.assertNotNull(builderFactory.createObjectBuilder()); 61 | } 62 | 63 | @Test 64 | void testObjectBuilderNPE() { 65 | Assertions.assertThrows(NullPointerException.class, () -> { 66 | JsonBuilderFactory builderFactory = Json.createBuilderFactory(null); 67 | builderFactory.createObjectBuilder((JsonObject) null); 68 | }); 69 | } 70 | 71 | @Test 72 | void testObjectBuilderNPE_map() { 73 | Assertions.assertThrows(NullPointerException.class, () -> { 74 | JsonBuilderFactory builderFactory = Json.createBuilderFactory(null); 75 | builderFactory.createObjectBuilder((Map) null); 76 | }); 77 | } 78 | 79 | @Test 80 | void testObjectBuilderFromObject() { 81 | JsonBuilderFactory builderFactory = Json.createBuilderFactory(null); 82 | JsonObjectBuilder builder = builderFactory.createObjectBuilder(JsonBuilderTest.buildPerson()); 83 | Assertions.assertEquals(JsonBuilderTest.buildPerson(), builder.build()); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonDuplicateKeyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.StringReader; 20 | import java.util.Collections; 21 | 22 | import jakarta.json.Json; 23 | import jakarta.json.JsonBuilderFactory; 24 | import jakarta.json.JsonConfig; 25 | import jakarta.json.JsonObject; 26 | import jakarta.json.JsonObjectBuilder; 27 | import jakarta.json.JsonReader; 28 | import jakarta.json.JsonReaderFactory; 29 | import jakarta.json.stream.JsonParsingException; 30 | 31 | import org.junit.jupiter.api.Assertions; 32 | import org.junit.jupiter.api.Test; 33 | 34 | 35 | public class JsonDuplicateKeyTest { 36 | @Test 37 | void testJsonReaderDuplicateKey1() { 38 | String json = "{\"a\":\"b\",\"a\":\"c\"}"; 39 | JsonReader jsonReader = Json.createReader(new StringReader(json)); 40 | JsonObject jsonObject = jsonReader.readObject(); 41 | Assertions.assertEquals(jsonObject.getString("a"), "c"); 42 | } 43 | 44 | @Test 45 | void testJsonReaderDuplicateKey2() { 46 | String json = "{\"a\":\"b\",\"a\":\"c\"}"; 47 | JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE)); 48 | JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(json)); 49 | JsonParsingException e = Assertions.assertThrows(JsonParsingException.class, jsonReader::readObject); 50 | Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage()); 51 | } 52 | 53 | @Test 54 | void testJsonReaderDuplicateKey3() { 55 | String json = "{\"a\":\"b\",\"b\":{\"c\":\"d\",\"c\":\"e\"}}"; 56 | JsonReader jsonReader = Json.createReader(new StringReader(json)); 57 | JsonObject jsonObject = jsonReader.readObject(); 58 | Assertions.assertEquals(jsonObject.getJsonObject("b").getString("c"), "e"); 59 | } 60 | 61 | @Test 62 | void testJsonReaderDuplicateKey4() { 63 | String json = "{\"a\":\"b\",\"b\":{\"c\":\"d\",\"c\":\"e\"}}"; 64 | JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE)); 65 | JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(json)); 66 | JsonParsingException e = Assertions.assertThrows(JsonParsingException.class, jsonReader::readObject); 67 | Assertions.assertEquals("Duplicate key 'c' is not allowed", e.getMessage()); 68 | } 69 | 70 | @Test 71 | void testJsonObjectBuilderDuplcateKey1() { 72 | JsonObjectBuilder objectBuilder = Json.createObjectBuilder(); 73 | JsonObject jsonObject = objectBuilder.add("a", "b").add("a", "c").build(); 74 | Assertions.assertEquals(jsonObject.getString("a"), "c"); 75 | } 76 | 77 | @Test 78 | void testJsonObjectBuilderDuplcateKey2() { 79 | JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(Collections.singletonMap(JsonConfig.KEY_STRATEGY, JsonConfig.KeyStrategy.NONE)); 80 | JsonObjectBuilder objectBuilder = jsonBuilderFactory.createObjectBuilder(); 81 | IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> 82 | objectBuilder.add("a", "b").add("a", "c").build()); 83 | Assertions.assertEquals("Duplicate key 'a' is not allowed", e.getMessage()); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonGeneratorFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.StringWriter; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import jakarta.json.Json; 24 | import jakarta.json.JsonException; 25 | import jakarta.json.stream.JsonGenerator; 26 | import jakarta.json.stream.JsonGeneratorFactory; 27 | 28 | import org.junit.jupiter.api.Assertions; 29 | import org.junit.jupiter.api.Test; 30 | 31 | 32 | /** 33 | * Tests JsonGeneratorFactory 34 | * 35 | * @author Jitendra Kotamraju 36 | */ 37 | public class JsonGeneratorFactoryTest { 38 | 39 | @Test 40 | void testGeneratorFactory() { 41 | JsonGeneratorFactory generatorFactory = Json.createGeneratorFactory(null); 42 | 43 | JsonGenerator generator1 = generatorFactory.createGenerator(new StringWriter()); 44 | generator1.writeStartArray().writeEnd(); 45 | generator1.close(); 46 | 47 | JsonGenerator generator2 = generatorFactory.createGenerator(new StringWriter()); 48 | generator2.writeStartArray().writeEnd(); 49 | generator2.close(); 50 | } 51 | 52 | @Test 53 | void testGeneratorFactoryWithConfig() { 54 | Map config = new HashMap<>(); 55 | config.put(JsonGenerator.PRETTY_PRINTING, true); 56 | JsonGeneratorFactory generatorFactory = Json.createGeneratorFactory(config); 57 | Map config1 = generatorFactory.getConfigInUse(); 58 | if (config1.size() != 1) { 59 | throw new JsonException("Expecting no of properties=1, got="+config1.size()); 60 | } 61 | Assertions.assertTrue(config1.containsKey(JsonGenerator.PRETTY_PRINTING)); 62 | 63 | JsonGenerator generator1 = generatorFactory.createGenerator(new StringWriter()); 64 | generator1.writeStartArray().writeEnd(); 65 | generator1.close(); 66 | 67 | JsonGenerator generator2 = generatorFactory.createGenerator(new StringWriter()); 68 | generator2.writeStartArray().writeEnd(); 69 | generator2.close(); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatch2Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import jakarta.json.Json; 20 | import jakarta.json.JsonArray; 21 | import jakarta.json.JsonMergePatch; 22 | import jakarta.json.JsonPatch; 23 | 24 | import org.eclipse.parsson.TestUtils; 25 | 26 | import org.junit.jupiter.api.Assertions; 27 | import org.junit.jupiter.api.Test; 28 | 29 | 30 | public class JsonMergePatch2Test { 31 | 32 | @Test 33 | void testToString() { 34 | JsonArray jsonArray = Json.createArrayBuilder().add(Json.createValue(1)).build(); 35 | JsonPatch jsonPatch = Json.createPatchBuilder(jsonArray).build(); 36 | Assertions.assertEquals("[1]", jsonPatch.toString()); 37 | JsonMergePatch jsonMergePatch = Json.createMergePatch(jsonArray); 38 | Assertions.assertEquals("[1]", jsonMergePatch.toString()); 39 | } 40 | 41 | @Test 42 | void testEquals() { 43 | JsonMergePatch j1 = TestUtils.createJsonMergePatchImpl(Json.createValue("test")); 44 | JsonMergePatch j2 = TestUtils.createJsonMergePatchImpl(Json.createValue("test")); 45 | JsonMergePatch j3 = TestUtils.createJsonMergePatchImpl(j1.toJsonValue()); 46 | JsonMergePatch j4 = TestUtils.createJsonMergePatchImpl(Json.createValue("test2")); 47 | JsonMergePatch j5 = TestUtils.createJsonMergePatchImpl(null); 48 | 49 | Assertions.assertTrue(j1.equals(j1)); 50 | 51 | Assertions.assertTrue(j1.equals(j2)); 52 | Assertions.assertTrue(j2.equals(j1)); 53 | 54 | Assertions.assertTrue(j1.equals(j3)); 55 | Assertions.assertTrue(j3.equals(j1)); 56 | 57 | Assertions.assertTrue(j2.equals(j3)); 58 | Assertions.assertTrue(j3.equals(j2)); 59 | 60 | Assertions.assertFalse(j1.equals(j4)); 61 | Assertions.assertFalse(j1.equals(j5)); 62 | Assertions.assertFalse(j1.equals(null)); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchDiffTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.InputStream; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import jakarta.json.Json; 24 | import jakarta.json.JsonArray; 25 | import jakarta.json.JsonObject; 26 | import jakarta.json.JsonReader; 27 | import jakarta.json.JsonString; 28 | import jakarta.json.JsonValue; 29 | 30 | import org.hamcrest.CoreMatchers; 31 | import org.hamcrest.MatcherAssert; 32 | import org.junit.jupiter.api.Assertions; 33 | import org.junit.jupiter.params.ParameterizedTest; 34 | import org.junit.jupiter.params.provider.MethodSource; 35 | 36 | 37 | /** 38 | * 39 | * @author Alex Soto 40 | * 41 | */ 42 | public class JsonMergePatchDiffTest { 43 | 44 | public static Iterable data() throws Exception { 45 | List examples = new ArrayList(); 46 | JsonArray data = loadData(); 47 | for (JsonValue jsonValue : data) { 48 | JsonObject test = (JsonObject) jsonValue; 49 | Object[] testData = new Object[4]; 50 | testData[0] = test.get("original"); 51 | testData[1] = test.get("target"); 52 | testData[2] = test.get("expected"); 53 | testData[3] = createExceptionClass((JsonString)test.get("exception")); 54 | 55 | examples.add(testData); 56 | } 57 | 58 | return examples; 59 | } 60 | 61 | @SuppressWarnings("unchecked") 62 | private static Class createExceptionClass( 63 | JsonString exceptionClassName) throws ClassNotFoundException { 64 | if (exceptionClassName != null) { 65 | return (Class) Class 66 | .forName(exceptionClassName.getString()); 67 | } 68 | return null; 69 | } 70 | 71 | private static JsonArray loadData() { 72 | InputStream testData = JsonPatchTest.class 73 | .getResourceAsStream("/jsonmergepatchdiff.json"); 74 | JsonReader reader = Json.createReader(testData); 75 | return (JsonArray) reader.read(); 76 | } 77 | 78 | @MethodSource("data") 79 | @ParameterizedTest(name = "{index}: ({0})={1}") 80 | void shouldExecuteJsonMergePatchDiffOperationsToJsonDocument(JsonValue original, JsonValue target, JsonValue expected, Class expectedException) { 81 | try { 82 | JsonValue output = Json.createMergeDiff(original, target).toJsonValue(); 83 | MatcherAssert.assertThat(output, CoreMatchers.is(expected)); 84 | MatcherAssert.assertThat(expectedException, CoreMatchers.nullValue()); 85 | } catch (Exception e) { 86 | if (expectedException == null) { 87 | Assertions.fail(e.getMessage()); 88 | } else { 89 | MatcherAssert.assertThat(e, CoreMatchers.instanceOf(expectedException)); 90 | } 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.InputStream; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import jakarta.json.Json; 24 | import jakarta.json.JsonArray; 25 | import jakarta.json.JsonObject; 26 | import jakarta.json.JsonReader; 27 | import jakarta.json.JsonString; 28 | import jakarta.json.JsonValue; 29 | 30 | import org.hamcrest.CoreMatchers; 31 | import org.hamcrest.MatcherAssert; 32 | import org.junit.jupiter.api.Assertions; 33 | import org.junit.jupiter.params.ParameterizedTest; 34 | import org.junit.jupiter.params.provider.MethodSource; 35 | 36 | 37 | /** 38 | * 39 | * @author Alex Soto 40 | * 41 | */ 42 | public class JsonMergePatchTest { 43 | 44 | public static Iterable data() throws Exception { 45 | List examples = new ArrayList(); 46 | JsonArray data = loadData(); 47 | for (JsonValue jsonValue : data) { 48 | JsonObject test = (JsonObject) jsonValue; 49 | Object[] testData = new Object[4]; 50 | testData[0] = test.get("patch"); 51 | testData[1] = test.get("target"); 52 | testData[2] = test.get("expected"); 53 | testData[3] = createExceptionClass((JsonString)test.get("exception")); 54 | 55 | examples.add(testData); 56 | } 57 | 58 | return examples; 59 | } 60 | 61 | @SuppressWarnings("unchecked") 62 | private static Class createExceptionClass( 63 | JsonString exceptionClassName) throws ClassNotFoundException { 64 | if (exceptionClassName != null) { 65 | return (Class) Class 66 | .forName(exceptionClassName.getString()); 67 | } 68 | return null; 69 | } 70 | 71 | private static JsonArray loadData() { 72 | InputStream testData = JsonPatchTest.class 73 | .getResourceAsStream("/jsonmergepatch.json"); 74 | JsonReader reader = Json.createReader(testData); 75 | return (JsonArray) reader.read(); 76 | } 77 | 78 | @MethodSource("data") 79 | @ParameterizedTest(name = "{index}: ({0})={1}") 80 | void shouldExecuteJsonMergePatchDiffOperationsToJsonDocument(JsonValue patch, JsonValue target, JsonValue expected, Class expectedException) { 81 | try { 82 | JsonValue output = Json.createMergePatch(patch).apply(target); 83 | MatcherAssert.assertThat(output, CoreMatchers.is(expected)); 84 | MatcherAssert.assertThat(expectedException, CoreMatchers.nullValue()); 85 | } catch (Exception e) { 86 | if (expectedException == null) { 87 | Assertions.fail(e.getMessage()); 88 | } else { 89 | MatcherAssert.assertThat(e, CoreMatchers.instanceOf(expectedException)); 90 | } 91 | } 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonNestingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.StringReader; 20 | 21 | import jakarta.json.Json; 22 | import jakarta.json.stream.JsonParser; 23 | 24 | import org.junit.jupiter.api.Assertions; 25 | import org.junit.jupiter.api.Test; 26 | 27 | public class JsonNestingTest { 28 | 29 | @Test 30 | void testArrayNestingException() { 31 | Assertions.assertThrows(RuntimeException.class, () -> { 32 | String json = createDeepNestedDoc(500); 33 | try (JsonParser parser = Json.createParser(new StringReader(json))) { 34 | while (parser.hasNext()) { 35 | JsonParser.Event ev = parser.next(); 36 | if (JsonParser.Event.START_ARRAY == ev) { 37 | parser.getArray(); 38 | } 39 | } 40 | } 41 | }); 42 | } 43 | 44 | @Test 45 | void testArrayNesting() { 46 | String json = createDeepNestedDoc(499); 47 | try (JsonParser parser = Json.createParser(new StringReader(json))) { 48 | while (parser.hasNext()) { 49 | JsonParser.Event ev = parser.next(); 50 | if (JsonParser.Event.START_ARRAY == ev) { 51 | parser.getArray(); 52 | } 53 | } 54 | } 55 | } 56 | 57 | @Test 58 | void testObjectNestingException() { 59 | Assertions.assertThrows(RuntimeException.class, () -> { 60 | String json = createDeepNestedDoc(500); 61 | try (JsonParser parser = Json.createParser(new StringReader(json))) { 62 | while (parser.hasNext()) { 63 | JsonParser.Event ev = parser.next(); 64 | if (JsonParser.Event.START_OBJECT == ev) { 65 | parser.getObject(); 66 | } 67 | } 68 | } 69 | }); 70 | } 71 | 72 | @Test 73 | void testObjectNesting() { 74 | String json = createDeepNestedDoc(499); 75 | try (JsonParser parser = Json.createParser(new StringReader(json))) { 76 | while (parser.hasNext()) { 77 | JsonParser.Event ev = parser.next(); 78 | if (JsonParser.Event.START_OBJECT == ev) { 79 | parser.getObject(); 80 | } 81 | } 82 | } 83 | } 84 | 85 | private static String createDeepNestedDoc(final int depth) { 86 | StringBuilder sb = new StringBuilder(); 87 | sb.append("["); 88 | for (int i = 0; i < depth; i++) { 89 | sb.append("{ \"a\": ["); 90 | } 91 | sb.append(" \"val\" "); 92 | for (int i = 0; i < depth; i++) { 93 | sb.append("]}"); 94 | } 95 | sb.append("]"); 96 | return sb.toString(); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonParserFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import jakarta.json.Json; 20 | import jakarta.json.stream.JsonParser; 21 | import jakarta.json.stream.JsonParserFactory; 22 | import java.io.StringReader; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import org.junit.jupiter.api.Test; 27 | 28 | /** 29 | * Tests JsonParserFactory 30 | * 31 | * @author Jitendra Kotamraju 32 | */ 33 | public class JsonParserFactoryTest { 34 | 35 | @Test 36 | void testParserFactory() { 37 | JsonParserFactory parserFactory = Json.createParserFactory(null); 38 | JsonParser parser1 = parserFactory.createParser(new StringReader("[]")); 39 | parser1.close(); 40 | JsonParser parser2 = parserFactory.createParser(new StringReader("[]")); 41 | parser2.close(); 42 | } 43 | 44 | @Test 45 | void testParserFactoryWithConfig() { 46 | Map config = new HashMap<>(); 47 | JsonParserFactory parserFactory = Json.createParserFactory(config); 48 | JsonParser parser1 = parserFactory.createParser(new StringReader("[]")); 49 | parser1.close(); 50 | JsonParser parser2 = parserFactory.createParser(new StringReader("[]")); 51 | parser2.close(); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPatchBugsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import jakarta.json.Json; 20 | import jakarta.json.JsonArray; 21 | import jakarta.json.JsonException; 22 | import jakarta.json.JsonPatch; 23 | import jakarta.json.JsonReader; 24 | import jakarta.json.JsonStructure; 25 | 26 | import java.io.StringReader; 27 | 28 | import org.junit.jupiter.api.Assertions; 29 | import org.junit.jupiter.api.Test; 30 | 31 | /** 32 | * 33 | * @author lukas 34 | */ 35 | public class JsonPatchBugsTest { 36 | 37 | // https://github.com/javaee/jsonp/issues/58 38 | @Test 39 | void applyThrowsJsonException() { 40 | Assertions.assertThrows(JsonException.class, () -> { 41 | JsonArray array = Json.createArrayBuilder() 42 | .add(Json.createObjectBuilder() 43 | .add("name", "Bob") 44 | .build()) 45 | .build(); 46 | JsonPatch patch = Json.createPatchBuilder() 47 | .replace("/0/name", "Bobek") 48 | .replace("/1/name", "Vila Amalka") 49 | .build(); 50 | patch.apply(array); 51 | }); 52 | } 53 | 54 | // https://github.com/eclipse-ee4j/jsonp/issues/181 55 | @Test 56 | void applyThrowsJsonException2() { 57 | Assertions.assertThrows(JsonException.class, () -> { 58 | // JSON document to be patched 59 | String targetDocument 60 | = "{\n" 61 | + " \"firstName\": \"John\",\n" 62 | + " \"lastName\": \"Doe\"\n" 63 | + "}"; 64 | 65 | // JSON Patch document 66 | // Instead of "op", we have "op_", which is invalid 67 | String patchDocument 68 | = "[\n" 69 | + " { \"op_\": \"replace\", \"path\": \"/firstName\", \"value\": \"Jane\" }\n" 70 | + "]"; 71 | 72 | try (JsonReader objectReader = Json.createReader(new StringReader(targetDocument)); 73 | JsonReader arrayReader = Json.createReader(new StringReader(patchDocument))) { 74 | 75 | JsonStructure target = objectReader.read(); 76 | JsonPatch patch = Json.createPatch(arrayReader.readArray()); 77 | 78 | // Applies the patch 79 | // It will throw a NullPointerException with no message 80 | patch.apply(target); 81 | } 82 | }); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPatchBuilderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import jakarta.json.Json; 20 | import jakarta.json.JsonObject; 21 | import jakarta.json.JsonPatchBuilder; 22 | 23 | import org.hamcrest.CoreMatchers; 24 | import org.hamcrest.MatcherAssert; 25 | import org.junit.jupiter.api.Test; 26 | 27 | /** 28 | * 29 | * @author Alex Soto 30 | * 31 | */ 32 | public class JsonPatchBuilderTest { 33 | 34 | @Test 35 | void shouldBuildJsonPatchExpressionUsingJsonPatchBuilder() { 36 | JsonPatchBuilder patchBuilder = Json.createPatchBuilder(); 37 | JsonObject result = patchBuilder.add("/email", "john@example.com") 38 | .replace("/age", 30) 39 | .remove("/phoneNumber") 40 | .test("/firstName", "John") 41 | .copy("/address/lastName", "/lastName") 42 | .build() 43 | .apply(buildPerson()); 44 | MatcherAssert.assertThat(result, CoreMatchers.is(expectedBuildPerson())); 45 | 46 | } 47 | 48 | static JsonObject expectedBuildPerson() { 49 | return Json.createObjectBuilder() 50 | .add("firstName", "John") 51 | .add("lastName", "Smith") 52 | .add("email", "john@example.com") 53 | .add("age", 30) 54 | .add("address", Json.createObjectBuilder() 55 | .add("lastName", "Smith") 56 | .add("streetAddress", "21 2nd Street") 57 | .add("city", "New York") 58 | .add("state", "NY") 59 | .add("postalCode", "10021")) 60 | .build(); 61 | } 62 | 63 | static JsonObject buildPerson() { 64 | return Json.createObjectBuilder() 65 | .add("firstName", "John") 66 | .add("lastName", "Smith") 67 | .add("age", 25) 68 | .add("address", Json.createObjectBuilder() 69 | .add("streetAddress", "21 2nd Street") 70 | .add("city", "New York") 71 | .add("state", "NY") 72 | .add("postalCode", "10021")) 73 | .add("phoneNumber", Json.createArrayBuilder() 74 | .add(Json.createObjectBuilder() 75 | .add("type", "home") 76 | .add("number", "212 555-1234")) 77 | .add(Json.createObjectBuilder() 78 | .add("type", "fax") 79 | .add("number", "646 555-4567"))) 80 | .build(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPatchDiffTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.InputStream; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import jakarta.json.Json; 24 | import jakarta.json.JsonArray; 25 | import jakarta.json.JsonObject; 26 | import jakarta.json.JsonPatch; 27 | import jakarta.json.JsonReader; 28 | import jakarta.json.JsonString; 29 | import jakarta.json.JsonStructure; 30 | import jakarta.json.JsonValue; 31 | 32 | import org.hamcrest.CoreMatchers; 33 | import org.hamcrest.MatcherAssert; 34 | import org.junit.jupiter.api.Assertions; 35 | import org.junit.jupiter.params.ParameterizedTest; 36 | import org.junit.jupiter.params.provider.MethodSource; 37 | 38 | 39 | /** 40 | * 41 | * @author Alex Soto 42 | * 43 | */ 44 | public class JsonPatchDiffTest { 45 | 46 | public static Iterable data() throws Exception { 47 | List examples = new ArrayList<>(); 48 | JsonArray data = JsonPatchDiffTest.loadData(); 49 | for (JsonValue jsonValue : data) { 50 | JsonObject test = (JsonObject) jsonValue; 51 | Object[] testData = new Object[4]; 52 | testData[0] = test.get("original"); 53 | testData[1] = test.get("target"); 54 | testData[2] = test.get("expected"); 55 | testData[3] = createExceptionClass((JsonString)test.get("exception")); 56 | 57 | examples.add(testData); 58 | } 59 | 60 | return examples; 61 | } 62 | 63 | @SuppressWarnings("unchecked") 64 | private static Class createExceptionClass( 65 | JsonString exceptionClassName) throws ClassNotFoundException { 66 | if (exceptionClassName != null) { 67 | return (Class) Class 68 | .forName(exceptionClassName.getString()); 69 | } 70 | return null; 71 | } 72 | 73 | private static JsonArray loadData() { 74 | InputStream testData = JsonPatchTest.class 75 | .getResourceAsStream("/jsonpatchdiff.json"); 76 | 77 | JsonArray data; 78 | try(JsonReader reader = Json.createReader(testData)){ 79 | data = (JsonArray) reader.read(); 80 | } 81 | 82 | return data; 83 | } 84 | 85 | @MethodSource("data") 86 | @ParameterizedTest(name = "{index}: ({0})={1}") 87 | void shouldExecuteJsonPatchDiffOperationsToJsonDocument(JsonStructure original, JsonStructure target, JsonValue expected, Class expectedException) { 88 | try { 89 | JsonPatch diff = Json.createDiff(original, target); 90 | MatcherAssert.assertThat(diff, CoreMatchers.is(Json.createPatchBuilder((JsonArray) expected).build())); 91 | MatcherAssert.assertThat(expectedException, CoreMatchers.nullValue()); 92 | } catch (Exception e) { 93 | if (expectedException == null) { 94 | Assertions.fail(e.getMessage()); 95 | } else { 96 | MatcherAssert.assertThat(e, CoreMatchers.instanceOf(expectedException)); 97 | } 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPatchOperationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import jakarta.json.JsonException; 20 | import jakarta.json.JsonPatch.Operation; 21 | 22 | import org.junit.jupiter.api.Assertions; 23 | import org.junit.jupiter.api.Test; 24 | 25 | /** 26 | * 27 | * @author lukas 28 | */ 29 | public class JsonPatchOperationTest { 30 | 31 | private static final String[] opNames = {"add", "remove", "replace", "move", "copy", "test"}; 32 | 33 | @Test 34 | void fromOperationName() { 35 | for (String op: opNames) { 36 | Assertions.assertEquals(Operation.valueOf(op.toUpperCase()), Operation.fromOperationName(op)); 37 | } 38 | for (String op: opNames) { 39 | Assertions.assertEquals(Operation.valueOf(op.toUpperCase()), Operation.fromOperationName(op.toUpperCase())); 40 | } 41 | } 42 | 43 | @Test 44 | void fromInvalidOperationName() { 45 | Assertions.assertThrows(JsonException.class, () -> Operation.fromOperationName("undef")); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPatchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.InputStream; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import jakarta.json.Json; 24 | import jakarta.json.JsonArray; 25 | import jakarta.json.JsonObject; 26 | import jakarta.json.JsonPatch; 27 | import jakarta.json.JsonReader; 28 | import jakarta.json.JsonString; 29 | import jakarta.json.JsonStructure; 30 | import jakarta.json.JsonValue; 31 | 32 | import org.hamcrest.CoreMatchers; 33 | import org.hamcrest.MatcherAssert; 34 | import org.junit.jupiter.api.Assertions; 35 | import org.junit.jupiter.params.ParameterizedTest; 36 | import org.junit.jupiter.params.provider.MethodSource; 37 | 38 | /** 39 | * 40 | * @author Alex Soto 41 | * 42 | */ 43 | public class JsonPatchTest { 44 | 45 | public static Iterable data() throws Exception { 46 | List examples = new ArrayList<>(); 47 | JsonArray data = loadData(); 48 | for (JsonValue jsonValue : data) { 49 | JsonObject test = (JsonObject) jsonValue; 50 | Object[] testData = new Object[4]; 51 | testData[0] = createPatchArray(test.get("op")); 52 | testData[1] = test.get("target"); 53 | testData[2] = test.get("expected"); 54 | testData[3] = createExceptionClass((JsonString)test.get("exception")); 55 | 56 | examples.add(testData); 57 | } 58 | 59 | return examples; 60 | } 61 | 62 | @SuppressWarnings("unchecked") 63 | private static Class createExceptionClass( 64 | JsonString exceptionClassName) throws ClassNotFoundException { 65 | if (exceptionClassName != null) { 66 | return (Class) Class 67 | .forName(exceptionClassName.getString()); 68 | } 69 | return null; 70 | } 71 | 72 | private static JsonArray createPatchArray(JsonValue object) { 73 | return Json.createArrayBuilder().add(object).build(); 74 | } 75 | 76 | private static JsonArray loadData() { 77 | InputStream testData = JsonPatchTest.class 78 | .getResourceAsStream("/jsonpatch.json"); 79 | JsonReader reader = Json.createReader(testData); 80 | return (JsonArray) reader.read(); 81 | } 82 | 83 | @MethodSource("data") 84 | @ParameterizedTest(name = "{index}: ({0})={1}") 85 | void shouldExecuteJsonPatchOperationsToJsonDocument(JsonArray arrayPatch, JsonStructure target, JsonValue expected, Class expectedException) { 86 | try { 87 | JsonPatch patch = Json.createPatchBuilder(arrayPatch).build(); 88 | JsonStructure output = patch.apply(target); 89 | MatcherAssert.assertThat(output, CoreMatchers.is(expected)); 90 | MatcherAssert.assertThat(expectedException, CoreMatchers.nullValue()); 91 | } catch (Exception e) { 92 | if (expectedException == null) { 93 | Assertions.fail(e.getMessage()); 94 | } else { 95 | MatcherAssert.assertThat(e, CoreMatchers.instanceOf(expectedException)); 96 | } 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPointerEscapeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | 20 | import jakarta.json.Json; 21 | 22 | import org.junit.jupiter.api.Assertions; 23 | import org.junit.jupiter.api.Test; 24 | 25 | /** 26 | * JSON pointer escape/unescape tests. 27 | * 28 | * @author Dmitry Kornilov 29 | */ 30 | public class JsonPointerEscapeTest { 31 | 32 | @Test 33 | void escapeTest() { 34 | Assertions.assertEquals("a~1b", Json.encodePointer("a/b")); 35 | Assertions.assertEquals("a~0b~1c", Json.encodePointer("a~b/c")); 36 | } 37 | 38 | @Test 39 | void unescapeTest() { 40 | Assertions.assertEquals("/a/b", Json.decodePointer("/a~1b")); 41 | Assertions.assertEquals("/~1", Json.decodePointer("/~01")); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPointerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.InputStreamReader; 20 | import java.io.Reader; 21 | import java.util.Arrays; 22 | import java.util.Objects; 23 | 24 | import jakarta.json.Json; 25 | import jakarta.json.JsonException; 26 | import jakarta.json.JsonObject; 27 | import jakarta.json.JsonPointer; 28 | import jakarta.json.JsonReader; 29 | import jakarta.json.JsonValue; 30 | 31 | import org.hamcrest.CoreMatchers; 32 | import org.hamcrest.MatcherAssert; 33 | import org.junit.jupiter.api.Assertions; 34 | import org.junit.jupiter.params.ParameterizedTest; 35 | import org.junit.jupiter.params.provider.MethodSource; 36 | 37 | /** 38 | * 39 | * @author Alex Soto 40 | * 41 | */ 42 | public class JsonPointerTest { 43 | 44 | private static JsonObject rfc6901Example; 45 | 46 | public static Iterable data() throws Exception { 47 | rfc6901Example = JsonPointerTest.readRfc6901Example(); 48 | return Arrays.asList(new Object[][] { 49 | {Json.createPointer(""), rfc6901Example, null }, 50 | {Json.createPointer("/foo"), rfc6901Example.getJsonArray("foo"), null}, 51 | {Json.createPointer("/foo/0"), rfc6901Example.getJsonArray("foo").get(0), null}, 52 | {Json.createPointer("/foo/5"), null, JsonException.class}, 53 | {Json.createPointer("/p/1"), null, JsonException.class}, 54 | {Json.createPointer("/"), rfc6901Example.getJsonNumber(""), null}, 55 | {Json.createPointer("/a~1b"), rfc6901Example.getJsonNumber("a/b"), null}, 56 | {Json.createPointer("/m~0n"), rfc6901Example.getJsonNumber("m~n"), null}, 57 | {Json.createPointer("/c%d"), rfc6901Example.getJsonNumber("c%d"), null}, 58 | {Json.createPointer("/e^f"), rfc6901Example.getJsonNumber("e^f"), null}, 59 | {Json.createPointer("/g|h"), rfc6901Example.getJsonNumber("g|h"), null}, 60 | {Json.createPointer("/i\\j"), rfc6901Example.getJsonNumber("i\\j"), null}, 61 | {Json.createPointer("/k\"l"), rfc6901Example.getJsonNumber("k\"l"), null}, 62 | {Json.createPointer("/ "), rfc6901Example.getJsonNumber(" "), null}, 63 | {Json.createPointer("/notexists"), null, JsonException.class}, 64 | {Json.createPointer("/s/t"), null, JsonException.class}, 65 | {Json.createPointer("/o"), JsonObject.NULL, null} 66 | }); 67 | } 68 | 69 | @MethodSource("data") 70 | @ParameterizedTest(name = "{index}: ({0})={1}") 71 | void shouldEvaluateJsonPointerExpressions(JsonPointer pointer, JsonValue expected, Class expectedException) { 72 | try { 73 | JsonValue result = pointer.getValue(rfc6901Example); 74 | MatcherAssert.assertThat(result, CoreMatchers.is(expected)); 75 | MatcherAssert.assertThat(expectedException, CoreMatchers.nullValue()); 76 | } catch(Exception e) { 77 | if(expectedException == null) { 78 | Assertions.fail(e.getMessage()); 79 | } else { 80 | MatcherAssert.assertThat(e, CoreMatchers.instanceOf(expectedException)); 81 | } 82 | } 83 | } 84 | 85 | static JsonObject readRfc6901Example() { 86 | Reader rfc6901Reader = new InputStreamReader(Objects.requireNonNull(JsonReaderTest.class.getResourceAsStream("/rfc6901.json"))); 87 | JsonReader reader = Json.createReader(rfc6901Reader); 88 | JsonObject value = reader.readObject(); 89 | reader.close(); 90 | return value; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonPointerToStringTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.util.Arrays; 20 | 21 | import jakarta.json.Json; 22 | import jakarta.json.JsonPointer; 23 | 24 | import org.hamcrest.CoreMatchers; 25 | import org.hamcrest.MatcherAssert; 26 | import org.junit.jupiter.params.ParameterizedTest; 27 | import org.junit.jupiter.params.provider.MethodSource; 28 | 29 | /** 30 | * JSON pointer toString tests. 31 | * 32 | * @author leadpony 33 | */ 34 | public class JsonPointerToStringTest { 35 | 36 | public static Iterable data() { 37 | return Arrays.asList("", "/", "/one/two/3", "/a~1b", "/m~0n"); 38 | } 39 | 40 | @MethodSource("data") 41 | @ParameterizedTest(name = "{index}: {0}") 42 | void shouldReturnOriginalEscapedString(String expected) { 43 | JsonPointer pointer = Json.createPointer(expected); 44 | MatcherAssert.assertThat(pointer.toString(), CoreMatchers.is(CoreMatchers.equalTo(expected))); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonSamplesParsingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.InputStreamReader; 20 | import java.io.Reader; 21 | import java.nio.charset.StandardCharsets; 22 | import java.util.Objects; 23 | 24 | import jakarta.json.Json; 25 | import jakarta.json.JsonException; 26 | import jakarta.json.stream.JsonParser; 27 | 28 | import org.junit.jupiter.api.Test; 29 | 30 | /** 31 | * JsonParser tests for sample files 32 | * 33 | * @author Jitendra Kotamraju 34 | */ 35 | public class JsonSamplesParsingTest { 36 | 37 | @Test 38 | void testSampleFiles() { 39 | String[] fileNames = { 40 | "facebook.json", "facebook1.json", "facebook2.json", 41 | "twitter.json" 42 | }; 43 | for(String fileName: fileNames) { 44 | try { 45 | testSampleFile(fileName); 46 | } catch(Exception e) { 47 | throw new JsonException("Exception while parsing "+fileName, e); 48 | } 49 | } 50 | } 51 | 52 | private void testSampleFile(String fileName) { 53 | Reader reader = new InputStreamReader( 54 | Objects.requireNonNull(JsonSamplesParsingTest.class.getResourceAsStream("/" + fileName)), StandardCharsets.UTF_8); 55 | try (JsonParser parser = Json.createParser(reader)) { 56 | while (parser.hasNext()) { 57 | parser.next(); 58 | } 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/JsonStringTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.StringReader; 20 | 21 | import jakarta.json.Json; 22 | import jakarta.json.JsonArray; 23 | import jakarta.json.JsonReader; 24 | import jakarta.json.JsonString; 25 | 26 | import org.junit.jupiter.api.Assertions; 27 | import org.junit.jupiter.api.Test; 28 | 29 | /** 30 | * @author Jitendra Kotamraju 31 | */ 32 | public class JsonStringTest { 33 | 34 | // tests JsonString#toString() 35 | @Test 36 | void testToString() { 37 | escapedString(""); 38 | escapedString("abc"); 39 | escapedString("abc\f"); 40 | escapedString("abc\na"); 41 | escapedString("abc\tabc"); 42 | escapedString("abc\n\tabc"); 43 | escapedString("abc\n\tabc\r"); 44 | escapedString("\n\tabc\r"); 45 | escapedString("\bab\tb\rc\\\"\ftesting1234"); 46 | escapedString("\f\babcdef\tb\rc\\\"\ftesting1234"); 47 | escapedString("\u0000\u00ff"); 48 | escapedString("abc\"\\/abc"); 49 | } 50 | 51 | @Test 52 | void testHashCode() { 53 | String string1 = "a"; 54 | JsonString jsonString1 = Json.createValue(string1); 55 | Assertions.assertTrue(jsonString1.hashCode() == jsonString1.getString().hashCode()); 56 | 57 | String string2 = new String("a"); 58 | JsonString jsonString2 = Json.createValue(string2); 59 | 60 | Assertions.assertTrue(jsonString1.equals(jsonString2)); 61 | Assertions.assertTrue(jsonString1.hashCode() == jsonString2.hashCode()); 62 | } 63 | 64 | void escapedString(String str) { 65 | JsonArray exp = Json.createArrayBuilder().add(str).build(); 66 | String parseStr = "["+exp.get(0).toString()+"]"; 67 | JsonReader jr = Json.createReader(new StringReader(parseStr)); 68 | JsonArray got = jr.readArray(); 69 | Assertions.assertEquals(exp, got); 70 | jr.close(); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/ToJsonTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import jakarta.json.Json; 20 | import jakarta.json.JsonArray; 21 | import jakarta.json.JsonArrayBuilder; 22 | import jakarta.json.JsonValue; 23 | 24 | import org.eclipse.parsson.TestUtils; 25 | import org.junit.jupiter.api.Assertions; 26 | import org.junit.jupiter.api.Test; 27 | 28 | /** 29 | * @author Kin-man Chung 30 | */ 31 | public class ToJsonTest { 32 | 33 | @Test 34 | void testToJson() { 35 | Assertions.assertEquals(Json.createValue("someString"), TestUtils.toJson("'someString'")); 36 | Assertions.assertEquals(Json.createValue("some'thing"), TestUtils.toJson("'some\\'thing'")); 37 | Assertions.assertEquals(Json.createValue("some\"thing"), TestUtils.toJson("'some\\\"thing'")); 38 | JsonArrayBuilder builder = Json.createArrayBuilder(); 39 | JsonArray array = builder 40 | .add(Json.createObjectBuilder() 41 | .add("name", "John") 42 | .add("age", 35) 43 | .add("educations", Json.createArrayBuilder() 44 | .add("Gunn High") 45 | .add("UC Berkeley"))) 46 | .add(Json.createObjectBuilder() 47 | .add("name", "Jane") 48 | .add("educations", Json.createArrayBuilder() 49 | .add("Oxford"))) 50 | .build(); 51 | JsonValue expected = TestUtils.toJson( 52 | "[ { 'name': 'John', " + 53 | "'age': 35, " + 54 | "'educations': ['Gunn High', 'UC Berkeley'] }, " + 55 | " { 'name': 'Jane', " + 56 | "'educations': ['Oxford']}]"); 57 | Assertions.assertEquals(expected, array); 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /impl/src/test/java/org/eclipse/parsson/tests/TwitterSearchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.tests; 18 | 19 | import java.io.InputStream; 20 | import java.net.URL; 21 | 22 | import jakarta.json.Json; 23 | import jakarta.json.JsonArray; 24 | import jakarta.json.JsonObject; 25 | import jakarta.json.JsonReader; 26 | import jakarta.json.stream.JsonParser; 27 | import jakarta.json.stream.JsonParser.Event; 28 | 29 | import org.junit.jupiter.api.Test; 30 | 31 | /** 32 | * JsonParser Tests using twitter search API 33 | * 34 | * @author Jitendra Kotamraju 35 | */ 36 | public class TwitterSearchTest { 37 | 38 | @Test 39 | void test() { 40 | // dummy test so that junit doesn't complain 41 | } 42 | 43 | void xtestStreamTwitter() throws Exception { 44 | URL url = new URL("http://search.twitter.com/search.json?q=%23java&rpp=100"); 45 | InputStream is = url.openStream(); 46 | JsonParser parser = Json.createParser(is); 47 | 48 | while(parser.hasNext()) { 49 | Event e = parser.next(); 50 | if (e == Event.KEY_NAME) { 51 | if (parser.getString().equals("from_user")) { 52 | parser.next(); 53 | System.out.print(parser.getString()); 54 | System.out.print(": "); 55 | } else if (parser.getString().equals("text")) { 56 | parser.next(); 57 | System.out.println(parser.getString()); 58 | System.out.println("---------"); 59 | } 60 | } 61 | } 62 | parser.close(); 63 | } 64 | 65 | void xtestObjectTwitter() throws Exception { 66 | URL url = new URL("http://search.twitter.com/search.json?q=%23java&rpp=100"); 67 | InputStream is = url.openStream(); 68 | JsonReader rdr = Json.createReader(is); 69 | JsonObject obj = rdr.readObject(); 70 | JsonArray results = obj.getJsonArray("results"); 71 | for(JsonObject result : results.getValuesAs(JsonObject.class)) { 72 | System.out.print(result.get("from_user")); 73 | System.out.print(": "); 74 | System.out.println(result.get("text")); 75 | System.out.println("-----------"); 76 | } 77 | rdr.close(); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /impl/src/test/resources/jsonmergepatch.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "target": {}, 4 | "patch": {"a": {"b":"c"}}, 5 | "expected": {"a":{"b":"c"}} 6 | }, 7 | { 8 | "target": {}, 9 | "patch": {"a":{"bb":{"ccc":null}}}, 10 | "expected": {"a":{"bb":{}}} 11 | }, 12 | { 13 | "target": {"a":"foo"}, 14 | "patch": "bar", 15 | "expected": "bar" 16 | }, 17 | { 18 | "target": {"a":"foo"}, 19 | "patch": null, 20 | "expected": null 21 | }, 22 | { 23 | "target": {"a": {"b":"c"}}, 24 | "patch": {"a": {"b":"d", "c":null}}, 25 | "expected": {"a": {"b":"d"}} 26 | }, 27 | { 28 | "target": { "c": "d" }, 29 | "patch": { "a": "b" }, 30 | "expected": { "a": "b", "c": "d" } 31 | }, 32 | { 33 | "target": { "a": { "d": 2 } }, 34 | "patch": { "a": { "d": 1 } }, 35 | "expected": { "a": { "d": 1 } } 36 | }, 37 | { 38 | "target": { "a": "b", "c": "d" }, 39 | "patch": { "c": null }, 40 | "expected": { "a": "b" } 41 | }, 42 | { 43 | "target": { "a": { "b": "c", "d": null} }, 44 | "patch": { "a": { "d": null} }, 45 | "expected": { "a": { "b": "c" } } 46 | }, 47 | { 48 | "target": { 49 | "a": { "b": "c" }, 50 | "d": "e" 51 | }, 52 | "patch": { 53 | "a": 1000010002020389.8787987983 54 | }, 55 | "expected": { 56 | "a": 1000010002020389.8787987983, 57 | "d": "e" 58 | } 59 | }, 60 | { 61 | "target": { "a": "b" }, 62 | "patch": { "c": [ null ] }, 63 | "expected": { "a": "b", "c": [ null ] } 64 | }, 65 | { 66 | "target": { "a": { "b": null, "d": 3}, "e": -1 }, 67 | "patch": { "a": { "b": "c", "d": null } }, 68 | "expected": { "a": { "b": "c" }, "e": -1 } 69 | }, 70 | { 71 | "target": [1,2], 72 | "patch": { "a": "b", "c": null }, 73 | "expected": { "a": "b"} 74 | }, 75 | { 76 | "target": { 77 | "title": "Goodbye!", 78 | "author": { 79 | "givenName": "John", 80 | "familyName": "Doe" 81 | }, 82 | "tags": [ "example", "sample" ], 83 | "content": "This will be unchanged" 84 | }, 85 | "patch": { 86 | "title": "Hello!", 87 | "phoneNumber": "+01-123-456-7890", 88 | "author": { 89 | "familyName": null 90 | }, 91 | "tags": [ "example" ] 92 | }, 93 | "expected": { 94 | "title": "Hello!", 95 | "author": { 96 | "givenName": "John" 97 | }, 98 | "tags": [ "example" ], 99 | "content": "This will be unchanged", 100 | "phoneNumber": "+01-123-456-7890" 101 | } 102 | } 103 | ] -------------------------------------------------------------------------------- /impl/src/test/resources/jsonmergepatchdiff.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "original": { 4 | "title": "Goodbye!", 5 | "author": { 6 | "givenName": "John", 7 | "familyName": "Doe" 8 | }, 9 | "tags": [ "example", "sample" ], 10 | "content": "This will be unchanged" 11 | }, 12 | "expected": { 13 | "title": "Hello!", 14 | "phoneNumber": "+01-123-456-7890", 15 | "author": { 16 | "familyName": null 17 | }, 18 | "tags": [ "example" ] 19 | }, 20 | "target": { 21 | "title": "Hello!", 22 | "author": { 23 | "givenName": "John" 24 | }, 25 | "tags": [ "example" ], 26 | "content": "This will be unchanged", 27 | "phoneNumber": "+01-123-456-7890" 28 | } 29 | }, 30 | { 31 | "original": {}, 32 | "expected": {"a": {"b":"c"}}, 33 | "target": {"a":{"b":"c"}} 34 | }, 35 | { 36 | "original": {"a":"foo"}, 37 | "expected": "bar", 38 | "target": "bar" 39 | }, 40 | { 41 | "original": {"a":"foo"}, 42 | "expected": null, 43 | "target": null 44 | }, 45 | { 46 | "original": { "c": "d" }, 47 | "expected": { "a": "b" }, 48 | "target": { "a": "b", "c": "d" } 49 | }, 50 | { 51 | "original": { "a": { "d": 2 } }, 52 | "expected": { "a": { "d": 1 } }, 53 | "target": { "a": { "d": 1 } } 54 | }, 55 | { 56 | "original": { "a": "b", "c": "d" }, 57 | "expected": { "c": null }, 58 | "target": { "a": "b" } 59 | } 60 | ] -------------------------------------------------------------------------------- /impl/src/test/resources/rfc6901.json: -------------------------------------------------------------------------------- 1 | { 2 | "foo": ["bar", "baz"], 3 | "": 0, 4 | "a/b": 1, 5 | "c%d": 2, 6 | "e^f": 3, 7 | "g|h": 4, 8 | "i\\j": 5, 9 | "k\"l": 6, 10 | " ": 7, 11 | "m~n": 8, 12 | "o" : null, 13 | "p": { 14 | "q":"r" 15 | }, 16 | "s": [ { 17 | "t":"u" 18 | } 19 | ] 20 | } -------------------------------------------------------------------------------- /impl/src/test/resources/wiki.json: -------------------------------------------------------------------------------- 1 | { 2 | "firstName": "John", 3 | "lastName": "Smith", 4 | "age": 25, 5 | "address": { 6 | "streetAddress": "21 2nd Street", 7 | "city": "New York", 8 | "state": "NY", 9 | "postalCode": "10021" 10 | }, 11 | "phoneNumber": [ 12 | { 13 | "type": "home", 14 | "number": "212 555-1234" 15 | }, 16 | { 17 | "type": "fax", 18 | "number": "646 555-4567" 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /providers/customprovider/pom.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | 4.0.0 21 | 22 | 23 | org.eclipse.parsson 24 | providers 25 | 1.1.7-SNAPSHOT 26 | ../pom.xml 27 | 28 | 29 | war 30 | customprovider 31 | 32 | customprovider 33 | 34 | 35 | customprovider 36 | 37 | 38 | -------------------------------------------------------------------------------- /providers/customprovider/src/main/java/org/eclipse/parsson/customprovider/TestProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.customprovider; 18 | 19 | import jakarta.json.*; 20 | import jakarta.json.spi.JsonProvider; 21 | import jakarta.json.stream.JsonGenerator; 22 | import jakarta.json.stream.JsonGeneratorFactory; 23 | import jakarta.json.stream.JsonParser; 24 | import jakarta.json.stream.JsonParserFactory; 25 | import java.io.InputStream; 26 | import java.io.OutputStream; 27 | import java.io.Reader; 28 | import java.io.Writer; 29 | import java.util.Map; 30 | 31 | /** 32 | * @author Jitendra Kotamraju 33 | */ 34 | public class TestProvider extends JsonProvider { 35 | 36 | @Override 37 | public JsonGenerator createGenerator(Writer writer) { 38 | return new TestGenerator(writer); 39 | } 40 | 41 | @Override 42 | public JsonGenerator createGenerator(OutputStream out) { 43 | return null; 44 | } 45 | 46 | @Override 47 | public JsonGeneratorFactory createGeneratorFactory(Map config) { 48 | return null; 49 | } 50 | 51 | @Override 52 | public JsonReader createReader(Reader reader) { 53 | return null; 54 | } 55 | 56 | @Override 57 | public JsonReader createReader(InputStream in) { 58 | return null; 59 | } 60 | 61 | @Override 62 | public JsonWriter createWriter(Writer writer) { 63 | return null; 64 | } 65 | 66 | @Override 67 | public JsonWriter createWriter(OutputStream out) { 68 | return null; 69 | } 70 | 71 | @Override 72 | public JsonWriterFactory createWriterFactory(Map config) { 73 | return null; 74 | } 75 | 76 | @Override 77 | public JsonReaderFactory createReaderFactory(Map config) { 78 | return null; 79 | } 80 | 81 | @Override 82 | public JsonObjectBuilder createObjectBuilder() { 83 | return null; 84 | } 85 | 86 | @Override 87 | public JsonArrayBuilder createArrayBuilder() { 88 | return null; 89 | } 90 | 91 | @Override 92 | public JsonBuilderFactory createBuilderFactory(Map config) { 93 | return null; 94 | } 95 | 96 | @Override 97 | public JsonParser createParser(Reader reader) { 98 | return null; 99 | } 100 | 101 | @Override 102 | public JsonParser createParser(InputStream in) { 103 | return null; 104 | } 105 | 106 | @Override 107 | public JsonParserFactory createParserFactory(Map config) { 108 | return null; 109 | } 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /providers/customprovider/src/main/java/org/eclipse/parsson/customprovider/TestServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.customprovider; 18 | 19 | import java.io.IOException; 20 | import java.io.OutputStream; 21 | import java.io.OutputStreamWriter; 22 | 23 | import jakarta.servlet.ServletException; 24 | import jakarta.servlet.annotation.WebServlet; 25 | import jakarta.servlet.http.HttpServlet; 26 | import jakarta.servlet.http.HttpServletRequest; 27 | import jakarta.servlet.http.HttpServletResponse; 28 | 29 | import jakarta.json.Json; 30 | import jakarta.json.stream.JsonGenerator; 31 | 32 | /** 33 | * @author Jitendra Kotamraju 34 | */ 35 | @WebServlet("/json") 36 | public class TestServlet extends HttpServlet { 37 | 38 | @Override 39 | public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException { 40 | try { 41 | res.setStatus(200); 42 | res.setContentType("application/json"); 43 | OutputStream os = res.getOutputStream(); 44 | JsonGenerator generator = Json.createGenerator(new OutputStreamWriter(os)); 45 | if (!(generator instanceof TestGenerator)) { 46 | throw new RuntimeException("MyGenerator is not picked up"); 47 | } 48 | generator.writeStartArray().writeEnd(); 49 | generator.close(); 50 | os.close(); 51 | } catch (IOException ioe) { 52 | throw new ServletException(ioe); 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /providers/customprovider/src/main/resources/META-INF/services/jakarta.json.spi.JsonProvider: -------------------------------------------------------------------------------- 1 | org.eclipse.parsson.customprovider.TestProvider 2 | -------------------------------------------------------------------------------- /providers/defaultprovider/pom.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | 4.0.0 21 | 22 | 23 | org.eclipse.parsson 24 | providers 25 | 1.1.7-SNAPSHOT 26 | ../pom.xml 27 | 28 | 29 | war 30 | defaultprovider 31 | 32 | defaultprovider 33 | 34 | 35 | defaultprovider 36 | 37 | 38 | -------------------------------------------------------------------------------- /providers/defaultprovider/src/main/java/org/eclipse/parsson/defaultprovider/TestServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.defaultprovider; 18 | 19 | import jakarta.json.Json; 20 | import jakarta.json.stream.JsonGenerator; 21 | import jakarta.servlet.ServletException; 22 | import jakarta.servlet.annotation.WebServlet; 23 | import jakarta.servlet.http.HttpServlet; 24 | import jakarta.servlet.http.HttpServletRequest; 25 | import jakarta.servlet.http.HttpServletResponse; 26 | import java.io.IOException; 27 | import java.io.OutputStream; 28 | 29 | /** 30 | * @author Jitendra Kotamraju 31 | */ 32 | @WebServlet("/json") 33 | public class TestServlet extends HttpServlet { 34 | 35 | @Override 36 | public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException { 37 | try { 38 | res.setStatus(200); 39 | res.setContentType("application/json"); 40 | OutputStream os = res.getOutputStream(); 41 | JsonGenerator generator = Json.createGenerator(os); 42 | generator.writeStartArray().writeEnd(); 43 | generator.close(); 44 | } catch(IOException ioe) { 45 | throw new ServletException(ioe); 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /providers/pom.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | 4.0.0 21 | 22 | 23 | org.eclipse.parsson 24 | project 25 | 1.1.7-SNAPSHOT 26 | ../pom.xml 27 | 28 | 29 | pom 30 | org.eclipse.parsson 31 | providers 32 | 33 | 34 | 35 | jakarta.servlet 36 | jakarta.servlet-api 37 | provided 38 | 39 | 40 | jakarta.json 41 | jakarta.json-api 42 | provided 43 | 44 | 45 | 46 | 47 | 48 | org.apache.maven.plugins 49 | maven-war-plugin 50 | 51 | 52 | customprovider 53 | 54 | 55 | customprovider 56 | defaultprovider 57 | 58 | 59 | -------------------------------------------------------------------------------- /rest/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 4.0.0 22 | 23 | 24 | org.eclipse.parsson 25 | project 26 | 1.1.7-SNAPSHOT 27 | ../pom.xml 28 | 29 | 30 | org.eclipse.parsson 31 | parsson-media 32 | jar 33 | 1.1.7-SNAPSHOT 34 | Eclipse Parsson Media for Jakarta RESTful Web Services 35 | Jakarta RESTful Web Services MessageBodyReader and MessageBodyWriter to support JsonValue API of Jakarta JSON Processing 36 | 37 | 38 | 39 | jakarta.json 40 | jakarta.json-api 41 | provided 42 | 43 | 44 | jakarta.ws.rs 45 | jakarta.ws.rs-api 46 | provided 47 | 48 | 49 | jakarta.xml.bind 50 | jakarta.xml.bind-api 51 | provided 52 | 53 | 54 | jakarta.annotation 55 | jakarta.annotation-api 56 | provided 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.apache.felix 64 | maven-bundle-plugin 65 | 66 | 67 | bundle-manifest 68 | process-classes 69 | 70 | manifest 71 | 72 | 73 | 74 | 75 | jakarta.annotation*;version=!, 76 | jakarta.ws.rs*;version=!, 77 | * 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | org.apache.maven.plugins 86 | maven-jar-plugin 87 | 88 | 89 | ${project.build.outputDirectory}/META-INF/MANIFEST.MF 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /rest/src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | /** 18 | * Provides integration with Jakarta REST framework. 19 | */ 20 | module org.eclipse.parsson.media { 21 | 22 | requires jakarta.json; 23 | requires jakarta.annotation; 24 | requires jakarta.ws.rs; 25 | 26 | exports org.eclipse.parsson.media; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /rest/src/main/java/org/eclipse/parsson/media/JsonValueBodyReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.media; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | import java.lang.annotation.Annotation; 22 | import java.lang.reflect.Type; 23 | import jakarta.json.Json; 24 | import jakarta.json.JsonReader; 25 | import jakarta.json.JsonReaderFactory; 26 | import jakarta.json.JsonValue; 27 | import jakarta.ws.rs.Consumes; 28 | import jakarta.ws.rs.WebApplicationException; 29 | import jakarta.ws.rs.core.MediaType; 30 | import jakarta.ws.rs.core.MultivaluedMap; 31 | import jakarta.ws.rs.ext.MessageBodyReader; 32 | import jakarta.ws.rs.ext.Provider; 33 | 34 | /** 35 | * Jakarta RESTful Web Services MessageBodyReader for JsonValue. 36 | * This allows JsonValue to be a parameter of a resource method. 37 | * 38 | * @author Jitendra Kotamraju 39 | * @author Blaise Doughan 40 | * @author Michal Gajdos 41 | */ 42 | @Provider 43 | @Consumes({"application/json", "text/json", "*/*"}) 44 | public class JsonValueBodyReader implements MessageBodyReader { 45 | private final JsonReaderFactory rf = Json.createReaderFactory(null); 46 | 47 | private static final String JSON = "json"; 48 | private static final String PLUS_JSON = "+json"; 49 | 50 | /** 51 | * Constructor. 52 | */ 53 | public JsonValueBodyReader() { 54 | } 55 | 56 | @Override 57 | public boolean isReadable(Class aClass, Type type, 58 | Annotation[] annotations, MediaType mediaType) { 59 | return JsonValue.class.isAssignableFrom(aClass) && supportsMediaType(mediaType); 60 | } 61 | 62 | /** 63 | * @return true for all media types of the pattern */json and 64 | * */*+json. 65 | */ 66 | private static boolean supportsMediaType(final MediaType mediaType) { 67 | return mediaType.getSubtype().equals(JSON) || mediaType.getSubtype().endsWith(PLUS_JSON); 68 | } 69 | 70 | @Override 71 | public JsonValue readFrom(Class jsonValueClass, 72 | Type type, Annotation[] annotations, MediaType mediaType, 73 | MultivaluedMap stringStringMultivaluedMap, 74 | InputStream inputStream) throws IOException, WebApplicationException { 75 | try (JsonReader reader = rf.createReader(inputStream)) { 76 | return reader.readValue(); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /rest/src/main/java/org/eclipse/parsson/media/JsonValueBodyWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | package org.eclipse.parsson.media; 18 | 19 | import java.io.IOException; 20 | import java.io.OutputStream; 21 | import java.lang.annotation.Annotation; 22 | import java.lang.reflect.Type; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | import jakarta.annotation.PostConstruct; 26 | import jakarta.json.Json; 27 | import jakarta.json.JsonValue; 28 | import jakarta.json.JsonWriter; 29 | import jakarta.json.JsonWriterFactory; 30 | import jakarta.json.stream.JsonGenerator; 31 | import jakarta.ws.rs.Produces; 32 | import jakarta.ws.rs.WebApplicationException; 33 | import jakarta.ws.rs.core.Configuration; 34 | import jakarta.ws.rs.core.Context; 35 | import jakarta.ws.rs.core.MediaType; 36 | import jakarta.ws.rs.core.MultivaluedMap; 37 | import jakarta.ws.rs.ext.MessageBodyWriter; 38 | import jakarta.ws.rs.ext.Provider; 39 | 40 | /** 41 | * Jakarta RESTful Web Services MessageBodyWriter for JsonValue. 42 | * This allows JsonValue to be return type of a resource method. 43 | * 44 | * @author Jitendra Kotamraju 45 | * @author Blaise Doughan 46 | * @author Michal Gajdos 47 | */ 48 | @Provider 49 | @Produces({"application/json", "text/json", "*/*"}) 50 | public class JsonValueBodyWriter implements MessageBodyWriter { 51 | private static final String JSON = "json"; 52 | private static final String PLUS_JSON = "+json"; 53 | 54 | private JsonWriterFactory wf = Json.createWriterFactory(null); 55 | 56 | private final Configuration config; 57 | 58 | /** 59 | * Constructor. 60 | * 61 | * @param config configuration 62 | */ 63 | public JsonValueBodyWriter(@Context Configuration config) { 64 | this.config = config; 65 | } 66 | 67 | @PostConstruct 68 | public void init() { 69 | Map props = new HashMap<>(); 70 | if (config != null && config.getProperties().containsKey(JsonGenerator.PRETTY_PRINTING)) { 71 | props.put(JsonGenerator.PRETTY_PRINTING, true); 72 | } 73 | wf = Json.createWriterFactory(props); 74 | } 75 | 76 | @Override 77 | public boolean isWriteable(Class aClass, 78 | Type type, Annotation[] annotations, MediaType mediaType) { 79 | return JsonValue.class.isAssignableFrom(aClass) && supportsMediaType(mediaType); 80 | } 81 | 82 | /** 83 | * @return true for all media types of the pattern */json and 84 | * */*+json. 85 | */ 86 | private static boolean supportsMediaType(final MediaType mediaType) { 87 | return mediaType.getSubtype().equals(JSON) || mediaType.getSubtype().endsWith(PLUS_JSON); 88 | } 89 | 90 | @Override 91 | public long getSize(JsonValue jsonValue, Class aClass, 92 | Type type, Annotation[] annotations, MediaType mediaType) { 93 | 94 | return -1; 95 | } 96 | 97 | @Override 98 | public void writeTo(JsonValue jsonValue, Class aClass, Type type, 99 | Annotation[] annotations, MediaType mediaType, 100 | MultivaluedMap stringObjectMultivaluedMap, 101 | OutputStream outputStream) throws IOException, WebApplicationException { 102 | try (JsonWriter writer = wf.createWriter(outputStream)) { 103 | writer.write(jsonValue); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/assembly/resources.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 23 | resources 24 | 25 | zip 26 | 27 | false 28 | 29 | 30 | ${project.basedir} 31 | legal 32 | 33 | LICENSE.md 34 | NOTICE.md 35 | 36 | 37 | 38 | ${project.basedir}/etc/config 39 | config 40 | 41 | * 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /tck-impl/settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v. 2.0, which is available at 6 | * http://www.eclipse.org/legal/epl-2.0. 7 | * 8 | * This Source Code may also be made available under the following Secondary 9 | * Licenses when the conditions for such availability set forth in the 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, 11 | * version 2 with the GNU Classpath Exception, which is available at 12 | * https://www.gnu.org/software/classpath/license.html. 13 | * 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 15 | */ 16 | 17 | rootProject.name = 'parsson-tck-runner' 18 | 19 | --------------------------------------------------------------------------------