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 |
19 |
link or URL to the original Eclipse Foundation document.
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.
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