diff --git a/Examples.md b/Examples.md index 3f45e78..fb010d5 100644 --- a/Examples.md +++ b/Examples.md @@ -1,7 +1,7 @@
Imports used in the examples:
-``` +```java import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -14,353 +14,353 @@ import java.util.Properties;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.
+1. testM3() //add "SWE262_" to every keys as prefix +2. testM3Reverse() //reverse the key names +3. testM3WithEmptyString() //throws exception: "Invalid: transform key to empty string" +4. testM3WithNull() //throws exception: "Invalid: transform key to null" +5. testM3WithDuplicateKeyName() //throws exception: "Invalid: transform key to all the same string" ++Run "mvn clean test -Dtest=XMLTest" to test XMLTest.java + +### Performance Comparison + +running time: in-library = 123 ms; +outside = 205 ms +The efficiency increased by 40%. + + + + diff --git a/Milestone4_Readme.md b/Milestone4_Readme.md new file mode 100644 index 0000000..36d8916 --- /dev/null +++ b/Milestone4_Readme.md @@ -0,0 +1,34 @@ +# Milestone4 + +### toStream() methods is added in src/main/java/org.json/JSONObject.java at [line 494] + +* toStream() method returns a stream of JSONObjects inside the input JSONObject. It returns the elements in DFS manner. + +For example, the input JSONObject is as follows: +
+{"contact": {
+"nick": "Crista",
+"address": {
+"zipcode": 92614,
+"street": "Ave of Nowhere"
+},
+"name": "Crista Lopes"
+}}
+
+
+The expected output stream should look like this:
+
+{"contact":{"nick":"Crista","address":{"zipcode":92614,"street":"Ave of Nowhere"},"name":"Crista Lopes"}}
+{"nick":"Crista"}
+{"address":{"zipcode":92614,"street":"Ave of Nowhere"}}
+{"zipcode":92614}
+{"street":"Ave of Nowhere"}
+{"name":"Crista Lopes"}
+
+
+### Unit Tests are added in src/test/java/org.json.junit/Milestone4Test.java
+
+`testForEach()` at line 19 ~ 34
+`testFilter()` at line 38 ~ 51
+`testMap()` at line 54 ~ 63
+
diff --git a/Milestone5_Readme.md b/Milestone5_Readme.md
new file mode 100644
index 0000000..c46f351
--- /dev/null
+++ b/Milestone5_Readme.md
@@ -0,0 +1,6 @@
+# Milestone5
+
+### toJSONObject() method is added in src/main/java/org.json/XML.java
+* ```Future+We wrote eight tests for testing Problem1 and Problem2. + +-----Problem 1----- +1. testM2P1WithNoSlash_1() //No slash at the end of JSONPointer +2. testM2P1WithNoSlash_2() //No slash at the end of JSONPointer +3. testM2P1WithSlash() //With slash at the end of JSONPointer (throws exception: "Invalid Path with '/' ending") +4. testM2P1WrongPath() //Wrong JSONPointer (throws exception: "Path not found") + +-----Problem 2----- +1. testM2P2WithNoSlash_1() //No slash at the end of JSONPointer +2. testM2P2WithNoSlash_2() //No slash at the end of JSONPointer +3. testM2P2WithSlash() //With slash at the end of JSONPointer (throws exception: "Invalid Path with '/' ending") +4. testM2P2WrongPath() //Wrong JSONPointer (throws exception: "Path not found") + +Run "mvn clean test -Dtest=XMLTest" to test XMLTest.java ++ +### < Milestone3 > +
+We wrote four tests for testing Milestone 3. + +1. testM3() //add "SWE262_" to every keys as prefix +2. testM3WithEmptyString() //throws exception: "Invalid: transform key to empty string" +3. testM3WithNull() //throws exception: "Invalid: transform key to null" +4. testM3WithDuplicateKeyName() //throws exception: "Invalid: transform key to all the same string" + +Run "mvn clean test -Dtest=XMLTest" to test XMLTest.java ++ JSON in Java [package org.json] =============================== [](https://mvnrepository.com/artifact/org.json/json) -**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20220320/json-20220320.jar)** +**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20220924/json-20220924.jar)** # Overview @@ -28,8 +59,6 @@ Project goals include: 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 For more information on contributions, please see [CONTRIBUTING.md](https://github.com/stleary/JSON-java/blob/master/docs/CONTRIBUTING.md) diff --git a/build.gradle b/build.gradle index 63a31a7..a10ee60 100644 --- a/build.gradle +++ b/build.gradle @@ -32,7 +32,7 @@ subprojects { group = 'org.json' version = 'v20211205-SNAPSHOT' description = 'JSON in Java' -sourceCompatibility = '1.7' +sourceCompatibility = "1.8" configurations.all { } @@ -53,3 +53,4 @@ publishing { tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } +targetCompatibility = JavaVersion.VERSION_1_8 diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 149525d..3fbab78 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -5,6 +5,8 @@ and artifactId "json". For example: [https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav) ~~~ +20220924 New License - public domain, and some minor updates + 20220320 Wrap StackOverflow with JSONException 20211205 Recent commits and some bug fixes for similar() @@ -18,6 +20,7 @@ and artifactId "json". For example: 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 diff --git a/m1_4.png b/m1_4.png new file mode 100644 index 0000000..a1a4f4a Binary files /dev/null and b/m1_4.png differ diff --git a/m3.png b/m3.png new file mode 100644 index 0000000..634a763 Binary files /dev/null and b/m3.png differ diff --git a/pom.xml b/pom.xml index e4a3503..93fed2c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@
""
* will have the Bean parser fall back to the default field name processing.
*/
+@Documented
+@Retention(RUNTIME)
+@Target({METHOD})
public @interface JSONPropertyName {
/**
* @return The name of the property as to be used in the JSON Object.
diff --git a/src/main/java/org/json/JSONString.java b/src/main/java/org/json/JSONString.java
index bcd9a81..cd8d184 100644
--- a/src/main/java/org/json/JSONString.java
+++ b/src/main/java/org/json/JSONString.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/JSONStringer.java b/src/main/java/org/json/JSONStringer.java
index d2a4dfb..2f6cf9e 100644
--- a/src/main/java/org/json/JSONStringer.java
+++ b/src/main/java/org/json/JSONStringer.java
@@ -1,27 +1,7 @@
package org.json;
/*
-Copyright (c) 2006 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.StringWriter;
diff --git a/src/main/java/org/json/JSONTokener.java b/src/main/java/org/json/JSONTokener.java
index 7f0c86a..8c98c77 100644
--- a/src/main/java/org/json/JSONTokener.java
+++ b/src/main/java/org/json/JSONTokener.java
@@ -8,27 +8,7 @@
import java.io.StringReader;
/*
-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/JSONWriter.java b/src/main/java/org/json/JSONWriter.java
index dafb1b2..11f4a5c 100644
--- a/src/main/java/org/json/JSONWriter.java
+++ b/src/main/java/org/json/JSONWriter.java
@@ -5,27 +5,7 @@
import java.util.Map;
/*
-Copyright (c) 2006 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/Property.java b/src/main/java/org/json/Property.java
index 7caeebb..83694c0 100644
--- a/src/main/java/org/json/Property.java
+++ b/src/main/java/org/json/Property.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.Enumeration;
diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java
index 33838a1..56318df 100644
--- a/src/main/java/org/json/XML.java
+++ b/src/main/java/org/json/XML.java
@@ -1,34 +1,20 @@
package org.json;
/*
-Copyright (c) 2015 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.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.*;
+import java.util.function.Consumer;
+import java.util.function.Function;
/**
@@ -41,31 +27,61 @@ of this software and associated documentation files (the "Software"), to deal
@SuppressWarnings("boxing")
public class XML {
- /** The Character '&'. */
+ public static boolean isReachedParse1 = false;
+ public static boolean isReachedParse2 = false;
+
+ private static class EarlyTermination extends Exception {
+
+ public JSONObject subObject;
+
+ public EarlyTermination(JSONObject obj) {
+ subObject = obj;
+ }
+ }
+
+ /**
+ * The Character '&'.
+ */
public static final Character AMP = '&';
- /** The Character '''. */
+ /**
+ * The Character '''.
+ */
public static final Character APOS = '\'';
- /** The Character '!'. */
+ /**
+ * The Character '!'.
+ */
public static final Character BANG = '!';
- /** The Character '='. */
+ /**
+ * The Character '='.
+ */
public static final Character EQ = '=';
- /** The Character {@code '>'. }*/
+ /**
+ * The Character {@code '>'. }
+ */
public static final Character GT = '>';
- /** The Character '<'. */
+ /**
+ * The Character '<'.
+ */
public static final Character LT = '<';
- /** The Character '?'. */
+ /**
+ * The Character '?'.
+ */
public static final Character QUEST = '?';
- /** The Character '"'. */
+ /**
+ * The Character '"'.
+ */
public static final Character QUOT = '"';
- /** The Character '/'. */
+ /**
+ * The Character '/'.
+ */
public static final Character SLASH = '/';
/**
@@ -84,7 +100,7 @@ public class XML {
* which is available in Java8 and above.
*
* @see http://stackoverflow.com/a/21791059/6030888
+ * "http://stackoverflow.com/a/21791059/6030888">http://stackoverflow.com/a/21791059/6030888
*/
private static Iterable{@code
+ * {@code
* & (ampersand) is replaced by &
* < (less than) is replaced by <
* > (greater than) is replaced by >
@@ -126,37 +142,36 @@ public void remove() {
* ' (single quote / apostrophe) is replaced by '
* }
*
- * @param string
- * The string to be escaped.
+ * @param string The string to be escaped.
* @return The escaped string.
*/
public static String escape(String string) {
StringBuilder sb = new StringBuilder(string.length());
for (final int cp : codePointIterator(string)) {
switch (cp) {
- case '&':
- sb.append("&");
- break;
- case '<':
- sb.append("<");
- break;
- case '>':
- sb.append(">");
- break;
- case '"':
- sb.append(""");
- break;
- case '\'':
- sb.append("'");
- break;
- default:
- if (mustEscape(cp)) {
- sb.append("");
- sb.append(Integer.toHexString(cp));
- sb.append(';');
- } else {
- sb.appendCodePoint(cp);
- }
+ case '&':
+ sb.append("&");
+ break;
+ case '<':
+ sb.append("<");
+ break;
+ case '>':
+ sb.append(">");
+ break;
+ case '"':
+ sb.append(""");
+ break;
+ case '\'':
+ sb.append("'");
+ break;
+ default:
+ if (mustEscape(cp)) {
+ sb.append("");
+ sb.append(Integer.toHexString(cp));
+ sb.append(';');
+ } else {
+ sb.appendCodePoint(cp);
+ }
}
}
return sb.toString();
@@ -179,20 +194,19 @@ private static boolean mustEscape(int cp) {
&& cp != 0x9
&& cp != 0xA
&& cp != 0xD
- ) || !(
+ ) || !(
// valid the range of acceptable characters that aren't control
(cp >= 0x20 && cp <= 0xD7FF)
- || (cp >= 0xE000 && cp <= 0xFFFD)
- || (cp >= 0x10000 && cp <= 0x10FFFF)
- )
- ;
+ || (cp >= 0xE000 && cp <= 0xFFFD)
+ || (cp >= 0x10000 && cp <= 0x10FFFF)
+ )
+ ;
}
/**
* Removes XML escapes from the string.
*
- * @param string
- * string to remove escapes from
+ * @param string string to remove escapes from
* @return string with converted entities
*/
public static String unescape(String string) {
@@ -223,8 +237,7 @@ public static String unescape(String string) {
* Throw an exception if the string contains whitespace. Whitespace is not
* allowed in tagNames and attributes.
*
- * @param string
- * A string.
+ * @param string A string.
* @throws JSONException Thrown if the string contains whitespace or is empty.
*/
public static void noSpace(String string) throws JSONException {
@@ -243,226 +256,22 @@ public static void noSpace(String string) throws JSONException {
/**
* Scan the content following the named tag, attaching it to the context.
*
- * @param x
- * The XMLTokener containing the source string.
- * @param context
- * The JSONObject that will include the new material.
- * @param name
- * The tag name.
+ * @param x The XMLTokener containing the source string.
+ * @param context The JSONObject that will include the new material.
+ * @param name The tag name.
* @return true if the close tag is processed.
* @throws JSONException
*/
- private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config)
- throws JSONException {
- char c;
- int i;
- JSONObject jsonObject = null;
- String string;
- String tagName;
- Object token;
- XMLXsiTypeConverter> xmlXsiTypeConverter;
-
- // Test for and skip past these forms:
- //
- //
- //
- // ... ?>
- // Report errors for these forms:
- // <>
- // <=
- // <<
-
- token = x.nextToken();
-
- // ");
- return false;
- }
- x.back();
- } else if (c == '[') {
- token = x.nextToken();
- if ("CDATA".equals(token)) {
- if (x.next() == '[') {
- string = x.nextCDATA();
- if (string.length() > 0) {
- context.accumulate(config.getcDataTagName(), string);
- }
- return false;
- }
- }
- throw x.syntaxError("Expected 'CDATA['");
- }
- i = 1;
- do {
- token = x.nextMeta();
- if (token == null) {
- throw x.syntaxError("Missing '>' after ' 0);
- return false;
- } else if (token == QUEST) {
-
- //
- x.skipPast("?>");
- return false;
- } else if (token == SLASH) {
-
- // Close tag
-
- token = x.nextToken();
- if (name == null) {
- throw x.syntaxError("Mismatched close tag " + token);
- }
- if (!token.equals(name)) {
- throw x.syntaxError("Mismatched " + name + " and " + token);
- }
- if (x.nextToken() != GT) {
- throw x.syntaxError("Misshaped close tag");
- }
- return true;
-
- } else if (token instanceof Character) {
- throw x.syntaxError("Misshaped tag");
-
- // Open tag <
-
- } else {
- tagName = (String) token;
- token = null;
- jsonObject = new JSONObject();
- boolean nilAttributeFound = false;
- xmlXsiTypeConverter = null;
- for (;;) {
- if (token == null) {
- token = x.nextToken();
- }
- // attribute = value
- if (token instanceof String) {
- string = (String) token;
- token = x.nextToken();
- if (token == EQ) {
- token = x.nextToken();
- if (!(token instanceof String)) {
- throw x.syntaxError("Missing value");
- }
-
- if (config.isConvertNilAttributeToNull()
- && NULL_ATTR.equals(string)
- && Boolean.parseBoolean((String) token)) {
- nilAttributeFound = true;
- } else if(config.getXsiTypeMap() != null && !config.getXsiTypeMap().isEmpty()
- && TYPE_ATTR.equals(string)) {
- xmlXsiTypeConverter = config.getXsiTypeMap().get(token);
- } else if (!nilAttributeFound) {
- jsonObject.accumulate(string,
- config.isKeepStrings()
- ? ((String) token)
- : stringToValue((String) token));
- }
- token = null;
- } else {
- jsonObject.accumulate(string, "");
- }
-
-
- } else if (token == SLASH) {
- // Empty tag <.../>
- if (x.nextToken() != GT) {
- throw x.syntaxError("Misshaped tag");
- }
- if (config.getForceList().contains(tagName)) {
- // Force the value to be an array
- if (nilAttributeFound) {
- context.append(tagName, JSONObject.NULL);
- } else if (jsonObject.length() > 0) {
- context.append(tagName, jsonObject);
- } else {
- context.put(tagName, new JSONArray());
- }
- } else {
- if (nilAttributeFound) {
- context.accumulate(tagName, JSONObject.NULL);
- } else if (jsonObject.length() > 0) {
- context.accumulate(tagName, jsonObject);
- } else {
- context.accumulate(tagName, "");
- }
- }
- return false;
-
- } else if (token == GT) {
- // Content, between <...> and
- for (;;) {
- token = x.nextContent();
- if (token == null) {
- if (tagName != null) {
- throw x.syntaxError("Unclosed tag " + tagName);
- }
- return false;
- } else if (token instanceof String) {
- string = (String) token;
- if (string.length() > 0) {
- if(xmlXsiTypeConverter != null) {
- jsonObject.accumulate(config.getcDataTagName(),
- stringToValue(string, xmlXsiTypeConverter));
- } else {
- jsonObject.accumulate(config.getcDataTagName(),
- config.isKeepStrings() ? string : stringToValue(string));
- }
- }
-
- } else if (token == LT) {
- // Nested element
- if (parse(x, jsonObject, tagName, config)) {
- if (config.getForceList().contains(tagName)) {
- // Force the value to be an array
- if (jsonObject.length() == 0) {
- context.put(tagName, new JSONArray());
- } else if (jsonObject.length() == 1
- && jsonObject.opt(config.getcDataTagName()) != null) {
- context.append(tagName, jsonObject.opt(config.getcDataTagName()));
- } else {
- context.append(tagName, jsonObject);
- }
- } else {
- if (jsonObject.length() == 0) {
- context.accumulate(tagName, "");
- } else if (jsonObject.length() == 1
- && jsonObject.opt(config.getcDataTagName()) != null) {
- context.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
- } else {
- context.accumulate(tagName, jsonObject);
- }
- }
-
- return false;
- }
- }
- }
- } else {
- throw x.syntaxError("Misshaped tag");
- }
- }
- }
- }
/**
* This method tries to convert the given string value to the target object
- * @param string String to convert
+ *
+ * @param string String to convert
* @param typeConverter value converter to convert string to integer, boolean e.t.c
* @return JSON value of this string or the string
*/
public static Object stringToValue(String string, XMLXsiTypeConverter> typeConverter) {
- if(typeConverter != null) {
+ if (typeConverter != null) {
return typeConverter.convert(string);
}
return stringToValue(string);
@@ -507,7 +316,7 @@ public static Object stringToValue(String string) {
}
return string;
}
-
+
/**
* direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support.
*/
@@ -521,7 +330,7 @@ private static Number stringToNumber(final String val) throws NumberFormatExcept
// keep that by forcing a decimal.
try {
BigDecimal bd = new BigDecimal(val);
- if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
+ if (initial == '-' && BigDecimal.ZERO.compareTo(bd) == 0) {
return Double.valueOf(-0.0);
}
return bd;
@@ -529,48 +338,48 @@ private static Number stringToNumber(final String val) throws NumberFormatExcept
// this is to support "Hex Floats" like this: 0x1.0P-1074
try {
Double d = Double.valueOf(val);
- if(d.isNaN() || d.isInfinite()) {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ if (d.isNaN() || d.isInfinite()) {
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
return d;
} catch (NumberFormatException ignore) {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
}
}
// block items like 00 01 etc. Java number parsers treat these as Octal.
- if(initial == '0' && val.length() > 1) {
+ if (initial == '0' && val.length() > 1) {
char at1 = val.charAt(1);
- if(at1 >= '0' && at1 <= '9') {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ if (at1 >= '0' && at1 <= '9') {
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
} else if (initial == '-' && val.length() > 2) {
char at1 = val.charAt(1);
char at2 = val.charAt(2);
- if(at1 == '0' && at2 >= '0' && at2 <= '9') {
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ if (at1 == '0' && at2 >= '0' && at2 <= '9') {
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
}
// integer representation.
// This will narrow any values to the smallest reasonable Object representation
// (Integer, Long, or BigInteger)
-
+
// BigInteger down conversion: We use a similar bitLength compare as
// BigInteger#intValueExact uses. Increases GC, but objects hold
// only what they need. i.e. Less runtime overhead if the value is
// long lived.
BigInteger bi = new BigInteger(val);
- if(bi.bitLength() <= 31){
+ if (bi.bitLength() <= 31) {
return Integer.valueOf(bi.intValue());
}
- if(bi.bitLength() <= 63){
+ if (bi.bitLength() <= 63) {
return Long.valueOf(bi.longValue());
}
return bi;
}
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ throw new NumberFormatException("val [" + val + "] is not a valid number.");
}
-
+
/**
* direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support.
*/
@@ -588,12 +397,11 @@ private static boolean isDecimalNotation(final String val) {
* name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a
- * "content" member. Comments, prologs, DTDs, and {@code
+ * "content" member. Comments, prologs, DTDs, and {@code
* <[ [ ]]>}
* are ignored.
*
- * @param string
- * The source string.
+ * @param string The source string.
* @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown if there is an errors while parsing the string
*/
@@ -601,6 +409,7 @@ public static JSONObject toJSONObject(String string) throws JSONException {
return toJSONObject(string, XMLParserConfiguration.ORIGINAL);
}
+
/**
* Convert a well-formed (but not necessarily valid) XML into a
* JSONObject. Some information may be lost in this transformation because
@@ -609,7 +418,7 @@ public static JSONObject toJSONObject(String string) throws JSONException {
* name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a
- * "content" member. Comments, prologs, DTDs, and {@code
+ * "content" member. Comments, prologs, DTDs, and {@code
* <[ [ ]]>}
* are ignored.
*
@@ -632,18 +441,18 @@ public static JSONObject toJSONObject(Reader reader) throws JSONException {
* "content" member. Comments, prologs, DTDs, and {@code
* <[ [ ]]>}
* are ignored.
- *
+ *
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
* numbers but will instead be the exact value as seen in the XML document.
*
- * @param reader The XML source reader.
+ * @param reader The XML source reader.
* @param keepStrings If true, then values will not be coerced into boolean
- * or numeric values and will instead be left as strings
+ * or numeric values and will instead be left as strings
* @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown if there is an errors while parsing the string
*/
public static JSONObject toJSONObject(Reader reader, boolean keepStrings) throws JSONException {
- if(keepStrings) {
+ if (keepStrings) {
return toJSONObject(reader, XMLParserConfiguration.KEEP_STRINGS);
}
return toJSONObject(reader, XMLParserConfiguration.ORIGINAL);
@@ -660,7 +469,7 @@ public static JSONObject toJSONObject(Reader reader, boolean keepStrings) throws
* "content" member. Comments, prologs, DTDs, and
{@code
* <[ [ ]]>}
* are ignored.
- *
+ *
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
* numbers but will instead be the exact value as seen in the XML document.
*
@@ -674,56 +483,131 @@ public static JSONObject toJSONObject(Reader reader, XMLParserConfiguration conf
XMLTokener x = new XMLTokener(reader);
while (x.more()) {
x.skipPast("<");
- if(x.more()) {
- parse(x, jo, null, config);
+ if (x.more()) {
+ parse(x, jo, null, config,s->s);
}
}
return jo;
}
- /**
- * Convert a well-formed (but not necessarily valid) XML string into a
- * JSONObject. Some information may be lost in this transformation because
- * JSON is a data format and XML is a document format. XML uses elements,
- * attributes, and content text, while JSON uses unordered collections of
- * name/value pairs and arrays of values. JSON does not does not like to
- * distinguish between elements and attributes. Sequences of similar
- * elements are represented as JSONArrays. Content text may be placed in a
- * "content" member. Comments, prologs, DTDs, and
{@code
- * <[ [ ]]>}
- * are ignored.
- *
- * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
- * numbers but will instead be the exact value as seen in the XML document.
- *
- * @param string
- * The source string.
- * @param keepStrings If true, then values will not be coerced into boolean
- * or numeric values and will instead be left as strings
- * @return A JSONObject containing the structured data from the XML string.
- * @throws JSONException Thrown if there is an errors while parsing the string
- */
- public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
- return toJSONObject(new StringReader(string), keepStrings);
- }
-
- /**
- * Convert a well-formed (but not necessarily valid) XML string into a
+ // milestone 2 P1
+ public static JSONObject toJSONObject(Reader reader, JSONPointer path) throws JSONException {
+ JSONObject jo = new JSONObject();
+ XMLTokener x = new XMLTokener(reader);
+ String desiredPath = path.toString();
+ isReachedParse1 = false;
+ if (Character.toString(desiredPath.charAt(desiredPath.length() - 1)).equalsIgnoreCase("/")) {
+ throw new JSONPointerException("Invalid Path with '/' ending");
+ }
+ while (x.more()) {
+ x.skipPast("<");
+ if (x.more()) {
+ try {
+ parse1(x, jo, null, XMLParserConfiguration.ORIGINAL, desiredPath, "");
+ } catch (EarlyTermination e) {
+ System.out.println(e);
+ return e.subObject;
+ }
+
+ }
+ }
+ if (isReachedParse1 == false) {
+ throw new JSONPointerException("Path not found");
+ }
+ return jo;
+ }
+
+ // milestone 2 P2
+ public static JSONObject toJSONObject(Reader reader, JSONPointer path, JSONObject replacement) throws JSONException {
+ JSONObject jo = new JSONObject();
+ XMLTokener x = new XMLTokener(reader);
+ String desiredPath = path.toString();
+ isReachedParse2 = false;
+ if (Character.toString(desiredPath.charAt(desiredPath.length() - 1)).equalsIgnoreCase("/")) {
+ throw new JSONPointerException("Invalid Path with '/' ending");
+ }
+ while (x.more()) {
+ x.skipPast("<");
+ if (x.more()) {
+ parse2(x, jo, null, XMLParserConfiguration.ORIGINAL, desiredPath, "", replacement);
+ }
+ }
+ if (isReachedParse2 == false) {
+ throw new JSONPointerException("Path not found");
+ }
+ return jo;
+ }
+
+ // milestone 3
+ public static JSONObject toJSONObject(Reader reader, Function keyTransformer) {
+ // check string condition
+ String res1 = keyTransformer.apply("res1");
+ String res2 = keyTransformer.apply("res2");
+ // Transform key to null
+ if (res1 == null) {
+ throw new JSONException("Invalid: transform key to null");
+ }
+ // Transform key to empty string
+ if (res2.equals("")) {
+ throw new JSONException("Invalid: transform key to empty string");
+ }
+ // Transform key to all the same string
+ if (res1.equals(res2)){
+ throw new JSONException("Invalid: transform key to all the same string");
+ }
+ JSONObject jo = new JSONObject();
+ XMLTokener x = new XMLTokener(reader);
+ while (x.more()) {
+ x.skipPast("<");
+ if (x.more()) {
+ parse(x, jo, null, XMLParserConfiguration.ORIGINAL, keyTransformer);
+ }
+ }
+ return jo;
+
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject. Some information may be lost in this transformation because
* JSON is a data format and XML is a document format. XML uses elements,
* attributes, and content text, while JSON uses unordered collections of
* name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a
- * "content" member. Comments, prologs, DTDs, and {@code
+ * "content" member. Comments, prologs, DTDs, and {@code
* <[ [ ]]>}
* are ignored.
+ *
+ * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
+ * numbers but will instead be the exact value as seen in the XML document.
*
+ * @param string The source string.
+ * @param keepStrings If true, then values will not be coerced into boolean
+ * or numeric values and will instead be left as strings
+ * @return A JSONObject containing the structured data from the XML string.
+ * @throws JSONException Thrown if there is an errors while parsing the string
+ */
+ public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
+ return toJSONObject(new StringReader(string), keepStrings);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONObject. Some information may be lost in this transformation because
+ * JSON is a data format and XML is a document format. XML uses elements,
+ * attributes, and content text, while JSON uses unordered collections of
+ * name/value pairs and arrays of values. JSON does not does not like to
+ * distinguish between elements and attributes. Sequences of similar
+ * elements are represented as JSONArrays. Content text may be placed in a
+ * "content" member. Comments, prologs, DTDs, and
{@code
+ * <[ [ ]]>}
+ * are ignored.
+ *
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
* numbers but will instead be the exact value as seen in the XML document.
*
- * @param string
- * The source string.
+ * @param string The source string.
* @param config Configuration options for the parser.
* @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown if there is an errors while parsing the string
@@ -735,8 +619,7 @@ public static JSONObject toJSONObject(String string, XMLParserConfiguration conf
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
*
- * @param object
- * A JSONObject.
+ * @param object A JSONObject.
* @return A string.
* @throws JSONException Thrown if there is an error parsing the string
*/
@@ -747,10 +630,8 @@ public static String toString(Object object) throws JSONException {
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
*
- * @param object
- * A JSONObject.
- * @param tagName
- * The optional name of the enclosing tag.
+ * @param object A JSONObject.
+ * @param tagName The optional name of the enclosing tag.
* @return A string.
* @throws JSONException Thrown if there is an error parsing the string
*/
@@ -761,17 +642,31 @@ public static String toString(final Object object, final String tagName) {
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
*
- * @param object
- * A JSONObject.
- * @param tagName
- * The optional name of the enclosing tag.
- * @param config
- * Configuration that can control output to XML.
+ * @param object A JSONObject.
+ * @param tagName The optional name of the enclosing tag.
+ * @param config Configuration that can control output to XML.
* @return A string.
* @throws JSONException Thrown if there is an error parsing the string
*/
public static String toString(final Object object, final String tagName, final XMLParserConfiguration config)
throws JSONException {
+ return toString(object, tagName, config, 0, 0);
+ }
+
+ /**
+ * Convert a JSONObject into a well-formed, element-normal XML string,
+ * either pretty print or single-lined depending on indent factor.
+ *
+ * @param object A JSONObject.
+ * @param tagName The optional name of the enclosing tag.
+ * @param config Configuration that can control output to XML.
+ * @param indentFactor The number of spaces to add to each level of indentation.
+ * @param indent The current ident level in spaces.
+ * @return
+ * @throws JSONException
+ */
+ private static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor, int indent)
+ throws JSONException {
StringBuilder sb = new StringBuilder();
JSONArray ja;
JSONObject jo;
@@ -781,9 +676,14 @@ public static String toString(final Object object, final String tagName, final X
// Emit
if (tagName != null) {
+ sb.append(indent(indent));
sb.append('<');
sb.append(tagName);
sb.append('>');
+ if (indentFactor > 0) {
+ sb.append("\n");
+ indent += indentFactor;
+ }
}
// Loop thru the keys.
@@ -803,7 +703,7 @@ public static String toString(final Object object, final String tagName, final X
ja = (JSONArray) value;
int jaLength = ja.length();
// don't use the new iterator API to maintain support for Android
- for (int i = 0; i < jaLength; i++) {
+ for (int i = 0; i < jaLength; i++) {
if (i > 0) {
sb.append('\n');
}
@@ -820,64 +720,888 @@ public static String toString(final Object object, final String tagName, final X
ja = (JSONArray) value;
int jaLength = ja.length();
// don't use the new iterator API to maintain support for Android
- for (int i = 0; i < jaLength; i++) {
+ for (int i = 0; i < jaLength; i++) {
Object val = ja.opt(i);
if (val instanceof JSONArray) {
sb.append('<');
sb.append(key);
sb.append('>');
- sb.append(toString(val, null, config));
+ sb.append(toString(val, null, config, indentFactor, indent));
sb.append("");
sb.append(key);
sb.append('>');
} else {
- sb.append(toString(val, key, config));
+ sb.append(toString(val, key, config, indentFactor, indent));
}
}
} else if ("".equals(value)) {
+ sb.append(indent(indent));
sb.append('<');
sb.append(key);
sb.append("/>");
+ if (indentFactor > 0) {
+ sb.append("\n");
+ }
// Emit a new tag
} else {
- sb.append(toString(value, key, config));
+ sb.append(toString(value, key, config, indentFactor, indent));
}
}
if (tagName != null) {
// Emit the close tag
+ sb.append(indent(indent - indentFactor));
sb.append("");
sb.append(tagName);
sb.append('>');
+ if (indentFactor > 0) {
+ sb.append("\n");
+ }
}
return sb.toString();
}
- if (object != null && (object instanceof JSONArray || object.getClass().isArray())) {
- if(object.getClass().isArray()) {
+ if (object != null && (object instanceof JSONArray || object.getClass().isArray())) {
+ if (object.getClass().isArray()) {
ja = new JSONArray(object);
} else {
ja = (JSONArray) object;
}
int jaLength = ja.length();
// don't use the new iterator API to maintain support for Android
- for (int i = 0; i < jaLength; i++) {
+ for (int i = 0; i < jaLength; i++) {
Object val = ja.opt(i);
// XML does not have good support for arrays. If an array
// appears in a place where XML is lacking, synthesize an
// element.
- sb.append(toString(val, tagName == null ? "array" : tagName, config));
+ sb.append(toString(val, tagName == null ? "array" : tagName, config, indentFactor, indent));
}
return sb.toString();
}
+
string = (object == null) ? "null" : escape(object.toString());
- return (tagName == null) ? "\"" + string + "\""
- : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
- + ">" + string + "" + tagName + ">";
+ if (tagName == null) {
+ return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : "");
+ } else if (string.length() == 0) {
+ return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : "");
+ } else {
+ return indent(indent) + "<" + tagName
+ + ">" + string + "" + tagName + ">" + ((indentFactor > 0) ? "\n" : "");
+ }
+ }
+
+ /**
+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
+ *
+ * @param object A JSONObject.
+ * @param indentFactor The number of spaces to add to each level of indentation.
+ * @return A string.
+ * @throws JSONException Thrown if there is an error parsing the string
+ */
+ public static String toString(Object object, int indentFactor) {
+ return toString(object, null, XMLParserConfiguration.ORIGINAL, indentFactor);
+ }
+
+ /**
+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
+ *
+ * @param object A JSONObject.
+ * @param tagName The optional name of the enclosing tag.
+ * @param indentFactor The number of spaces to add to each level of indentation.
+ * @return A string.
+ * @throws JSONException Thrown if there is an error parsing the string
+ */
+ public static String toString(final Object object, final String tagName, int indentFactor) {
+ return toString(object, tagName, XMLParserConfiguration.ORIGINAL, indentFactor);
+ }
+
+ /**
+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
+ *
+ * @param object A JSONObject.
+ * @param tagName The optional name of the enclosing tag.
+ * @param config Configuration that can control output to XML.
+ * @param indentFactor The number of spaces to add to each level of indentation.
+ * @return A string.
+ * @throws JSONException Thrown if there is an error parsing the string
+ */
+ public static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor)
+ throws JSONException {
+ return toString(object, tagName, config, indentFactor, 0);
+ }
+
+ /**
+ * Return a String consisting of a number of space characters specified by indent
+ *
+ * @param indent The number of spaces to be appended to the String.
+ * @return
+ */
+ private static final String indent(int indent) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < indent; i++) {
+ sb.append(' ');
+ }
+ return sb.toString();
}
+
+ /**
+ * Scan the content following the named tag, attaching it to the context.
+ *
+ * @param x The XMLTokener containing the source string.
+ * @param context The JSONObject that will include the new material.
+ * @param name The tag name.
+ * @return true if the close tag is processed.
+ * @throws JSONException
+ */
+ private static boolean parse1(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, String desiredPath, String currPath)
+ throws JSONException, EarlyTermination {
+ // keep track of the path
+ char c;
+ int i;
+ JSONObject jsonObject = null;
+ String string;
+ String tagName;
+ Object token;
+ XMLXsiTypeConverter> xmlXsiTypeConverter;
+
+ // Test for and skip past these forms:
+ //
+ //
+ //
+ // ... ?>
+ // Report errors for these forms:
+ // <>
+ // <=
+ // <<
+
+ token = x.nextToken();
+// System.out.println(currPath);
+// System.out.println("token: " + token);
+
+
+ // ");
+ return false;
+ }
+ x.back();
+ } else if (c == '[') {
+ token = x.nextToken();
+ if ("CDATA".equals(token)) {
+ if (x.next() == '[') {
+ string = x.nextCDATA();
+ if (string.length() > 0) {
+ context.accumulate(config.getcDataTagName(), string);
+ }
+ return false;
+ }
+ }
+ throw x.syntaxError("Expected 'CDATA['");
+ }
+ i = 1;
+ do {
+ token = x.nextMeta();
+ if (token == null) {
+ throw x.syntaxError("Missing '>' after ' 0);
+ return false;
+ } else if (token == QUEST) {
+
+ //
+ x.skipPast("?>");
+ return false;
+ } else if (token == SLASH) {
+
+ // Close tag
+
+ token = x.nextToken();
+// System.out.println("token after /: " + token);
+
+ List level = new ArrayList<>(Arrays.asList(desiredPath.split("/")));
+// System.out.println("level: " + level.get(level.size()-1));
+ if (currPath.equalsIgnoreCase(desiredPath) && token.toString().equalsIgnoreCase(level.get(level.size() - 1))) {
+ isReachedParse1 = true;
+// System.out.println("isReached:" + isReached);
+ }
+ if (name == null) {
+ throw x.syntaxError("Mismatched close tag " + token);
+ }
+ if (!token.equals(name)) {
+ throw x.syntaxError("Mismatched " + name + " and " + token);
+ }
+ if (x.nextToken() != GT) {
+ throw x.syntaxError("Misshaped close tag");
+ }
+ return true;
+
+ } else if (token instanceof Character) {
+
+ throw x.syntaxError("Misshaped tag");
+
+ // Open tag <
+
+ } else {
+ tagName = (String) token;
+ currPath += "/" + tagName;
+// System.out.println("curr: " + currPath);
+ token = null;
+ jsonObject = new JSONObject();
+ boolean nilAttributeFound = false;
+ xmlXsiTypeConverter = null;
+
+ for (; ; ) {
+ if (token == null) {
+ token = x.nextToken();
+// System.out.println(x.next());
+// System.out.println("token after tagName: " + token);
+ }
+ // attribute = value
+ if (token instanceof String) {
+
+ string = (String) token;
+// System.out.println("inString: " + string);
+ token = x.nextToken();
+ if (token == EQ) {
+ token = x.nextToken();
+ if (!(token instanceof String)) {
+ throw x.syntaxError("Missing value");
+ }
+
+ if (config.isConvertNilAttributeToNull()
+ && NULL_ATTR.equals(string)
+ && Boolean.parseBoolean((String) token)) {
+ nilAttributeFound = true;
+ } else if (config.getXsiTypeMap() != null && !config.getXsiTypeMap().isEmpty()
+ && TYPE_ATTR.equals(string)) {
+ xmlXsiTypeConverter = config.getXsiTypeMap().get(token);
+ } else if (!nilAttributeFound) {
+ jsonObject.accumulate(string,
+ config.isKeepStrings()
+ ? ((String) token)
+ : stringToValue((String) token));
+ }
+ token = null;
+ } else {
+ jsonObject.accumulate(string, "");
+ }
+
+
+ } else if (token == SLASH) {
+ // Empty tag <.../>
+ if (x.nextToken() != GT) {
+
+ throw x.syntaxError("Misshaped tag");
+ }
+ if (currPath.startsWith(desiredPath)) {
+ if (config.getForceList().contains(tagName)) {
+ // Force the value to be an array
+ if (nilAttributeFound) {
+ context.append(tagName, JSONObject.NULL);
+ } else if (jsonObject.length() > 0) {
+ context.append(tagName, jsonObject);
+ } else {
+ context.put(tagName, new JSONArray());
+ }
+ } else {
+ if (nilAttributeFound) {
+ context.accumulate(tagName, JSONObject.NULL);
+ } else if (jsonObject.length() > 0) {
+ context.accumulate(tagName, jsonObject);
+ } else {
+ context.accumulate(tagName, "");
+ }
+ }
+ return false;
+ }
+
+
+ } else if (token == GT) {
+ // Content, between <...> and
+ for (; ; ) {
+
+ token = x.nextContent();
+// System.out.println("token next Content: " + token);
+ if (token == null) {
+ if (tagName != null) {
+ throw x.syntaxError("Unclosed tag " + tagName);
+ }
+ return false;
+ } else if (token instanceof String) {
+ string = (String) token;
+ if (string.length() > 0) {
+ if (xmlXsiTypeConverter != null) {
+ jsonObject.accumulate(config.getcDataTagName(),
+ stringToValue(string, xmlXsiTypeConverter));
+ } else {
+// System.out.println("In");
+ jsonObject.accumulate(config.getcDataTagName(),
+ config.isKeepStrings() ? string : stringToValue(string));
+ }
+ }
+
+
+ } else if (token == LT) {
+ // Nested element
+// System.out.println("In LT");
+// System.out.println(currPath + " ----" + desiredPath);
+ if (parse1(x, jsonObject, tagName, config, desiredPath, currPath)) {
+// System.out.println(currPath + " " + desiredPath);
+// System.out.println("subJo: " +jsonObject);
+ if (currPath.startsWith(desiredPath)) {
+// System.out.println("TEST");
+// System.out.println("subJo: " +jsonObject);
+// System.out.println(token );
+ if (config.getForceList().contains(tagName)) {
+ // Force the value to be an array
+ if (jsonObject.length() == 0) {
+ context.put(tagName, new JSONArray());
+ } else if (jsonObject.length() == 1
+ && jsonObject.opt(config.getcDataTagName()) != null) {
+ context.append(tagName, jsonObject.opt(config.getcDataTagName()));
+ } else {
+ context.append(tagName, jsonObject);
+ }
+ } else {
+ if (jsonObject.length() == 0) {
+ context.accumulate(tagName, "");
+ } else if (jsonObject.length() == 1
+ && jsonObject.opt(config.getcDataTagName()) != null) {
+ context.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
+ } else {
+ context.accumulate(tagName, jsonObject);
+ }
+ }
+ if (isReachedParse1 == true) {
+ System.out.println("context: " + context);
+ throw new EarlyTermination(context);
+ }
+
+ }
+// System.out.println("isReached1:" + isReachedParse1);
+ /*if (isReachedParse1 == true){
+ System.out.println("context: " + context);
+ throw new EarlyTermination(context);
+ }*/
+
+ return false;
+ }
+ }
+
+ }
+
+ } else {
+ throw x.syntaxError("Misshaped tag");
+ }
+ }
+ }
+ }
+
+ private static boolean parse2(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, String desiredPath, String currPath, JSONObject replacement)
+ throws JSONException {
+ // keep track of the path
+ char c;
+ int i;
+ JSONObject jsonObject = null;
+ String string;
+ String tagName;
+ Object token;
+ XMLXsiTypeConverter> xmlXsiTypeConverter;
+
+ // Test for and skip past these forms:
+ //
+ //
+ //
+ // ... ?>
+ // Report errors for these forms:
+ // <>
+ // <=
+ // <<
+
+ token = x.nextToken();
+
+
+
+ // ");
+ return false;
+ }
+ x.back();
+ } else if (c == '[') {
+ token = x.nextToken();
+ if ("CDATA".equals(token)) {
+ if (x.next() == '[') {
+ string = x.nextCDATA();
+ if (string.length() > 0) {
+ context.accumulate(config.getcDataTagName(), string);
+ }
+ return false;
+ }
+ }
+ throw x.syntaxError("Expected 'CDATA['");
+ }
+ i = 1;
+ do {
+ token = x.nextMeta();
+ if (token == null) {
+ throw x.syntaxError("Missing '>' after ' 0);
+ return false;
+ } else if (token == QUEST) {
+
+ //
+ x.skipPast("?>");
+ return false;
+ } else if (token == SLASH) {
+
+ // Close tag
+
+ token = x.nextToken();
+// System.out.println("token after /: " + token);
+
+ List level = new ArrayList<>(Arrays.asList(desiredPath.split("/")));
+// System.out.println("level: " + level.get(level.size()-1));
+ if (currPath.equalsIgnoreCase(desiredPath) && token.toString().equalsIgnoreCase(level.get(level.size() - 1))) {
+ isReachedParse2 = true;
+// System.out.println("isReached:" + isReached);
+ }
+ if (name == null) {
+ throw x.syntaxError("Mismatched close tag " + token);
+ }
+ if (!token.equals(name)) {
+ throw x.syntaxError("Mismatched " + name + " and " + token);
+ }
+ if (x.nextToken() != GT) {
+ throw x.syntaxError("Misshaped close tag");
+ }
+ return true;
+
+ } else if (token instanceof Character) {
+
+ throw x.syntaxError("Misshaped tag");
+
+ // Open tag <
+
+ } else {
+ tagName = (String) token;
+ currPath += "/" + tagName;
+// System.out.println("curr: " + currPath);
+ token = null;
+ jsonObject = new JSONObject();
+ boolean nilAttributeFound = false;
+ xmlXsiTypeConverter = null;
+
+ for (; ; ) {
+ if (token == null) {
+ token = x.nextToken();
+// System.out.println(x.next());
+// System.out.println("token after tagName: " + token);
+ }
+ // attribute = value
+ if (token instanceof String) {
+
+ string = (String) token;
+// System.out.println("inString: " + string);
+ token = x.nextToken();
+ if (token == EQ) {
+ token = x.nextToken();
+ if (!(token instanceof String)) {
+ throw x.syntaxError("Missing value");
+ }
+
+ if (config.isConvertNilAttributeToNull()
+ && NULL_ATTR.equals(string)
+ && Boolean.parseBoolean((String) token)) {
+ nilAttributeFound = true;
+ } else if (config.getXsiTypeMap() != null && !config.getXsiTypeMap().isEmpty()
+ && TYPE_ATTR.equals(string)) {
+ xmlXsiTypeConverter = config.getXsiTypeMap().get(token);
+ } else if (!nilAttributeFound) {
+ jsonObject.accumulate(string,
+ config.isKeepStrings()
+ ? ((String) token)
+ : stringToValue((String) token));
+ }
+ token = null;
+ } else {
+ jsonObject.accumulate(string, "");
+ }
+
+
+ } else if (token == SLASH) {
+ // Empty tag <.../>
+ if (x.nextToken() != GT) {
+
+ throw x.syntaxError("Misshaped tag");
+ }
+
+ if (config.getForceList().contains(tagName)) {
+ // Force the value to be an array
+ if (nilAttributeFound) {
+ context.append(tagName, JSONObject.NULL);
+ } else if (jsonObject.length() > 0) {
+ context.append(tagName, jsonObject);
+ } else {
+ context.put(tagName, new JSONArray());
+ }
+ } else {
+ if (nilAttributeFound) {
+ context.accumulate(tagName, JSONObject.NULL);
+ } else if (jsonObject.length() > 0) {
+ context.accumulate(tagName, jsonObject);
+ } else {
+ context.accumulate(tagName, "");
+ }
+ }
+ return false;
+
+
+ } else if (token == GT) {
+ // Content, between <...> and
+ for (; ; ) {
+
+ token = x.nextContent();
+// System.out.println("token next Content: " + token);
+ if (token == null) {
+ if (tagName != null) {
+ throw x.syntaxError("Unclosed tag " + tagName);
+ }
+ return false;
+ } else if (token instanceof String) {
+ string = (String) token;
+ if (string.length() > 0) {
+ if (xmlXsiTypeConverter != null) {
+ jsonObject.accumulate(config.getcDataTagName(),
+ stringToValue(string, xmlXsiTypeConverter));
+ } else {
+// System.out.println("In");
+ jsonObject.accumulate(config.getcDataTagName(),
+ config.isKeepStrings() ? string : stringToValue(string));
+ }
+ }
+
+
+ } else if (token == LT) {
+ // Nested element
+// System.out.println("In LT");
+// System.out.println(currPath + " ----" + desiredPath);
+ if (parse2(x, jsonObject, tagName, config, desiredPath, currPath, replacement)) {
+
+ if (config.getForceList().contains(tagName)) {
+ // Force the value to be an array
+ if (jsonObject.length() == 0) {
+// System.out.println("a" + tagName);
+ context.put(tagName, new JSONArray());
+ } else if (jsonObject.length() == 1
+ && jsonObject.opt(config.getcDataTagName()) != null) {
+// System.out.println("b" + tagName);
+ context.append(tagName, jsonObject.opt(config.getcDataTagName()));
+ } else {
+// System.out.println("c" + tagName);
+ context.append(tagName, jsonObject);
+ }
+ } else {
+ if (jsonObject.length() == 0) {
+// System.out.println("d" + tagName);
+ context.accumulate(tagName, "");
+ } else if (jsonObject.length() == 1
+ && jsonObject.opt(config.getcDataTagName()) != null) {
+ if (currPath.equalsIgnoreCase(desiredPath)) {
+
+// System.out.println("tagName: " + tagName);
+ context.accumulate(tagName, replacement.get(tagName));
+ } else {
+ context.accumulate(tagName, jsonObject.opt(config.getcDataTagName()));
+// System.out.println("f" + tagName);
+ }
+ } else {
+// System.out.println("e" + tagName);
+ if (currPath.equalsIgnoreCase(desiredPath)) {
+ context.accumulate(tagName, replacement);
+ } else {
+ context.accumulate(tagName, jsonObject);
+ }
+
+ }
+// System.out.println(currPath);
+// System.out.println("context: " + context);
+ }
+
+ return false;
+ }
+ }
+
+ }
+ } else {
+ throw x.syntaxError("Misshaped tag");
+ }
+ }
+ }
+ }
+
+ // milestone 3
+ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, Function keyTransformer)
+ throws JSONException {
+ char c;
+ int i;
+ JSONObject jsonObject = null;
+ String string;
+ String tagName;
+ Object token;
+ XMLXsiTypeConverter> xmlXsiTypeConverter;
+
+ // Test for and skip past these forms:
+ //
+ //
+ //
+ // ... ?>
+ // Report errors for these forms:
+ // <>
+ // <=
+ // <<
+
+ token = x.nextToken();
+// System.out.println("token: " + token);
+
+ // ");
+ return false;
+ }
+ x.back();
+ } else if (c == '[') {
+ token = x.nextToken();
+ if ("CDATA".equals(token)) {
+ if (x.next() == '[') {
+ string = x.nextCDATA();
+ if (string.length() > 0) {
+ context.accumulate(config.getcDataTagName(), string);
+ }
+ return false;
+ }
+ }
+ throw x.syntaxError("Expected 'CDATA['");
+ }
+ i = 1;
+ do {
+ token = x.nextMeta();
+ if (token == null) {
+ throw x.syntaxError("Missing '>' after ' 0);
+ return false;
+ } else if (token == QUEST) {
+
+ //
+ x.skipPast("?>"); //
+ return false;
+ } else if (token == SLASH) {
+
+ // Close tag return True
+
+ token = x.nextToken();
+ if (name == null) {
+ throw x.syntaxError("Mismatched close tag " + token);
+ }
+ if (!token.equals(name)) {
+ throw x.syntaxError("Mismatched " + name + " and " + token);
+ }
+ if (x.nextToken() != GT) {
+ throw x.syntaxError("Misshaped close tag");
+ }
+ return true;
+
+ } else if (token instanceof Character) {
+ throw x.syntaxError("Misshaped tag");
+
+ // Open tag <
+
+ } else {
+ tagName = (String) token;
+ token = null;
+ jsonObject = new JSONObject();
+ boolean nilAttributeFound = false;
+ xmlXsiTypeConverter = null;
+ for (; ; ) {
+ if (token == null) {
+ token = x.nextToken();
+ }
+ // attribute = value
+ if (token instanceof String) {
+ string = (String) token;
+ token = x.nextToken();
+ if (token == EQ) {
+ token = x.nextToken();
+ if (!(token instanceof String)) {
+ throw x.syntaxError("Missing value");
+ }
+
+ if (config.isConvertNilAttributeToNull()
+ && NULL_ATTR.equals(string)
+ && Boolean.parseBoolean((String) token)) {
+ nilAttributeFound = true;
+ } else if (config.getXsiTypeMap() != null && !config.getXsiTypeMap().isEmpty()
+ && TYPE_ATTR.equals(string)) {
+ xmlXsiTypeConverter = config.getXsiTypeMap().get(token);
+ } else if (!nilAttributeFound) {
+ jsonObject.accumulate(keyTransformer.apply(string),
+ config.isKeepStrings()
+ ? ((String) token)
+ : stringToValue((String) token));
+ }
+ token = null;
+ } else {
+ jsonObject.accumulate(keyTransformer.apply(string), "");
+ }
+
+
+ } else if (token == SLASH) {
+ // Empty tag <.../>
+ if (x.nextToken() != GT) {
+ throw x.syntaxError("Misshaped tag");
+ }
+
+ String newTagName = keyTransformer.apply(tagName);
+
+ if (config.getForceList().contains(tagName)) {
+
+ // Force the value to be an array
+ if (nilAttributeFound) {
+ context.append(newTagName, JSONObject.NULL);
+ } else if (jsonObject.length() > 0) {
+ context.append(newTagName, jsonObject);
+ } else {
+ context.put(newTagName, new JSONArray());
+ }
+ } else {
+ if (nilAttributeFound) {
+ context.accumulate(newTagName, JSONObject.NULL);
+ } else if (jsonObject.length() > 0) {
+ context.accumulate(newTagName, jsonObject);
+ } else {
+ context.accumulate(newTagName, "");
+ }
+ }
+ return false;
+
+ } else if (token == GT) {
+ // Content, between <...> and
+ for (; ; ) {
+ token = x.nextContent();
+ if (token == null) {
+ if (tagName != null) {
+ throw x.syntaxError("Unclosed tag " + tagName);
+ }
+ return false;
+ } else if (token instanceof String) {
+ string = (String) token;
+ if (string.length() > 0) {
+ if (xmlXsiTypeConverter != null) {
+ jsonObject.accumulate(config.getcDataTagName(),
+ stringToValue(string, xmlXsiTypeConverter));
+ } else {
+ jsonObject.accumulate(config.getcDataTagName(),
+ config.isKeepStrings() ? string : stringToValue(string));
+ }
+ }
+
+ } else if (token == LT) {
+ // Nested element
+ if (parse(x, jsonObject, tagName, config, keyTransformer)) {
+ String newTagName = keyTransformer.apply(tagName);
+
+ if (config.getForceList().contains(tagName)) {
+ // Force the value to be an array
+
+ if (jsonObject.length() == 0) {
+ context.put(newTagName, new JSONArray());
+ } else if (jsonObject.length() == 1
+ && jsonObject.opt(config.getcDataTagName()) != null) {
+ context.append(newTagName, jsonObject.opt(config.getcDataTagName()));
+ } else {
+ context.append(newTagName, jsonObject);
+ }
+ } else {
+ if (jsonObject.length() == 0) {
+ context.accumulate(newTagName, "");
+ } else if (jsonObject.length() == 1
+ && jsonObject.opt(config.getcDataTagName()) != null) {
+ context.accumulate(newTagName, jsonObject.opt(config.getcDataTagName()));
+ } else {
+ context.accumulate(newTagName, jsonObject);
+ }
+ }
+
+ return false;
+ }
+ }
+ }
+ } else {
+ throw x.syntaxError("Misshaped tag");
+ }
+ }
+ }
+ }
+
+ /*
+ milestone 5
+
+ When a task is submitted to the ExecutorService, it is added to a queue of tasks to be executed.
+ The ExecutorService then uses a pool of worker threads to execute the tasks in the queue.
+
+ The ExecutorService also provides a way to obtain a Future object when a task is submitted.
+ A Future represents the result of an asynchronous computation, and allows the calling thread to continue executing while the task is being processed by the worker thread.
+ The calling thread can later retrieve the result of the computation by calling the get() method on the Future.
+ */
+
+ public static Future toJSONObject(Reader reader, Function keyTransformer,
+ Consumer exceptionHandler) {
+
+ ExecutorService executor = Executors.newSingleThreadExecutor();
+
+ Future future;
+ if (keyTransformer == null) {
+ exceptionHandler.accept(new Exception());
+ return null;
+ }
+ //milestone 3 function using in lambda expression body as a task
+ future = executor.submit(()->XML.toJSONObject(reader, keyTransformer));
+
+ if (future.isDone()) {
+ executor.shutdown();
+ }
+ return future;
+ }
+
}
diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java
index a1fd63e..9f00710 100644
--- a/src/main/java/org/json/XMLParserConfiguration.java
+++ b/src/main/java/org/json/XMLParserConfiguration.java
@@ -1,26 +1,6 @@
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.Collections;
diff --git a/src/main/java/org/json/XMLTokener.java b/src/main/java/org/json/XMLTokener.java
index 3bbd382..957498c 100644
--- a/src/main/java/org/json/XMLTokener.java
+++ b/src/main/java/org/json/XMLTokener.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.io.Reader;
diff --git a/src/main/java/org/json/XMLXsiTypeConverter.java b/src/main/java/org/json/XMLXsiTypeConverter.java
index 0f8a8c3..0011eff 100644
--- a/src/main/java/org/json/XMLXsiTypeConverter.java
+++ b/src/main/java/org/json/XMLXsiTypeConverter.java
@@ -1,26 +1,6 @@
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/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java
index b8bdede..f3364fb 100644
--- a/src/test/java/org/json/junit/CDLTest.java
+++ b/src/test/java/org/json/junit/CDLTest.java
@@ -1,27 +1,7 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.*;
diff --git a/src/test/java/org/json/junit/CookieListTest.java b/src/test/java/org/json/junit/CookieListTest.java
index c3f647f..0af9640 100644
--- a/src/test/java/org/json/junit/CookieListTest.java
+++ b/src/test/java/org/json/junit/CookieListTest.java
@@ -1,27 +1,7 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.*;
diff --git a/src/test/java/org/json/junit/CookieTest.java b/src/test/java/org/json/junit/CookieTest.java
index 7e7b62b..edd8a7e 100644
--- a/src/test/java/org/json/junit/CookieTest.java
+++ b/src/test/java/org/json/junit/CookieTest.java
@@ -1,27 +1,7 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.*;
diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java
index 6867123..1496a63 100644
--- a/src/test/java/org/json/junit/EnumTest.java
+++ b/src/test/java/org/json/junit/EnumTest.java
@@ -1,27 +1,7 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.assertEquals;
diff --git a/src/test/java/org/json/junit/HTTPTest.java b/src/test/java/org/json/junit/HTTPTest.java
index 8182b60..703d5ad 100644
--- a/src/test/java/org/json/junit/HTTPTest.java
+++ b/src/test/java/org/json/junit/HTTPTest.java
@@ -1,27 +1,7 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.*;
diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java
index 946f407..1a2df7f 100644
--- a/src/test/java/org/json/junit/JSONArrayTest.java
+++ b/src/test/java/org/json/junit/JSONArrayTest.java
@@ -1,31 +1,12 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -49,7 +30,9 @@ of this software and associated documentation files (the "Software"), to deal
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONPointerException;
+import org.json.JSONString;
import org.json.JSONTokener;
+import org.json.junit.data.MyJsonString;
import org.junit.Test;
import com.jayway.jsonpath.Configuration;
@@ -1354,4 +1337,25 @@ public void issue654StackOverflowInputWellFormed() {
fail("Excepected Exception.");
Util.checkJSONArrayMaps(json_input);
}
+
+ @Test
+ public void testIssue682SimilarityOfJSONString() {
+ JSONArray ja1 = new JSONArray()
+ .put(new MyJsonString())
+ .put(2);
+ JSONArray ja2 = new JSONArray()
+ .put(new MyJsonString())
+ .put(2);
+ assertTrue(ja1.similar(ja2));
+
+ JSONArray ja3 = new JSONArray()
+ .put(new JSONString() {
+ @Override
+ public String toJSONString() {
+ return "\"different value\"";
+ }
+ })
+ .put(2);
+ assertFalse(ja1.similar(ja3));
+ }
}
diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java
index 390cbd8..34bc9f0 100644
--- a/src/test/java/org/json/junit/JSONMLTest.java
+++ b/src/test/java/org/json/junit/JSONMLTest.java
@@ -1,27 +1,7 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.*;
diff --git a/src/test/java/org/json/junit/JSONObjectLocaleTest.java b/src/test/java/org/json/junit/JSONObjectLocaleTest.java
index 5112bf5..1cdaf74 100755
--- a/src/test/java/org/json/junit/JSONObjectLocaleTest.java
+++ b/src/test/java/org/json/junit/JSONObjectLocaleTest.java
@@ -1,27 +1,7 @@
package org.json.junit;
/*
-Copyright (c) 2020 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 static org.junit.Assert.*;
diff --git a/src/test/java/org/json/junit/JSONObjectNumberTest.java b/src/test/java/org/json/junit/JSONObjectNumberTest.java
new file mode 100644
index 0000000..f6e13c6
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONObjectNumberTest.java
@@ -0,0 +1,126 @@
+package org.json.junit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(value = Parameterized.class)
+public class JSONObjectNumberTest {
+ private final String objectString;
+ private Integer value = 50;
+
+ @Parameters(name = "{index}: {0}")
+ public static Collection