-```
- private static void JSONExampleArray1() {
- //We create a JSONObject from a String containing an array using JSONArray
- //Firstly, we declare an Array in a String
-
- String arrayStr =
- "["+"true,"+"false,"+ "\"true\","+ "\"false\","+"\"hello\","+"23.45e-4,"+
- "\"23.45\","+"42,"+"\"43\","+"["+"\"world\""+"],"+
- "{"+
- "\"key1\":\"value1\","+
- "\"key2\":\"value2\","+
- "\"key3\":\"value3\","+
- "\"key4\":\"value4\""+
- "},"+
- "0,"+"\"-1\""+
- "]";
-
- //Then, we initializate the JSONArray thanks to its constructor
-
- JSONArray array = new JSONArray(arrayStr);
- System.out.println("Values array: "+ array);
-
- //We convert that array into a JSONObject, but first, we need the labels, so we need another JSONArray with the labels.
- //Here we will use an auxiliary function to get one for the example.
-
- JSONArray list = listNumberArray(array.length());
- System.out.println("Label Array: "+ list.toString());
- //Now, we construct the JSONObject using both the value array and the label array.
- JSONObject object = array.toJSONObject(list);
- System.out.println("Final JSONOBject: " + object);
- }
+```java
+private static void JSONExampleArray1() {
+ //We create a JSONObject from a String containing an array using JSONArray
+ //Firstly, we declare an Array in a String
+
+ String arrayStr =
+ "["+"true,"+"false,"+ "\"true\","+ "\"false\","+"\"hello\","+"23.45e-4,"+
+ "\"23.45\","+"42,"+"\"43\","+"["+"\"world\""+"],"+
+ "{"+
+ "\"key1\":\"value1\","+
+ "\"key2\":\"value2\","+
+ "\"key3\":\"value3\","+
+ "\"key4\":\"value4\""+
+ "},"+
+ "0,"+"\"-1\""+
+ "]";
+
+ //Then, we initializate the JSONArray thanks to its constructor
+
+ JSONArray array = new JSONArray(arrayStr);
+ System.out.println("Values array: "+ array);
+
+ //We convert that array into a JSONObject, but first, we need the labels, so we need another JSONArray with the labels.
+ //Here we will use an auxiliary function to get one for the example.
+
+ JSONArray list = listNumberArray(array.length());
+ System.out.println("Label Array: "+ list.toString());
+ //Now, we construct the JSONObject using both the value array and the label array.
+ JSONObject object = array.toJSONObject(list);
+ System.out.println("Final JSONOBject: " + object);
+}
- //This method creates an JSONArray of labels in which those are generated by their positions
+//This method creates an JSONArray of labels in which those are generated by their positions
- private static JSONArray listNumberArray(int max){
- JSONArray res = new JSONArray();
- for (int i=0; iUsing JSONStringer
-```
- private static void JSONExampleStringer() {
+```java
+private static void JSONExampleStringer() {
- //We initializate the JSONStringer
+ //We initializate the JSONStringer
- JSONStringer jsonStringer = new JSONStringer();
+ JSONStringer jsonStringer = new JSONStringer();
- //Now we start the process of adding elements with .object()
+ //Now we start the process of adding elements with .object()
- jsonStringer.object();
+ jsonStringer.object();
- //We can now add elements as keys and values with .values () and .key()
+ //We can now add elements as keys and values with .values () and .key()
- jsonStringer.key("trueValue").value(true);
- jsonStringer.key("falseValue").value(false);
- jsonStringer.key("nullValue").value(null);
- jsonStringer.key("stringValue").value("hello world!");
- jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!");
- jsonStringer.key("intValue").value(42);
- jsonStringer.key("doubleValue").value(-23.45e67);
+ jsonStringer.key("trueValue").value(true);
+ jsonStringer.key("falseValue").value(false);
+ jsonStringer.key("nullValue").value(null);
+ jsonStringer.key("stringValue").value("hello world!");
+ jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!");
+ jsonStringer.key("intValue").value(42);
+ jsonStringer.key("doubleValue").value(-23.45e67);
- //We end this prcedure with .ednObject
+ //We end this prcedure with .ednObject
- jsonStringer.endObject();
+ jsonStringer.endObject();
- //Once we have a JSONStringer, we convert it to JSONObject generating a String and using JSONObject's contructor.
+ //Once we have a JSONStringer, we convert it to JSONObject generating a String and using JSONObject's contructor.
- String str = jsonStringer.toString();
- JSONObject jsonObject = new JSONObject(str);
-
- System.out.println("Final JSONOBject: " + jsonObject);
- }
+ String str = jsonStringer.toString();
+ JSONObject jsonObject = new JSONObject(str);
+
+ System.out.println("Final JSONOBject: " + jsonObject);
+}
```
Using JSONObject
-```
- private static void JSONExampleObject1() {
+```java
+private static void JSONExampleObject1() {
- //We can create a JSONObject from a String with the class builder
+ //We can create a JSONObject from a String with the class builder
- String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
- JSONObject example = new JSONObject(string);
- System.out.println("Final JSONObject: " + example);
-
- }
-```
+ String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
+ JSONObject example = new JSONObject(string);
+ System.out.println("Final JSONObject: " + example);
+
+}
```
- private static void JSONExampleObject2() {
+```java
+private static void JSONExampleObject2() {
- //We can also create a JSONObject directly without messing around with any of the other functions.
+ //We can also create a JSONObject directly without messing around with any of the other functions.
- JSONObject example = new JSONObject();
+ JSONObject example = new JSONObject();
- //Now we add the keys and values in a similar way as the Stringer method
- example.put("key", "value");
+ //Now we add the keys and values in a similar way as the Stringer method
+ example.put("key", "value");
- //As you can see, the first entry is the key and the second would be its associeted value.
+ //As you can see, the first entry is the key and the second would be its associeted value.
- example.put("key2", 5);
- example.put("key3", -23.45e67);
- example.put("trueValue", true);
+ example.put("key2", 5);
+ example.put("key3", -23.45e67);
+ example.put("trueValue", true);
- //We can't add null values thougth
+ //We can't add null values thougth
- //example.put("nullValue", null); //This is not possible
-
- System.out.println("Final JSONOBject: " + example);
- }
-```
+ //example.put("nullValue", null); //This is not possible
+
+ System.out.println("Final JSONOBject: " + example);
+}
```
- private static void JSONExampleObject3() {
+```java
+private static void JSONExampleObject3() {
- //We can also create a JSONObject with a Java Map
- //YoU will need a Map whose keys are Strings. The values can be whatever you want
+ //We can also create a JSONObject with a Java Map
+ //YoU will need a Map whose keys are Strings. The values can be whatever you want
- Map map = new HashMap();
-
- map.put("key1", 1.0);
- map.put("key2", -23.45e67);
-
- //We create the JSONObject with the map with its class builder
+ Map map = new HashMap();
- JSONObject example = new JSONObject(map);
- System.out.println("Final JSONOBject: " + example);
- }
+ map.put("key1", 1.0);
+ map.put("key2", -23.45e67);
+
+ //We create the JSONObject with the map with its class builder
+
+ JSONObject example = new JSONObject(map);
+ System.out.println("Final JSONOBject: " + example);
+}
```
Using JSONWriter
-```
- private static void JSONExamplWriter() {
-
- //This method works in a very similar way to Object and Stringer in the construction of the JSON.
- //The difference is that it needs a Java object called "Appendable" like StringBuilder
-
- StringBuilder write = new StringBuilder();
- JSONWriter jsonWriter = new JSONWriter(write);
-
- //We behave now the same way as Stringer
-
- jsonWriter.object();
-
- jsonWriter.key("trueValue").value(true);
- jsonWriter.key("falseValue").value(false);
- jsonWriter.key("nullValue").value(null);
- jsonWriter.key("stringValue").value("hello world!");
- jsonWriter.key("complexStringValue").value("h\be\tllo w\u1234orld!");
- jsonWriter.key("intValue").value(42);
- jsonWriter.key("doubleValue").value(-23.45e67);
-
- jsonWriter.endObject();
-
- //The resoult should be in the "write" object
-
- System.out.println("JSON: " + write.toString());
-
- //The difference is that we don't get a JSONObject in this one.
-
-
- }
+```java
+private static void JSONExamplWriter() {
+
+ //This method works in a very similar way to Object and Stringer in the construction of the JSON.
+ //The difference is that it needs a Java object called "Appendable" like StringBuilder
+
+ StringBuilder write = new StringBuilder();
+ JSONWriter jsonWriter = new JSONWriter(write);
+
+ //We behave now the same way as Stringer
+
+ jsonWriter.object();
+
+ jsonWriter.key("trueValue").value(true);
+ jsonWriter.key("falseValue").value(false);
+ jsonWriter.key("nullValue").value(null);
+ jsonWriter.key("stringValue").value("hello world!");
+ jsonWriter.key("complexStringValue").value("h\be\tllo w\u1234orld!");
+ jsonWriter.key("intValue").value(42);
+ jsonWriter.key("doubleValue").value(-23.45e67);
+
+ jsonWriter.endObject();
+
+ //The resoult should be in the "write" object
+
+ System.out.println("JSON: " + write.toString());
+
+ //The difference is that we don't get a JSONObject in this one.
+
+
+}
```
-```
- private static void JSONExampleTokener() {
+```java
+private static void JSONExampleTokener() {
- //A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject
+ //A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject
- String string = "this is not a valid JSON string";
- JSONTokener token = new JSONTokener(string);
-
- //Now you can use the token in JSONObject and Array the same way as a String
+ String string = "this is not a valid JSON string";
+ JSONTokener token = new JSONTokener(string);
- JSONObject object = new JSONObject(token);
- JSONArray array = new JSONArray(token);
-
- }
+ //Now you can use the token in JSONObject and Array the same way as a String
+
+ JSONObject object = new JSONObject(token);
+ JSONArray array = new JSONArray(token);
+
+}
```
Part 2: Conversion methods
-
We don't need to have a JSON docuemnt to work. This project also admits conversions from other type of files.
+
We don't need to have a JSON document to work. This project also admits conversions from other type of files.
Secondly, we can also convert from JSON to those type of files.
Extra: Conversion to JSONArray
-```
- private static void JSONObjectToArray() {
- //We start with a JSONObject
-
- String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
-
- JSONObject example = new JSONObject(string);
-
- //We need a list of key strings like the reverse operation
-
- JSONArray keyStrings = listNumberArray(example.length());
-
- //Then we convert to the Array using both elelements
-
- JSONArray array = example.toJSONArray(keyStrings);
-
- System.out.println("Final JSONArray: " + array);
- }
+```java
+private static void JSONObjectToArray() {
+ //We start with a JSONObject
+
+ String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
+
+ JSONObject example = new JSONObject(string);
+
+ //We need a list of key strings like the reverse operation
+
+ JSONArray keyStrings = listNumberArray(example.length());
+
+ //Then we convert to the Array using both elelements
+
+ JSONArray array = example.toJSONArray(keyStrings);
+
+ System.out.println("Final JSONArray: " + array);
+}
```
XML Conversions
-```
- private static void XMLToExampleConversion() {
+```java
+private static void XMLToExampleConversion() {
+
+ //We start with a JSONObject
- //We start with a JSONObject
-
- String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
- JSONObject example = new JSONObject(string);
+ String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
+ JSONObject example = new JSONObject(string);
- //We obtain a String with XML format with toString()
+ //We obtain a String with XML format with toString()
- String output = XML.toString(example);
- System.out.println("Final XML: " + output);
- }
+ String output = XML.toString(example);
+ System.out.println("Final XML: " + output);
+}
```
-```
- private static void XMLFromExampleConversion() {
+```java
+private static void XMLFromExampleConversion() {
- //We start with a string with the XML format
+ //We start with a string with the XML format
- String string = "<0>value0><1>51><2>-2.345E+682><3>true3>";
+ String string = "<0>value0><1>51><2>-2.345E+682><3>true3>";
- //We obtain a JSONObject with toJSONOBject()
+ //We obtain a JSONObject with toJSONOBject()
- JSONObject output = XML.toJSONObject(string);
-
- System.out.println("Final JSONObject: " + output);
- }
+ JSONObject output = XML.toJSONObject(string);
+
+ System.out.println("Final JSONObject: " + output);
+}
```
Cookie Conversions
-```
- private static void CookieToExampleConversion() {
+```java
+private static void CookieToExampleConversion() {
- //We start with a JSONObject
- //The JSONOBject needs to entries that gives the cookie a name and gives the field "name" a name too.
- //The Cokkie format doesn't support booleans
+ //We start with a JSONObject
+ //The JSONOBject needs to entries that gives the cookie a name and gives the field "name" a name too.
+ //The Cokkie format doesn't support booleans
- String string = "{\"name\":\"Cookie-Name\",\"value\":\"name\",\"1\":5,\"2\":-2.345E68,\"3\":'true'}";
- JSONObject example = new JSONObject(string);
-
- //We obtain a String with Cookie format with toString()
+ String string = "{\"name\":\"Cookie-Name\",\"value\":\"name\",\"1\":5,\"2\":-2.345E68,\"3\":'true'}";
+ JSONObject example = new JSONObject(string);
- String output = Cookie.toString(example);
- System.out.println("Final Cookie: " + output);
- }
-```
+ //We obtain a String with Cookie format with toString()
+
+ String output = Cookie.toString(example);
+ System.out.println("Final Cookie: " + output);
+}
```
- private static void CookieFromExampleConversion() {
+```java
+private static void CookieFromExampleConversion() {
- //We start with a string with the Cookie format
+ //We start with a string with the Cookie format
- String string = "Cookie-Name=name;1=5;2=-2.345E%2b68;3=true";
+ String string = "Cookie-Name=name;1=5;2=-2.345E%2b68;3=true";
- //We obtain a JSONObject with toJSONOBject()
+ //We obtain a JSONObject with toJSONOBject()
- JSONObject output = Cookie.toJSONObject(string);
- System.out.println("Final JSONObject: " + output);
- }
+ JSONObject output = Cookie.toJSONObject(string);
+ System.out.println("Final JSONObject: " + output);
+}
```
HTTP Conversions
-```
- private static void HTTPToExampleConversion() {
+```java
+private static void HTTPToExampleConversion() {
- //We start with a JSONObject
- //The JSONObject must have the minimun header for a HTTP request or header
+ //We start with a JSONObject
+ //The JSONObject must have the minimun header for a HTTP request or header
- String string = "{\"Method\":\"POST\",\"Request-URI\":'/',\"HTTP-Version\":'HTTP/1.1',\"Value1\":true,\"Value2\":2,\"Value3\":-2.345E68}";
+ String string = "{\"Method\":\"POST\",\"Request-URI\":'/',\"HTTP-Version\":'HTTP/1.1',\"Value1\":true,\"Value2\":2,\"Value3\":-2.345E68}";
- JSONObject example = new JSONObject(string);
+ JSONObject example = new JSONObject(string);
- //We obtain a String with HTTP format with toString()
+ //We obtain a String with HTTP format with toString()
- String output = HTTP.toString(example);
- System.out.println("Final HTTP: " + output);
- }
+ String output = HTTP.toString(example);
+ System.out.println("Final HTTP: " + output);
+}
```
-```
- private static void HTTPFromExampleConversion() {
+```java
+private static void HTTPFromExampleConversion() {
- //We start with a string with the HTTP format
+ //We start with a string with the HTTP format
- String string = "Final HTTP: POST '/' HTTP/1.1 Value3: -2.345E+68 Value1: true Value2: 2";
+ String string = "Final HTTP: POST '/' HTTP/1.1 Value3: -2.345E+68 Value1: true Value2: 2";
- //We obtain a JSONObject with toJSONOBject()
+ //We obtain a JSONObject with toJSONOBject()
- JSONObject output = HTTP.toJSONObject(string);
- System.out.println("Final JSONObject: " + output);
- }
+ JSONObject output = HTTP.toJSONObject(string);
+ System.out.println("Final JSONObject: " + output);
+}
```
CDL Conversions
-```
+```java
private static void CDLToExampleConversion() {
- //We start with some JSONObjects with the same values in the keys but different values in the "values"
+ //We start with some JSONObjects with the same values in the keys but different values in the "values"
- String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
- JSONObject example = new JSONObject(string);
-
- String string2 = "{\"0\":\"value2\",\"1\":6,\"2\":-8.345E68,\"3\":false}";
- JSONObject example2 = new JSONObject(string2);
-
- //We need now a JSONArray with those JSONObjects
+ String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
+ JSONObject example = new JSONObject(string);
- JSONArray array = new JSONArray();
- array.put(example);
- array.put(example2);
+ String string2 = "{\"0\":\"value2\",\"1\":6,\"2\":-8.345E68,\"3\":false}";
+ JSONObject example2 = new JSONObject(string2);
- //We obtain a String with XML format with toString()
+ //We need now a JSONArray with those JSONObjects
- String output = CDL.toString(array);
- System.out.println("Final CDL: \r\n" + output);
- }
-```
+ JSONArray array = new JSONArray();
+ array.put(example);
+ array.put(example2);
+
+ //We obtain a String with XML format with toString()
+
+ String output = CDL.toString(array);
+ System.out.println("Final CDL: \r\n" + output);
+}
```
+```java
private static void CDLFromExampleConversion() {
//We start wtih a String with the CDL format
@@ -368,7 +368,7 @@ private static void CDLFromExampleConversion() {
String string = "0,1,2,3\n"
+ "value,5,-2.345E+68,true\n"
+ "value2,6,-8.345E+68,false";
-
+
//We obtain a JSONArray with toJSONOBject()
JSONArray output = CDL.toJSONArray(string);
@@ -377,8 +377,8 @@ private static void CDLFromExampleConversion() {
```
+
+```java
+public static void main(String[] args) {
+ //JSONObjectToArray();
+ //JSONExampleArray1();
+ //JSONExampleArray2();
+ //JSONExampleStringer();
+ //JSONExampleObject1();
+ //JSONExampleObject2();
+ //JSONExampleObject3();
+ //JSONExamplWriter();
+ //XMLToExampleConversion();
+ //XMLFromExampleConversion();
+ //CookieToExampleConversion();
+ //CookieFromExampleConversion();
+ //HTTPToExampleConversion();
+ //HTTPFromExampleConversion();
+ //CDLToExampleConversion();
+ //CDLFromExampleConversion();
+ //PropertyToExampleConversion();
+ //PropertyFromExampleConversion();
+}
```
diff --git a/LICENSE b/LICENSE
index 6cfb9b2d0..2ef9799e0 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,23 +1,2 @@
-
-Copyright (c) 2002 JSON.org
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-The Software shall be used for Good, not Evil.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+Public Domain.
diff --git a/README.md b/README.md
index 40bcbfdfd..5cc3bd451 100644
--- a/README.md
+++ b/README.md
@@ -7,8 +7,11 @@ JSON in Java [package org.json]
===============================
[](https://mvnrepository.com/artifact/org.json/json)
+[](https://github.com/stleary/JSON-java/actions/workflows/pipeline.yml)
+[](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml)
+[](https://javadoc.io/doc/org.json/json)
-**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20210307/json-20210307.jar)**
+**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20251224/json-20251224.jar)**
# Overview
@@ -24,13 +27,14 @@ Project goals include:
* No external dependencies
* Fast execution and low memory footprint
* Maintain backward compatibility
-* Designed and tested to use on Java versions 1.6 - 1.11
+* Designed and tested to use on Java versions 1.6 - 25
+
The files in this package implement JSON encoders and decoders. The package can also convert between JSON and XML, HTTP headers, Cookies, and CDL.
-The license includes this restriction: ["The software shall be used for good, not evil."](https://en.wikipedia.org/wiki/Douglas_Crockford#%22Good,_not_Evil%22) If your conscience cannot live with that, then choose a different package.
+# If you would like to contribute to this project
-**If you would like to contribute to this project**
+For more information on contributions, please see [CONTRIBUTING.md](https://github.com/stleary/JSON-java/blob/master/docs/CONTRIBUTING.md)
Bug fixes, code improvements, and unit test coverage changes are welcome! Because this project is currently in the maintenance phase, the kinds of changes that can be accepted are limited. For more information, please read the [FAQ](https://github.com/stleary/JSON-java/wiki/FAQ).
@@ -41,245 +45,79 @@ The org.json package can be built from the command line, Maven, and Gradle. The
**Building from the command line**
*Build the class files from the package root directory src/main/java*
-````
-javac org\json\*.java
-````
+```shell
+javac org/json/*.java
+```
*Create the jar file in the current directory*
-````
+```shell
jar cf json-java.jar org/json/*.class
-````
+```
*Compile a program that uses the jar (see example code below)*
-````
-javac -cp .;json-java.jar Test.java
-````
+```shell
+javac -cp .;json-java.jar Test.java (Windows)
+javac -cp .:json-java.jar Test.java (Unix Systems)
+```
*Test file contents*
-````
+```java
import org.json.JSONObject;
public class Test {
public static void main(String args[]){
JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }");
- System.out.println(jo.toString());
+ System.out.println(jo);
}
}
-````
+```
*Execute the Test file*
-````
-java -cp .;json-java.jar Test
-````
+```shell
+java -cp .;json-java.jar Test (Windows)
+java -cp .:json-java.jar Test (Unix Systems)
+```
*Expected output*
-````
+```json
{"abc":"def"}
-````
+```
**Tools to build the package and execute the unit tests**
Execute the test suite with Maven:
-```
+```shell
mvn clean test
```
Execute the test suite with Gradlew:
-```
+```shell
gradlew clean build test
```
-# Notes
-
-**Recent directory structure change**
-
-_Due to a recent commit - [#515 Merge tests and pom and code](https://github.com/stleary/JSON-java/pull/515) - the structure of the project has changed from a flat directory containing all of the Java files to a directory structure that includes unit tests and several tools used to build the project jar and run the unit tests. If you have difficulty using the new structure, please open an issue so we can work through it._
-
-**Implementation notes**
-
-Numeric types in this package comply with
-[ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) and
-[RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259#section-6).
-This package fully supports `Integer`, `Long`, and `Double` Java types. Partial support
-for `BigInteger` and `BigDecimal` values in `JSONObject` and `JSONArray` objects is provided
-in the form of `get()`, `opt()`, and `put()` API methods.
+*Optional* Execute the test suite in strict mode with Gradlew:
-Although 1.6 compatibility is currently supported, it is not a project goal and might be
-removed in some future release.
-
-In compliance with RFC8259 page 10 section 9, the parser is more lax with what is valid
-JSON then the Generator. For Example, the tab character (U+0009) is allowed when reading
-JSON Text strings, but when output by the Generator, the tab is properly converted to \t in
-the string. Other instances may occur where reading invalid JSON text does not cause an
-error to be generated. Malformed JSON Texts such as missing end " (quote) on strings or
-invalid number formats (1.2e6.3) will cause errors as such documents can not be read
-reliably.
-
-Some notable exceptions that the JSON Parser in this library accepts are:
-* Unquoted keys `{ key: "value" }`
-* Unquoted values `{ "key": value }`
-* Unescaped literals like "tab" in string values `{ "key": "value with an unescaped tab" }`
-* Numbers out of range for `Double` or `Long` are parsed as strings
-
-Recent pull requests added a new method `putAll` on the JSONArray. The `putAll` method
-works similarly to other `put` methods in that it does not call `JSONObject.wrap` for items
-added. This can lead to inconsistent object representation in JSONArray structures.
-
-For example, code like this will create a mixed JSONArray, some items wrapped, others
-not:
-
-```java
-SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
-// these will be wrapped
-JSONArray jArr = new JSONArray(myArr);
-// these will not be wrapped
-jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
+```shell
+gradlew testWithStrictMode
```
-For structure consistency, it would be recommended that the above code is changed
-to look like 1 of 2 ways.
-
-Option 1:
-```Java
-SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
-JSONArray jArr = new JSONArray();
-// these will not be wrapped
-jArr.putAll(myArr);
-// these will not be wrapped
-jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
-// our jArr is now consistent.
-```
+*Optional* Execute the test suite in strict mode with Maven:
-Option 2:
-```Java
-SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
-// these will be wrapped
-JSONArray jArr = new JSONArray(myArr);
-// these will be wrapped
-jArr.putAll(new JSONArray(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }));
-// our jArr is now consistent.
+```shell
+mvn test -P test-strict-mode
```
-**Unit Test Conventions**
-
-Test filenames should consist of the name of the module being tested, with the suffix "Test".
-For example, Cookie.java is tested by CookieTest.java.
-
-The fundamental issues with JSON-Java testing are:
-* JSONObjects are unordered, making simple string comparison ineffective.
-* Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject override hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful.
-* Access to the JSONArray and JSONObject internal containers for comparison is not currently available.
-
-General issues with unit testing are:
-* Just writing tests to make coverage goals tends to result in poor tests.
-* Unit tests are a form of documentation - how a given method works is demonstrated by the test. So for a code reviewer or future developer looking at code a good test helps explain how a function is supposed to work according to the original author. This can be difficult if you are not the original developer.
-* It is difficult to evaluate unit tests in a vacuum. You also need to see the code being tested to understand if a test is good.
-* Without unit tests, it is hard to feel confident about the quality of the code, especially when fixing bugs or refactoring. Good tests prevent regressions and keep the intent of the code correct.
-* If you have unit test results along with pull requests, the reviewer has an easier time understanding your code and determining if it works as intended.
+# Notes
+For more information, please see [NOTES.md](https://github.com/stleary/JSON-java/blob/master/docs/NOTES.md)
# Files
-**JSONObject.java**: The `JSONObject` can parse text from a `String` or a `JSONTokener`
-to produce a map-like object. The object provides methods for manipulating its
-contents, and for producing a JSON compliant object serialization.
-
-**JSONArray.java**: The `JSONArray` can parse text from a String or a `JSONTokener`
-to produce a vector-like object. The object provides methods for manipulating
-its contents, and for producing a JSON compliant array serialization.
-
-**JSONTokener.java**: The `JSONTokener` breaks a text into a sequence of individual
-tokens. It can be constructed from a `String`, `Reader`, or `InputStream`. It also can
-parse text from a `String`, `Number`, `Boolean` or `null` like `"hello"`, `42`, `true`,
-`null` to produce a simple json object.
-
-**JSONException.java**: The `JSONException` is the standard exception type thrown
-by this package.
-
-**JSONPointer.java**: Implementation of
-[JSON Pointer (RFC 6901)](https://tools.ietf.org/html/rfc6901). Supports
-JSON Pointers both in the form of string representation and URI fragment
-representation.
-
-**JSONPropertyIgnore.java**: Annotation class that can be used on Java Bean getter methods.
-When used on a bean method that would normally be serialized into a `JSONObject`, it
-overrides the getter-to-key-name logic and forces the property to be excluded from the
-resulting `JSONObject`.
-
-**JSONPropertyName.java**: Annotation class that can be used on Java Bean getter methods.
-When used on a bean method that would normally be serialized into a `JSONObject`, it
-overrides the getter-to-key-name logic and uses the value of the annotation. The Bean
-processor will look through the class hierarchy. This means you can use the annotation on
-a base class or interface and the value of the annotation will be used even if the getter
-is overridden in a child class.
-
-**JSONString.java**: The `JSONString` interface requires a `toJSONString` method,
-allowing an object to provide its own serialization.
-
-**JSONStringer.java**: The `JSONStringer` provides a convenient facility for
-building JSON strings.
-
-**JSONWriter.java**: The `JSONWriter` provides a convenient facility for building
-JSON text through a writer.
-
-
-**CDL.java**: `CDL` provides support for converting between JSON and comma
-delimited lists.
-
-**Cookie.java**: `Cookie` provides support for converting between JSON and cookies.
-
-**CookieList.java**: `CookieList` provides support for converting between JSON and
-cookie lists.
-
-**HTTP.java**: `HTTP` provides support for converting between JSON and HTTP headers.
-
-**HTTPTokener.java**: `HTTPTokener` extends `JSONTokener` for parsing HTTP headers.
-
-**XML.java**: `XML` provides support for converting between JSON and XML.
-
-**JSONML.java**: `JSONML` provides support for converting between JSONML and XML.
-
-**XMLTokener.java**: `XMLTokener` extends `JSONTokener` for parsing XML text.
-
+For more information on files, please see [FILES.md](https://github.com/stleary/JSON-java/blob/master/docs/FILES.md)
# Release history:
-JSON-java releases can be found by searching the Maven repository for groupId "org.json"
-and artifactId "json". For example:
-[https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav)
-
-~~~
-20210307 Recent commits and potentially breaking fix to JSONPointer
-
-20201115 Recent commits and first release after project structure change
-
-20200518 Recent commits and snapshot before project structure change
-
-20190722 Recent commits
-
-20180813 POM change to include Automatic-Module-Name (#431)
-
-20180130 Recent commits
-
-20171018 Checkpoint for recent commits.
-
-20170516 Roll up recent commits.
-
-20160810 Revert code that was breaking opt*() methods.
-
-20160807 This release contains a bug in the JSONObject.opt*() and JSONArray.opt*() methods,
-it is not recommended for use.
-Java 1.6 compatability fixed, JSONArray.toList() and JSONObject.toMap(),
-RFC4180 compatibility, JSONPointer, some exception fixes, optional XML type conversion.
-Contains the latest code as of 7 Aug 2016
-
-20160212 Java 1.6 compatibility, OSGi bundle. Contains the latest code as of 12 Feb 2016.
-
-20151123 JSONObject and JSONArray initialization with generics. Contains the latest code as of 23 Nov 2015.
-
-20150729 Checkpoint for Maven central repository release. Contains the latest code
-as of 29 July 2015.
-~~~
+For the release history, please see [RELEASES.md](https://github.com/stleary/JSON-java/blob/master/docs/RELEASES.md)
diff --git a/SECURITY.md b/SECURITY.md
new file mode 100644
index 000000000..5af9a566b
--- /dev/null
+++ b/SECURITY.md
@@ -0,0 +1,5 @@
+# Security Policy
+
+## Reporting a Vulnerability
+
+Please follow the instructions in the ["How are vulnerabilities and exploits handled?"](https://github.com/stleary/JSON-java/wiki/FAQ#how-are-vulnerabilities-and-exploits-handled) section in the FAQ.
diff --git a/build.gradle b/build.gradle
index 3b403ece4..898f10dc7 100644
--- a/build.gradle
+++ b/build.gradle
@@ -3,9 +3,10 @@
*/
apply plugin: 'java'
apply plugin: 'eclipse'
-// apply plugin: 'jacoco'
+apply plugin: 'jacoco'
apply plugin: 'maven-publish'
+// for now, publishing to maven is still a manual process
//plugins {
// id 'java'
//id 'maven-publish'
@@ -19,10 +20,21 @@ repositories {
}
}
+// To view the report open build/reports/jacoco/test/html/index.html
+jacocoTestReport {
+ reports {
+ html.required = true
+ }
+}
+
+test {
+ finalizedBy jacocoTestReport
+}
+
dependencies {
- testImplementation 'junit:junit:4.13.1'
- testImplementation 'com.jayway.jsonpath:json-path:2.1.0'
- testImplementation 'org.mockito:mockito-core:1.9.5'
+ testImplementation 'junit:junit:4.13.2'
+ testImplementation 'com.jayway.jsonpath:json-path:2.9.0'
+ testImplementation 'org.mockito:mockito-core:4.2.0'
}
subprojects {
@@ -30,9 +42,9 @@ subprojects {
}
group = 'org.json'
-version = 'v20200429-SNAPSHOT'
+version = 'v20251224-SNAPSHOT'
description = 'JSON in Java'
-sourceCompatibility = '1.7'
+sourceCompatibility = '1.8'
configurations.all {
}
@@ -53,3 +65,75 @@ publishing {
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
+// Add these imports at the top of your build.gradle file
+import java.nio.file.Files
+import java.nio.file.Path
+import java.nio.file.Paths
+import java.nio.file.StandardCopyOption
+
+// Your existing build configurations...
+
+// Add a new task to modify the file
+task modifyStrictMode {
+ doLast {
+ println "Modifying JSONParserConfiguration.java to enable strictMode..."
+
+ def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
+
+ if (!filePath.exists()) {
+ throw new GradleException("Could not find file: ${filePath.absolutePath}")
+ }
+
+ // Create a backup of the original file
+ def backupFile = new File(filePath.absolutePath + '.bak')
+ Files.copy(filePath.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
+
+ // Read and modify the file content
+ def content = filePath.text
+ def modifiedContent = content.replace('// this.strictMode = true;', 'this.strictMode = true;')
+
+ // Write the modified content back to the file
+ filePath.text = modifiedContent
+
+ println "File modified successfully at: ${filePath.absolutePath}"
+ }
+}
+
+// Add a task to restore the original file
+task restoreStrictMode {
+ doLast {
+ println "Restoring original JSONParserConfiguration.java..."
+
+ def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
+ def backupFile = new File(filePath.absolutePath + '.bak')
+
+ if (backupFile.exists()) {
+ Files.copy(backupFile.toPath(), filePath.toPath(), StandardCopyOption.REPLACE_EXISTING)
+ backupFile.delete()
+ println "Original file restored successfully at: ${filePath.absolutePath}"
+ } else {
+ println "Backup file not found at: ${backupFile.absolutePath}. No restoration performed."
+ }
+ }
+}
+
+// Create a task to run the workflow
+task testWithStrictMode {
+ dependsOn modifyStrictMode
+ finalizedBy restoreStrictMode
+
+ doLast {
+ // This will trigger a clean build and run tests with strictMode enabled
+ if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
+ exec {
+ executable 'cmd'
+ args '/c', 'gradlew.bat', 'clean', 'build'
+ }
+ } else {
+ exec {
+ executable './gradlew'
+ args 'clean', 'build'
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md
new file mode 100644
index 000000000..d81ff6147
--- /dev/null
+++ b/docs/CONTRIBUTING.md
@@ -0,0 +1,22 @@
+# Contribution Guidelines
+
+Feel free to work on any issue with a #hacktoberfest label.
+
+If you discover an issue you would like to work on, you can add a new issue to the list. If it meets our criteria, a hacktoberfest label will be added.
+
+# Who is allowed to submit pull requests for this project?
+
+Anyone can submit pull requests for code, tests, or documentation.
+
+# How do you decide which pull requests to accept?
+
+* Does it call out a bug that needs to be fixed? If so, it goes to the top of the list.
+* Does it fix a major user inconvenience? These are given high priority as well.
+* Does it align with the specs? If not, it will probably not be accepted. It turns out there are gray areas in the specs. If this is in a gray area, it will likely be given the benefit of the doubt.
+* Does it break the existing behavior of the lib? If so, it will not be accepted, unless it fixes an egregious bug. This is happening less frequently now.
+
+# For more guidance, see these links:
+
+[README.md (includes build instructions)](https://github.com/stleary/JSON-java#readme)
+
+[FAQ - all your questions answered](https://github.com/stleary/JSON-java/wiki/FAQ)
diff --git a/docs/FILES.md b/docs/FILES.md
new file mode 100644
index 000000000..152272190
--- /dev/null
+++ b/docs/FILES.md
@@ -0,0 +1,62 @@
+# Files
+
+**JSONObject.java**: The `JSONObject` can parse text from a `String` or a `JSONTokener`
+to produce a map-like object. The object provides methods for manipulating its
+contents, and for producing a JSON compliant object serialization.
+
+**JSONArray.java**: The `JSONArray` can parse text from a String or a `JSONTokener`
+to produce a vector-like object. The object provides methods for manipulating
+its contents, and for producing a JSON compliant array serialization.
+
+**JSONTokener.java**: The `JSONTokener` breaks a text into a sequence of individual
+tokens. It can be constructed from a `String`, `Reader`, or `InputStream`. It also can
+parse text from a `String`, `Number`, `Boolean` or `null` like `"hello"`, `42`, `true`,
+`null` to produce a simple json object.
+
+**JSONException.java**: The `JSONException` is the standard exception type thrown
+by this package.
+
+**JSONPointer.java**: Implementation of
+[JSON Pointer (RFC 6901)](https://tools.ietf.org/html/rfc6901). Supports
+JSON Pointers both in the form of string representation and URI fragment
+representation.
+
+**JSONPropertyIgnore.java**: Annotation class that can be used on Java Bean getter methods.
+When used on a bean method that would normally be serialized into a `JSONObject`, it
+overrides the getter-to-key-name logic and forces the property to be excluded from the
+resulting `JSONObject`.
+
+**JSONPropertyName.java**: Annotation class that can be used on Java Bean getter methods.
+When used on a bean method that would normally be serialized into a `JSONObject`, it
+overrides the getter-to-key-name logic and uses the value of the annotation. The Bean
+processor will look through the class hierarchy. This means you can use the annotation on
+a base class or interface and the value of the annotation will be used even if the getter
+is overridden in a child class.
+
+**JSONString.java**: The `JSONString` interface requires a `toJSONString` method,
+allowing an object to provide its own serialization.
+
+**JSONStringer.java**: The `JSONStringer` provides a convenient facility for
+building JSON strings.
+
+**JSONWriter.java**: The `JSONWriter` provides a convenient facility for building
+JSON text through a writer.
+
+
+**CDL.java**: `CDL` provides support for converting between JSON and comma
+delimited lists.
+
+**Cookie.java**: `Cookie` provides support for converting between JSON and cookies.
+
+**CookieList.java**: `CookieList` provides support for converting between JSON and
+cookie lists.
+
+**HTTP.java**: `HTTP` provides support for converting between JSON and HTTP headers.
+
+**HTTPTokener.java**: `HTTPTokener` extends `JSONTokener` for parsing HTTP headers.
+
+**XML.java**: `XML` provides support for converting between JSON and XML.
+
+**JSONML.java**: `JSONML` provides support for converting between JSONML and XML.
+
+**XMLTokener.java**: `XMLTokener` extends `JSONTokener` for parsing XML text.
\ No newline at end of file
diff --git a/docs/NOTES.md b/docs/NOTES.md
new file mode 100644
index 000000000..a8298ddfa
--- /dev/null
+++ b/docs/NOTES.md
@@ -0,0 +1,87 @@
+# Notes
+
+**Recent directory structure change**
+
+_Due to a recent commit - [#515 Merge tests and pom and code](https://github.com/stleary/JSON-java/pull/515) - the structure of the project has changed from a flat directory containing all of the Java files to a directory structure that includes unit tests and several tools used to build the project jar and run the unit tests. If you have difficulty using the new structure, please open an issue so we can work through it._
+
+**Implementation notes**
+
+Numeric types in this package comply with
+[ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) and
+[RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259#section-6).
+This package fully supports `Integer`, `Long`, and `Double` Java types. Partial support
+for `BigInteger` and `BigDecimal` values in `JSONObject` and `JSONArray` objects is provided
+in the form of `get()`, `opt()`, and `put()` API methods.
+
+Although 1.6 compatibility is currently supported, it is not a project goal and might be
+removed in some future release.
+
+In compliance with RFC8259 page 10 section 9, the parser is more lax with what is valid
+JSON then the Generator. For Example, the tab character (U+0009) is allowed when reading
+JSON Text strings, but when output by the Generator, the tab is properly converted to \t in
+the string. Other instances may occur where reading invalid JSON text does not cause an
+error to be generated. Malformed JSON Texts such as missing end " (quote) on strings or
+invalid number formats (1.2e6.3) will cause errors as such documents can not be read
+reliably.
+
+Some notable exceptions that the JSON Parser in this library accepts are:
+* Unquoted keys `{ key: "value" }`
+* Unquoted values `{ "key": value }`
+* Unescaped literals like "tab" in string values `{ "key": "value with an unescaped tab" }`
+* Numbers out of range for `Double` or `Long` are parsed as strings
+
+Recent pull requests added a new method `putAll` on the JSONArray. The `putAll` method
+works similarly to other `put` methods in that it does not call `JSONObject.wrap` for items
+added. This can lead to inconsistent object representation in JSONArray structures.
+
+For example, code like this will create a mixed JSONArray, some items wrapped, others
+not:
+
+```java
+SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
+// these will be wrapped
+JSONArray jArr = new JSONArray(myArr);
+// these will not be wrapped
+jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
+```
+
+For structure consistency, it would be recommended that the above code is changed
+to look like 1 of 2 ways.
+
+Option 1:
+```Java
+SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
+JSONArray jArr = new JSONArray();
+// these will not be wrapped
+jArr.putAll(myArr);
+// these will not be wrapped
+jArr.putAll(new SomeBean[]{ new SomeBean(3), new SomeBean(4) });
+// our jArr is now consistent.
+```
+
+Option 2:
+```Java
+SomeBean[] myArr = new SomeBean[]{ new SomeBean(1), new SomeBean(2) };
+// these will be wrapped
+JSONArray jArr = new JSONArray(myArr);
+// these will be wrapped
+jArr.putAll(new JSONArray(new SomeBean[]{ new SomeBean(3), new SomeBean(4) }));
+// our jArr is now consistent.
+```
+
+**Unit Test Conventions**
+
+Test filenames should consist of the name of the module being tested, with the suffix "Test".
+For example, Cookie.java is tested by CookieTest.java.
+
+The fundamental issues with JSON-Java testing are:
+* JSONObjects are unordered, making simple string comparison ineffective.
+* Comparisons via **equals()** is not currently supported. Neither JSONArray nor JSONObject override hashCode() or equals(), so comparison defaults to the Object equals(), which is not useful.
+* Access to the JSONArray and JSONObject internal containers for comparison is not currently available.
+
+General issues with unit testing are:
+* Just writing tests to make coverage goals tends to result in poor tests.
+* Unit tests are a form of documentation - how a given method works is demonstrated by the test. So for a code reviewer or future developer looking at code a good test helps explain how a function is supposed to work according to the original author. This can be difficult if you are not the original developer.
+* It is difficult to evaluate unit tests in a vacuum. You also need to see the code being tested to understand if a test is good.
+* Without unit tests, it is hard to feel confident about the quality of the code, especially when fixing bugs or refactoring. Good tests prevent regressions and keep the intent of the code correct.
+* If you have unit test results along with pull requests, the reviewer has an easier time understanding your code and determining if it works as intended.
\ No newline at end of file
diff --git a/docs/RELEASES.md b/docs/RELEASES.md
new file mode 100644
index 000000000..653e2bb8c
--- /dev/null
+++ b/docs/RELEASES.md
@@ -0,0 +1,64 @@
+# Release history:
+
+JSON-java releases can be found by searching the Maven repository for groupId "org.json"
+and artifactId "json". For example:
+[https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav)
+
+~~~
+20251224 Records, fromJson(), and recent commits
+
+20250517 Strict mode hardening and recent commits
+
+20250107 Restore moditect in pom.xml
+
+20241224 Strict mode opt-in feature, and recent commits. This release does not contain module-info.class.
+It is not recommended if you need this feature.
+
+20240303 Revert optLong/getLong changes, and recent commits.
+
+20240205 Recent commits.
+
+20231013 First release with minimum Java version 1.8. Recent commits, including fixes for CVE-2023-5072.
+
+20230618 Final release with Java 1.6 compatibility. Future releases will require Java 1.8 or greater.
+
+20230227 Fix for CVE-2022-45688 and recent commits
+
+20220924 New License - public domain, and some minor updates
+
+20220320 Wrap StackOverflow with JSONException
+
+20211205 Recent commits and some bug fixes for similar()
+
+20210307 Recent commits and potentially breaking fix to JSONPointer
+
+20201115 Recent commits and first release after project structure change
+
+20200518 Recent commits and snapshot before project structure change
+
+20190722 Recent commits
+
+20180813 POM change to include Automatic-Module-Name (#431)
+ JSONObject(Map) now throws an exception if any of a map keys are null (#405)
+
+20180130 Recent commits
+
+20171018 Checkpoint for recent commits.
+
+20170516 Roll up recent commits.
+
+20160810 Revert code that was breaking opt*() methods.
+
+20160807 This release contains a bug in the JSONObject.opt*() and JSONArray.opt*() methods,
+it is not recommended for use.
+Java 1.6 compatability fixed, JSONArray.toList() and JSONObject.toMap(),
+RFC4180 compatibility, JSONPointer, some exception fixes, optional XML type conversion.
+Contains the latest code as of 7 Aug 2016
+
+20160212 Java 1.6 compatibility, OSGi bundle. Contains the latest code as of 12 Feb 2016.
+
+20151123 JSONObject and JSONArray initialization with generics. Contains the latest code as of 23 Nov 2015.
+
+20150729 Checkpoint for Maven central repository release. Contains the latest code
+as of 29 July 2015.
+~~~
diff --git a/docs/SECURITY.md b/docs/SECURITY.md
new file mode 100644
index 000000000..5af9a566b
--- /dev/null
+++ b/docs/SECURITY.md
@@ -0,0 +1,5 @@
+# Security Policy
+
+## Reporting a Vulnerability
+
+Please follow the instructions in the ["How are vulnerabilities and exploits handled?"](https://github.com/stleary/JSON-java/wiki/FAQ#how-are-vulnerabilities-and-exploits-handled) section in the FAQ.
diff --git a/gradlew b/gradlew
old mode 100644
new mode 100755
diff --git a/pom.xml b/pom.xml
index 643bea532..8d0881cbe 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
org.jsonjson
- 20210307
+ 20251224bundleJSON in Java
@@ -15,22 +15,12 @@
It also includes the capability to convert between JSON and XML, HTTP
headers, Cookies, and CDL.
- This is a reference implementation. There is a large number of JSON packages
+ This is a reference implementation. There are a large number of JSON packages
in Java. Perhaps someday the Java community will standardize on one. Until
then, choose carefully.
-
- The license includes this restriction: "The software shall be used for good,
- not evil." If your conscience cannot live with that, then choose a different
- package.
https://github.com/douglascrockford/JSON-java
-
- org.sonatype.oss
- oss-parent
- 9
-
-
https://github.com/douglascrockford/JSON-java.gitscm:git:git://github.com/douglascrockford/JSON-java.git
@@ -39,28 +29,9 @@
- The JSON License
- http://json.org/license.html
+ Public Domain
+ https://github.com/stleary/JSON-java/blob/master/LICENSErepo
- Copyright (c) 2002 JSON.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
- associated documentation files (the "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
- following conditions:
-
- The above copyright notice and this permission notice shall be included in all copies or substantial
- portions of the Software.
-
- The Software shall be used for Good, not Evil.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
- LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
- NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
@@ -76,23 +47,36 @@
+
+
+ ossrh
+ Central Repository OSSRH
+ https://oss.sonatype.org/service/local/staging/deploy/maven2/
+
+
+ ossrh
+ https://oss.sonatype.org/content/repositories/snapshots
+
+
+
+
junitjunit
- 4.13.1
+ 4.13.2testcom.jayway.jsonpathjson-path
- 2.1.0
+ 2.9.0testorg.mockitomockito-core
- 1.9.5
+ 4.2.0test
@@ -102,7 +86,7 @@
org.apache.felixmaven-bundle-plugin
- 3.0.1
+ 5.1.9true
@@ -116,16 +100,19 @@
org.apache.maven.pluginsmaven-compiler-plugin
- 2.3.2
+ 3.11.0
- 1.6
- 1.6
+ 1.8
+ 1.8
+
+ -Xlint:unchecked
+ org.apache.maven.pluginsmaven-source-plugin
- 2.1.2
+ 3.3.0attach-sources
@@ -138,7 +125,7 @@
org.apache.maven.pluginsmaven-javadoc-plugin
- 2.7
+ 3.5.0attach-javadocs
@@ -154,7 +141,7 @@
org.apache.maven.pluginsmaven-gpg-plugin
- 1.5
+ 1.6sign-artifacts
@@ -162,6 +149,12 @@
sign
+
+
+ --pinentry-mode
+ loopback
+
+
@@ -176,18 +169,86 @@
false
+
+ org.moditect
+ moditect-maven-plugin
+ 1.0.0.Final
+
+
+ add-module-infos
+ package
+
+ add-module-info
+
+
+ 9
+
+
+ module org.json {
+ exports org.json;
+ }
+
+
+
+
+
+ org.apache.maven.pluginsmaven-jar-plugin
- 3.2.0
-
-
-
- org.json
-
-
-
+ 3.3.0
+
+
+ test-strict-mode
+
+
+
+ com.google.code.maven-replacer-plugin
+ replacer
+ 1.5.3
+
+
+
+ enable-strict-mode
+ process-sources
+
+ replace
+
+
+ src/main/java/org/json/JSONParserConfiguration.java
+
+
+ // this.strictMode = true;
+ this.strictMode = true;
+
+
+
+
+
+
+ restore-original
+ test
+
+ replace
+
+
+ src/main/java/org/json/JSONParserConfiguration.java
+
+
+ this.strictMode = true;
+ // this.strictMode = true;
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/java/org/json/CDL.java b/src/main/java/org/json/CDL.java
index f12cfc054..f9afb8338 100644
--- a/src/main/java/org/json/CDL.java
+++ b/src/main/java/org/json/CDL.java
@@ -1,39 +1,19 @@
package org.json;
/*
-Copyright (c) 2002 JSON.org
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-The Software shall be used for Good, not Evil.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+Public Domain.
*/
/**
- * This provides static methods to convert comma delimited text into a
- * JSONArray, and to convert a JSONArray into comma delimited text. Comma
+ * This provides static methods to convert comma (or otherwise) delimited text into a
+ * JSONArray, and to convert a JSONArray into comma (or otherwise) delimited text. Comma
* delimited text is a very popular format for data interchange. It is
* understood by most database, spreadsheet, and organizer programs.
*
* Each row of text represents a row in a table or a data record. Each row
* ends with a NEWLINE character. Each row contains one or more values.
* Values are separated by commas. A value can contain any character except
- * for comma, unless is is wrapped in single quotes or double quotes.
+ * for comma, unless it is wrapped in single quotes or double quotes.
*
* The first row usually contains the names of the columns.
*
@@ -45,25 +25,32 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class CDL {
+ /**
+ * Constructs a new CDL object.
+ * @deprecated (Utility class cannot be instantiated)
+ */
+ @Deprecated
+ public CDL() {
+ }
+
/**
* Get the next value. The value can be wrapped in quotes. The value can
* be empty.
* @param x A JSONTokener of the source text.
+ * @param delimiter used in the file
* @return The value string, or null if empty.
* @throws JSONException if the quoted string is badly formed.
*/
- private static String getValue(JSONTokener x) throws JSONException {
+ private static String getValue(JSONTokener x, char delimiter) throws JSONException {
char c;
char q;
StringBuilder sb;
do {
c = x.next();
} while (c == ' ' || c == '\t');
- switch (c) {
- case 0:
+ if (c == 0) {
return null;
- case '"':
- case '\'':
+ } else if (c == '"' || c == '\'') {
q = c;
sb = new StringBuilder();
for (;;) {
@@ -71,9 +58,9 @@ private static String getValue(JSONTokener x) throws JSONException {
if (c == q) {
//Handle escaped double-quote
char nextC = x.next();
- if(nextC != '\"') {
+ if (nextC != '\"') {
// if our quote was the end of the file, don't step
- if(nextC > 0) {
+ if (nextC > 0) {
x.back();
}
break;
@@ -85,13 +72,12 @@ private static String getValue(JSONTokener x) throws JSONException {
sb.append(c);
}
return sb.toString();
- case ',':
+ } else if (c == delimiter) {
x.back();
return "";
- default:
- x.back();
- return x.nextTo(',');
}
+ x.back();
+ return x.nextTo(delimiter);
}
/**
@@ -101,17 +87,32 @@ private static String getValue(JSONTokener x) throws JSONException {
* @throws JSONException if a called function fails
*/
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
+ return rowToJSONArray(x, ',');
+ }
+
+ /**
+ * Produce a JSONArray of strings from a row of comma delimited values.
+ * @param x A JSONTokener of the source text.
+ * @param delimiter custom delimiter char
+ * @return A JSONArray of strings.
+ * @throws JSONException if a called function fails
+ */
+ public static JSONArray rowToJSONArray(JSONTokener x, char delimiter) throws JSONException {
JSONArray ja = new JSONArray();
for (;;) {
- String value = getValue(x);
+ String value = getValue(x,delimiter);
char c = x.next();
- if (value == null ||
- (ja.length() == 0 && value.length() == 0 && c != ',')) {
+ if (value != null) {
+ ja.put(value);
+ } else if (ja.length() == 0 && c != delimiter) {
return null;
+ } else {
+ // This line accounts for CSV ending with no newline
+ ja.put("");
}
- ja.put(value);
+
for (;;) {
- if (c == ',') {
+ if (c == delimiter) {
break;
}
if (c != ' ') {
@@ -136,9 +137,23 @@ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
* @return A JSONObject combining the names and values.
* @throws JSONException if a called function fails
*/
- public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
- throws JSONException {
- JSONArray ja = rowToJSONArray(x);
+ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws JSONException {
+ return rowToJSONObject(names, x, ',');
+ }
+
+ /**
+ * Produce a JSONObject from a row of comma delimited text, using a
+ * parallel JSONArray of strings to provides the names of the elements.
+ * @param names A JSONArray of names. This is commonly obtained from the
+ * first row of a comma delimited text file using the rowToJSONArray
+ * method.
+ * @param x A JSONTokener of the source text.
+ * @param delimiter custom delimiter char
+ * @return A JSONObject combining the names and values.
+ * @throws JSONException if a called function fails
+ */
+ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
+ JSONArray ja = rowToJSONArray(x, delimiter);
return ja != null ? ja.toJSONObject(names) : null;
}
@@ -150,15 +165,27 @@ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
* @return A string ending in NEWLINE.
*/
public static String rowToString(JSONArray ja) {
+ return rowToString(ja, ',');
+ }
+
+ /**
+ * Produce a comma delimited text row from a JSONArray. Values containing
+ * the comma character will be quoted. Troublesome characters may be
+ * removed.
+ * @param ja A JSONArray of strings.
+ * @param delimiter custom delimiter char
+ * @return A string ending in NEWLINE.
+ */
+ public static String rowToString(JSONArray ja, char delimiter) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ja.length(); i += 1) {
if (i > 0) {
- sb.append(',');
+ sb.append(delimiter);
}
Object object = ja.opt(i);
if (object != null) {
String string = object.toString();
- if (string.length() > 0 && (string.indexOf(',') >= 0 ||
+ if (!string.isEmpty() && (string.indexOf(delimiter) >= 0 ||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
sb.append('"');
@@ -187,7 +214,19 @@ public static String rowToString(JSONArray ja) {
* @throws JSONException if a called function fails
*/
public static JSONArray toJSONArray(String string) throws JSONException {
- return toJSONArray(new JSONTokener(string));
+ return toJSONArray(string, ',');
+ }
+
+ /**
+ * Produce a JSONArray of JSONObjects from a comma delimited text string,
+ * using the first row as a source of names.
+ * @param string The comma delimited text.
+ * @param delimiter custom delimiter char
+ * @return A JSONArray of JSONObjects.
+ * @throws JSONException if a called function fails
+ */
+ public static JSONArray toJSONArray(String string, char delimiter) throws JSONException {
+ return toJSONArray(new JSONTokener(string), delimiter);
}
/**
@@ -198,7 +237,31 @@ public static JSONArray toJSONArray(String string) throws JSONException {
* @throws JSONException if a called function fails
*/
public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
- return toJSONArray(rowToJSONArray(x), x);
+ return toJSONArray(x, ',');
+ }
+
+ /**
+ * Produce a JSONArray of JSONObjects from a comma delimited text string,
+ * using the first row as a source of names.
+ * @param x The JSONTokener containing the comma delimited text.
+ * @param delimiter custom delimiter char
+ * @return A JSONArray of JSONObjects.
+ * @throws JSONException if a called function fails
+ */
+ public static JSONArray toJSONArray(JSONTokener x, char delimiter) throws JSONException {
+ return toJSONArray(rowToJSONArray(x, delimiter), x, delimiter);
+ }
+
+ /**
+ * Produce a JSONArray of JSONObjects from a comma delimited text string
+ * using a supplied JSONArray as the source of element names.
+ * @param names A JSONArray of strings.
+ * @param string The comma delimited text.
+ * @return A JSONArray of JSONObjects.
+ * @throws JSONException if a called function fails
+ */
+ public static JSONArray toJSONArray(JSONArray names, String string) throws JSONException {
+ return toJSONArray(names, string, ',');
}
/**
@@ -206,12 +269,24 @@ public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
* using a supplied JSONArray as the source of element names.
* @param names A JSONArray of strings.
* @param string The comma delimited text.
+ * @param delimiter custom delimiter char
+ * @return A JSONArray of JSONObjects.
+ * @throws JSONException if a called function fails
+ */
+ public static JSONArray toJSONArray(JSONArray names, String string, char delimiter) throws JSONException {
+ return toJSONArray(names, new JSONTokener(string), delimiter);
+ }
+
+ /**
+ * Produce a JSONArray of JSONObjects from a comma delimited text string
+ * using a supplied JSONArray as the source of element names.
+ * @param names A JSONArray of strings.
+ * @param x A JSONTokener of the source text.
* @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails
*/
- public static JSONArray toJSONArray(JSONArray names, String string)
- throws JSONException {
- return toJSONArray(names, new JSONTokener(string));
+ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException {
+ return toJSONArray(names, x, ',');
}
/**
@@ -219,17 +294,17 @@ public static JSONArray toJSONArray(JSONArray names, String string)
* using a supplied JSONArray as the source of element names.
* @param names A JSONArray of strings.
* @param x A JSONTokener of the source text.
+ * @param delimiter custom delimiter char
* @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails
*/
- public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
- throws JSONException {
+ public static JSONArray toJSONArray(JSONArray names, JSONTokener x, char delimiter) throws JSONException {
if (names == null || names.length() == 0) {
return null;
}
JSONArray ja = new JSONArray();
for (;;) {
- JSONObject jo = rowToJSONObject(names, x);
+ JSONObject jo = rowToJSONObject(names, x, delimiter);
if (jo == null) {
break;
}
@@ -238,6 +313,17 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
if (ja.length() == 0) {
return null;
}
+
+ // The following block accounts for empty datasets (no keys or vals)
+ if (ja.length() == 1) {
+ JSONObject j = ja.getJSONObject(0);
+ if (j.length() == 1) {
+ String key = j.keys().next();
+ if ("".equals(key) && "".equals(j.get(key))) {
+ return null;
+ }
+ }
+ }
return ja;
}
@@ -251,11 +337,24 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
* @throws JSONException if a called function fails
*/
public static String toString(JSONArray ja) throws JSONException {
+ return toString(ja, ',');
+ }
+
+ /**
+ * Produce a comma delimited text from a JSONArray of JSONObjects. The
+ * first row will be a list of names obtained by inspecting the first
+ * JSONObject.
+ * @param ja A JSONArray of JSONObjects.
+ * @param delimiter custom delimiter char
+ * @return A comma delimited text.
+ * @throws JSONException if a called function fails
+ */
+ public static String toString(JSONArray ja, char delimiter) throws JSONException {
JSONObject jo = ja.optJSONObject(0);
if (jo != null) {
JSONArray names = jo.names();
if (names != null) {
- return rowToString(names) + toString(names, ja);
+ return rowToString(names, delimiter) + toString(names, ja, delimiter);
}
}
return null;
@@ -270,8 +369,21 @@ public static String toString(JSONArray ja) throws JSONException {
* @return A comma delimited text.
* @throws JSONException if a called function fails
*/
- public static String toString(JSONArray names, JSONArray ja)
- throws JSONException {
+ public static String toString(JSONArray names, JSONArray ja) throws JSONException {
+ return toString(names, ja, ',');
+ }
+
+ /**
+ * Produce a comma delimited text from a JSONArray of JSONObjects using
+ * a provided list of names. The list of names is not included in the
+ * output.
+ * @param names A JSONArray of strings.
+ * @param ja A JSONArray of JSONObjects.
+ * @param delimiter custom delimiter char
+ * @return A comma delimited text.
+ * @throws JSONException if a called function fails
+ */
+ public static String toString(JSONArray names, JSONArray ja, char delimiter) throws JSONException {
if (names == null || names.length() == 0) {
return null;
}
@@ -279,7 +391,7 @@ public static String toString(JSONArray names, JSONArray ja)
for (int i = 0; i < ja.length(); i += 1) {
JSONObject jo = ja.optJSONObject(i);
if (jo != null) {
- sb.append(rowToString(jo.toJSONArray(names)));
+ sb.append(rowToString(jo.toJSONArray(names), delimiter));
}
}
return sb.toString();
diff --git a/src/main/java/org/json/Cookie.java b/src/main/java/org/json/Cookie.java
index a43d1eddd..f7bab236f 100644
--- a/src/main/java/org/json/Cookie.java
+++ b/src/main/java/org/json/Cookie.java
@@ -3,27 +3,7 @@
import java.util.Locale;
/*
-Copyright (c) 2002 JSON.org
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-The Software shall be used for Good, not Evil.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+Public Domain.
*/
/**
@@ -35,6 +15,14 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class Cookie {
+ /**
+ * Constructs a new Cookie object.
+ * @deprecated (Utility class cannot be instantiated)
+ */
+ @Deprecated()
+ public Cookie() {
+ }
+
/**
* Produce a copy of a string in which the characters '+', '%', '=', ';'
* and control characters are replaced with "%hh". This is a gentle form
@@ -109,7 +97,7 @@ public static JSONObject toJSONObject(String string) {
// parse the remaining cookie attributes
while (x.more()) {
name = unescape(x.nextTo("=;")).trim().toLowerCase(Locale.ROOT);
- // don't allow a cookies attributes to overwrite it's name or value.
+ // don't allow a cookies attributes to overwrite its name or value.
if("name".equalsIgnoreCase(name)) {
throw new JSONException("Illegal attribute name: 'name'");
}
diff --git a/src/main/java/org/json/CookieList.java b/src/main/java/org/json/CookieList.java
index 83b2630e5..ce47aee02 100644
--- a/src/main/java/org/json/CookieList.java
+++ b/src/main/java/org/json/CookieList.java
@@ -1,27 +1,7 @@
package org.json;
/*
-Copyright (c) 2002 JSON.org
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-The Software shall be used for Good, not Evil.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+Public Domain.
*/
/**
@@ -31,6 +11,14 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class CookieList {
+ /**
+ * Constructs a new CookieList object.
+ * @deprecated (Utility class cannot be instantiated)
+ */
+ @Deprecated
+ public CookieList() {
+ }
+
/**
* Convert a cookie list into a JSONObject. A cookie list is a sequence
* of name/value pairs. The names are separated from the values by '='.
@@ -66,19 +54,19 @@ public static JSONObject toJSONObject(String string) throws JSONException {
* @throws JSONException if a called function fails
*/
public static String toString(JSONObject jo) throws JSONException {
- boolean b = false;
+ boolean isEndOfPair = false;
final StringBuilder sb = new StringBuilder();
// Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) {
final Object value = jo.opt(key);
if (!JSONObject.NULL.equals(value)) {
- if (b) {
+ if (isEndOfPair) {
sb.append(';');
}
sb.append(Cookie.escape(key));
sb.append("=");
sb.append(Cookie.escape(value.toString()));
- b = true;
+ isEndOfPair = true;
}
}
return sb.toString();
diff --git a/src/main/java/org/json/HTTP.java b/src/main/java/org/json/HTTP.java
index cc01167c6..44ab3a6d3 100644
--- a/src/main/java/org/json/HTTP.java
+++ b/src/main/java/org/json/HTTP.java
@@ -1,27 +1,7 @@
package org.json;
/*
-Copyright (c) 2002 JSON.org
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-The Software shall be used for Good, not Evil.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+Public Domain.
*/
import java.util.Locale;
@@ -33,6 +13,12 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class HTTP {
+ /**
+ * Constructs a new HTTP object.
+ */
+ public HTTP() {
+ }
+
/** Carriage return/line feed. */
public static final String CRLF = "\r\n";
diff --git a/src/main/java/org/json/HTTPTokener.java b/src/main/java/org/json/HTTPTokener.java
index 16c7081a9..48cad31a3 100644
--- a/src/main/java/org/json/HTTPTokener.java
+++ b/src/main/java/org/json/HTTPTokener.java
@@ -1,27 +1,7 @@
package org.json;
/*
-Copyright (c) 2002 JSON.org
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-The Software shall be used for Good, not Evil.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+Public Domain.
*/
/**
diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java
index 1e6a8a6f9..2a3c553a6 100644
--- a/src/main/java/org/json/JSONArray.java
+++ b/src/main/java/org/json/JSONArray.java
@@ -1,31 +1,10 @@
package org.json;
/*
- Copyright (c) 2002 JSON.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
-
- The Software shall be used for Good, not Evil.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
+Public Domain.
*/
import java.io.IOException;
-import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.math.BigDecimal;
@@ -104,15 +83,30 @@ public JSONArray() {
* If there is a syntax error.
*/
public JSONArray(JSONTokener x) throws JSONException {
+ this(x, x.getJsonParserConfiguration());
+ }
+
+ /**
+ * Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
+ *
+ * @param x A JSONTokener instance from which the JSONArray is constructed.
+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
+ * @throws JSONException If a syntax error occurs during the construction of the JSONArray.
+ */
+ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this();
+
+ boolean isInitial = x.getPrevious() == 0;
if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
-
+
char nextChar = x.nextClean();
if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
+ } else if (nextChar==',' && jsonParserConfiguration.isStrictMode()) {
+ throw x.syntaxError("Array content starts with a ','");
}
if (nextChar != ']') {
x.back();
@@ -124,28 +118,59 @@ public JSONArray(JSONTokener x) throws JSONException {
x.back();
this.myArrayList.add(x.nextValue());
}
- switch (x.nextClean()) {
- case 0:
- // array is unclosed. No ']' found, instead EOF
- throw x.syntaxError("Expected a ',' or ']'");
- case ',':
- nextChar = x.nextClean();
- if (nextChar == 0) {
- // array is unclosed. No ']' found, instead EOF
- throw x.syntaxError("Expected a ',' or ']'");
- }
- if (nextChar == ']') {
- return;
- }
- x.back();
- break;
- case ']':
- return;
- default:
- throw x.syntaxError("Expected a ',' or ']'");
+ if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
+ }
+ } else {
+ if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
+ throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
+ }
+ }
+ }
+
+ /** Convenience function. Checks for JSON syntax error.
+ * @param x A JSONTokener instance from which the JSONArray is constructed.
+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
+ * @param isInitial Boolean indicating position of char
+ * @return
+ */
+ private static boolean checkForSyntaxError(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) {
+ char nextChar;
+ switch (x.nextClean()) {
+ case 0:
+ // array is unclosed. No ']' found, instead EOF
+ throw x.syntaxError("Expected a ',' or ']'");
+ case ',':
+ nextChar = x.nextClean();
+ if (nextChar == 0) {
+ // array is unclosed. No ']' found, instead EOF
+ throw x.syntaxError("Expected a ',' or ']'");
+ }
+ if (nextChar == ']') {
+ // trailing commas are not allowed in strict mode
+ if (jsonParserConfiguration.isStrictMode()) {
+ throw x.syntaxError("Strict mode error: Expected another array element");
+ }
+ return true;
+ }
+ if (nextChar == ',') {
+ // consecutive commas are not allowed in strict mode
+ if (jsonParserConfiguration.isStrictMode()) {
+ throw x.syntaxError("Strict mode error: Expected a valid array element");
}
+ return true;
}
+ x.back();
+ break;
+ case ']':
+ if (isInitial && jsonParserConfiguration.isStrictMode() &&
+ x.nextClean() != 0) {
+ throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
+ }
+ return true;
+ default:
+ throw x.syntaxError("Expected a ',' or ']'");
}
+ return false;
}
/**
@@ -159,7 +184,22 @@ public JSONArray(JSONTokener x) throws JSONException {
* If there is a syntax error.
*/
public JSONArray(String source) throws JSONException {
- this(new JSONTokener(source));
+ this(source, new JSONParserConfiguration());
+ }
+
+ /**
+ * Construct a JSONArray from a source JSON text.
+ *
+ * @param source
+ * A string that begins with [ (left
+ * bracket) and ends with ]
+ * (right bracket).
+ * @param jsonParserConfiguration the parser config object
+ * @throws JSONException
+ * If there is a syntax error.
+ */
+ public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
+ this(new JSONTokener(source, jsonParserConfiguration), jsonParserConfiguration);
}
/**
@@ -169,11 +209,40 @@ public JSONArray(String source) throws JSONException {
* A Collection.
*/
public JSONArray(Collection> collection) {
+ this(collection, 0, new JSONParserConfiguration());
+ }
+
+ /**
+ * Construct a JSONArray from a Collection.
+ *
+ * @param collection
+ * A Collection.
+ * @param jsonParserConfiguration
+ * Configuration object for the JSON parser
+ */
+ public JSONArray(Collection> collection, JSONParserConfiguration jsonParserConfiguration) {
+ this(collection, 0, jsonParserConfiguration);
+ }
+
+ /**
+ * Construct a JSONArray from a collection with recursion depth.
+ *
+ * @param collection
+ * A Collection.
+ * @param recursionDepth
+ * Variable for tracking the count of nested object creations.
+ * @param jsonParserConfiguration
+ * Configuration object for the JSON parser
+ */
+ JSONArray(Collection> collection, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
+ if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) {
+ throw new JSONException("JSONArray has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth());
+ }
if (collection == null) {
this.myArrayList = new ArrayList