element.
- sb.append(toString(val, tagName == null ? "array" : tagName));
- }
- return sb.toString();
- }
-
- string = (object == null) ? "null" : escape(object.toString());
- return (tagName == null) ? "\"" + string + "\""
- : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
- + ">" + string + "" + tagName + ">";
-
- }
-}
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 000000000..6dcdca6fc
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,139 @@
+/*
+ * This file was generated by the Gradle 'init' task.
+ */
+apply plugin: 'java'
+apply plugin: 'eclipse'
+apply plugin: 'jacoco'
+apply plugin: 'maven-publish'
+
+// for now, publishing to maven is still a manual process
+//plugins {
+ // id 'java'
+ //id 'maven-publish'
+// }
+
+repositories {
+ mavenLocal()
+ mavenCentral()
+ maven {
+ url = uri('https://oss.sonatype.org/content/repositories/snapshots')
+ }
+}
+
+// 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.2'
+ testImplementation 'com.jayway.jsonpath:json-path:2.9.0'
+ testImplementation 'org.mockito:mockito-core:4.2.0'
+}
+
+subprojects {
+ tasks.withType(Javadoc).all { enabled = false }
+}
+
+group = 'org.json'
+version = 'v20250517-SNAPSHOT'
+description = 'JSON in Java'
+sourceCompatibility = '1.8'
+
+configurations.all {
+}
+
+java {
+ withSourcesJar()
+ withJavadocJar()
+}
+
+publishing {
+ publications {
+ maven(MavenPublication) {
+ from(components.java)
+ }
+ }
+}
+
+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..cd53bbe55
--- /dev/null
+++ b/docs/RELEASES.md
@@ -0,0 +1,62 @@
+# 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)
+
+~~~
+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/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 000000000..f3d88b1c2
Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 000000000..a4b442974
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,5 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
diff --git a/gradlew b/gradlew
new file mode 100755
index 000000000..2fe81a7d9
--- /dev/null
+++ b/gradlew
@@ -0,0 +1,183 @@
+#!/usr/bin/env sh
+
+#
+# Copyright 2015 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+##############################################################################
+##
+## Gradle start up script for UN*X
+##
+##############################################################################
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn () {
+ echo "$*"
+}
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "`uname`" in
+ CYGWIN* )
+ cygwin=true
+ ;;
+ Darwin* )
+ darwin=true
+ ;;
+ MINGW* )
+ msys=true
+ ;;
+ NONSTOP* )
+ nonstop=true
+ ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD="java"
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ $? -eq 0 ] ; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ] ; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW ; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+ fi
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ i=0
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
+
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+ else
+ eval `echo args$i`="\"$arg\""
+ fi
+ i=`expr $i + 1`
+ done
+ case $i in
+ 0) set -- ;;
+ 1) set -- "$args0" ;;
+ 2) set -- "$args0" "$args1" ;;
+ 3) set -- "$args0" "$args1" "$args2" ;;
+ 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+ 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+ 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+ 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+ 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+ 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+ esac
+fi
+
+# Escape application args
+save () {
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
+ echo " "
+}
+APP_ARGS=`save "$@"`
+
+# Collect all arguments for the java command, following the shell quoting and substitution rules
+eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
+
+exec "$JAVACMD" "$@"
diff --git a/gradlew.bat b/gradlew.bat
new file mode 100644
index 000000000..9109989e3
--- /dev/null
+++ b/gradlew.bat
@@ -0,0 +1,103 @@
+@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windows variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/images/JsonJava.png b/images/JsonJava.png
new file mode 100644
index 000000000..28c5c9cbc
Binary files /dev/null and b/images/JsonJava.png differ
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 000000000..81f5c3c2c
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,254 @@
+
+ 4.0.0
+
+ org.json
+ json
+ 20250517
+ bundle
+
+ JSON in Java
+
+ JSON is a light-weight, language independent, data interchange format.
+ See http://www.JSON.org/
+
+ The files in this package implement JSON encoders/decoders in Java.
+ It also includes the capability to convert between JSON and XML, HTTP
+ headers, Cookies, and CDL.
+
+ 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.
+
+ https://github.com/douglascrockford/JSON-java
+
+
+ https://github.com/douglascrockford/JSON-java.git
+ scm:git:git://github.com/douglascrockford/JSON-java.git
+ scm:git:git@github.com:douglascrockford/JSON-java.git
+
+
+
+
+ Public Domain
+ https://github.com/stleary/JSON-java/blob/master/LICENSE
+ repo
+
+
+
+
+
+ Douglas Crockford
+ douglas@crockford.com
+
+
+
+
+ UTF-8
+
+
+
+
+
+ ossrh
+ Central Repository OSSRH
+ https://oss.sonatype.org/service/local/staging/deploy/maven2/
+
+
+ ossrh
+ https://oss.sonatype.org/content/repositories/snapshots
+
+
+
+
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
+
+ com.jayway.jsonpath
+ json-path
+ 2.9.0
+ test
+
+
+ org.mockito
+ mockito-core
+ 4.2.0
+ test
+
+
+
+
+
+
+ org.apache.felix
+ maven-bundle-plugin
+ 5.1.9
+ true
+
+
+
+ org.json
+
+ ${project.artifactId}
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.11.0
+
+ 1.8
+ 1.8
+
+ -Xlint:unchecked
+
+
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
+ 3.3.0
+
+
+ attach-sources
+
+ jar-no-fork
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.5.0
+
+
+ attach-javadocs
+
+ jar
+
+
+ -Xdoclint:none
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-gpg-plugin
+ 1.6
+
+
+ sign-artifacts
+ verify
+
+ sign
+
+
+
+ --pinentry-mode
+ loopback
+
+
+
+
+
+
+ org.sonatype.plugins
+ nexus-staging-maven-plugin
+ 1.6.3
+ true
+
+ ossrh
+ https://oss.sonatype.org/
+ 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.plugins
+ maven-jar-plugin
+ 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/CDL.java b/src/main/java/org/json/CDL.java
similarity index 50%
rename from CDL.java
rename to src/main/java/org/json/CDL.java
index 1c7df3223..dd631bf8f 100644
--- a/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,35 +25,40 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class CDL {
+ /**
+ * Constructs a new CDL object.
+ */
+ 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;
- StringBuffer sb;
+ 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 StringBuffer();
+ sb = new StringBuilder();
for (;;) {
c = x.next();
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,33 +70,47 @@ 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);
}
/**
* Produce a JSONArray of strings from a row of comma delimited values.
* @param x A JSONTokener of the source text.
* @return A JSONArray of strings.
- * @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 != ' ') {
@@ -134,11 +133,25 @@ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
* method.
* @param x A JSONTokener of the source text.
* @return A JSONObject combining the names and values.
- * @throws JSONException
+ * @throws JSONException if a called function fails
+ */
+ 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)
- throws JSONException {
- JSONArray ja = rowToJSONArray(x);
+ 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 +163,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.length() > 0 && (string.indexOf(delimiter) >= 0 ||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
sb.append('"');
@@ -184,10 +209,22 @@ public static String rowToString(JSONArray ja) {
* using the first row as a source of names.
* @param string The comma delimited text.
* @return A JSONArray of JSONObjects.
- * @throws JSONException
+ * @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);
}
/**
@@ -195,10 +232,34 @@ public static JSONArray toJSONArray(String string) throws JSONException {
* using the first row as a source of names.
* @param x The JSONTokener containing the comma delimited text.
* @return A JSONArray of JSONObjects.
- * @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 +267,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
+ * @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 +292,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
+ * @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 +311,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;
}
@@ -248,14 +332,27 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
* JSONObject.
* @param ja A JSONArray of JSONObjects.
* @return A comma delimited text.
- * @throws JSONException
+ * @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;
@@ -268,18 +365,31 @@ public static String toString(JSONArray ja) throws JSONException {
* @param names A JSONArray of strings.
* @param ja A JSONArray of JSONObjects.
* @return A comma delimited text.
- * @throws JSONException
+ * @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;
}
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
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
new file mode 100644
index 000000000..ab908a304
--- /dev/null
+++ b/src/main/java/org/json/Cookie.java
@@ -0,0 +1,210 @@
+package org.json;
+
+import java.util.Locale;
+
+/*
+Public Domain.
+*/
+
+/**
+ * Convert a web browser cookie specification to a JSONObject and back.
+ * JSON and Cookies are both notations for name/value pairs.
+ * See also: https://tools.ietf.org/html/rfc6265
+ * @author JSON.org
+ * @version 2015-12-09
+ */
+public class Cookie {
+
+ /**
+ * Constructs a new Cookie object.
+ */
+ public Cookie() {
+ }
+
+ /**
+ * Produce a copy of a string in which the characters '+', '%', '=', ';'
+ * and control characters are replaced with "%hh". This is a gentle form
+ * of URL encoding, attempting to cause as little distortion to the
+ * string as possible. The characters '=' and ';' are meta characters in
+ * cookies. By convention, they are escaped using the URL-encoding. This is
+ * only a convention, not a standard. Often, cookies are expected to have
+ * encoded values. We encode '=' and ';' because we must. We encode '%' and
+ * '+' because they are meta characters in URL encoding.
+ * @param string The source string.
+ * @return The escaped result.
+ */
+ public static String escape(String string) {
+ char c;
+ String s = string.trim();
+ int length = s.length();
+ StringBuilder sb = new StringBuilder(length);
+ for (int i = 0; i < length; i += 1) {
+ c = s.charAt(i);
+ if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
+ sb.append('%');
+ sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16));
+ sb.append(Character.forDigit((char)(c & 0x0f), 16));
+ } else {
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+
+ /**
+ * Convert a cookie specification string into a JSONObject. The string
+ * must contain a name value pair separated by '='. The name and the value
+ * will be unescaped, possibly converting '+' and '%' sequences. The
+ * cookie properties may follow, separated by ';', also represented as
+ * name=value (except the Attribute properties like "Secure" or "HttpOnly",
+ * which do not have a value. The value {@link Boolean#TRUE} will be used for these).
+ * The name will be stored under the key "name", and the value will be
+ * stored under the key "value". This method does not do checking or
+ * validation of the parameters. It only converts the cookie string into
+ * a JSONObject. All attribute names are converted to lower case keys in the
+ * JSONObject (HttpOnly => httponly). If an attribute is specified more than
+ * once, only the value found closer to the end of the cookie-string is kept.
+ * @param string The cookie specification string.
+ * @return A JSONObject containing "name", "value", and possibly other
+ * members.
+ * @throws JSONException If there is an error parsing the Cookie String.
+ * Cookie strings must have at least one '=' character and the 'name'
+ * portion of the cookie must not be blank.
+ */
+ public static JSONObject toJSONObject(String string) {
+ final JSONObject jo = new JSONObject();
+ String name;
+ Object value;
+
+
+ JSONTokener x = new JSONTokener(string);
+
+ name = unescape(x.nextTo('=').trim());
+ //per RFC6265, if the name is blank, the cookie should be ignored.
+ if("".equals(name)) {
+ throw new JSONException("Cookies must have a 'name'");
+ }
+ jo.put("name", name);
+ // per RFC6265, if there is no '=', the cookie should be ignored.
+ // the 'next' call here throws an exception if the '=' is not found.
+ x.next('=');
+ jo.put("value", unescape(x.nextTo(';')).trim());
+ // discard the ';'
+ x.next();
+ // parse the remaining cookie attributes
+ while (x.more()) {
+ name = unescape(x.nextTo("=;")).trim().toLowerCase(Locale.ROOT);
+ // don't allow a cookies attributes to overwrite its name or value.
+ if("name".equalsIgnoreCase(name)) {
+ throw new JSONException("Illegal attribute name: 'name'");
+ }
+ if("value".equalsIgnoreCase(name)) {
+ throw new JSONException("Illegal attribute name: 'value'");
+ }
+ // check to see if it's a flag property
+ if (x.next() != '=') {
+ value = Boolean.TRUE;
+ } else {
+ value = unescape(x.nextTo(';')).trim();
+ x.next();
+ }
+ // only store non-blank attributes
+ if(!"".equals(name) && !"".equals(value)) {
+ jo.put(name, value);
+ }
+ }
+ return jo;
+ }
+
+
+ /**
+ * Convert a JSONObject into a cookie specification string. The JSONObject
+ * must contain "name" and "value" members (case insensitive).
+ * If the JSONObject contains other members, they will be appended to the cookie
+ * specification string. User-Agents are instructed to ignore unknown attributes,
+ * so ensure your JSONObject is using only known attributes.
+ * See also: https://tools.ietf.org/html/rfc6265
+ * @param jo A JSONObject
+ * @return A cookie specification string
+ * @throws JSONException thrown if the cookie has no name.
+ */
+ public static String toString(JSONObject jo) throws JSONException {
+ StringBuilder sb = new StringBuilder();
+
+ String name = null;
+ Object value = null;
+ for(String key : jo.keySet()){
+ if("name".equalsIgnoreCase(key)) {
+ name = jo.getString(key).trim();
+ }
+ if("value".equalsIgnoreCase(key)) {
+ value=jo.getString(key).trim();
+ }
+ if(name != null && value != null) {
+ break;
+ }
+ }
+
+ if(name == null || "".equals(name.trim())) {
+ throw new JSONException("Cookie does not have a name");
+ }
+ if(value == null) {
+ value = "";
+ }
+
+ sb.append(escape(name));
+ sb.append("=");
+ sb.append(escape((String)value));
+
+ for(String key : jo.keySet()){
+ if("name".equalsIgnoreCase(key)
+ || "value".equalsIgnoreCase(key)) {
+ // already processed above
+ continue;
+ }
+ value = jo.opt(key);
+ if(value instanceof Boolean) {
+ if(Boolean.TRUE.equals(value)) {
+ sb.append(';').append(escape(key));
+ }
+ // don't emit false values
+ } else {
+ sb.append(';')
+ .append(escape(key))
+ .append('=')
+ .append(escape(value.toString()));
+ }
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Convert %
hh sequences to single characters, and
+ * convert plus to space.
+ * @param string A string that may contain
+ * +
(plus) and
+ * %
hh sequences.
+ * @return The unescaped string.
+ */
+ public static String unescape(String string) {
+ int length = string.length();
+ StringBuilder sb = new StringBuilder(length);
+ for (int i = 0; i < length; ++i) {
+ char c = string.charAt(i);
+ if (c == '+') {
+ c = ' ';
+ } else if (c == '%' && i + 2 < length) {
+ int d = JSONTokener.dehexchar(string.charAt(i + 1));
+ int e = JSONTokener.dehexchar(string.charAt(i + 2));
+ if (d >= 0 && e >= 0) {
+ c = (char)(d * 16 + e);
+ i += 2;
+ }
+ }
+ sb.append(c);
+ }
+ return sb.toString();
+ }
+}
diff --git a/CookieList.java b/src/main/java/org/json/CookieList.java
similarity index 53%
rename from CookieList.java
rename to src/main/java/org/json/CookieList.java
index 8cb4e5ed1..d1064db52 100644
--- a/CookieList.java
+++ b/src/main/java/org/json/CookieList.java
@@ -1,30 +1,8 @@
package org.json;
-import java.util.Map.Entry;
-
/*
-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.
+ */
/**
* Convert a web browser cookie list string to a JSONObject and back.
@@ -33,6 +11,12 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class CookieList {
+ /**
+ * Constructs a new CookieList object.
+ */
+ 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 '='.
@@ -44,7 +28,7 @@ public class CookieList {
* cookieJSONObject.getString("value"));
* @param string A cookie list string
* @return A JSONObject
- * @throws JSONException
+ * @throws JSONException if a called function fails
*/
public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject jo = new JSONObject();
@@ -65,22 +49,22 @@ public static JSONObject toJSONObject(String string) throws JSONException {
* in the names and values are replaced by "%hh".
* @param jo A JSONObject
* @return A cookie list string
- * @throws JSONException
+ * @throws JSONException if a called function fails
*/
public static String toString(JSONObject jo) throws JSONException {
- boolean b = false;
- StringBuilder sb = new StringBuilder();
- for (final Entry entry : jo.entrySet()) {
- final String key = entry.getKey();
- final Object value = entry.getValue();
+ 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;
+ sb.append(Cookie.escape(value.toString()));
+ isEndOfPair = true;
}
}
return sb.toString();
diff --git a/HTTP.java b/src/main/java/org/json/HTTP.java
similarity index 73%
rename from HTTP.java
rename to src/main/java/org/json/HTTP.java
index 22635fffd..44ab3a6d3 100644
--- a/HTTP.java
+++ b/src/main/java/org/json/HTTP.java
@@ -1,162 +1,148 @@
-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.
-*/
-
-import java.util.Locale;
-import java.util.Map.Entry;
-
-/**
- * Convert an HTTP header to a JSONObject and back.
- * @author JSON.org
- * @version 2015-12-09
- */
-public class HTTP {
-
- /** Carriage return/line feed. */
- public static final String CRLF = "\r\n";
-
- /**
- * Convert an HTTP header string into a JSONObject. It can be a request
- * header or a response header. A request header will contain
- * {
- * Method: "POST" (for example),
- * "Request-URI": "/" (for example),
- * "HTTP-Version": "HTTP/1.1" (for example)
- * }
- * A response header will contain
- * {
- * "HTTP-Version": "HTTP/1.1" (for example),
- * "Status-Code": "200" (for example),
- * "Reason-Phrase": "OK" (for example)
- * }
- * In addition, the other parameters in the header will be captured, using
- * the HTTP field names as JSON names, so that
- * Date: Sun, 26 May 2002 18:06:04 GMT
- * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
- * Cache-Control: no-cache
- * become
- * {...
- * Date: "Sun, 26 May 2002 18:06:04 GMT",
- * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
- * "Cache-Control": "no-cache",
- * ...}
- * It does no further checking or conversion. It does not parse dates.
- * It does not do '%' transforms on URLs.
- * @param string An HTTP header string.
- * @return A JSONObject containing the elements and attributes
- * of the XML string.
- * @throws JSONException
- */
- public static JSONObject toJSONObject(String string) throws JSONException {
- JSONObject jo = new JSONObject();
- HTTPTokener x = new HTTPTokener(string);
- String token;
-
- token = x.nextToken();
- if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) {
-
-// Response
-
- jo.put("HTTP-Version", token);
- jo.put("Status-Code", x.nextToken());
- jo.put("Reason-Phrase", x.nextTo('\0'));
- x.next();
-
- } else {
-
-// Request
-
- jo.put("Method", token);
- jo.put("Request-URI", x.nextToken());
- jo.put("HTTP-Version", x.nextToken());
- }
-
-// Fields
-
- while (x.more()) {
- String name = x.nextTo(':');
- x.next(':');
- jo.put(name, x.nextTo('\0'));
- x.next();
- }
- return jo;
- }
-
-
- /**
- * Convert a JSONObject into an HTTP header. A request header must contain
- * {
- * Method: "POST" (for example),
- * "Request-URI": "/" (for example),
- * "HTTP-Version": "HTTP/1.1" (for example)
- * }
- * A response header must contain
- * {
- * "HTTP-Version": "HTTP/1.1" (for example),
- * "Status-Code": "200" (for example),
- * "Reason-Phrase": "OK" (for example)
- * }
- * Any other members of the JSONObject will be output as HTTP fields.
- * The result will end with two CRLF pairs.
- * @param jo A JSONObject
- * @return An HTTP header string.
- * @throws JSONException if the object does not contain enough
- * information.
- */
- public static String toString(JSONObject jo) throws JSONException {
- StringBuilder sb = new StringBuilder();
- if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
- sb.append(jo.getString("HTTP-Version"));
- sb.append(' ');
- sb.append(jo.getString("Status-Code"));
- sb.append(' ');
- sb.append(jo.getString("Reason-Phrase"));
- } else if (jo.has("Method") && jo.has("Request-URI")) {
- sb.append(jo.getString("Method"));
- sb.append(' ');
- sb.append('"');
- sb.append(jo.getString("Request-URI"));
- sb.append('"');
- sb.append(' ');
- sb.append(jo.getString("HTTP-Version"));
- } else {
- throw new JSONException("Not enough material for an HTTP header.");
- }
- sb.append(CRLF);
- for (final Entry entry : jo.entrySet()) {
- final String key = entry.getKey();
- if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) &&
- !"Reason-Phrase".equals(key) && !"Method".equals(key) &&
- !"Request-URI".equals(key) && !JSONObject.NULL.equals(entry.getValue())) {
- sb.append(key);
- sb.append(": ");
- sb.append(jo.optString(key));
- sb.append(CRLF);
- }
- }
- sb.append(CRLF);
- return sb.toString();
- }
-}
+package org.json;
+
+/*
+Public Domain.
+*/
+
+import java.util.Locale;
+
+/**
+ * Convert an HTTP header to a JSONObject and back.
+ * @author JSON.org
+ * @version 2015-12-09
+ */
+public class HTTP {
+
+ /**
+ * Constructs a new HTTP object.
+ */
+ public HTTP() {
+ }
+
+ /** Carriage return/line feed. */
+ public static final String CRLF = "\r\n";
+
+ /**
+ * Convert an HTTP header string into a JSONObject. It can be a request
+ * header or a response header. A request header will contain
+ * {
+ * Method: "POST" (for example),
+ * "Request-URI": "/" (for example),
+ * "HTTP-Version": "HTTP/1.1" (for example)
+ * }
+ * A response header will contain
+ * {
+ * "HTTP-Version": "HTTP/1.1" (for example),
+ * "Status-Code": "200" (for example),
+ * "Reason-Phrase": "OK" (for example)
+ * }
+ * In addition, the other parameters in the header will be captured, using
+ * the HTTP field names as JSON names, so that {@code
+ * Date: Sun, 26 May 2002 18:06:04 GMT
+ * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
+ * Cache-Control: no-cache}
+ * become
+ * {@code
+ * Date: "Sun, 26 May 2002 18:06:04 GMT",
+ * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
+ * "Cache-Control": "no-cache",
+ * ...}
+ * It does no further checking or conversion. It does not parse dates.
+ * It does not do '%' transforms on URLs.
+ * @param string An HTTP header string.
+ * @return A JSONObject containing the elements and attributes
+ * of the XML string.
+ * @throws JSONException if a called function fails
+ */
+ public static JSONObject toJSONObject(String string) throws JSONException {
+ JSONObject jo = new JSONObject();
+ HTTPTokener x = new HTTPTokener(string);
+ String token;
+
+ token = x.nextToken();
+ if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) {
+
+// Response
+
+ jo.put("HTTP-Version", token);
+ jo.put("Status-Code", x.nextToken());
+ jo.put("Reason-Phrase", x.nextTo('\0'));
+ x.next();
+
+ } else {
+
+// Request
+
+ jo.put("Method", token);
+ jo.put("Request-URI", x.nextToken());
+ jo.put("HTTP-Version", x.nextToken());
+ }
+
+// Fields
+
+ while (x.more()) {
+ String name = x.nextTo(':');
+ x.next(':');
+ jo.put(name, x.nextTo('\0'));
+ x.next();
+ }
+ return jo;
+ }
+
+
+ /**
+ * Convert a JSONObject into an HTTP header. A request header must contain
+ * {
+ * Method: "POST" (for example),
+ * "Request-URI": "/" (for example),
+ * "HTTP-Version": "HTTP/1.1" (for example)
+ * }
+ * A response header must contain
+ * {
+ * "HTTP-Version": "HTTP/1.1" (for example),
+ * "Status-Code": "200" (for example),
+ * "Reason-Phrase": "OK" (for example)
+ * }
+ * Any other members of the JSONObject will be output as HTTP fields.
+ * The result will end with two CRLF pairs.
+ * @param jo A JSONObject
+ * @return An HTTP header string.
+ * @throws JSONException if the object does not contain enough
+ * information.
+ */
+ public static String toString(JSONObject jo) throws JSONException {
+ StringBuilder sb = new StringBuilder();
+ if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
+ sb.append(jo.getString("HTTP-Version"));
+ sb.append(' ');
+ sb.append(jo.getString("Status-Code"));
+ sb.append(' ');
+ sb.append(jo.getString("Reason-Phrase"));
+ } else if (jo.has("Method") && jo.has("Request-URI")) {
+ sb.append(jo.getString("Method"));
+ sb.append(' ');
+ sb.append('"');
+ sb.append(jo.getString("Request-URI"));
+ sb.append('"');
+ sb.append(' ');
+ sb.append(jo.getString("HTTP-Version"));
+ } else {
+ throw new JSONException("Not enough material for an HTTP header.");
+ }
+ sb.append(CRLF);
+ // Don't use the new entrySet API to maintain Android support
+ for (final String key : jo.keySet()) {
+ String value = jo.optString(key);
+ if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) &&
+ !"Reason-Phrase".equals(key) && !"Method".equals(key) &&
+ !"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) {
+ sb.append(key);
+ sb.append(": ");
+ sb.append(jo.optString(key));
+ sb.append(CRLF);
+ }
+ }
+ sb.append(CRLF);
+ return sb.toString();
+ }
+}
diff --git a/HTTPTokener.java b/src/main/java/org/json/HTTPTokener.java
similarity index 53%
rename from HTTPTokener.java
rename to src/main/java/org/json/HTTPTokener.java
index 55f48ffa5..48cad31a3 100644
--- a/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.
*/
/**
@@ -43,8 +23,8 @@ public HTTPTokener(String string) {
/**
* Get the next token or string. This is used in parsing HTTP headers.
- * @throws JSONException
* @return A String.
+ * @throws JSONException if a syntax error occurs
*/
public String nextToken() throws JSONException {
char c;
diff --git a/JSONArray.java b/src/main/java/org/json/JSONArray.java
similarity index 56%
rename from JSONArray.java
rename to src/main/java/org/json/JSONArray.java
index 8775a1984..c2e5c9a5b 100644
--- a/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;
@@ -36,6 +15,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.List;
import java.util.Map;
+
/**
* A JSONArray is an ordered sequence of values. Its external text form is a
* string wrapped in square brackets with commas separating the values. The
@@ -103,11 +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 '['");
}
- if (x.nextClean() != ']') {
+
+ char nextChar = x.nextClean();
+ if (nextChar == 0) {
+ // array is unclosed. No ']' found, instead EOF
+ throw x.syntaxError("Expected a ',' or ']'");
+ }
+ if (nextChar != ']') {
x.back();
for (;;) {
if (x.nextClean() == ',') {
@@ -118,18 +117,45 @@ public JSONArray(JSONTokener x) throws JSONException {
this.myArrayList.add(x.nextValue());
}
switch (x.nextClean()) {
+ case 0:
+ // array is unclosed. No ']' found, instead EOF
+ throw x.syntaxError("Expected a ',' or ']'");
case ',':
- if (x.nextClean() == ']') {
+ 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;
+ }
+ 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;
}
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;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
}
+ } else {
+ if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
+ throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
+ }
}
}
@@ -144,7 +170,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);
}
/**
@@ -154,34 +195,108 @@ 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();
} else {
this.myArrayList = new ArrayList(collection.size());
- for (Object o: collection){
- this.myArrayList.add(JSONObject.wrap(o));
- }
+ this.addAll(collection, true, recursionDepth, jsonParserConfiguration);
}
}
/**
- * Construct a JSONArray from an array
+ * Construct a JSONArray from an Iterable. This is a shallow copy.
+ *
+ * @param iter
+ * A Iterable collection.
+ */
+ public JSONArray(Iterable> iter) {
+ this();
+ if (iter == null) {
+ return;
+ }
+ this.addAll(iter, true);
+ }
+
+ /**
+ * Construct a JSONArray from another JSONArray. This is a shallow copy.
+ *
+ * @param array
+ * A array.
+ */
+ public JSONArray(JSONArray array) {
+ if (array == null) {
+ this.myArrayList = new ArrayList();
+ } else {
+ // shallow copy directly the internal array lists as any wrapping
+ // should have been done already in the original JSONArray
+ this.myArrayList = new ArrayList(array.myArrayList);
+ }
+ }
+
+ /**
+ * Construct a JSONArray from an array.
+ *
+ * @param array
+ * Array. If the parameter passed is null, or not an array, an
+ * exception will be thrown.
*
* @throws JSONException
- * If not an array.
+ * If not an array or if an array value is non-finite number.
+ * @throws NullPointerException
+ * Thrown if the array parameter is null.
*/
public JSONArray(Object array) throws JSONException {
this();
- if (array.getClass().isArray()) {
- int length = Array.getLength(array);
- this.myArrayList.ensureCapacity(length);
- for (int i = 0; i < length; i += 1) {
- this.put(JSONObject.wrap(Array.get(array, i)));
- }
- } else {
+ if (!array.getClass().isArray()) {
throw new JSONException(
"JSONArray initial value should be a string or collection or array.");
}
+ this.addAll(array, true, 0);
+ }
+
+ /**
+ * Construct a JSONArray with the specified initial capacity.
+ *
+ * @param initialCapacity
+ * the initial capacity of the JSONArray.
+ * @throws JSONException
+ * If the initial capacity is negative.
+ */
+ public JSONArray(int initialCapacity) throws JSONException {
+ if (initialCapacity < 0) {
+ throw new JSONException(
+ "JSONArray initial capacity cannot be negative.");
+ }
+ this.myArrayList = new ArrayList(initialCapacity);
}
@Override
@@ -219,16 +334,14 @@ public Object get(int index) throws JSONException {
*/
public boolean getBoolean(int index) throws JSONException {
Object object = this.get(index);
- if (object.equals(Boolean.FALSE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("false"))) {
+ if (Boolean.FALSE.equals(object)
+ || (object instanceof String && "false".equalsIgnoreCase((String) object))) {
return false;
- } else if (object.equals(Boolean.TRUE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("true"))) {
+ } else if (Boolean.TRUE.equals(object)
+ || (object instanceof String && "true".equalsIgnoreCase((String) object))) {
return true;
}
- throw new JSONException("JSONArray[" + index + "] is not a boolean.");
+ throw wrongValueFormatException(index, "boolean", object, null);
}
/**
@@ -242,12 +355,14 @@ public boolean getBoolean(int index) throws JSONException {
* to a number.
*/
public double getDouble(int index) throws JSONException {
- Object object = this.get(index);
+ final Object object = this.get(index);
+ if(object instanceof Number) {
+ return ((Number)object).doubleValue();
+ }
try {
- return object instanceof Number ? ((Number) object).doubleValue()
- : Double.parseDouble((String) object);
+ return Double.parseDouble(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONArray[" + index + "] is not a number.", e);
+ throw wrongValueFormatException(index, "double", object, e);
}
}
@@ -262,13 +377,14 @@ public double getDouble(int index) throws JSONException {
* object and cannot be converted to a number.
*/
public float getFloat(int index) throws JSONException {
- Object object = this.get(index);
+ final Object object = this.get(index);
+ if(object instanceof Number) {
+ return ((Number)object).floatValue();
+ }
try {
- return object instanceof Number ? ((Number) object).floatValue()
- : Float.parseFloat(object.toString());
+ return Float.parseFloat(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONArray[" + index
- + "] is not a number.", e);
+ throw wrongValueFormatException(index, "float", object, e);
}
}
@@ -290,36 +406,41 @@ public Number getNumber(int index) throws JSONException {
}
return JSONObject.stringToNumber(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONArray[" + index + "] is not a number.", e);
+ throw wrongValueFormatException(index, "number", object, e);
}
}
/**
- * Get the enum value associated with an index.
- *
- * @param clazz
- * The type of enum to retrieve.
- * @param index
- * The index must be between 0 and length() - 1.
- * @return The enum value at the index location
- * @throws JSONException
- * if the key is not found or if the value cannot be converted
- * to an enum.
- */
+ * Get the enum value associated with an index.
+ *
+ * @param
+ * Enum Type
+ * @param clazz
+ * The type of enum to retrieve.
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The enum value at the index location
+ * @throws JSONException
+ * if the key is not found or if the value cannot be converted
+ * to an enum.
+ */
public > E getEnum(Class clazz, int index) throws JSONException {
E val = optEnum(clazz, index);
if(val==null) {
// JSONException should really take a throwable argument.
// If it did, I would re-implement this with the Enum.valueOf
// method and place any thrown exception in the JSONException
- throw new JSONException("JSONArray[" + index + "] is not an enum of type "
- + JSONObject.quote(clazz.getSimpleName()) + ".");
+ throw wrongValueFormatException(index, "enum of type "
+ + JSONObject.quote(clazz.getSimpleName()), opt(index), null);
}
return val;
}
/**
- * Get the BigDecimal value associated with an index.
+ * Get the BigDecimal value associated with an index. If the value is float
+ * or double, the {@link BigDecimal#BigDecimal(double)} constructor
+ * will be used. See notes on the constructor for conversion issues that
+ * may arise.
*
* @param index
* The index must be between 0 and length() - 1.
@@ -330,12 +451,11 @@ public > E getEnum(Class clazz, int index) throws JSONExcep
*/
public BigDecimal getBigDecimal (int index) throws JSONException {
Object object = this.get(index);
- try {
- return new BigDecimal(object.toString());
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index +
- "] could not convert to BigDecimal.", e);
+ BigDecimal val = JSONObject.objectToBigDecimal(object, null);
+ if(val == null) {
+ throw wrongValueFormatException(index, "BigDecimal", object, null);
}
+ return val;
}
/**
@@ -350,12 +470,11 @@ public BigDecimal getBigDecimal (int index) throws JSONException {
*/
public BigInteger getBigInteger (int index) throws JSONException {
Object object = this.get(index);
- try {
- return new BigInteger(object.toString());
- } catch (Exception e) {
- throw new JSONException("JSONArray[" + index +
- "] could not convert to BigInteger.", e);
+ BigInteger val = JSONObject.objectToBigInteger(object, null);
+ if(val == null) {
+ throw wrongValueFormatException(index, "BigInteger", object, null);
}
+ return val;
}
/**
@@ -368,12 +487,14 @@ public BigInteger getBigInteger (int index) throws JSONException {
* If the key is not found or if the value is not a number.
*/
public int getInt(int index) throws JSONException {
- Object object = this.get(index);
+ final Object object = this.get(index);
+ if(object instanceof Number) {
+ return ((Number)object).intValue();
+ }
try {
- return object instanceof Number ? ((Number) object).intValue()
- : Integer.parseInt((String) object);
+ return Integer.parseInt(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONArray[" + index + "] is not a number.", e);
+ throw wrongValueFormatException(index, "int", object, e);
}
}
@@ -392,7 +513,7 @@ public JSONArray getJSONArray(int index) throws JSONException {
if (object instanceof JSONArray) {
return (JSONArray) object;
}
- throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
+ throw wrongValueFormatException(index, "JSONArray", object, null);
}
/**
@@ -410,7 +531,7 @@ public JSONObject getJSONObject(int index) throws JSONException {
if (object instanceof JSONObject) {
return (JSONObject) object;
}
- throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
+ throw wrongValueFormatException(index, "JSONObject", object, null);
}
/**
@@ -424,12 +545,14 @@ public JSONObject getJSONObject(int index) throws JSONException {
* to a number.
*/
public long getLong(int index) throws JSONException {
- Object object = this.get(index);
+ final Object object = this.get(index);
+ if(object instanceof Number) {
+ return ((Number)object).longValue();
+ }
try {
- return object instanceof Number ? ((Number) object).longValue()
- : Long.parseLong((String) object);
+ return Long.parseLong(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONArray[" + index + "] is not a number.", e);
+ throw wrongValueFormatException(index, "long", object, e);
}
}
@@ -447,15 +570,15 @@ public String getString(int index) throws JSONException {
if (object instanceof String) {
return (String) object;
}
- throw new JSONException("JSONArray[" + index + "] not a string.");
+ throw wrongValueFormatException(index, "String", object, null);
}
/**
- * Determine if the value is null.
+ * Determine if the value is null
.
*
* @param index
* The index must be between 0 and length() - 1.
- * @return true if the value at the index is null, or if there is no value.
+ * @return true if the value at the index is null
, or if there is no value.
*/
public boolean isNull(int index) {
return JSONObject.NULL.equals(this.opt(index));
@@ -474,13 +597,16 @@ public boolean isNull(int index) {
*/
public String join(String separator) throws JSONException {
int len = this.length();
- StringBuilder sb = new StringBuilder();
+ if (len == 0) {
+ return "";
+ }
+
+ StringBuilder sb = new StringBuilder(
+ JSONObject.valueToString(this.myArrayList.get(0)));
- for (int i = 0; i < len; i += 1) {
- if (i > 0) {
- sb.append(separator);
- }
- sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
+ for (int i = 1; i < len; i++) {
+ sb.append(separator)
+ .append(JSONObject.valueToString(this.myArrayList.get(i)));
}
return sb.toString();
}
@@ -494,6 +620,14 @@ public int length() {
return this.myArrayList.size();
}
+ /**
+ * Removes all of the elements from this JSONArray.
+ * The JSONArray will be empty after this call returns.
+ */
+ public void clear() {
+ this.myArrayList.clear();
+ }
+
/**
* Get the optional object value associated with an index.
*
@@ -538,6 +672,38 @@ public boolean optBoolean(int index, boolean defaultValue) {
}
}
+ /**
+ * Get the optional Boolean object associated with an index. It returns false
+ * if there is no value at that index, or if the value is not Boolean.TRUE
+ * or the String "true".
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The truth.
+ */
+ public Boolean optBooleanObject(int index) {
+ return this.optBooleanObject(index, false);
+ }
+
+ /**
+ * Get the optional Boolean object associated with an index. It returns the
+ * defaultValue if there is no value at that index or if it is not a Boolean
+ * or the String "true" or "false" (case insensitive).
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @param defaultValue
+ * A boolean default.
+ * @return The truth.
+ */
+ public Boolean optBooleanObject(int index, Boolean defaultValue) {
+ try {
+ return this.getBoolean(index);
+ } catch (Exception e) {
+ return defaultValue;
+ }
+ }
+
/**
* Get the optional double value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
@@ -563,21 +729,51 @@ public double optDouble(int index) {
* @return The value.
*/
public double optDouble(int index, double defaultValue) {
- Object val = this.opt(index);
- if (JSONObject.NULL.equals(val)) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).doubleValue();
- }
- if (val instanceof String) {
- try {
- return Double.parseDouble((String) val);
- } catch (Exception e) {
- return defaultValue;
- }
+ final double doubleValue = val.doubleValue();
+ // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
+ // return defaultValue;
+ // }
+ return doubleValue;
+ }
+
+ /**
+ * Get the optional Double object associated with an index. NaN is returned
+ * if there is no value for the index, or if the value is not a number and
+ * cannot be converted to a number.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The object.
+ */
+ public Double optDoubleObject(int index) {
+ return this.optDoubleObject(index, Double.NaN);
+ }
+
+ /**
+ * Get the optional double value associated with an index. The defaultValue
+ * is returned if there is no value for the index, or if the value is not a
+ * number and cannot be converted to a number.
+ *
+ * @param index
+ * subscript
+ * @param defaultValue
+ * The default object.
+ * @return The object.
+ */
+ public Double optDoubleObject(int index, Double defaultValue) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+ final Double doubleValue = val.doubleValue();
+ // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
+ // return defaultValue;
+ // }
+ return doubleValue;
}
/**
@@ -605,21 +801,51 @@ public float optFloat(int index) {
* @return The value.
*/
public float optFloat(int index, float defaultValue) {
- Object val = this.opt(index);
- if (JSONObject.NULL.equals(val)) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).floatValue();
- }
- if (val instanceof String) {
- try {
- return Float.parseFloat((String) val);
- } catch (Exception e) {
- return defaultValue;
- }
+ final float floatValue = val.floatValue();
+ // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
+ // return floatValue;
+ // }
+ return floatValue;
+ }
+
+ /**
+ * Get the optional Float object associated with an index. NaN is returned
+ * if there is no value for the index, or if the value is not a number and
+ * cannot be converted to a number.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The object.
+ */
+ public Float optFloatObject(int index) {
+ return this.optFloatObject(index, Float.NaN);
+ }
+
+ /**
+ * Get the optional Float object associated with an index. The defaultValue
+ * is returned if there is no value for the index, or if the value is not a
+ * number and cannot be converted to a number.
+ *
+ * @param index
+ * subscript
+ * @param defaultValue
+ * The default object.
+ * @return The object.
+ */
+ public Float optFloatObject(int index, Float defaultValue) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+ final Float floatValue = val.floatValue();
+ // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
+ // return floatValue;
+ // }
+ return floatValue;
}
/**
@@ -647,27 +873,50 @@ public int optInt(int index) {
* @return The value.
*/
public int optInt(int index, int defaultValue) {
- Object val = this.opt(index);
- if (JSONObject.NULL.equals(val)) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).intValue();
- }
-
- if (val instanceof String) {
- try {
- return new BigDecimal(val.toString()).intValue();
- } catch (Exception e) {
- return defaultValue;
- }
+ return val.intValue();
+ }
+
+ /**
+ * Get the optional Integer object associated with an index. Zero is returned if
+ * there is no value for the index, or if the value is not a number and
+ * cannot be converted to a number.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The object.
+ */
+ public Integer optIntegerObject(int index) {
+ return this.optIntegerObject(index, 0);
+ }
+
+ /**
+ * Get the optional Integer object associated with an index. The defaultValue is
+ * returned if there is no value for the index, or if the value is not a
+ * number and cannot be converted to a number.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @param defaultValue
+ * The default object.
+ * @return The object.
+ */
+ public Integer optIntegerObject(int index, Integer defaultValue) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+ return val.intValue();
}
/**
* Get the enum value associated with a key.
*
+ * @param
+ * Enum Type
* @param clazz
* The type of enum to retrieve.
* @param index
@@ -681,6 +930,8 @@ public > E optEnum(Class clazz, int index) {
/**
* Get the enum value associated with a key.
*
+ * @param
+ * Enum Type
* @param clazz
* The type of enum to retrieve.
* @param index
@@ -710,7 +961,6 @@ public > E optEnum(Class clazz, int index, E defaultValue)
}
}
-
/**
* Get the optional BigInteger value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the
@@ -724,37 +974,16 @@ public > E optEnum(Class clazz, int index, E defaultValue)
*/
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
Object val = this.opt(index);
- if (JSONObject.NULL.equals(val)) {
- return defaultValue;
- }
- if (val instanceof BigInteger){
- return (BigInteger) val;
- }
- if (val instanceof BigDecimal){
- return ((BigDecimal) val).toBigInteger();
- }
- if (val instanceof Double || val instanceof Float){
- return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
- }
- if (val instanceof Long || val instanceof Integer
- || val instanceof Short || val instanceof Byte){
- return BigInteger.valueOf(((Number) val).longValue());
- }
- try {
- final String valStr = val.toString();
- if(JSONObject.isDecimalNotation(valStr)) {
- return new BigDecimal(valStr).toBigInteger();
- }
- return new BigInteger(valStr);
- } catch (Exception e) {
- return defaultValue;
- }
+ return JSONObject.objectToBigInteger(val, defaultValue);
}
/**
* Get the optional BigDecimal value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the
- * value is not a number and cannot be converted to a number.
+ * value is not a number and cannot be converted to a number. If the value
+ * is float or double, the {@link BigDecimal#BigDecimal(double)}
+ * constructor will be used. See notes on the constructor for conversion
+ * issues that may arise.
*
* @param index
* The index must be between 0 and length() - 1.
@@ -764,54 +993,61 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
*/
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
Object val = this.opt(index);
- if (JSONObject.NULL.equals(val)) {
- return defaultValue;
- }
- if (val instanceof BigDecimal){
- return (BigDecimal) val;
- }
- if (val instanceof BigInteger){
- return new BigDecimal((BigInteger) val);
- }
- if (val instanceof Double || val instanceof Float){
- return new BigDecimal(((Number) val).doubleValue());
- }
- if (val instanceof Long || val instanceof Integer
- || val instanceof Short || val instanceof Byte){
- return new BigDecimal(((Number) val).longValue());
- }
- try {
- return new BigDecimal(val.toString());
- } catch (Exception e) {
- return defaultValue;
- }
+ return JSONObject.objectToBigDecimal(val, defaultValue);
}
/**
- * Get the optional JSONArray associated with an index.
+ * Get the optional JSONArray associated with an index. Null is returned if
+ * there is no value at that index or if the value is not a JSONArray.
*
* @param index
- * subscript
- * @return A JSONArray value, or null if the index has no value, or if the
- * value is not a JSONArray.
+ * The index must be between 0 and length() - 1.
+ * @return A JSONArray value.
*/
public JSONArray optJSONArray(int index) {
- Object o = this.opt(index);
- return o instanceof JSONArray ? (JSONArray) o : null;
+ return this.optJSONArray(index, null);
+ }
+
+ /**
+ * Get the optional JSONArray associated with an index. The defaultValue is returned if
+ * there is no value at that index or if the value is not a JSONArray.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @param defaultValue
+ * The default.
+ * @return A JSONArray value.
+ */
+ public JSONArray optJSONArray(int index, JSONArray defaultValue) {
+ Object object = this.opt(index);
+ return object instanceof JSONArray ? (JSONArray) object : defaultValue;
}
/**
* Get the optional JSONObject associated with an index. Null is returned if
- * the key is not found, or null if the index has no value, or if the value
- * is not a JSONObject.
+ * there is no value at that index or if the value is not a JSONObject.
*
* @param index
* The index must be between 0 and length() - 1.
* @return A JSONObject value.
*/
public JSONObject optJSONObject(int index) {
- Object o = this.opt(index);
- return o instanceof JSONObject ? (JSONObject) o : null;
+ return this.optJSONObject(index, null);
+ }
+
+ /**
+ * Get the optional JSONObject associated with an index. The defaultValue is returned if
+ * there is no value at that index or if the value is not a JSONObject.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @param defaultValue
+ * The default.
+ * @return A JSONObject value.
+ */
+ public JSONObject optJSONObject(int index, JSONObject defaultValue) {
+ Object object = this.opt(index);
+ return object instanceof JSONObject ? (JSONObject) object : defaultValue;
}
/**
@@ -839,22 +1075,43 @@ public long optLong(int index) {
* @return The value.
*/
public long optLong(int index, long defaultValue) {
- Object val = this.opt(index);
- if (JSONObject.NULL.equals(val)) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).longValue();
- }
-
- if (val instanceof String) {
- try {
- return new BigDecimal(val.toString()).longValue();
- } catch (Exception e) {
- return defaultValue;
- }
+ return val.longValue();
+ }
+
+ /**
+ * Get the optional Long object associated with an index. Zero is returned if
+ * there is no value for the index, or if the value is not a number and
+ * cannot be converted to a number.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @return The object.
+ */
+ public Long optLongObject(int index) {
+ return this.optLongObject(index, 0L);
+ }
+
+ /**
+ * Get the optional Long object associated with an index. The defaultValue is
+ * returned if there is no value for the index, or if the value is not a
+ * number and cannot be converted to a number.
+ *
+ * @param index
+ * The index must be between 0 and length() - 1.
+ * @param defaultValue
+ * The default object.
+ * @return The object.
+ */
+ public Long optLongObject(int index, Long defaultValue) {
+ final Number val = this.optNumber(index, null);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+ return val.longValue();
}
/**
@@ -939,8 +1196,7 @@ public String optString(int index, String defaultValue) {
* @return this.
*/
public JSONArray put(boolean value) {
- this.put(value ? Boolean.TRUE : Boolean.FALSE);
- return this;
+ return this.put(value ? Boolean.TRUE : Boolean.FALSE);
}
/**
@@ -950,10 +1206,11 @@ public JSONArray put(boolean value) {
* @param value
* A Collection value.
* @return this.
+ * @throws JSONException
+ * If the value is non-finite number.
*/
public JSONArray put(Collection> value) {
- this.put(new JSONArray(value));
- return this;
+ return this.put(new JSONArray(value));
}
/**
@@ -961,15 +1218,25 @@ public JSONArray put(Collection> value) {
*
* @param value
* A double value.
+ * @return this.
* @throws JSONException
* if the value is not finite.
- * @return this.
*/
public JSONArray put(double value) throws JSONException {
- Double d = new Double(value);
- JSONObject.testValidity(d);
- this.put(d);
- return this;
+ return this.put(Double.valueOf(value));
+ }
+
+ /**
+ * Append a float value. This increases the array's length by one.
+ *
+ * @param value
+ * A float value.
+ * @return this.
+ * @throws JSONException
+ * if the value is not finite.
+ */
+ public JSONArray put(float value) throws JSONException {
+ return this.put(Float.valueOf(value));
}
/**
@@ -980,8 +1247,7 @@ public JSONArray put(double value) throws JSONException {
* @return this.
*/
public JSONArray put(int value) {
- this.put(new Integer(value));
- return this;
+ return this.put(Integer.valueOf(value));
}
/**
@@ -992,8 +1258,7 @@ public JSONArray put(int value) {
* @return this.
*/
public JSONArray put(long value) {
- this.put(new Long(value));
- return this;
+ return this.put(Long.valueOf(value));
}
/**
@@ -1003,10 +1268,13 @@ public JSONArray put(long value) {
* @param value
* A Map value.
* @return this.
+ * @throws JSONException
+ * If a value in the map is non-finite number.
+ * @throws NullPointerException
+ * If a key in the map is null
*/
public JSONArray put(Map, ?> value) {
- this.put(new JSONObject(value));
- return this;
+ return this.put(new JSONObject(value));
}
/**
@@ -1017,8 +1285,11 @@ public JSONArray put(Map, ?> value) {
* Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object.
* @return this.
+ * @throws JSONException
+ * If the value is non-finite number.
*/
public JSONArray put(Object value) {
+ JSONObject.testValidity(value);
this.myArrayList.add(value);
return this;
}
@@ -1037,8 +1308,7 @@ public JSONArray put(Object value) {
* If the index is negative.
*/
public JSONArray put(int index, boolean value) throws JSONException {
- this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
- return this;
+ return this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
}
/**
@@ -1051,11 +1321,10 @@ public JSONArray put(int index, boolean value) throws JSONException {
* A Collection value.
* @return this.
* @throws JSONException
- * If the index is negative or if the value is not finite.
+ * If the index is negative or if the value is non-finite.
*/
public JSONArray put(int index, Collection> value) throws JSONException {
- this.put(index, new JSONArray(value));
- return this;
+ return this.put(index, new JSONArray(value));
}
/**
@@ -1069,11 +1338,27 @@ public JSONArray put(int index, Collection> value) throws JSONException {
* A double value.
* @return this.
* @throws JSONException
- * If the index is negative or if the value is not finite.
+ * If the index is negative or if the value is non-finite.
*/
public JSONArray put(int index, double value) throws JSONException {
- this.put(index, new Double(value));
- return this;
+ return this.put(index, Double.valueOf(value));
+ }
+
+ /**
+ * Put or replace a float value. If the index is greater than the length of
+ * the JSONArray, then null elements will be added as necessary to pad it
+ * out.
+ *
+ * @param index
+ * The subscript.
+ * @param value
+ * A float value.
+ * @return this.
+ * @throws JSONException
+ * If the index is negative or if the value is non-finite.
+ */
+ public JSONArray put(int index, float value) throws JSONException {
+ return this.put(index, Float.valueOf(value));
}
/**
@@ -1090,8 +1375,7 @@ public JSONArray put(int index, double value) throws JSONException {
* If the index is negative.
*/
public JSONArray put(int index, int value) throws JSONException {
- this.put(index, new Integer(value));
- return this;
+ return this.put(index, Integer.valueOf(value));
}
/**
@@ -1108,8 +1392,7 @@ public JSONArray put(int index, int value) throws JSONException {
* If the index is negative.
*/
public JSONArray put(int index, long value) throws JSONException {
- this.put(index, new Long(value));
- return this;
+ return this.put(index, Long.valueOf(value));
}
/**
@@ -1120,13 +1403,36 @@ public JSONArray put(int index, long value) throws JSONException {
* The subscript.
* @param value
* The Map value.
- * @return this.
+ * @return
+ * reference to self
* @throws JSONException
- * If the index is negative or if the the value is an invalid
+ * If the index is negative or if the value is an invalid
* number.
+ * @throws NullPointerException
+ * If a key in the map is null
*/
public JSONArray put(int index, Map, ?> value) throws JSONException {
- this.put(index, new JSONObject(value));
+ this.put(index, new JSONObject(value, new JSONParserConfiguration()));
+ return this;
+ }
+
+ /**
+ * Put a value in the JSONArray, where the value will be a JSONObject that
+ * is produced from a Map.
+ *
+ * @param index
+ * The subscript
+ * @param value
+ * The Map value.
+ * @param jsonParserConfiguration
+ * Configuration object for the JSON parser
+ * @return reference to self
+ * @throws JSONException
+ * If the index is negative or if the value is an invalid
+ * number.
+ */
+ public JSONArray put(int index, Map, ?> value, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
+ this.put(index, new JSONObject(value, jsonParserConfiguration));
return this;
}
@@ -1143,28 +1449,85 @@ public JSONArray put(int index, Map, ?> value) throws JSONException {
* String, or the JSONObject.NULL object.
* @return this.
* @throws JSONException
- * If the index is negative or if the the value is an invalid
+ * If the index is negative or if the value is an invalid
* number.
*/
public JSONArray put(int index, Object value) throws JSONException {
- JSONObject.testValidity(value);
if (index < 0) {
throw new JSONException("JSONArray[" + index + "] not found.");
}
if (index < this.length()) {
+ JSONObject.testValidity(value);
this.myArrayList.set(index, value);
- } else if(index == this.length()){
+ return this;
+ }
+ if(index == this.length()){
// simple append
- this.put(value);
- } else {
- // if we are inserting past the length, we want to grow the array all at once
- // instead of incrementally.
- this.myArrayList.ensureCapacity(index + 1);
- while (index != this.length()) {
- this.put(JSONObject.NULL);
- }
- this.put(value);
+ return this.put(value);
}
+ // if we are inserting past the length, we want to grow the array all at once
+ // instead of incrementally.
+ this.myArrayList.ensureCapacity(index + 1);
+ while (index != this.length()) {
+ // we don't need to test validity of NULL objects
+ this.myArrayList.add(JSONObject.NULL);
+ }
+ return this.put(value);
+ }
+
+ /**
+ * Put a collection's elements in to the JSONArray.
+ *
+ * @param collection
+ * A Collection.
+ * @return this.
+ */
+ public JSONArray putAll(Collection> collection) {
+ this.addAll(collection, false);
+ return this;
+ }
+
+ /**
+ * Put an Iterable's elements in to the JSONArray.
+ *
+ * @param iter
+ * An Iterable.
+ * @return this.
+ */
+ public JSONArray putAll(Iterable> iter) {
+ this.addAll(iter, false);
+ return this;
+ }
+
+ /**
+ * Put a JSONArray's elements in to the JSONArray.
+ *
+ * @param array
+ * A JSONArray.
+ * @return this.
+ */
+ public JSONArray putAll(JSONArray array) {
+ // directly copy the elements from the source array to this one
+ // as all wrapping should have been done already in the source.
+ this.myArrayList.addAll(array.myArrayList);
+ return this;
+ }
+
+ /**
+ * Put an array's elements in to the JSONArray.
+ *
+ * @param array
+ * Array. If the parameter passed is null, or not an array or Iterable, an
+ * exception will be thrown.
+ * @return this.
+ *
+ * @throws JSONException
+ * If not an array, JSONArray, Iterable or if an value is non-finite number.
+ * @throws NullPointerException
+ * Thrown if the array parameter is null.
+ */
+ public JSONArray putAll(Object array) throws JSONException {
+ this.addAll(array, false);
return this;
}
@@ -1192,8 +1555,8 @@ public Object query(String jsonPointer) {
}
/**
- * Uses a uaer initialized JSONPointer and tries to
- * match it to an item whithin this JSONArray. For example, given a
+ * Uses a user initialized JSONPointer and tries to
+ * match it to an item within this JSONArray. For example, given a
* JSONArray initialized with this document:
*
* [
@@ -1275,7 +1638,7 @@ public boolean similar(Object other) {
Object valueThis = this.myArrayList.get(i);
Object valueOther = ((JSONArray)other).myArrayList.get(i);
if(valueThis == valueOther) {
- return true;
+ continue;
}
if(valueThis == null) {
return false;
@@ -1288,6 +1651,14 @@ public boolean similar(Object other) {
if (!((JSONArray)valueThis).similar(valueOther)) {
return false;
}
+ } else if (valueThis instanceof Number && valueOther instanceof Number) {
+ if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) {
+ return false;
+ }
+ } else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
+ if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
+ return false;
+ }
} else if (!valueThis.equals(valueOther)) {
return false;
}
@@ -1308,7 +1679,7 @@ public boolean similar(Object other) {
* If any of the names are null.
*/
public JSONObject toJSONObject(JSONArray names) throws JSONException {
- if (names == null || names.length() == 0 || this.length() == 0) {
+ if (names == null || names.isEmpty() || this.isEmpty()) {
return null;
}
JSONObject jo = new JSONObject(names.length());
@@ -1342,7 +1713,7 @@ public String toString() {
/**
* Make a pretty-printed JSON text of this JSONArray.
*
- * If indentFactor > 0
and the {@link JSONArray} has only
+ *
If
{@code indentFactor > 0} and the {@link JSONArray} has only
* one element, then the array will be output on a single line:
* {@code [1]}
*
@@ -1364,13 +1735,15 @@ public String toString() {
* object, beginning with [
(left
* bracket) and ending with ]
* (right bracket) .
- * @throws JSONException
+ * @throws JSONException if a called function fails
*/
+ @SuppressWarnings("resource")
public String toString(int indentFactor) throws JSONException {
- StringWriter sw = new StringWriter();
- synchronized (sw.getBuffer()) {
- return this.write(sw, indentFactor, 0).toString();
- }
+ // each value requires a comma, so multiply the count by 2
+ // We don't want to oversize the initial capacity
+ int initialSize = myArrayList.size() * 2;
+ Writer sw = new StringBuilderWriter(Math.max(initialSize, 16));
+ return this.write(sw, indentFactor, 0).toString();
}
/**
@@ -1379,9 +1752,9 @@ public String toString(int indentFactor) throws JSONException {
*
* Warning: This method assumes that the data structure is acyclical.
*
- *
+ * @param writer the writer object
* @return The writer.
- * @throws JSONException
+ * @throws JSONException if a called function fails
*/
public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
@@ -1390,7 +1763,7 @@ public Writer write(Writer writer) throws JSONException {
/**
* Write the contents of the JSONArray as JSON text to a writer.
*
- *
If indentFactor > 0
and the {@link JSONArray} has only
+ *
If
{@code indentFactor > 0} and the {@link JSONArray} has only
* one element, then the array will be output on a single line:
* {@code [1]}
*
@@ -1413,12 +1786,13 @@ public Writer write(Writer writer) throws JSONException {
* @param indent
* The indentation of the top level.
* @return The writer.
- * @throws JSONException
+ * @throws JSONException if a called function fails or unable to write
*/
+ @SuppressWarnings("resource")
public Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
try {
- boolean commanate = false;
+ boolean needsComma = false;
int length = this.length();
writer.write('[');
@@ -1430,23 +1804,23 @@ public Writer write(Writer writer, int indentFactor, int indent)
throw new JSONException("Unable to write JSONArray value at index: 0", e);
}
} else if (length != 0) {
- final int newindent = indent + indentFactor;
+ final int newIndent = indent + indentFactor;
for (int i = 0; i < length; i += 1) {
- if (commanate) {
+ if (needsComma) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
- JSONObject.indent(writer, newindent);
+ JSONObject.indent(writer, newIndent);
try {
JSONObject.writeValue(writer, this.myArrayList.get(i),
- indentFactor, newindent);
+ indentFactor, newIndent);
} catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
}
- commanate = true;
+ needsComma = true;
}
if (indentFactor > 0) {
writer.write('\n');
@@ -1463,7 +1837,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
/**
* Returns a java.util.List containing all of the elements in this array.
* If an element in the array is a JSONArray or JSONObject it will also
- * be converted.
+ * be converted to a List and a Map respectively.
*
* Warning: This method assumes that the data structure is acyclical.
*
@@ -1484,4 +1858,167 @@ public List toList() {
}
return results;
}
+
+ /**
+ * Check if JSONArray is empty.
+ *
+ * @return true if JSONArray is empty, otherwise false.
+ */
+ public boolean isEmpty() {
+ return this.myArrayList.isEmpty();
+ }
+
+ /**
+ * Add a collection's elements to the JSONArray.
+ *
+ * @param collection
+ * A Collection.
+ * @param wrap
+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
+ * {@code false} to add the items directly
+ * @param recursionDepth
+ * Variable for tracking the count of nested object creations.
+ */
+ private void addAll(Collection> collection, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
+ this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size());
+ if (wrap) {
+ for (Object o: collection){
+ this.put(JSONObject.wrap(o, recursionDepth + 1, jsonParserConfiguration));
+ }
+ } else {
+ for (Object o: collection){
+ this.put(o);
+ }
+ }
+ }
+
+ /**
+ * Add an Iterable's elements to the JSONArray.
+ *
+ * @param iter
+ * An Iterable.
+ * @param wrap
+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
+ * {@code false} to add the items directly
+ */
+ private void addAll(Iterable> iter, boolean wrap) {
+ if (wrap) {
+ for (Object o: iter){
+ this.put(JSONObject.wrap(o));
+ }
+ } else {
+ for (Object o: iter){
+ this.put(o);
+ }
+ }
+ }
+
+ /**
+ * Add an array's elements to the JSONArray.
+ *
+ * @param array
+ * Array. If the parameter passed is null, or not an array,
+ * JSONArray, Collection, or Iterable, an exception will be
+ * thrown.
+ * @param wrap
+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
+ * {@code false} to add the items directly
+ * @throws JSONException
+ * If not an array or if an array value is non-finite number.
+ */
+ private void addAll(Object array, boolean wrap) throws JSONException {
+ this.addAll(array, wrap, 0);
+ }
+
+ /**
+ * Add an array's elements to the JSONArray.
+ *
+ * @param array
+ * Array. If the parameter passed is null, or not an array,
+ * JSONArray, Collection, or Iterable, an exception will be
+ * thrown.
+ * @param wrap
+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
+ * {@code false} to add the items directly
+ * @param recursionDepth
+ * Variable for tracking the count of nested object creations.
+ */
+ private void addAll(Object array, boolean wrap, int recursionDepth) {
+ addAll(array, wrap, recursionDepth, new JSONParserConfiguration());
+ }
+ /**
+ * Add an array's elements to the JSONArray.
+ *`
+ * @param array
+ * Array. If the parameter passed is null, or not an array,
+ * JSONArray, Collection, or Iterable, an exception will be
+ * thrown.
+ * @param wrap
+ * {@code true} to call {@link JSONObject#wrap(Object)} for each item,
+ * {@code false} to add the items directly
+ * @param recursionDepth
+ * Variable for tracking the count of nested object creations.
+ * @param jsonParserConfiguration
+ * Variable to pass parser custom configuration for json parsing.
+ * @throws JSONException
+ * If not an array or if an array value is non-finite number.
+ * @throws NullPointerException
+ * Thrown if the array parameter is null.
+ */
+ private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
+ if (array.getClass().isArray()) {
+ int length = Array.getLength(array);
+ this.myArrayList.ensureCapacity(this.myArrayList.size() + length);
+ if (wrap) {
+ for (int i = 0; i < length; i += 1) {
+ this.put(JSONObject.wrap(Array.get(array, i), recursionDepth + 1, jsonParserConfiguration));
+ }
+ } else {
+ for (int i = 0; i < length; i += 1) {
+ this.put(Array.get(array, i));
+ }
+ }
+ } else if (array instanceof JSONArray) {
+ // use the built in array list `addAll` as all object
+ // wrapping should have been completed in the original
+ // JSONArray
+ this.myArrayList.addAll(((JSONArray)array).myArrayList);
+ } else if (array instanceof Collection) {
+ this.addAll((Collection>)array, wrap, recursionDepth, jsonParserConfiguration);
+ } else if (array instanceof Iterable) {
+ this.addAll((Iterable>)array, wrap);
+ } else {
+ throw new JSONException(
+ "JSONArray initial value should be a string or collection or array.");
+ }
+ }
+
+ /**
+ * Create a new JSONException in a common format for incorrect conversions.
+ * @param idx index of the item
+ * @param valueType the type of value being coerced to
+ * @param cause optional cause of the coercion failure
+ * @return JSONException that can be thrown.
+ */
+ private static JSONException wrongValueFormatException(
+ int idx,
+ String valueType,
+ Object value,
+ Throwable cause) {
+ if(value == null) {
+ return new JSONException(
+ "JSONArray[" + idx + "] is not a " + valueType + " (null)."
+ , cause);
+ }
+ // don't try to toString collections or known object types that could be large.
+ if(value instanceof Map || value instanceof Iterable || value instanceof JSONObject) {
+ return new JSONException(
+ "JSONArray[" + idx + "] is not a " + valueType + " (" + value.getClass() + ")."
+ , cause);
+ }
+ return new JSONException(
+ "JSONArray[" + idx + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")."
+ , cause);
+ }
+
}
diff --git a/JSONException.java b/src/main/java/org/json/JSONException.java
similarity index 97%
rename from JSONException.java
rename to src/main/java/org/json/JSONException.java
index 72542dfb6..02c29f3fc 100644
--- a/JSONException.java
+++ b/src/main/java/org/json/JSONException.java
@@ -1,5 +1,9 @@
package org.json;
+/*
+Public Domain.
+ */
+
/**
* The JSONException is thrown by the JSON.org classes when things are amiss.
*
diff --git a/JSONML.java b/src/main/java/org/json/JSONML.java
similarity index 69%
rename from JSONML.java
rename to src/main/java/org/json/JSONML.java
index be16693e2..6e98c8267 100644
--- a/JSONML.java
+++ b/src/main/java/org/json/JSONML.java
@@ -1,29 +1,7 @@
package org.json;
-import java.util.Map.Entry;
-
/*
-Copyright (c) 2008 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 +13,13 @@ of this software and associated documentation files (the "Software"), to deal
* @version 2016-01-30
*/
public class JSONML {
+
+ /**
+ * Constructs a new JSONML object.
+ */
+ public JSONML() {
+ }
+
/**
* Parse XML values and store them in a JSONArray.
* @param x The XMLTokener containing the source string.
@@ -43,13 +28,38 @@ public class JSONML {
* if we are at the outermost level.
* @param keepStrings Don't type-convert text nodes and attribute values
* @return A JSONArray if the value is the outermost tag, otherwise null.
- * @throws JSONException
+ * @throws JSONException if a parsing error occurs
*/
private static Object parse(
XMLTokener x,
boolean arrayForm,
JSONArray ja,
- boolean keepStrings
+ boolean keepStrings,
+ int currentNestingDepth
+ ) throws JSONException {
+ return parse(x,arrayForm, ja,
+ keepStrings ? JSONMLParserConfiguration.KEEP_STRINGS : JSONMLParserConfiguration.ORIGINAL,
+ currentNestingDepth);
+ }
+
+ /**
+ * Parse XML values and store them in a JSONArray.
+ * @param x The XMLTokener containing the source string.
+ * @param arrayForm true if array form, false if object form.
+ * @param ja The JSONArray that is containing the current tag or null
+ * if we are at the outermost level.
+ * @param config The parser configuration:
+ * JSONMLParserConfiguration.ORIGINAL is the default behaviour;
+ * JSONMLParserConfiguration.KEEP_STRINGS means Don't type-convert text nodes and attribute values.
+ * @return A JSONArray if the value is the outermost tag, otherwise null.
+ * @throws JSONException if a parsing error occurs
+ */
+ private static Object parse(
+ XMLTokener x,
+ boolean arrayForm,
+ JSONArray ja,
+ JSONMLParserConfiguration config,
+ int currentNestingDepth
) throws JSONException {
String attribute;
char c;
@@ -101,7 +111,7 @@ private static Object parse(
}
} else if (c == '[') {
token = x.nextToken();
- if (token.equals("CDATA") && x.next() == '[') {
+ if ("CDATA".equals(token) && x.next() == '[') {
if (ja != null) {
ja.put(x.nextCDATA());
}
@@ -174,7 +184,7 @@ private static Object parse(
if (!(token instanceof String)) {
throw x.syntaxError("Missing value");
}
- newjo.accumulate(attribute, keepStrings ? ((String)token) :XML.stringToValue((String)token));
+ newjo.accumulate(attribute, config.isKeepStrings() ? ((String)token) :XML.stringToValue((String)token));
token = null;
} else {
newjo.accumulate(attribute, "");
@@ -203,7 +213,12 @@ private static Object parse(
if (token != XML.GT) {
throw x.syntaxError("Misshaped tag");
}
- closeTag = (String)parse(x, arrayForm, newja, keepStrings);
+
+ if (currentNestingDepth == config.getMaxNestingDepth()) {
+ throw x.syntaxError("Maximum nesting depth of " + config.getMaxNestingDepth() + " reached");
+ }
+
+ closeTag = (String)parse(x, arrayForm, newja, config, currentNestingDepth + 1);
if (closeTag != null) {
if (!closeTag.equals(tagName)) {
throw x.syntaxError("Mismatched '" + tagName +
@@ -225,7 +240,7 @@ private static Object parse(
} else {
if (ja != null) {
ja.put(token instanceof String
- ? keepStrings ? XML.unescape((String)token) :XML.stringToValue((String)token)
+ ? (config.isKeepStrings() ? XML.unescape((String)token) : XML.stringToValue((String)token))
: token);
}
}
@@ -240,13 +255,13 @@ private static Object parse(
* attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child tags.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @param string The source string.
* @return A JSONArray containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONArray
*/
public static JSONArray toJSONArray(String string) throws JSONException {
- return (JSONArray)parse(new XMLTokener(string), true, null, false);
+ return (JSONArray)parse(new XMLTokener(string), true, null, JSONMLParserConfiguration.ORIGINAL, 0);
}
@@ -257,10 +272,10 @@ public static JSONArray toJSONArray(String string) throws JSONException {
* attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child tags.
- * As opposed to toJSONArray this method does not attempt to convert
- * any text node or attribute value to any type
+ * As opposed to toJSONArray this method does not attempt to convert
+ * any text node or attribute value to any type
* but just leaves it as a string.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @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
@@ -268,7 +283,32 @@ public static JSONArray toJSONArray(String string) throws JSONException {
* @throws JSONException Thrown on error converting to a JSONArray
*/
public static JSONArray toJSONArray(String string, boolean keepStrings) throws JSONException {
- return (JSONArray)parse(new XMLTokener(string), true, null, keepStrings);
+ return (JSONArray)parse(new XMLTokener(string), true, null, keepStrings, 0);
+ }
+
+
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONArray using the JsonML transform. Each XML tag is represented as
+ * a JSONArray in which the first element is the tag name. If the tag has
+ * attributes, then the second element will be JSONObject containing the
+ * name/value pairs. If the tag contains children, then strings and
+ * JSONArrays will represent the child tags.
+ * As opposed to toJSONArray this method does not attempt to convert
+ * any text node or attribute value to any type
+ * but just leaves it as a string.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
+ * @param string The source string.
+ * @param config The parser configuration:
+ * JSONMLParserConfiguration.ORIGINAL is the default behaviour;
+ * JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean
+ * or numeric values and will instead be left as strings
+ * @return A JSONArray containing the structured data from the XML string.
+ * @throws JSONException Thrown on error converting to a JSONArray
+ */
+ public static JSONArray toJSONArray(String string, JSONMLParserConfiguration config) throws JSONException {
+ return (JSONArray)parse(new XMLTokener(string), true, null, config, 0);
}
@@ -279,10 +319,34 @@ public static JSONArray toJSONArray(String string, boolean keepStrings) throws J
* attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child content and tags.
- * As opposed to toJSONArray this method does not attempt to convert
- * any text node or attribute value to any type
+ * As opposed to toJSONArray this method does not attempt to convert
+ * any text node or attribute value to any type
* but just leaves it as a string.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
+ * @param x An XMLTokener.
+ * @param config The parser configuration:
+ * JSONMLParserConfiguration.ORIGINAL is the default behaviour;
+ * JSONMLParserConfiguration.KEEP_STRINGS means values will not be coerced into boolean
+ * or numeric values and will instead be left as strings
+ * @return A JSONArray containing the structured data from the XML string.
+ * @throws JSONException Thrown on error converting to a JSONArray
+ */
+ public static JSONArray toJSONArray(XMLTokener x, JSONMLParserConfiguration config) throws JSONException {
+ return (JSONArray)parse(x, true, null, config, 0);
+ }
+
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONArray using the JsonML transform. Each XML tag is represented as
+ * a JSONArray in which the first element is the tag name. If the tag has
+ * attributes, then the second element will be JSONObject containing the
+ * name/value pairs. If the tag contains children, then strings and
+ * JSONArrays will represent the child content and tags.
+ * As opposed to toJSONArray this method does not attempt to convert
+ * any text node or attribute value to any type
+ * but just leaves it as a string.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @param x An XMLTokener.
* @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings
@@ -290,7 +354,7 @@ public static JSONArray toJSONArray(String string, boolean keepStrings) throws J
* @throws JSONException Thrown on error converting to a JSONArray
*/
public static JSONArray toJSONArray(XMLTokener x, boolean keepStrings) throws JSONException {
- return (JSONArray)parse(x, true, null, keepStrings);
+ return (JSONArray)parse(x, true, null, keepStrings, 0);
}
@@ -301,13 +365,13 @@ public static JSONArray toJSONArray(XMLTokener x, boolean keepStrings) throws JS
* attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child content and tags.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @param x An XMLTokener.
* @return A JSONArray containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONArray
*/
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
- return (JSONArray)parse(x, true, null, false);
+ return (JSONArray)parse(x, true, null, false, 0);
}
@@ -319,16 +383,16 @@ public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
* contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @param string The XML source text.
* @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONObject
*/
public static JSONObject toJSONObject(String string) throws JSONException {
- return (JSONObject)parse(new XMLTokener(string), false, null, false);
+ return (JSONObject)parse(new XMLTokener(string), false, null, false, 0);
}
-
-
+
+
/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject using the JsonML transform. Each XML tag is represented as
@@ -337,7 +401,7 @@ public static JSONObject toJSONObject(String string) throws JSONException {
* contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @param string The XML source text.
* @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings
@@ -345,10 +409,32 @@ public static JSONObject toJSONObject(String string) throws JSONException {
* @throws JSONException Thrown on error converting to a JSONObject
*/
public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
- return (JSONObject)parse(new XMLTokener(string), false, null, keepStrings);
+ return (JSONObject)parse(new XMLTokener(string), false, null, keepStrings, 0);
+ }
+
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONObject using the JsonML transform. Each XML tag is represented as
+ * a JSONObject with a "tagName" property. If the tag has attributes, then
+ * the attributes will be in the JSONObject as properties. If the tag
+ * contains children, the object will have a "childNodes" property which
+ * will be an array of strings and JsonML JSONObjects.
+
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
+ * @param string The XML source text.
+ * @param config The parser configuration:
+ * JSONMLParserConfiguration.ORIGINAL is the default behaviour;
+ * JSONMLParserConfiguration.KEEP_STRINGS means 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 on error converting to a JSONObject
+ */
+ public static JSONObject toJSONObject(String string, JSONMLParserConfiguration config) throws JSONException {
+ return (JSONObject)parse(new XMLTokener(string), false, null, config, 0);
}
-
+
/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject using the JsonML transform. Each XML tag is represented as
@@ -357,13 +443,13 @@ public static JSONObject toJSONObject(String string, boolean keepStrings) throws
* contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @param x An XMLTokener of the XML source text.
* @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONObject
*/
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
- return (JSONObject)parse(x, false, null, false);
+ return (JSONObject)parse(x, false, null, false, 0);
}
@@ -375,7 +461,7 @@ public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
* contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects.
- * Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
* @param x An XMLTokener of the XML source text.
* @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings
@@ -383,7 +469,29 @@ public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
* @throws JSONException Thrown on error converting to a JSONObject
*/
public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings) throws JSONException {
- return (JSONObject)parse(x, false, null, keepStrings);
+ return (JSONObject)parse(x, false, null, keepStrings, 0);
+ }
+
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML string into a
+ * JSONObject using the JsonML transform. Each XML tag is represented as
+ * a JSONObject with a "tagName" property. If the tag has attributes, then
+ * the attributes will be in the JSONObject as properties. If the tag
+ * contains children, the object will have a "childNodes" property which
+ * will be an array of strings and JsonML JSONObjects.
+
+ * Comments, prologs, DTDs, and {@code <[ [ ]]>} are ignored.
+ * @param x An XMLTokener of the XML source text.
+ * @param config The parser configuration:
+ * JSONMLParserConfiguration.ORIGINAL is the default behaviour;
+ * JSONMLParserConfiguration.KEEP_STRINGS means 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 on error converting to a JSONObject
+ */
+ public static JSONObject toJSONObject(XMLTokener x, JSONMLParserConfiguration config) throws JSONException {
+ return (JSONObject)parse(x, false, null, config, 0);
}
@@ -416,10 +524,10 @@ public static String toString(JSONArray ja) throws JSONException {
// Emit the attributes
- for (final Entry entry : jo.entrySet()) {
- final String key = entry.getKey();
+ // Don't use the new entrySet API to maintain Android support
+ for (final String key : jo.keySet()) {
+ final Object value = jo.opt(key);
XML.noSpace(key);
- final Object value = entry.getValue();
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
@@ -464,6 +572,7 @@ public static String toString(JSONArray ja) throws JSONException {
return sb.toString();
}
+
/**
* Reverse the JSONML transformation, making an XML text from a JSONObject.
* The JSONObject must contain a "tagName" property. If it has children,
@@ -495,11 +604,11 @@ public static String toString(JSONObject jo) throws JSONException {
//Emit the attributes
- for (final Entry entry : jo.entrySet()) {
- final String key = entry.getKey();
+ // Don't use the new entrySet API to maintain Android support
+ for (final String key : jo.keySet()) {
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
XML.noSpace(key);
- value = entry.getValue();
+ value = jo.opt(key);
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
diff --git a/src/main/java/org/json/JSONMLParserConfiguration.java b/src/main/java/org/json/JSONMLParserConfiguration.java
new file mode 100644
index 000000000..43ba0db62
--- /dev/null
+++ b/src/main/java/org/json/JSONMLParserConfiguration.java
@@ -0,0 +1,69 @@
+package org.json;
+/*
+Public Domain.
+*/
+
+/**
+ * Configuration object for the XML to JSONML parser. The configuration is immutable.
+ */
+@SuppressWarnings({""})
+public class JSONMLParserConfiguration extends ParserConfiguration {
+
+ /**
+ * We can override the default maximum nesting depth if needed.
+ */
+ public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH;
+
+ /** Original Configuration of the XML to JSONML Parser. */
+ public static final JSONMLParserConfiguration ORIGINAL
+ = new JSONMLParserConfiguration();
+ /** Original configuration of the XML to JSONML Parser except that values are kept as strings. */
+ public static final JSONMLParserConfiguration KEEP_STRINGS
+ = new JSONMLParserConfiguration().withKeepStrings(true);
+
+ /**
+ * Default parser configuration. Does not keep strings (tries to implicitly convert values).
+ */
+ public JSONMLParserConfiguration() {
+ super();
+ this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
+ }
+
+ /**
+ * Configure the parser string processing and use the default CDATA Tag Name as "content".
+ * @param keepStrings true
to parse all values as string.
+ * false
to try and convert XML string values into a JSON value.
+ * @param maxNestingDepth int
to limit the nesting depth
+ */
+ protected JSONMLParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
+ super(keepStrings, maxNestingDepth);
+ }
+
+ /**
+ * Provides a new instance of the same configuration.
+ */
+ @Override
+ protected JSONMLParserConfiguration clone() {
+ // future modifications to this method should always ensure a "deep"
+ // clone in the case of collections. i.e. if a Map is added as a configuration
+ // item, a new map instance should be created and if possible each value in the
+ // map should be cloned as well. If the values of the map are known to also
+ // be immutable, then a shallow clone of the map is acceptable.
+ return new JSONMLParserConfiguration(
+ this.keepStrings,
+ this.maxNestingDepth
+ );
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public JSONMLParserConfiguration withKeepStrings(final boolean newVal) {
+ return super.withKeepStrings(newVal);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
+ return super.withMaxNestingDepth(maxNestingDepth);
+ }
+}
diff --git a/JSONObject.java b/src/main/java/org/json/JSONObject.java
similarity index 57%
rename from JSONObject.java
rename to src/main/java/org/json/JSONObject.java
index 1b7b0a10a..d85258696 100644
--- a/JSONObject.java
+++ b/src/main/java/org/json/JSONObject.java
@@ -1,49 +1,22 @@
package org.json;
-import java.io.Closeable;
-
/*
- 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.Closeable;
import java.io.IOException;
-import java.io.StringWriter;
import java.io.Writer;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Map;
+import java.util.*;
import java.util.Map.Entry;
-import java.util.ResourceBundle;
-import java.util.Set;
+import java.util.regex.Pattern;
/**
* A JSONObject is an unordered collection of name/value pairs. Its external
@@ -106,17 +79,6 @@ public class JSONObject {
*/
private static final class Null {
- /**
- * There is only intended to be a single instance of the NULL object,
- * so the clone method returns itself.
- *
- * @return NULL.
- */
- @Override
- protected final Object clone() {
- return this;
- }
-
/**
* A Null object is equal to the null value and to itself.
*
@@ -126,6 +88,7 @@ protected final Object clone() {
* null.
*/
@Override
+ @SuppressWarnings("lgtm[java/unchecked-cast-in-equals]")
public boolean equals(Object object) {
return object == null || object == this;
}
@@ -150,11 +113,26 @@ public String toString() {
}
}
+ /**
+ * Regular Expression Pattern that matches JSON Numbers. This is primarily used for
+ * output to guarantee that we are always writing valid JSON.
+ */
+ static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?");
+
/**
* The map where the JSONObject's properties are kept.
*/
private final Map map;
+ /**
+ * Retrieves the type of the underlying Map in this class.
+ *
+ * @return The class object representing the type of the underlying Map.
+ */
+ public Class extends Map> getMapType() {
+ return map.getClass();
+ }
+
/**
* It is sometimes more convenient and less ambiguous to have a
* NULL
object than to use Java's null
value.
@@ -167,10 +145,10 @@ public String toString() {
* Construct an empty JSONObject.
*/
public JSONObject() {
- // HashMap is used on purpose to ensure that elements are unordered by
+ // HashMap is used on purpose to ensure that elements are unordered by
// the specification.
- // JSON tends to be a portable transfer format to allows the container
- // implementations to rearrange their items for a faster element
+ // JSON tends to be a portable transfer format to allows the container
+ // implementations to rearrange their items for a faster element
// retrieval based on associative access.
// Therefore, an implementation mustn't rely on the order of the item.
this.map = new HashMap();
@@ -186,12 +164,12 @@ public JSONObject() {
* @param names
* An array of strings.
*/
- public JSONObject(JSONObject jo, String[] names) {
+ public JSONObject(JSONObject jo, String ... names) {
this(names.length);
for (int i = 0; i < names.length; i += 1) {
try {
this.putOnce(names[i], jo.opt(names[i]));
- } catch (Exception ignore) {
+ } catch (Exception ignore) { // exception thrown for missing key
}
}
}
@@ -206,61 +184,150 @@ public JSONObject(JSONObject jo, String[] names) {
* duplicated key.
*/
public JSONObject(JSONTokener x) throws JSONException {
+ this(x, x.getJsonParserConfiguration());
+ }
+
+ /**
+ * Construct a JSONObject from a JSONTokener with custom json parse configurations.
+ *
+ * @param x
+ * A JSONTokener object containing the source string.
+ * @param jsonParserConfiguration
+ * Variable to pass parser custom configuration for json parsing.
+ * @throws JSONException
+ * If there is a syntax error in the source string or a
+ * duplicated key.
+ */
+ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this();
- char c;
- String key;
+ boolean isInitial = x.getPrevious() == 0;
if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
}
for (;;) {
- c = x.nextClean();
- switch (c) {
+ if (parseJSONObject(x, jsonParserConfiguration, isInitial)) {
+ return;
+ }
+ }
+ }
+
+ /**
+ * Parses entirety of JSON object
+ *
+ * @param jsonTokener Parses text as tokens
+ * @param jsonParserConfiguration Variable to pass parser custom configuration for json parsing.
+ * @param isInitial True if start of document, else false
+ * @return True if done building object, else false
+ */
+ private boolean parseJSONObject(JSONTokener jsonTokener, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) {
+ Object obj;
+ String key;
+ boolean doneParsing = false;
+ char c = jsonTokener.nextClean();
+
+ switch (c) {
case 0:
- throw x.syntaxError("A JSONObject text must end with '}'");
+ throw jsonTokener.syntaxError("A JSONObject text must end with '}'");
case '}':
- return;
+ if (isInitial && jsonParserConfiguration.isStrictMode() && jsonTokener.nextClean() != 0) {
+ throw jsonTokener.syntaxError("Strict mode error: Unparsed characters found at end of input text");
+ }
+ return true;
default:
- x.back();
- key = x.nextValue().toString();
- }
+ obj = jsonTokener.nextSimpleValue(c);
+ key = obj.toString();
+ }
+
+ checkKeyForStrictMode(jsonTokener, jsonParserConfiguration, obj);
- // The key is followed by ':'.
+ // The key is followed by ':'.
+ c = jsonTokener.nextClean();
+ if (c != ':') {
+ throw jsonTokener.syntaxError("Expected a ':' after a key");
+ }
- c = x.nextClean();
- if (c != ':') {
- throw x.syntaxError("Expected a ':' after a key");
+ // Use syntaxError(..) to include error location
+ if (key != null) {
+ // Check if key exists
+ boolean keyExists = this.opt(key) != null;
+ if (keyExists && !jsonParserConfiguration.isOverwriteDuplicateKey()) {
+ throw jsonTokener.syntaxError("Duplicate key \"" + key + "\"");
}
-
- // Use syntaxError(..) to include error location
-
- if (key != null) {
- // Check if key exists
- if (this.opt(key) != null) {
- // key already exists
- throw x.syntaxError("Duplicate key \"" + key + "\"");
- }
- // Only add value if non-null
- Object value = x.nextValue();
- if (value!=null) {
- this.put(key, value);
- }
+
+ Object value = jsonTokener.nextValue();
+ // Only add value if non-null
+ if (value != null) {
+ this.put(key, value);
}
+ }
- // Pairs are separated by ','.
+ // Pairs are separated by ','.
+ if (parseEndOfKeyValuePair(jsonTokener, jsonParserConfiguration, isInitial)) {
+ doneParsing = true;
+ }
+
+ return doneParsing;
+ }
- switch (x.nextClean()) {
+ /**
+ * Checks for valid end of key:value pair
+ * @param jsonTokener Parses text as tokens
+ * @param jsonParserConfiguration Variable to pass parser custom configuration for json parsing.
+ * @param isInitial True if end of JSON object, else false
+ * @return
+ */
+ private static boolean parseEndOfKeyValuePair(JSONTokener jsonTokener, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) {
+ switch (jsonTokener.nextClean()) {
case ';':
+ // In strict mode semicolon is not a valid separator
+ if (jsonParserConfiguration.isStrictMode()) {
+ throw jsonTokener.syntaxError("Strict mode error: Invalid character ';' found");
+ }
+ break;
case ',':
- if (x.nextClean() == '}') {
- return;
+ if (jsonTokener.nextClean() == '}') {
+ // trailing commas are not allowed in strict mode
+ if (jsonParserConfiguration.isStrictMode()) {
+ throw jsonTokener.syntaxError("Strict mode error: Expected another object element");
+ }
+ // End of JSON object
+ return true;
}
- x.back();
+ if (jsonTokener.end()) {
+ throw jsonTokener.syntaxError("A JSONObject text must end with '}'");
+ }
+ jsonTokener.back();
break;
case '}':
- return;
+ if (isInitial && jsonParserConfiguration.isStrictMode() && jsonTokener.nextClean() != 0) {
+ throw jsonTokener.syntaxError("Strict mode error: Unparsed characters found at end of input text");
+ }
+ // End of JSON object
+ return true;
default:
- throw x.syntaxError("Expected a ',' or '}'");
+ throw jsonTokener.syntaxError("Expected a ',' or '}'");
+ }
+ // Not at end of JSON object
+ return false;
+ }
+
+ /**
+ * Throws error if key violates strictMode
+ * @param jsonTokener Parses text as tokens
+ * @param jsonParserConfiguration Variable to pass parser custom configuration for json parsing.
+ * @param obj Value to be checked
+ */
+ private static void checkKeyForStrictMode(JSONTokener jsonTokener, JSONParserConfiguration jsonParserConfiguration, Object obj) {
+ if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) {
+ if(obj instanceof Boolean) {
+ throw jsonTokener.syntaxError(String.format("Strict mode error: key '%s' cannot be boolean", obj.toString()));
+ }
+ if(obj == JSONObject.NULL) {
+ throw jsonTokener.syntaxError(String.format("Strict mode error: key '%s' cannot be null", obj.toString()));
+ }
+ if(obj instanceof Number) {
+ throw jsonTokener.syntaxError(String.format("Strict mode error: key '%s' cannot be number", obj.toString()));
}
}
}
@@ -271,16 +338,47 @@ public JSONObject(JSONTokener x) throws JSONException {
* @param m
* A map object that can be used to initialize the contents of
* the JSONObject.
+ * @throws JSONException
+ * If a value in the map is non-finite number.
+ * @throws NullPointerException
+ * If a key in the map is null
*/
public JSONObject(Map, ?> m) {
+ this(m, 0, new JSONParserConfiguration());
+ }
+
+ /**
+ * Construct a JSONObject from a Map with custom json parse configurations.
+ *
+ * @param m
+ * A map object that can be used to initialize the contents of
+ * the JSONObject.
+ * @param jsonParserConfiguration
+ * Variable to pass parser custom configuration for json parsing.
+ */
+ public JSONObject(Map, ?> m, JSONParserConfiguration jsonParserConfiguration) {
+ this(m, 0, jsonParserConfiguration);
+ }
+
+ /**
+ * Construct a JSONObject from a map with recursion depth.
+ */
+ private JSONObject(Map, ?> m, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
+ if (recursionDepth > jsonParserConfiguration.getMaxNestingDepth()) {
+ throw new JSONException("JSONObject has reached recursion depth limit of " + jsonParserConfiguration.getMaxNestingDepth());
+ }
if (m == null) {
this.map = new HashMap();
} else {
this.map = new HashMap(m.size());
for (final Entry, ?> e : m.entrySet()) {
+ if(e.getKey() == null) {
+ throw new NullPointerException("Null key.");
+ }
final Object value = e.getValue();
- if (value != null) {
- this.map.put(String.valueOf(e.getKey()), wrap(value));
+ if (value != null || jsonParserConfiguration.isUseNativeNulls()) {
+ testValidity(value);
+ this.map.put(String.valueOf(e.getKey()), wrap(value, recursionDepth + 1, jsonParserConfiguration));
}
}
}
@@ -298,21 +396,66 @@ public JSONObject(Map, ?> m) {
* prefix. If the second remaining character is not upper case, then the
* first character is converted to lower case.
*
+ * Methods that are static
, return void
,
+ * have parameters, or are "bridge" methods, are ignored.
+ *
* For example, if an object has a method named "getName"
, and
* if the result of calling object.getName()
is
* "Larry Fine"
, then the JSONObject will contain
* "name": "Larry Fine"
.
*
- * Methods that return void
as well as static
- * methods are ignored.
- *
+ * The {@link JSONPropertyName} annotation can be used on a bean getter to
+ * override key name used in the JSONObject. For example, using the object
+ * above with the getName
method, if we annotated it with:
+ *
+ * @JSONPropertyName("FullName")
+ * public String getName() { return this.name; }
+ *
+ * The resulting JSON object would contain "FullName": "Larry Fine"
+ *
+ * Similarly, the {@link JSONPropertyName} annotation can be used on non-
+ * get
and is
methods. We can also override key
+ * name used in the JSONObject as seen below even though the field would normally
+ * be ignored:
+ *
+ * @JSONPropertyName("FullName")
+ * public String fullName() { return this.name; }
+ *
+ * The resulting JSON object would contain "FullName": "Larry Fine"
+ *
+ * The {@link JSONPropertyIgnore} annotation can be used to force the bean property
+ * to not be serialized into JSON. If both {@link JSONPropertyIgnore} and
+ * {@link JSONPropertyName} are defined on the same method, a depth comparison is
+ * performed and the one closest to the concrete class being serialized is used.
+ * If both annotations are at the same level, then the {@link JSONPropertyIgnore}
+ * annotation takes precedent and the field is not serialized.
+ * For example, the following declaration would prevent the getName
+ * method from being serialized:
+ *
+ * @JSONPropertyName("FullName")
+ * @JSONPropertyIgnore
+ * public String getName() { return this.name; }
+ *
+ *
* @param bean
* An object that has getter methods that should be used to make
* a JSONObject.
+ * @throws JSONException
+ * If a getter returned a non-finite number.
*/
public JSONObject(Object bean) {
this();
- this.populateMap(bean);
+ this.populateMap(bean, new JSONParserConfiguration());
+ }
+
+ public JSONObject(Object bean, JSONParserConfiguration jsonParserConfiguration) {
+ this();
+ this.populateMap(bean, jsonParserConfiguration);
+ }
+
+ private JSONObject(Object bean, Set objectsRecord) {
+ this();
+ this.populateMap(bean, objectsRecord, new JSONParserConfiguration());
}
/**
@@ -329,7 +472,7 @@ public JSONObject(Object bean) {
* An array of strings, the names of the fields to be obtained
* from the object.
*/
- public JSONObject(Object object, String names[]) {
+ public JSONObject(Object object, String ... names) {
this(names.length);
Class> c = object.getClass();
for (int i = 0; i < names.length; i += 1) {
@@ -354,7 +497,25 @@ public JSONObject(Object object, String names[]) {
* duplicated key.
*/
public JSONObject(String source) throws JSONException {
- this(new JSONTokener(source));
+ this(source, new JSONParserConfiguration());
+ }
+
+ /**
+ * Construct a JSONObject from a source JSON text string with custom json parse configurations.
+ * This is the most commonly used JSONObject constructor.
+ *
+ * @param source
+ * A string beginning with {
(left
+ * brace) and ending with }
+ * (right brace) .
+ * @param jsonParserConfiguration
+ * Variable to pass parser custom configuration for json parsing.
+ * @exception JSONException
+ * If there is a syntax error in the source string or a
+ * duplicated key.
+ */
+ public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
+ this(new JSONTokener(source, jsonParserConfiguration), jsonParserConfiguration);
}
/**
@@ -399,12 +560,12 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
}
}
}
-
+
/**
- * Constructor to specify an initial capacity of the internal map. Useful for library
+ * Constructor to specify an initial capacity of the internal map. Useful for library
* internal calls where we know, or at least can best guess, how big this JSONObject
* will be.
- *
+ *
* @param initialCapacity initial capacity of the internal map.
*/
protected JSONObject(int initialCapacity){
@@ -428,7 +589,9 @@ protected JSONObject(int initialCapacity){
* An object to be accumulated under the key.
* @return this.
* @throws JSONException
- * If the value is an invalid number or if the key is null.
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject accumulate(String key, Object value) throws JSONException {
testValidity(value);
@@ -457,8 +620,10 @@ public JSONObject accumulate(String key, Object value) throws JSONException {
* An object to be accumulated under the key.
* @return this.
* @throws JSONException
- * If the key is null or if the current value associated with
+ * If the value is non-finite number or if the current value associated with
* the key is not a JSONArray.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject append(String key, Object value) throws JSONException {
testValidity(value);
@@ -468,8 +633,7 @@ public JSONObject append(String key, Object value) throws JSONException {
} else if (object instanceof JSONArray) {
this.put(key, ((JSONArray) object).put(value));
} else {
- throw new JSONException("JSONObject[" + key
- + "] is not a JSONArray.");
+ throw wrongValueFormatException(key, "JSONArray", null, null);
}
return this;
}
@@ -523,26 +687,26 @@ public Object get(String key) throws JSONException {
}
/**
- * Get the enum value associated with a key.
- *
- * @param clazz
- * The type of enum to retrieve.
- * @param key
- * A key string.
- * @return The enum value associated with the key
- * @throws JSONException
- * if the key is not found or if the value cannot be converted
- * to an enum.
- */
+ * Get the enum value associated with a key.
+ *
+ * @param
+ * Enum Type
+ * @param clazz
+ * The type of enum to retrieve.
+ * @param key
+ * A key string.
+ * @return The enum value associated with the key
+ * @throws JSONException
+ * if the key is not found or if the value cannot be converted
+ * to an enum.
+ */
public > E getEnum(Class clazz, String key) throws JSONException {
E val = optEnum(clazz, key);
if(val==null) {
// JSONException should really take a throwable argument.
// If it did, I would re-implement this with the Enum.valueOf
// method and place any thrown exception in the JSONException
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not an enum of type " + quote(clazz.getSimpleName())
- + ".");
+ throw wrongValueFormatException(key, "enum of type " + quote(clazz.getSimpleName()), opt(key), null);
}
return val;
}
@@ -559,17 +723,14 @@ public > E getEnum(Class clazz, String key) throws JSONExce
*/
public boolean getBoolean(String key) throws JSONException {
Object object = this.get(key);
- if (object.equals(Boolean.FALSE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("false"))) {
+ if (Boolean.FALSE.equals(object)
+ || (object instanceof String && "false".equalsIgnoreCase((String) object))) {
return false;
- } else if (object.equals(Boolean.TRUE)
- || (object instanceof String && ((String) object)
- .equalsIgnoreCase("true"))) {
+ } else if (Boolean.TRUE.equals(object)
+ || (object instanceof String && "true".equalsIgnoreCase((String) object))) {
return true;
}
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not a Boolean.");
+ throw wrongValueFormatException(key, "Boolean", object, null);
}
/**
@@ -579,21 +740,23 @@ public boolean getBoolean(String key) throws JSONException {
* A key string.
* @return The numeric value.
* @throws JSONException
- * if the key is not found or if the value cannot
+ * if the key is not found or if the value cannot
* be converted to BigInteger.
*/
public BigInteger getBigInteger(String key) throws JSONException {
Object object = this.get(key);
- try {
- return new BigInteger(object.toString());
- } catch (Exception e) {
- throw new JSONException("JSONObject[" + quote(key)
- + "] could not be converted to BigInteger.", e);
+ BigInteger ret = objectToBigInteger(object, null);
+ if (ret != null) {
+ return ret;
}
+ throw wrongValueFormatException(key, "BigInteger", object, null);
}
/**
- * Get the BigDecimal value associated with a key.
+ * Get the BigDecimal value associated with a key. If the value is float or
+ * double, the {@link BigDecimal#BigDecimal(double)} constructor will
+ * be used. See notes on the constructor for conversion issues that may
+ * arise.
*
* @param key
* A key string.
@@ -604,15 +767,11 @@ public BigInteger getBigInteger(String key) throws JSONException {
*/
public BigDecimal getBigDecimal(String key) throws JSONException {
Object object = this.get(key);
- if (object instanceof BigDecimal) {
- return (BigDecimal)object;
- }
- try {
- return new BigDecimal(object.toString());
- } catch (Exception e) {
- throw new JSONException("JSONObject[" + quote(key)
- + "] could not be converted to BigDecimal.", e);
+ BigDecimal ret = objectToBigDecimal(object, null);
+ if (ret != null) {
+ return ret;
}
+ throw wrongValueFormatException(key, "BigDecimal", object, null);
}
/**
@@ -626,13 +785,14 @@ public BigDecimal getBigDecimal(String key) throws JSONException {
* object and cannot be converted to a number.
*/
public double getDouble(String key) throws JSONException {
- Object object = this.get(key);
+ final Object object = this.get(key);
+ if(object instanceof Number) {
+ return ((Number)object).doubleValue();
+ }
try {
- return object instanceof Number ? ((Number) object).doubleValue()
- : Double.parseDouble(object.toString());
+ return Double.parseDouble(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not a number.", e);
+ throw wrongValueFormatException(key, "double", object, e);
}
}
@@ -647,13 +807,14 @@ public double getDouble(String key) throws JSONException {
* object and cannot be converted to a number.
*/
public float getFloat(String key) throws JSONException {
- Object object = this.get(key);
+ final Object object = this.get(key);
+ if(object instanceof Number) {
+ return ((Number)object).floatValue();
+ }
try {
- return object instanceof Number ? ((Number) object).floatValue()
- : Float.parseFloat(object.toString());
+ return Float.parseFloat(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not a number.", e);
+ throw wrongValueFormatException(key, "float", object, e);
}
}
@@ -675,8 +836,7 @@ public Number getNumber(String key) throws JSONException {
}
return stringToNumber(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not a number.", e);
+ throw wrongValueFormatException(key, "number", object, e);
}
}
@@ -691,13 +851,14 @@ public Number getNumber(String key) throws JSONException {
* to an integer.
*/
public int getInt(String key) throws JSONException {
- Object object = this.get(key);
+ final Object object = this.get(key);
+ if(object instanceof Number) {
+ return ((Number)object).intValue();
+ }
try {
- return object instanceof Number ? ((Number) object).intValue()
- : Integer.parseInt((String) object);
+ return Integer.parseInt(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not an int.", e);
+ throw wrongValueFormatException(key, "int", object, e);
}
}
@@ -715,8 +876,7 @@ public JSONArray getJSONArray(String key) throws JSONException {
if (object instanceof JSONArray) {
return (JSONArray) object;
}
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not a JSONArray.");
+ throw wrongValueFormatException(key, "JSONArray", object, null);
}
/**
@@ -733,8 +893,7 @@ public JSONObject getJSONObject(String key) throws JSONException {
if (object instanceof JSONObject) {
return (JSONObject) object;
}
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not a JSONObject.");
+ throw wrongValueFormatException(key, "JSONObject", object, null);
}
/**
@@ -748,32 +907,36 @@ public JSONObject getJSONObject(String key) throws JSONException {
* to a long.
*/
public long getLong(String key) throws JSONException {
- Object object = this.get(key);
+ final Object object = this.get(key);
+ if(object instanceof Number) {
+ return ((Number)object).longValue();
+ }
try {
- return object instanceof Number ? ((Number) object).longValue()
- : Long.parseLong((String) object);
+ return Long.parseLong(object.toString());
} catch (Exception e) {
- throw new JSONException("JSONObject[" + quote(key)
- + "] is not a long.", e);
+ throw wrongValueFormatException(key, "long", object, e);
}
}
/**
* Get an array of field names from a JSONObject.
*
+ * @param jo
+ * JSON object
* @return An array of field names, or null if there are no names.
*/
public static String[] getNames(JSONObject jo) {
- int length = jo.length();
- if (length == 0) {
+ if (jo.isEmpty()) {
return null;
}
- return jo.keySet().toArray(new String[length]);
+ return jo.keySet().toArray(new String[jo.length()]);
}
/**
- * Get an array of field names from an Object.
+ * Get an array of public field names from an Object.
*
+ * @param object
+ * object to read
* @return An array of field names, or null if there are no names.
*/
public static String[] getNames(Object object) {
@@ -807,7 +970,7 @@ public String getString(String key) throws JSONException {
if (object instanceof String) {
return (String) object;
}
- throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
+ throw wrongValueFormatException(key, "string", object, null);
}
/**
@@ -823,8 +986,11 @@ public boolean has(String key) {
/**
* Increment a property of a JSONObject. If there is no such property,
- * create one with a value of 1. If there is such a property, and if it is
- * an Integer, Long, Double, or Float, then add one to it.
+ * create one with a value of 1 (Integer). If there is such a property, and if it is
+ * an Integer, Long, Double, Float, BigInteger, or BigDecimal then add one to it.
+ * No overflow bounds checking is performed, so callers should initialize the key
+ * prior to this call with an appropriate type that can handle the maximum expected
+ * value.
*
* @param key
* A key string.
@@ -837,18 +1003,18 @@ public JSONObject increment(String key) throws JSONException {
Object value = this.opt(key);
if (value == null) {
this.put(key, 1);
- } else if (value instanceof BigInteger) {
- this.put(key, ((BigInteger)value).add(BigInteger.ONE));
- } else if (value instanceof BigDecimal) {
- this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
} else if (value instanceof Integer) {
this.put(key, ((Integer) value).intValue() + 1);
} else if (value instanceof Long) {
this.put(key, ((Long) value).longValue() + 1L);
- } else if (value instanceof Double) {
- this.put(key, ((Double) value).doubleValue() + 1.0d);
+ } else if (value instanceof BigInteger) {
+ this.put(key, ((BigInteger)value).add(BigInteger.ONE));
} else if (value instanceof Float) {
this.put(key, ((Float) value).floatValue() + 1.0f);
+ } else if (value instanceof Double) {
+ this.put(key, ((Double) value).doubleValue() + 1.0d);
+ } else if (value instanceof BigDecimal) {
+ this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
} else {
throw new JSONException("Unable to increment [" + quote(key) + "].");
}
@@ -856,13 +1022,13 @@ public JSONObject increment(String key) throws JSONException {
}
/**
- * Determine if the value associated with the key is null or if there is no
+ * Determine if the value associated with the key is null
or if there is no
* value.
*
* @param key
* A key string.
* @return true if there is no value associated with the key or if the value
- * is the JSONObject.NULL object.
+ * is the JSONObject.NULL object.
*/
public boolean isNull(String key) {
return JSONObject.NULL.equals(this.opt(key));
@@ -873,7 +1039,7 @@ public boolean isNull(String key) {
* modify the JSONObject. Use with caution.
*
* @see Set#iterator()
- *
+ *
* @return An iterator of the keys.
*/
public Iterator keys() {
@@ -894,10 +1060,10 @@ public Set keySet() {
/**
* Get a set of entries of the JSONObject. These are raw values and may not
- * match what is returned by the JSONObject get* and opt* functions. Modifying
+ * match what is returned by the JSONObject get* and opt* functions. Modifying
* the returned EntrySet or the Entry objects contained therein will modify the
* backing JSONObject. This does not return a clone or a read-only view.
- *
+ *
* Use with caution.
*
* @see Map#entrySet()
@@ -917,12 +1083,29 @@ public int length() {
return this.map.size();
}
+ /**
+ * Removes all of the elements from this JSONObject.
+ * The JSONObject will be empty after this call returns.
+ */
+ public void clear() {
+ this.map.clear();
+ }
+
+ /**
+ * Check if JSONObject is empty.
+ *
+ * @return true if JSONObject is empty, otherwise false.
+ */
+ public boolean isEmpty() {
+ return this.map.isEmpty();
+ }
+
/**
* Produce a JSONArray containing the names of the elements of this
* JSONObject.
*
* @return A JSONArray containing the key strings, or null if the JSONObject
- * is empty.
+ * is empty.
*/
public JSONArray names() {
if(this.map.isEmpty()) {
@@ -974,7 +1157,9 @@ public Object opt(String key) {
/**
* Get the enum value associated with a key.
- *
+ *
+ * @param
+ * Enum Type
* @param clazz
* The type of enum to retrieve.
* @param key
@@ -987,7 +1172,9 @@ public > E optEnum(Class clazz, String key) {
/**
* Get the enum value associated with a key.
- *
+ *
+ * @param
+ * Enum Type
* @param clazz
* The type of enum to retrieve.
* @param key
@@ -1056,10 +1243,52 @@ public boolean optBoolean(String key, boolean defaultValue) {
}
}
+ /**
+ * Get an optional boolean object associated with a key. It returns false if there
+ * is no such key, or if the value is not Boolean.TRUE or the String "true".
+ *
+ * @param key
+ * A key string.
+ * @return The truth.
+ */
+ public Boolean optBooleanObject(String key) {
+ return this.optBooleanObject(key, false);
+ }
+
+ /**
+ * Get an optional boolean object associated with a key. It returns the
+ * defaultValue if there is no such key, or if it is not a Boolean or the
+ * String "true" or "false" (case insensitive).
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return The truth.
+ */
+ public Boolean optBooleanObject(String key, Boolean defaultValue) {
+ Object val = this.opt(key);
+ if (NULL.equals(val)) {
+ return defaultValue;
+ }
+ if (val instanceof Boolean){
+ return ((Boolean) val).booleanValue();
+ }
+ try {
+ // we'll use the get anyway because it does string conversion.
+ return this.getBoolean(key);
+ } catch (Exception e) {
+ return defaultValue;
+ }
+ }
+
/**
* Get an optional BigDecimal associated with a key, or the defaultValue if
* there is no such key or if its value is not a number. If the value is a
- * string, an attempt will be made to evaluate it as a number.
+ * string, an attempt will be made to evaluate it as a number. If the value
+ * is float or double, then the {@link BigDecimal#BigDecimal(double)}
+ * constructor will be used. See notes on the constructor for conversion
+ * issues that may arise.
*
* @param key
* A key string.
@@ -1069,6 +1298,28 @@ public boolean optBoolean(String key, boolean defaultValue) {
*/
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
Object val = this.opt(key);
+ return objectToBigDecimal(val, defaultValue);
+ }
+
+ /**
+ * @param val value to convert
+ * @param defaultValue default value to return is the conversion doesn't work or is null.
+ * @return BigDecimal conversion of the original value, or the defaultValue if unable
+ * to convert.
+ */
+ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
+ return objectToBigDecimal(val, defaultValue, true);
+ }
+
+ /**
+ * @param val value to convert
+ * @param defaultValue default value to return is the conversion doesn't work or is null.
+ * @param exact When true
, then {@link Double} and {@link Float} values will be converted exactly.
+ * When false
, they will be converted to {@link String} values before converting to {@link BigDecimal}.
+ * @return BigDecimal conversion of the original value, or the defaultValue if unable
+ * to convert.
+ */
+ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolean exact) {
if (NULL.equals(val)) {
return defaultValue;
}
@@ -1079,7 +1330,16 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
return new BigDecimal((BigInteger) val);
}
if (val instanceof Double || val instanceof Float){
- return new BigDecimal(((Number) val).doubleValue());
+ if (!numberIsFinite((Number)val)) {
+ return defaultValue;
+ }
+ if (exact) {
+ return new BigDecimal(((Number)val).doubleValue());
+ }
+ // use the string constructor so that we maintain "nice" values for doubles and floats
+ // the double constructor will translate doubles to "exact" values instead of the likely
+ // intended representation
+ return new BigDecimal(val.toString());
}
if (val instanceof Long || val instanceof Integer
|| val instanceof Short || val instanceof Byte){
@@ -1106,6 +1366,16 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
*/
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
Object val = this.opt(key);
+ return objectToBigInteger(val, defaultValue);
+ }
+
+ /**
+ * @param val value to convert
+ * @param defaultValue default value to return is the conversion doesn't work or is null.
+ * @return BigInteger conversion of the original value, or the defaultValue if unable
+ * to convert.
+ */
+ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
if (NULL.equals(val)) {
return defaultValue;
}
@@ -1116,6 +1386,9 @@ public BigInteger optBigInteger(String key, BigInteger defaultValue) {
return ((BigDecimal) val).toBigInteger();
}
if (val instanceof Double || val instanceof Float){
+ if (!numberIsFinite((Number)val)) {
+ return defaultValue;
+ }
return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
}
if (val instanceof Long || val instanceof Integer
@@ -1124,7 +1397,7 @@ public BigInteger optBigInteger(String key, BigInteger defaultValue) {
}
// don't check if it's a string in case of unchecked Number subclasses
try {
- // the other opt functions handle implicit conversions, i.e.
+ // the other opt functions handle implicit conversions, i.e.
// jo.put("double",1.1d);
// jo.optInt("double"); -- will return 1, not an error
// this conversion to BigDecimal then to BigInteger is to maintain
@@ -1164,25 +1437,47 @@ public double optDouble(String key) {
* @return An object which is the value.
*/
public double optDouble(String key, double defaultValue) {
- Object val = this.opt(key);
- if (NULL.equals(val)) {
+ Number val = this.optNumber(key);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).doubleValue();
- }
- if (val instanceof String) {
- try {
- return Double.parseDouble((String) val);
- } catch (Exception e) {
- return defaultValue;
- }
+ return val.doubleValue();
+ }
+
+ /**
+ * Get an optional Double object associated with a key, or NaN if there is no such
+ * key or if its value is not a number. If the value is a string, an attempt
+ * will be made to evaluate it as a number.
+ *
+ * @param key
+ * A string which is the key.
+ * @return An object which is the value.
+ */
+ public Double optDoubleObject(String key) {
+ return this.optDoubleObject(key, Double.NaN);
+ }
+
+ /**
+ * Get an optional Double object associated with a key, or the defaultValue if
+ * there is no such key or if its value is not a number. If the value is a
+ * string, an attempt will be made to evaluate it as a number.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return An object which is the value.
+ */
+ public Double optDoubleObject(String key, Double defaultValue) {
+ Number val = this.optNumber(key);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+ return val.doubleValue();
}
/**
- * Get the optional double value associated with an index. NaN is returned
+ * Get the optional float value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
@@ -1195,7 +1490,7 @@ public float optFloat(String key) {
}
/**
- * Get the optional double value associated with an index. The defaultValue
+ * Get the optional float value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
@@ -1206,21 +1501,51 @@ public float optFloat(String key) {
* @return The value.
*/
public float optFloat(String key, float defaultValue) {
- Object val = this.opt(key);
- if (JSONObject.NULL.equals(val)) {
+ Number val = this.optNumber(key);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).floatValue();
- }
- if (val instanceof String) {
- try {
- return Float.parseFloat((String) val);
- } catch (Exception e) {
- return defaultValue;
- }
+ final float floatValue = val.floatValue();
+ // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
+ // return defaultValue;
+ // }
+ return floatValue;
+ }
+
+ /**
+ * Get the optional Float object associated with an index. NaN is returned
+ * if there is no value for the index, or if the value is not a number and
+ * cannot be converted to a number.
+ *
+ * @param key
+ * A key string.
+ * @return The object.
+ */
+ public Float optFloatObject(String key) {
+ return this.optFloatObject(key, Float.NaN);
+ }
+
+ /**
+ * Get the optional Float object associated with an index. The defaultValue
+ * is returned if there is no value for the index, or if the value is not a
+ * number and cannot be converted to a number.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default object.
+ * @return The object.
+ */
+ public Float optFloatObject(String key, Float defaultValue) {
+ Number val = this.optNumber(key);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+ final Float floatValue = val.floatValue();
+ // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
+ // return defaultValue;
+ // }
+ return floatValue;
}
/**
@@ -1248,22 +1573,43 @@ public int optInt(String key) {
* @return An object which is the value.
*/
public int optInt(String key, int defaultValue) {
- Object val = this.opt(key);
- if (NULL.equals(val)) {
+ final Number val = this.optNumber(key, null);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).intValue();
- }
-
- if (val instanceof String) {
- try {
- return new BigDecimal((String) val).intValue();
- } catch (Exception e) {
- return defaultValue;
- }
+ return val.intValue();
+ }
+
+ /**
+ * Get an optional Integer object associated with a key, or zero if there is no
+ * such key or if the value is not a number. If the value is a string, an
+ * attempt will be made to evaluate it as a number.
+ *
+ * @param key
+ * A key string.
+ * @return An object which is the value.
+ */
+ public Integer optIntegerObject(String key) {
+ return this.optIntegerObject(key, 0);
+ }
+
+ /**
+ * Get an optional Integer object associated with a key, or the default if there
+ * is no such key or if the value is not a number. If the value is a string,
+ * an attempt will be made to evaluate it as a number.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return An object which is the value.
+ */
+ public Integer optIntegerObject(String key, Integer defaultValue) {
+ final Number val = this.optNumber(key, null);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+ return val.intValue();
}
/**
@@ -1275,8 +1621,22 @@ public int optInt(String key, int defaultValue) {
* @return A JSONArray which is the value.
*/
public JSONArray optJSONArray(String key) {
- Object o = this.opt(key);
- return o instanceof JSONArray ? (JSONArray) o : null;
+ return this.optJSONArray(key, null);
+ }
+
+ /**
+ * Get an optional JSONArray associated with a key, or the default if there
+ * is no such key, or if its value is not a JSONArray.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return A JSONArray which is the value.
+ */
+ public JSONArray optJSONArray(String key, JSONArray defaultValue) {
+ Object object = this.opt(key);
+ return object instanceof JSONArray ? (JSONArray) object : defaultValue;
}
/**
@@ -1287,9 +1647,21 @@ public JSONArray optJSONArray(String key) {
* A key string.
* @return A JSONObject which is the value.
*/
- public JSONObject optJSONObject(String key) {
+ public JSONObject optJSONObject(String key) { return this.optJSONObject(key, null); }
+
+ /**
+ * Get an optional JSONObject associated with a key, or the default if there
+ * is no such key or if the value is not a JSONObject.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return An JSONObject which is the value.
+ */
+ public JSONObject optJSONObject(String key, JSONObject defaultValue) {
Object object = this.opt(key);
- return object instanceof JSONObject ? (JSONObject) object : null;
+ return object instanceof JSONObject ? (JSONObject) object : defaultValue;
}
/**
@@ -1317,24 +1689,47 @@ public long optLong(String key) {
* @return An object which is the value.
*/
public long optLong(String key, long defaultValue) {
- Object val = this.opt(key);
- if (NULL.equals(val)) {
+ final Number val = this.optNumber(key, null);
+ if (val == null) {
return defaultValue;
}
- if (val instanceof Number){
- return ((Number) val).longValue();
- }
-
- if (val instanceof String) {
- try {
- return new BigDecimal((String) val).longValue();
- } catch (Exception e) {
- return defaultValue;
- }
+
+ return val.longValue();
+ }
+
+ /**
+ * Get an optional Long object associated with a key, or zero if there is no
+ * such key or if the value is not a number. If the value is a string, an
+ * attempt will be made to evaluate it as a number.
+ *
+ * @param key
+ * A key string.
+ * @return An object which is the value.
+ */
+ public Long optLongObject(String key) {
+ return this.optLongObject(key, 0L);
+ }
+
+ /**
+ * Get an optional Long object associated with a key, or the default if there
+ * is no such key or if the value is not a number. If the value is a string,
+ * an attempt will be made to evaluate it as a number.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return An object which is the value.
+ */
+ public Long optLongObject(String key, Long defaultValue) {
+ final Number val = this.optNumber(key, null);
+ if (val == null) {
+ return defaultValue;
}
- return defaultValue;
+
+ return val.longValue();
}
-
+
/**
* Get an optional {@link Number} value associated with a key, or null
* if there is no such key or if the value is not a number. If the value is a string,
@@ -1369,17 +1764,14 @@ public Number optNumber(String key, Number defaultValue) {
if (val instanceof Number){
return (Number) val;
}
-
- if (val instanceof String) {
- try {
- return stringToNumber((String) val);
- } catch (Exception e) {
- return defaultValue;
- }
+
+ try {
+ return stringToNumber(val.toString());
+ } catch (Exception e) {
+ return defaultValue;
}
- return defaultValue;
}
-
+
/**
* Get an optional string associated with a key. It returns an empty string
* if there is no such key. If the value is not a string and is not null,
@@ -1409,70 +1801,289 @@ public String optString(String key, String defaultValue) {
}
/**
- * Populates the internal map of the JSONObject with the bean properties.
- * The bean can not be recursive.
+ * Populates the internal map of the JSONObject with the bean properties. The
+ * bean can not be recursive.
*
* @see JSONObject#JSONObject(Object)
*
* @param bean
* the bean
+ * @throws JSONException
+ * If a getter returned a non-finite number.
*/
- private void populateMap(Object bean) {
- Class> klass = bean.getClass();
+ private void populateMap(Object bean, JSONParserConfiguration jsonParserConfiguration) {
+ populateMap(bean, Collections.newSetFromMap(new IdentityHashMap()), jsonParserConfiguration);
+ }
-// If klass is a System class then set includeSuperClass to false.
+ /**
+ * Convert a bean into a json object
+ * @param bean object tobe converted
+ * @param objectsRecord set of all objects for this method
+ * @param jsonParserConfiguration json parser settings
+ */
+ private void populateMap(Object bean, Set objectsRecord, JSONParserConfiguration jsonParserConfiguration) {
+ Class> klass = bean.getClass();
- boolean includeSuperClass = klass.getClassLoader() != null;
+ // If klass is a System class then set includeSuperClass to false.
- Method[] methods = includeSuperClass ? klass.getMethods() : klass
- .getDeclaredMethods();
+ Method[] methods = getMethods(klass);
for (final Method method : methods) {
- final int modifiers = method.getModifiers();
- if (Modifier.isPublic(modifiers)
- && !Modifier.isStatic(modifiers)
- && method.getParameterTypes().length == 0
- && !method.isBridge()
- && method.getReturnType() != Void.TYPE ) {
- final String name = method.getName();
- String key;
- if (name.startsWith("get")) {
- if ("getClass".equals(name) || "getDeclaringClass".equals(name)) {
- continue;
- }
- key = name.substring(3);
- } else if (name.startsWith("is")) {
- key = name.substring(2);
- } else {
- continue;
+ if (isValidMethod(method)) {
+ final String key = getKeyNameFromMethod(method);
+ if (key != null && !key.isEmpty()) {
+ processMethod(bean, objectsRecord, jsonParserConfiguration, method, key);
}
- if (key.length() > 0
- && Character.isUpperCase(key.charAt(0))) {
- if (key.length() == 1) {
- key = key.toLowerCase(Locale.ROOT);
- } else if (!Character.isUpperCase(key.charAt(1))) {
- key = key.substring(0, 1).toLowerCase(Locale.ROOT)
- + key.substring(1);
- }
+ }
+ }
+ }
- try {
- final Object result = method.invoke(bean);
- if (result != null) {
- this.map.put(key, wrap(result));
- // we don't use the result anywhere outside of wrap
- // if it's a resource we should be sure to close it after calling toString
- if(result instanceof Closeable) {
- try {
- ((Closeable)result).close();
- } catch (IOException ignore) {
- }
- }
- }
- } catch (IllegalAccessException ignore) {
- } catch (IllegalArgumentException ignore) {
- } catch (InvocationTargetException ignore) {
- }
+ /**
+ * Processes method into json object entry if appropriate
+ * @param bean object being processed (owns the method)
+ * @param objectsRecord set of all objects for this method
+ * @param jsonParserConfiguration json parser settings
+ * @param method method being processed
+ * @param key name of the method
+ */
+ private void processMethod(Object bean, Set objectsRecord, JSONParserConfiguration jsonParserConfiguration,
+ Method method, String key) {
+ try {
+ final Object result = method.invoke(bean);
+ if (result != null || jsonParserConfiguration.isUseNativeNulls()) {
+ // check cyclic dependency and throw error if needed
+ // the wrap and populateMap combination method is
+ // itself DFS recursive
+ if (objectsRecord.contains(result)) {
+ throw recursivelyDefinedObjectException(key);
+ }
+
+ objectsRecord.add(result);
+
+ testValidity(result);
+ this.map.put(key, wrap(result, objectsRecord));
+
+ objectsRecord.remove(result);
+
+ closeClosable(result);
+ }
+ } catch (IllegalAccessException ignore) {
+ // ignore exception
+ } catch (IllegalArgumentException ignore) {
+ // ignore exception
+ } catch (InvocationTargetException ignore) {
+ // ignore exception
+ }
+ }
+
+ /**
+ * This is a convenience method to simplify populate maps
+ * @param klass the name of the object being checked
+ * @return methods of klass
+ */
+ private static Method[] getMethods(Class> klass) {
+ boolean includeSuperClass = klass.getClassLoader() != null;
+
+ return includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();
+ }
+
+ private static boolean isValidMethodName(String name) {
+ return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
+ }
+
+ private static String getKeyNameFromMethod(Method method) {
+ final int ignoreDepth = getAnnotationDepth(method, JSONPropertyIgnore.class);
+ if (ignoreDepth > 0) {
+ final int forcedNameDepth = getAnnotationDepth(method, JSONPropertyName.class);
+ if (forcedNameDepth < 0 || ignoreDepth <= forcedNameDepth) {
+ // the hierarchy asked to ignore, and the nearest name override
+ // was higher or non-existent
+ return null;
+ }
+ }
+ JSONPropertyName annotation = getAnnotation(method, JSONPropertyName.class);
+ if (annotationValueNotEmpty(annotation)) {
+ return annotation.value();
+ }
+ String key;
+ final String name = method.getName();
+ if (name.startsWith("get") && name.length() > 3) {
+ key = name.substring(3);
+ } else if (name.startsWith("is") && name.length() > 2) {
+ key = name.substring(2);
+ } else {
+ return null;
+ }
+ // if the first letter in the key is not uppercase, then skip.
+ // This is to maintain backwards compatibility before PR406
+ // (https://github.com/stleary/JSON-java/pull/406/)
+ if (key.length() == 0 || Character.isLowerCase(key.charAt(0))) {
+ return null;
+ }
+ if (key.length() == 1) {
+ key = key.toLowerCase(Locale.ROOT);
+ } else if (!Character.isUpperCase(key.charAt(1))) {
+ key = key.substring(0, 1).toLowerCase(Locale.ROOT) + key.substring(1);
+ }
+ return key;
+ }
+
+ /**
+ * checks if the annotation is not null and the {@link JSONPropertyName#value()} is not null and is not empty.
+ * @param annotation the annotation to check
+ * @return true if the annotation and the value is not null and not empty, false otherwise.
+ */
+ private static boolean annotationValueNotEmpty(JSONPropertyName annotation) {
+ return annotation != null && annotation.value() != null && !annotation.value().isEmpty();
+ }
+
+ /**
+ * Checks if the method is valid for the {@link #populateMap(Object, Set, JSONParserConfiguration)} use case
+ * @param method the Method to check
+ * @return true, if valid, false otherwise.
+ */
+ private static boolean isValidMethod(Method method) {
+ final int modifiers = method.getModifiers();
+ return Modifier.isPublic(modifiers)
+ && !Modifier.isStatic(modifiers)
+ && method.getParameterTypes().length == 0
+ && !method.isBridge()
+ && method.getReturnType() != Void.TYPE
+ && isValidMethodName(method.getName());
+ }
+
+ /**
+ * calls {@link Closeable#close()} on the input, if it is an instance of Closable.
+ * @param input the input to close, if possible.
+ */
+ private static void closeClosable(Object input) {
+ // we don't use the result anywhere outside of wrap
+ // if it's a resource we should be sure to close it
+ // after calling toString
+ if (input instanceof Closeable) {
+ try {
+ ((Closeable) input).close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+
+ /**
+ * Searches the class hierarchy to see if the method or it's super
+ * implementations and interfaces has the annotation.
+ *
+ * @param
+ * type of the annotation
+ *
+ * @param m
+ * method to check
+ * @param annotationClass
+ * annotation to look for
+ * @return the {@link Annotation} if the annotation exists on the current method
+ * or one of its super class definitions
+ */
+ private static A getAnnotation(final Method m, final Class annotationClass) {
+ // if we have invalid data the result is null
+ if (m == null || annotationClass == null) {
+ return null;
+ }
+
+ if (m.isAnnotationPresent(annotationClass)) {
+ return m.getAnnotation(annotationClass);
+ }
+
+ // if we've already reached the Object class, return null;
+ Class> c = m.getDeclaringClass();
+ if (c.getSuperclass() == null) {
+ return null;
+ }
+
+ // check directly implemented interfaces for the method being checked
+ for (Class> i : c.getInterfaces()) {
+ try {
+ Method im = i.getMethod(m.getName(), m.getParameterTypes());
+ return getAnnotation(im, annotationClass);
+ } catch (final SecurityException ex) {
+ continue;
+ } catch (final NoSuchMethodException ex) {
+ continue;
+ }
+ }
+
+ //If the superclass is Object, no annotations will be found any more
+ if (Object.class.equals(c.getSuperclass()))
+ return null;
+
+ try {
+ return getAnnotation(
+ c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
+ annotationClass);
+ } catch (final SecurityException ex) {
+ return null;
+ } catch (final NoSuchMethodException ex) {
+ return null;
+ }
+ }
+
+ /**
+ * Searches the class hierarchy to see if the method or it's super
+ * implementations and interfaces has the annotation. Returns the depth of the
+ * annotation in the hierarchy.
+ *
+ * @param m
+ * method to check
+ * @param annotationClass
+ * annotation to look for
+ * @return Depth of the annotation or -1 if the annotation is not on the method.
+ */
+ private static int getAnnotationDepth(final Method m, final Class extends Annotation> annotationClass) {
+ // if we have invalid data the result is -1
+ if (m == null || annotationClass == null) {
+ return -1;
+ }
+
+ if (m.isAnnotationPresent(annotationClass)) {
+ return 1;
+ }
+
+ // if we've already reached the Object class, return -1;
+ Class> c = m.getDeclaringClass();
+ if (c.getSuperclass() == null) {
+ return -1;
+ }
+
+ // check directly implemented interfaces for the method being checked
+ for (Class> i : c.getInterfaces()) {
+ try {
+ Method im = i.getMethod(m.getName(), m.getParameterTypes());
+ int d = getAnnotationDepth(im, annotationClass);
+ if (d > 0) {
+ // since the annotation was on the interface, add 1
+ return d + 1;
}
+ } catch (final SecurityException ex) {
+ continue;
+ } catch (final NoSuchMethodException ex) {
+ continue;
+ }
+ }
+
+ //If the superclass is Object, no annotations will be found any more
+ if (Object.class.equals(c.getSuperclass()))
+ return -1;
+
+ try {
+ int d = getAnnotationDepth(
+ c.getSuperclass().getMethod(m.getName(), m.getParameterTypes()),
+ annotationClass);
+ if (d > 0) {
+ // since the annotation was on the superclass, add 1
+ return d + 1;
}
+ return -1;
+ } catch (final SecurityException ex) {
+ return -1;
+ } catch (final NoSuchMethodException ex) {
+ return -1;
}
}
@@ -1485,11 +2096,12 @@ private void populateMap(Object bean) {
* A boolean which is the value.
* @return this.
* @throws JSONException
- * If the key is null.
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, boolean value) throws JSONException {
- this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
- return this;
+ return this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
}
/**
@@ -1502,10 +2114,12 @@ public JSONObject put(String key, boolean value) throws JSONException {
* A Collection value.
* @return this.
* @throws JSONException
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, Collection> value) throws JSONException {
- this.put(key, new JSONArray(value));
- return this;
+ return this.put(key, new JSONArray(value));
}
/**
@@ -1517,13 +2131,14 @@ public JSONObject put(String key, Collection> value) throws JSONException {
* A double which is the value.
* @return this.
* @throws JSONException
- * If the key is null or if the number is invalid.
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, double value) throws JSONException {
- this.put(key, Double.valueOf(value));
- return this;
+ return this.put(key, Double.valueOf(value));
}
-
+
/**
* Put a key/float pair in the JSONObject.
*
@@ -1533,11 +2148,12 @@ public JSONObject put(String key, double value) throws JSONException {
* A float which is the value.
* @return this.
* @throws JSONException
- * If the key is null or if the number is invalid.
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, float value) throws JSONException {
- this.put(key, Float.valueOf(value));
- return this;
+ return this.put(key, Float.valueOf(value));
}
/**
@@ -1549,11 +2165,12 @@ public JSONObject put(String key, float value) throws JSONException {
* An int which is the value.
* @return this.
* @throws JSONException
- * If the key is null.
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, int value) throws JSONException {
- this.put(key, Integer.valueOf(value));
- return this;
+ return this.put(key, Integer.valueOf(value));
}
/**
@@ -1565,11 +2182,12 @@ public JSONObject put(String key, int value) throws JSONException {
* A long which is the value.
* @return this.
* @throws JSONException
- * If the key is null.
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, long value) throws JSONException {
- this.put(key, Long.valueOf(value));
- return this;
+ return this.put(key, Long.valueOf(value));
}
/**
@@ -1582,14 +2200,16 @@ public JSONObject put(String key, long value) throws JSONException {
* A Map value.
* @return this.
* @throws JSONException
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, Map, ?> value) throws JSONException {
- this.put(key, new JSONObject(value));
- return this;
+ return this.put(key, new JSONObject(value));
}
/**
- * Put a key/value pair in the JSONObject. If the value is null, then the
+ * Put a key/value pair in the JSONObject. If the value is null
, then the
* key will be removed from the JSONObject if it is present.
*
* @param key
@@ -1600,7 +2220,9 @@ public JSONObject put(String key, Map, ?> value) throws JSONException {
* String, or the JSONObject.NULL object.
* @return this.
* @throws JSONException
- * If the value is non-finite number or if the key is null.
+ * If the value is non-finite number.
+ * @throws NullPointerException
+ * If the key is null
.
*/
public JSONObject put(String key, Object value) throws JSONException {
if (key == null) {
@@ -1620,8 +2242,10 @@ public JSONObject put(String key, Object value) throws JSONException {
* are both non-null, and only if there is not already a member with that
* name.
*
- * @param key string
- * @param value object
+ * @param key
+ * key to insert into
+ * @param value
+ * value to insert
* @return this.
* @throws JSONException
* if the key is a duplicate
@@ -1631,7 +2255,7 @@ public JSONObject putOnce(String key, Object value) throws JSONException {
if (this.opt(key) != null) {
throw new JSONException("Duplicate key \"" + key + "\"");
}
- this.put(key, value);
+ return this.put(key, value);
}
return this;
}
@@ -1652,13 +2276,13 @@ public JSONObject putOnce(String key, Object value) throws JSONException {
*/
public JSONObject putOpt(String key, Object value) throws JSONException {
if (key != null && value != null) {
- this.put(key, value);
+ return this.put(key, value);
}
return this;
}
/**
- * Creates a JSONPointer using an initialization string and tries to
+ * Creates a JSONPointer using an initialization string and tries to
* match it to an item within this JSONObject. For example, given a
* JSONObject initialized with this document:
*
@@ -1666,13 +2290,13 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
* "a":{"b":"c"}
* }
*
- * and this JSONPointer string:
+ * and this JSONPointer string:
*
* "/a/b"
*
* Then this method will return the String "c".
* A JSONPointerException may be thrown from code called by this method.
- *
+ *
* @param jsonPointer string that can be used to create a JSONPointer
* @return the item matched by the JSONPointer, otherwise null
*/
@@ -1680,7 +2304,7 @@ public Object query(String jsonPointer) {
return query(new JSONPointer(jsonPointer));
}
/**
- * Uses a user initialized JSONPointer and tries to
+ * Uses a user initialized JSONPointer and tries to
* match it to an item within this JSONObject. For example, given a
* JSONObject initialized with this document:
*
@@ -1688,24 +2312,24 @@ public Object query(String jsonPointer) {
* "a":{"b":"c"}
* }
*
- * and this JSONPointer:
+ * and this JSONPointer:
*
* "/a/b"
*
* Then this method will return the String "c".
* A JSONPointerException may be thrown from code called by this method.
- *
+ *
* @param jsonPointer string that can be used to create a JSONPointer
* @return the item matched by the JSONPointer, otherwise null
*/
public Object query(JSONPointer jsonPointer) {
return jsonPointer.queryFrom(this);
}
-
+
/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
- *
+ *
* @param jsonPointer the string representation of the JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
@@ -1713,11 +2337,11 @@ public Object query(JSONPointer jsonPointer) {
public Object optQuery(String jsonPointer) {
return optQuery(new JSONPointer(jsonPointer));
}
-
+
/**
* Queries and returns a value from this object using {@code jsonPointer}, or
* returns null if the query fails due to a missing key.
- *
+ *
* @param jsonPointer The JSON pointer
* @return the queried value or {@code null}
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
@@ -1732,28 +2356,39 @@ public Object optQuery(JSONPointer jsonPointer) {
/**
* Produce a string in double quotes with backslash sequences in all the
- * right places. A backslash will be inserted within , producing <\/,
- * allowing JSON text to be delivered in HTML. In JSON text, a string cannot
- * contain a control character or an unescaped quote or backslash.
+ * right places. A backslash will be inserted within </, producing
+ * <\/, allowing JSON text to be delivered in HTML. In JSON text, a
+ * string cannot contain a control character or an unescaped quote or
+ * backslash.
*
* @param string
* A String
* @return A String correctly formatted for insertion in a JSON text.
*/
+ @SuppressWarnings("resource")
public static String quote(String string) {
- StringWriter sw = new StringWriter();
- synchronized (sw.getBuffer()) {
- try {
- return quote(string, sw).toString();
- } catch (IOException ignored) {
- // will never happen - we are writing to a string writer
- return "";
- }
+ if (string == null || string.isEmpty()) {
+ return "\"\"";
+ }
+ Writer sw = new StringBuilderWriter(string.length() + 2);
+ try {
+ return quote(string, sw).toString();
+ } catch (IOException ignored) {
+ // will never happen - we are writing to a string writer
+ return "";
}
}
+ /**
+ * Quotes a string and appends the result to a given Writer.
+ *
+ * @param string The input string to be quoted.
+ * @param w The Writer to which the quoted string will be appended.
+ * @return The same Writer instance after appending the quoted string.
+ * @throws IOException If an I/O error occurs while writing to the Writer.
+ */
public static Writer quote(String string, Writer w) throws IOException {
- if (string == null || string.length() == 0) {
+ if (string == null || string.isEmpty()) {
w.write("\"\"");
return w;
}
@@ -1844,7 +2479,7 @@ public boolean similar(Object other) {
Object valueThis = entry.getValue();
Object valueOther = ((JSONObject)other).get(name);
if(valueThis == valueOther) {
- return true;
+ continue;
}
if(valueThis == null) {
return false;
@@ -1857,6 +2492,14 @@ public boolean similar(Object other) {
if (!((JSONArray)valueThis).similar(valueOther)) {
return false;
}
+ } else if (valueThis instanceof Number && valueOther instanceof Number) {
+ if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
+ return false;
+ }
+ } else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
+ if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
+ return false;
+ }
} else if (!valueThis.equals(valueOther)) {
return false;
}
@@ -1866,10 +2509,59 @@ public boolean similar(Object other) {
return false;
}
}
-
+
+ /**
+ * Compares two numbers to see if they are similar.
+ *
+ * If either of the numbers are Double or Float instances, then they are checked to have
+ * a finite value. If either value is not finite (NaN or ±infinity), then this
+ * function will always return false. If both numbers are finite, they are first checked
+ * to be the same type and implement {@link Comparable}. If they do, then the actual
+ * {@link Comparable#compareTo(Object)} is called. If they are not the same type, or don't
+ * implement Comparable, then they are converted to {@link BigDecimal}s. Finally the
+ * BigDecimal values are compared using {@link BigDecimal#compareTo(BigDecimal)}.
+ *
+ * @param l the Left value to compare. Can not be null
.
+ * @param r the right value to compare. Can not be null
.
+ * @return true if the numbers are similar, false otherwise.
+ */
+ static boolean isNumberSimilar(Number l, Number r) {
+ if (!numberIsFinite(l) || !numberIsFinite(r)) {
+ // non-finite numbers are never similar
+ return false;
+ }
+
+ // if the classes are the same and implement Comparable
+ // then use the built in compare first.
+ if(l.getClass().equals(r.getClass()) && l instanceof Comparable) {
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ int compareTo = ((Comparable)l).compareTo(r);
+ return compareTo==0;
+ }
+
+ // BigDecimal should be able to handle all of our number types that we support through
+ // documentation. Convert to BigDecimal first, then use the Compare method to
+ // decide equality.
+ final BigDecimal lBigDecimal = objectToBigDecimal(l, null, false);
+ final BigDecimal rBigDecimal = objectToBigDecimal(r, null, false);
+ if (lBigDecimal == null || rBigDecimal == null) {
+ return false;
+ }
+ return lBigDecimal.compareTo(rBigDecimal) == 0;
+ }
+
+ private static boolean numberIsFinite(Number n) {
+ if (n instanceof Double && (((Double) n).isInfinite() || ((Double) n).isNaN())) {
+ return false;
+ } else if (n instanceof Float && (((Float) n).isInfinite() || ((Float) n).isNaN())) {
+ return false;
+ }
+ return true;
+ }
+
/**
* Tests if the value should be tried as a decimal. It makes no test if there are actual digits.
- *
+ *
* @param val value to test
* @return true if the string is "-0" or if it contains '.', 'e', or 'E', false otherwise.
*/
@@ -1877,90 +2569,32 @@ protected static boolean isDecimalNotation(final String val) {
return val.indexOf('.') > -1 || val.indexOf('e') > -1
|| val.indexOf('E') > -1 || "-0".equals(val);
}
-
- /**
- * Converts a string to a number using the narrowest possible type. Possible
- * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer.
- * When a Double is returned, it should always be a valid Double and not NaN or +-infinity.
- *
- * @param val value to convert
- * @return Number representation of the value.
- * @throws NumberFormatException thrown if the value is not a valid number. A public
- * caller should catch this and wrap it in a {@link JSONException} if applicable.
- */
- protected static Number stringToNumber(final String val) throws NumberFormatException {
- char initial = val.charAt(0);
- if ((initial >= '0' && initial <= '9') || initial == '-') {
- // decimal representation
- if (isDecimalNotation(val)) {
- // quick dirty way to see if we need a BigDecimal instead of a Double
- // this only handles some cases of overflow or underflow
- if (val.length()>14) {
- return new BigDecimal(val);
- }
- final Double d = Double.valueOf(val);
- if (d.isInfinite() || d.isNaN()) {
- // if we can't parse it as a double, go up to BigDecimal
- // this is probably due to underflow like 4.32e-678
- // or overflow like 4.65e5324. The size of the string is small
- // but can't be held in a Double.
- return new BigDecimal(val);
- }
- return d;
- }
- // integer representation.
- // This will narrow any values to the smallest reasonable Object representation
- // (Integer, Long, or BigInteger)
-
- // string version
- // The compare string length method reduces GC,
- // but leads to smaller integers being placed in larger wrappers even though not
- // needed. i.e. 1,000,000,000 -> Long even though it's an Integer
- // 1,000,000,000,000,000,000 -> BigInteger even though it's a Long
- //if(val.length()<=9){
- // return Integer.valueOf(val);
- //}
- //if(val.length()<=18){
- // return Long.valueOf(val);
- //}
- //return new BigInteger(val);
-
- // BigInteger version: We use a similar bitLenth 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. Which is the better tradeoff? This is closer to what's
- // in stringToValue.
- BigInteger bi = new BigInteger(val);
- if(bi.bitLength()<=31){
- return Integer.valueOf(bi.intValue());
- }
- if(bi.bitLength()<=63){
- return Long.valueOf(bi.longValue());
- }
- return bi;
- }
- throw new NumberFormatException("val ["+val+"] is not a valid number.");
- }
/**
* Try to convert a string into a number, boolean, or null. If the string
* can't be converted, return the string.
*
* @param string
- * A String.
+ * A String. can not be null.
* @return A simple JSON value.
+ * @throws NullPointerException
+ * Thrown if the string is null.
*/
+ // Changes to this method must be copied to the corresponding method in
+ // the XML class to keep full support for Android
public static Object stringToValue(String string) {
- if (string.equals("")) {
+ if ("".equals(string)) {
return string;
}
- if (string.equalsIgnoreCase("true")) {
+
+ // check JSON key words true/false/null
+ if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE;
}
- if (string.equalsIgnoreCase("false")) {
+ if ("false".equalsIgnoreCase(string)) {
return Boolean.FALSE;
}
- if (string.equalsIgnoreCase("null")) {
+ if ("null".equalsIgnoreCase(string)) {
return JSONObject.NULL;
}
@@ -1972,26 +2606,81 @@ public static Object stringToValue(String string) {
char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') {
try {
- // if we want full Big Number support this block can be replaced with:
- // return stringToNumber(string);
- if (isDecimalNotation(string)) {
- Double d = Double.valueOf(string);
- if (!d.isInfinite() && !d.isNaN()) {
- return d;
+ return stringToNumber(string);
+ } catch (Exception ignore) {
+ }
+ }
+ return string;
+ }
+
+ /**
+ * Converts a string to a number using the narrowest possible type. Possible
+ * returns for this function are BigDecimal, Double, BigInteger, Long, and Integer.
+ * When a Double is returned, it should always be a valid Double and not NaN or +-infinity.
+ *
+ * @param val value to convert
+ * @return Number representation of the value.
+ * @throws NumberFormatException thrown if the value is not a valid number. A public
+ * caller should catch this and wrap it in a {@link JSONException} if applicable.
+ */
+ protected static Number stringToNumber(final String val) throws NumberFormatException {
+ char initial = val.charAt(0);
+ if ((initial >= '0' && initial <= '9') || initial == '-') {
+ // decimal representation
+ if (isDecimalNotation(val)) {
+ // Use a BigDecimal all the time so we keep the original
+ // representation. BigDecimal doesn't support -0.0, ensure we
+ // keep that by forcing a decimal.
+ try {
+ BigDecimal bd = new BigDecimal(val);
+ if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
+ return Double.valueOf(-0.0);
}
- } else {
- Long myLong = Long.valueOf(string);
- if (string.equals(myLong.toString())) {
- if (myLong.longValue() == myLong.intValue()) {
- return Integer.valueOf(myLong.intValue());
+ return bd;
+ } catch (NumberFormatException retryAsDouble) {
+ // 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.");
}
- return myLong;
+ return d;
+ } catch (NumberFormatException ignore) {
+ throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
}
- } catch (Exception ignore) {
}
+ // block items like 00 01 etc. Java number parsers treat these as Octal.
+ 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.");
+ }
+ } 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.");
+ }
+ }
+ // 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){
+ return Integer.valueOf(bi.intValue());
+ }
+ if(bi.bitLength() <= 63){
+ return Long.valueOf(bi.longValue());
+ }
+ return bi;
}
- return string;
+ throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
/**
@@ -2003,18 +2692,8 @@ public static Object stringToValue(String string) {
* If o is a non-finite number.
*/
public static void testValidity(Object o) throws JSONException {
- if (o != null) {
- if (o instanceof Double) {
- if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
- throw new JSONException(
- "JSON does not allow non-finite numbers.");
- }
- } else if (o instanceof Float) {
- if (((Float) o).isInfinite() || ((Float) o).isNaN()) {
- throw new JSONException(
- "JSON does not allow non-finite numbers.");
- }
- }
+ if (o instanceof Number && !numberIsFinite((Number) o)) {
+ throw new JSONException("JSON does not allow non-finite numbers.");
}
}
@@ -2030,7 +2709,7 @@ public static void testValidity(Object o) throws JSONException {
* If any of the values are non-finite numbers.
*/
public JSONArray toJSONArray(JSONArray names) throws JSONException {
- if (names == null || names.length() == 0) {
+ if (names == null || names.isEmpty()) {
return null;
}
JSONArray ja = new JSONArray();
@@ -2047,7 +2726,7 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
*
* Warning: This method assumes that the data structure is acyclical.
*
- *
+ *
* @return a printable, displayable, portable, transmittable representation
* of the object, beginning with {
(left
* brace) and ending with }
(right
@@ -2064,17 +2743,17 @@ public String toString() {
/**
* Make a pretty-printed JSON text of this JSONObject.
- *
- *
If indentFactor > 0
and the {@link JSONObject}
+ *
+ *
If
{@code indentFactor > 0} and the {@link JSONObject}
* has only one key, then the object will be output on a single line:
* {@code {"key": 1}}
- *
+ *
* If an object has 2 or more keys, then it will be output across
- * multiple lines: {
+ * multiple lines: {@code {
* "key1": 1,
* "key2": "value 2",
* "key3": 3
- * }
+ * }}
*
* Warning: This method assumes that the data structure is acyclical.
*
@@ -2088,11 +2767,13 @@ public String toString() {
* @throws JSONException
* If the object contains an invalid number.
*/
+ @SuppressWarnings("resource")
public String toString(int indentFactor) throws JSONException {
- StringWriter w = new StringWriter();
- synchronized (w.getBuffer()) {
- return this.write(w, indentFactor, 0).toString();
- }
+ // 6 characters are the minimum to serialise a key value pair e.g.: "k":1,
+ // and we don't want to oversize the initial capacity
+ int initialSize = map.size() * 6;
+ Writer w = new StringBuilderWriter(Math.max(initialSize, 16));
+ return this.write(w, indentFactor, 0).toString();
}
/**
@@ -2120,59 +2801,15 @@ public String toString(int indentFactor) throws JSONException {
* If the value is or contains an invalid number.
*/
public static String valueToString(Object value) throws JSONException {
- if (value == null || value.equals(null)) {
- return "null";
- }
- if (value instanceof JSONString) {
- Object object;
- try {
- object = ((JSONString) value).toJSONString();
- } catch (Exception e) {
- throw new JSONException(e);
- }
- if (object instanceof String) {
- return (String) object;
- }
- throw new JSONException("Bad value from toJSONString: " + object);
- }
- if (value instanceof Number) {
- // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
- final String numberAsString = numberToString((Number) value);
- try {
- // Use the BigDecimal constructor for it's parser to validate the format.
- @SuppressWarnings("unused")
- BigDecimal unused = new BigDecimal(numberAsString);
- // Close enough to a JSON number that we will return it unquoted
- return numberAsString;
- } catch (NumberFormatException ex){
- // The Number value is not a valid JSON number.
- // Instead we will quote it as a string
- return quote(numberAsString);
- }
- }
- if (value instanceof Boolean || value instanceof JSONObject
- || value instanceof JSONArray) {
- return value.toString();
- }
- if (value instanceof Map) {
- Map, ?> map = (Map, ?>) value;
- return new JSONObject(map).toString();
- }
- if (value instanceof Collection) {
- Collection> coll = (Collection>) value;
- return new JSONArray(coll).toString();
- }
- if (value.getClass().isArray()) {
- return new JSONArray(value).toString();
- }
- if(value instanceof Enum>){
- return quote(((Enum>)value).name());
- }
- return quote(value.toString());
+ // moves the implementation to JSONWriter as:
+ // 1. It makes more sense to be part of the writer class
+ // 2. For Android support this method is not available. By implementing it in the Writer
+ // Android users can use the writer with the built in Android JSONObject implementation.
+ return JSONWriter.valueToString(value);
}
/**
- * Wrap an object, if necessary. If the object is null, return the NULL
+ * Wrap an object, if necessary. If the object is null
, return the NULL
* object. If it is an array or collection, wrap it in a JSONArray. If it is
* a map, wrap it in a JSONObject. If it is a standard property (Double,
* String, et al) then it is already wrapped. Otherwise, if it comes from
@@ -2184,31 +2821,59 @@ public static String valueToString(Object value) throws JSONException {
* @return The wrapped value
*/
public static Object wrap(Object object) {
+ return wrap(object, null);
+ }
+
+ /**
+ * Wrap an object, if necessary. If the object is null
, return the NULL
+ * object. If it is an array or collection, wrap it in a JSONArray. If it is
+ * a map, wrap it in a JSONObject. If it is a standard property (Double,
+ * String, et al) then it is already wrapped. Otherwise, if it comes from
+ * one of the java packages, turn it into a string. And if it doesn't, try
+ * to wrap it in a JSONObject. If the wrapping fails, then null is returned.
+ *
+ * @param object
+ * The object to wrap
+ * @param recursionDepth
+ * Variable for tracking the count of nested object creations.
+ * @param jsonParserConfiguration
+ * Variable to pass parser custom configuration for json parsing.
+ * @return The wrapped value
+ */
+ static Object wrap(Object object, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
+ return wrap(object, null, recursionDepth, jsonParserConfiguration);
+ }
+
+ private static Object wrap(Object object, Set objectsRecord) {
+ return wrap(object, objectsRecord, 0, new JSONParserConfiguration());
+ }
+
+ private static Object wrap(Object object, Set objectsRecord, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {
try {
- if (object == null) {
+ if (NULL.equals(object)) {
return NULL;
}
if (object instanceof JSONObject || object instanceof JSONArray
- || NULL.equals(object) || object instanceof JSONString
+ || object instanceof JSONString || object instanceof String
|| object instanceof Byte || object instanceof Character
|| object instanceof Short || object instanceof Integer
|| object instanceof Long || object instanceof Boolean
|| object instanceof Float || object instanceof Double
- || object instanceof String || object instanceof BigInteger
- || object instanceof BigDecimal || object instanceof Enum) {
+ || object instanceof BigInteger || object instanceof BigDecimal
+ || object instanceof Enum) {
return object;
}
if (object instanceof Collection) {
Collection> coll = (Collection>) object;
- return new JSONArray(coll);
+ return new JSONArray(coll, recursionDepth, jsonParserConfiguration);
}
if (object.getClass().isArray()) {
return new JSONArray(object);
}
if (object instanceof Map) {
Map, ?> map = (Map, ?>) object;
- return new JSONObject(map);
+ return new JSONObject(map, recursionDepth, jsonParserConfiguration);
}
Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? objectPackage
@@ -2218,7 +2883,13 @@ public static Object wrap(Object object) {
|| object.getClass().getClassLoader() == null) {
return object.toString();
}
+ if (objectsRecord != null) {
+ return new JSONObject(object, objectsRecord);
+ }
return new JSONObject(object);
+ }
+ catch (JSONException exception) {
+ throw exception;
} catch (Exception exception) {
return null;
}
@@ -2230,19 +2901,21 @@ public static Object wrap(Object object) {
*
* Warning: This method assumes that the data structure is acyclical.
*
- *
+ * @param writer the writer object
* @return The writer.
- * @throws JSONException
+ * @throws JSONException if a called function has an error
*/
public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
}
+ @SuppressWarnings("resource")
static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent) throws JSONException, IOException {
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof JSONString) {
+ // JSONString must be checked first, so it can overwrite behaviour of other types below
Object o;
try {
o = ((JSONString) value).toJSONString();
@@ -2250,16 +2923,16 @@ static final Writer writeValue(Writer writer, Object value,
throw new JSONException(e);
}
writer.write(o != null ? o.toString() : quote(value.toString()));
+ } else if (value instanceof String) {
+ // assuming most values are Strings, so testing it early
+ quote(value.toString(), writer);
+ return writer;
} else if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
final String numberAsString = numberToString((Number) value);
- try {
- // Use the BigDecimal constructor for it's parser to validate the format.
- @SuppressWarnings("unused")
- BigDecimal testNum = new BigDecimal(numberAsString);
- // Close enough to a JSON number that we will use it unquoted
+ if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
writer.write(numberAsString);
- } catch (NumberFormatException ex){
+ } else {
// The Number value is not a valid JSON number.
// Instead we will quote it as a string
quote(numberAsString, writer);
@@ -2294,17 +2967,17 @@ static final void indent(Writer writer, int indent) throws IOException {
/**
* Write the contents of the JSONObject as JSON text to a writer.
- *
- *
If indentFactor > 0
and the {@link JSONObject}
+ *
+ *
If
{@code indentFactor > 0} and the {@link JSONObject}
* has only one key, then the object will be output on a single line:
* {@code {"key": 1}}
- *
+ *
* If an object has 2 or more keys, then it will be output across
- * multiple lines: {
+ * multiple lines: {@code {
* "key1": 1,
* "key2": "value 2",
* "key3": 3
- * }
+ * }}
*
* Warning: This method assumes that the data structure is acyclical.
*
@@ -2316,12 +2989,14 @@ static final void indent(Writer writer, int indent) throws IOException {
* @param indent
* The indentation of the top level.
* @return The writer.
- * @throws JSONException
+ * @throws JSONException if a called function has an error or a write error
+ * occurs
*/
+ @SuppressWarnings("resource")
public Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
try {
- boolean commanate = false;
+ boolean needsComma = false;
final int length = this.length();
writer.write('{');
@@ -2339,15 +3014,15 @@ public Writer write(Writer writer, int indentFactor, int indent)
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
}
} else if (length != 0) {
- final int newindent = indent + indentFactor;
+ final int newIndent = indent + indentFactor;
for (final Entry entry : this.entrySet()) {
- if (commanate) {
+ if (needsComma) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
- indent(writer, newindent);
+ indent(writer, newIndent);
final String key = entry.getKey();
writer.write(quote(key));
writer.write(':');
@@ -2355,11 +3030,11 @@ public Writer write(Writer writer, int indentFactor, int indent)
writer.write(' ');
}
try {
- writeValue(writer, entry.getValue(), indentFactor, newindent);
+ writeValue(writer, entry.getValue(), indentFactor, newIndent);
} catch (Exception e) {
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
}
- commanate = true;
+ needsComma = true;
}
if (indentFactor > 0) {
writer.write('\n');
@@ -2399,4 +3074,44 @@ public Map toMap() {
}
return results;
}
+
+ /**
+ * Create a new JSONException in a common format for incorrect conversions.
+ * @param key name of the key
+ * @param valueType the type of value being coerced to
+ * @param cause optional cause of the coercion failure
+ * @return JSONException that can be thrown.
+ */
+ private static JSONException wrongValueFormatException(
+ String key,
+ String valueType,
+ Object value,
+ Throwable cause) {
+ if(value == null) {
+
+ return new JSONException(
+ "JSONObject[" + quote(key) + "] is not a " + valueType + " (null)."
+ , cause);
+ }
+ // don't try to toString collections or known object types that could be large.
+ if(value instanceof Map || value instanceof Iterable || value instanceof JSONObject) {
+ return new JSONException(
+ "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + ")."
+ , cause);
+ }
+ return new JSONException(
+ "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")."
+ , cause);
+ }
+
+ /**
+ * Create a new JSONException in a common format for recursive object definition.
+ * @param key name of the key
+ * @return JSONException that can be thrown.
+ */
+ private static JSONException recursivelyDefinedObjectException(String key) {
+ return new JSONException(
+ "JavaBean object contains recursively defined member variable of key " + quote(key)
+ );
+ }
}
diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java
new file mode 100644
index 000000000..0cfa2eaef
--- /dev/null
+++ b/src/main/java/org/json/JSONParserConfiguration.java
@@ -0,0 +1,152 @@
+package org.json;
+
+/**
+ * Configuration object for the JSON parser. The configuration is immutable.
+ */
+public class JSONParserConfiguration extends ParserConfiguration {
+ /**
+ * Used to indicate whether to overwrite duplicate key or not.
+ */
+ private boolean overwriteDuplicateKey;
+
+ /**
+ * Used to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps.
+ */
+ private boolean useNativeNulls;
+
+ /**
+ * Configuration with the default values.
+ */
+ public JSONParserConfiguration() {
+ super();
+ this.overwriteDuplicateKey = false;
+ // DO NOT DELETE THE FOLLOWING LINE -- it is used for strictMode testing
+ // this.strictMode = true;
+ }
+
+ /**
+ * This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text.
+ * Garbage chars at the end of the doc, unquoted string, and single-quoted strings are all disallowed.
+ */
+ private boolean strictMode;
+
+ @Override
+ protected JSONParserConfiguration clone() {
+ JSONParserConfiguration clone = new JSONParserConfiguration();
+ clone.overwriteDuplicateKey = overwriteDuplicateKey;
+ clone.strictMode = strictMode;
+ clone.maxNestingDepth = maxNestingDepth;
+ clone.keepStrings = keepStrings;
+ clone.useNativeNulls = useNativeNulls;
+ return clone;
+ }
+
+ /**
+ * Defines the maximum nesting depth that the parser will descend before throwing an exception
+ * when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into
+ * JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException
+ * if the maximum depth is reached.
+ *
+ * @param maxNestingDepth the maximum nesting depth allowed to the JSON parser
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) {
+ JSONParserConfiguration clone = this.clone();
+ clone.maxNestingDepth = maxNestingDepth;
+
+ return clone;
+ }
+
+ /**
+ * Controls the parser's behavior when meeting duplicate keys.
+ * If set to false, the parser will throw a JSONException when meeting a duplicate key.
+ * Or the duplicate key's value will be overwritten.
+ *
+ * @param overwriteDuplicateKey defines should the parser overwrite duplicate keys.
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwriteDuplicateKey) {
+ JSONParserConfiguration clone = this.clone();
+ clone.overwriteDuplicateKey = overwriteDuplicateKey;
+
+ return clone;
+ }
+
+ /**
+ * Controls the parser's behavior when meeting Java null values while converting maps.
+ * If set to true, the parser will put a JSONObject.NULL into the resulting JSONObject.
+ * Or the map entry will be ignored.
+ *
+ * @param useNativeNulls defines if the parser should convert null values in Java maps
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public JSONParserConfiguration withUseNativeNulls(final boolean useNativeNulls) {
+ JSONParserConfiguration clone = this.clone();
+ clone.useNativeNulls = useNativeNulls;
+
+ return clone;
+ }
+
+ /**
+ * Sets the strict mode configuration for the JSON parser with default true value
+ *
+ * When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
+ * immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
+ * JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
+ * @return a new JSONParserConfiguration instance with the updated strict mode setting
+ */
+ public JSONParserConfiguration withStrictMode() {
+ return withStrictMode(true);
+ }
+
+ /**
+ * Sets the strict mode configuration for the JSON parser.
+ *
+ * When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
+ * immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
+ * JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
+ *
+ * @param mode a boolean value indicating whether strict mode should be enabled or not
+ * @return a new JSONParserConfiguration instance with the updated strict mode setting
+ */
+ public JSONParserConfiguration withStrictMode(final boolean mode) {
+ JSONParserConfiguration clone = this.clone();
+ clone.strictMode = mode;
+
+ return clone;
+ }
+
+ /**
+ * The parser's behavior when meeting duplicate keys, controls whether the parser should
+ * overwrite duplicate keys or not.
+ *
+ * @return The overwriteDuplicateKey
configuration value.
+ */
+ public boolean isOverwriteDuplicateKey() {
+ return this.overwriteDuplicateKey;
+ }
+
+ /**
+ * The parser's behavior when meeting a null value in a java map, controls whether the parser should
+ * write a JSON entry with a null value (isUseNativeNulls() == true
)
+ * or ignore that map entry (isUseNativeNulls() == false
).
+ *
+ * @return The useNativeNulls
configuration value.
+ */
+ public boolean isUseNativeNulls() {
+ return this.useNativeNulls;
+ }
+
+
+ /**
+ * The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.
+ * Otherwise, the parser is more relaxed and might tolerate some invalid characters.
+ *
+ * @return the current strict mode setting.
+ */
+ public boolean isStrictMode() {
+ return this.strictMode;
+ }
+}
diff --git a/JSONPointer.java b/src/main/java/org/json/JSONPointer.java
similarity index 74%
rename from JSONPointer.java
rename to src/main/java/org/json/JSONPointer.java
index 8142f9a6c..34066c1aa 100644
--- a/JSONPointer.java
+++ b/src/main/java/org/json/JSONPointer.java
@@ -5,30 +5,12 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
/*
-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.
*/
/**
@@ -60,12 +42,19 @@ public class JSONPointer {
*/
public static class Builder {
+ /**
+ * Constructs a new Builder object.
+ */
+ public Builder() {
+ }
+
// Segments for the eventual JSONPointer string
private final List refTokens = new ArrayList();
/**
* Creates a {@code JSONPointer} instance using the tokens previously set using the
* {@link #append(String)} method calls.
+ * @return a JSONPointer object
*/
public JSONPointer build() {
return new JSONPointer(this.refTokens);
@@ -138,7 +127,7 @@ public JSONPointer(final String pointer) {
if (pointer == null) {
throw new NullPointerException("pointer cannot be null");
}
- if (pointer.isEmpty() || pointer.equals("#")) {
+ if (pointer.isEmpty() || "#".equals(pointer)) {
this.refTokens = Collections.emptyList();
return;
}
@@ -156,19 +145,45 @@ public JSONPointer(final String pointer) {
throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'");
}
this.refTokens = new ArrayList();
- for (String token : refs.split("/")) {
- this.refTokens.add(unescape(token));
- }
+ int slashIdx = -1;
+ int prevSlashIdx = 0;
+ do {
+ prevSlashIdx = slashIdx + 1;
+ slashIdx = refs.indexOf('/', prevSlashIdx);
+ if(prevSlashIdx == slashIdx || prevSlashIdx == refs.length()) {
+ // found 2 slashes in a row ( obj//next )
+ // or single slash at the end of a string ( obj/test/ )
+ this.refTokens.add("");
+ } else if (slashIdx >= 0) {
+ final String token = refs.substring(prevSlashIdx, slashIdx);
+ this.refTokens.add(unescape(token));
+ } else {
+ // last item after separator, or no separator at all.
+ final String token = refs.substring(prevSlashIdx);
+ this.refTokens.add(unescape(token));
+ }
+ } while (slashIdx >= 0);
+ // using split does not take into account consecutive separators or "ending nulls"
+ //for (String token : refs.split("/")) {
+ // this.refTokens.add(unescape(token));
+ //}
}
+ /**
+ * Constructs a new JSONPointer instance with the provided list of reference tokens.
+ *
+ * @param refTokens A list of strings representing the reference tokens for the JSON Pointer.
+ * Each token identifies a step in the path to the targeted value.
+ */
public JSONPointer(List refTokens) {
this.refTokens = new ArrayList(refTokens);
}
- private String unescape(String token) {
- return token.replace("~1", "/").replace("~0", "~")
- .replace("\\\"", "\"")
- .replace("\\\\", "\\");
+ /**
+ * @see rfc6901 section 3
+ */
+ private static String unescape(String token) {
+ return token.replace("~1", "/").replace("~0", "~");
}
/**
@@ -181,7 +196,7 @@ private String unescape(String token) {
* @return the result of the evaluation
* @throws JSONPointerException if an error occurs during evaluation
*/
- public Object queryFrom(Object document) {
+ public Object queryFrom(Object document) throws JSONPointerException {
if (this.refTokens.isEmpty()) {
return document;
}
@@ -205,18 +220,21 @@ public Object queryFrom(Object document) {
* @param current the JSONArray to be evaluated
* @param indexToken the array index in string form
* @return the matched object. If no matching item is found a
- * JSONPointerException is thrown
+ * @throws JSONPointerException is thrown if the index is out of bounds
*/
- @SuppressWarnings("boxing")
- private Object readByIndexToken(Object current, String indexToken) {
+ private static Object readByIndexToken(Object current, String indexToken) throws JSONPointerException {
try {
int index = Integer.parseInt(indexToken);
JSONArray currentArr = (JSONArray) current;
if (index >= currentArr.length()) {
- throw new JSONPointerException(format("index %d is out of bounds - the array has %d elements", index,
- currentArr.length()));
+ throw new JSONPointerException(format("index %s is out of bounds - the array has %d elements", indexToken,
+ Integer.valueOf(currentArr.length())));
}
- return currentArr.get(index);
+ try {
+ return currentArr.get(index);
+ } catch (JSONException e) {
+ throw new JSONPointerException("Error reading value at index position " + index, e);
+ }
} catch (NumberFormatException e) {
throw new JSONPointerException(format("%s is not an array index", indexToken), e);
}
@@ -228,7 +246,7 @@ private Object readByIndexToken(Object current, String indexToken) {
*/
@Override
public String toString() {
- StringBuilder rval = new StringBuilder("");
+ StringBuilder rval = new StringBuilder();
for (String token: this.refTokens) {
rval.append('/').append(escape(token));
}
@@ -238,21 +256,21 @@ public String toString() {
/**
* Escapes path segment values to an unambiguous form.
* The escape char to be inserted is '~'. The chars to be escaped
- * are ~, which maps to ~0, and /, which maps to ~1. Backslashes
- * and double quote chars are also escaped.
+ * are ~, which maps to ~0, and /, which maps to ~1.
* @param token the JSONPointer segment value to be escaped
* @return the escaped value for the token
+ *
+ * @see rfc6901 section 3
*/
- private String escape(String token) {
+ private static String escape(String token) {
return token.replace("~", "~0")
- .replace("/", "~1")
- .replace("\\", "\\\\")
- .replace("\"", "\\\"");
+ .replace("/", "~1");
}
/**
* Returns a string representing the JSONPointer path value using URI
* fragment identifier representation
+ * @return a uri fragment string
*/
public String toURIFragment() {
try {
diff --git a/src/main/java/org/json/JSONPointerException.java b/src/main/java/org/json/JSONPointerException.java
new file mode 100644
index 000000000..dc5a25ad6
--- /dev/null
+++ b/src/main/java/org/json/JSONPointerException.java
@@ -0,0 +1,36 @@
+package org.json;
+
+/*
+Public Domain.
+*/
+
+/**
+ * The JSONPointerException is thrown by {@link JSONPointer} if an error occurs
+ * during evaluating a pointer.
+ *
+ * @author JSON.org
+ * @version 2016-05-13
+ */
+public class JSONPointerException extends JSONException {
+ private static final long serialVersionUID = 8872944667561856751L;
+
+ /**
+ * Constructs a new JSONPointerException with the specified error message.
+ *
+ * @param message The detail message describing the reason for the exception.
+ */
+ public JSONPointerException(String message) {
+ super(message);
+ }
+
+ /**
+ * Constructs a new JSONPointerException with the specified error message and cause.
+ *
+ * @param message The detail message describing the reason for the exception.
+ * @param cause The cause of the exception.
+ */
+ public JSONPointerException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
diff --git a/src/main/java/org/json/JSONPropertyIgnore.java b/src/main/java/org/json/JSONPropertyIgnore.java
new file mode 100644
index 000000000..d3a5bc5a1
--- /dev/null
+++ b/src/main/java/org/json/JSONPropertyIgnore.java
@@ -0,0 +1,23 @@
+package org.json;
+
+/*
+Public Domain.
+*/
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Use this annotation on a getter method to override the Bean name
+ * parser for Bean -> JSONObject mapping. If this annotation is
+ * present at any level in the class hierarchy, then the method will
+ * not be serialized from the bean into the JSONObject.
+ */
+@Documented
+@Retention(RUNTIME)
+@Target({METHOD})
+public @interface JSONPropertyIgnore { }
diff --git a/src/main/java/org/json/JSONPropertyName.java b/src/main/java/org/json/JSONPropertyName.java
new file mode 100644
index 000000000..0e4123f37
--- /dev/null
+++ b/src/main/java/org/json/JSONPropertyName.java
@@ -0,0 +1,28 @@
+package org.json;
+
+/*
+Public Domain.
+*/
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Use this annotation on a getter method to override the Bean name
+ * parser for Bean -> JSONObject mapping. A value set to empty string ""
+ * will have the Bean parser fall back to the default field name processing.
+ */
+@Documented
+@Retention(RUNTIME)
+@Target({METHOD})
+public @interface JSONPropertyName {
+ /**
+ * The value of the JSON property.
+ * @return The name of the property as to be used in the JSON Object.
+ */
+ String value();
+}
diff --git a/JSONString.java b/src/main/java/org/json/JSONString.java
similarity index 96%
rename from JSONString.java
rename to src/main/java/org/json/JSONString.java
index 1f2d77dd1..cd8d1847d 100644
--- a/JSONString.java
+++ b/src/main/java/org/json/JSONString.java
@@ -1,4 +1,9 @@
package org.json;
+
+/*
+Public Domain.
+ */
+
/**
* The JSONString
interface allows a toJSONString()
* method so that a class can change the behavior of
diff --git a/JSONStringer.java b/src/main/java/org/json/JSONStringer.java
similarity index 62%
rename from JSONStringer.java
rename to src/main/java/org/json/JSONStringer.java
index 6e05d228f..2f6cf9ed8 100644
--- a/JSONStringer.java
+++ b/src/main/java/org/json/JSONStringer.java
@@ -1,79 +1,59 @@
-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.
-*/
-
-import java.io.StringWriter;
-
-/**
- * JSONStringer provides a quick and convenient way of producing JSON text.
- * The texts produced strictly conform to JSON syntax rules. No whitespace is
- * added, so the results are ready for transmission or storage. Each instance of
- * JSONStringer can produce one JSON text.
- *
- * A JSONStringer instance provides a value
method for appending
- * values to the
- * text, and a key
- * method for adding keys before values in objects. There are array
- * and endArray
methods that make and bound array values, and
- * object
and endObject
methods which make and bound
- * object values. All of these methods return the JSONWriter instance,
- * permitting cascade style. For example,
- * myString = new JSONStringer()
- * .object()
- * .key("JSON")
- * .value("Hello, World!")
- * .endObject()
- * .toString(); which produces the string
- * {"JSON":"Hello, World!"}
- *
- * The first method called must be array
or object
.
- * There are no methods for adding commas or colons. JSONStringer adds them for
- * you. Objects and arrays can be nested up to 20 levels deep.
- *
- * This can sometimes be easier than using a JSONObject to build a string.
- * @author JSON.org
- * @version 2015-12-09
- */
-public class JSONStringer extends JSONWriter {
- /**
- * Make a fresh JSONStringer. It can be used to build one JSON text.
- */
- public JSONStringer() {
- super(new StringWriter());
- }
-
- /**
- * Return the JSON text. This method is used to obtain the product of the
- * JSONStringer instance. It will return null
if there was a
- * problem in the construction of the JSON text (such as the calls to
- * array
were not properly balanced with calls to
- * endArray
).
- * @return The JSON text.
- */
- @Override
- public String toString() {
- return this.mode == 'd' ? this.writer.toString() : null;
- }
-}
+package org.json;
+
+/*
+Public Domain.
+*/
+
+import java.io.StringWriter;
+
+/**
+ * JSONStringer provides a quick and convenient way of producing JSON text.
+ * The texts produced strictly conform to JSON syntax rules. No whitespace is
+ * added, so the results are ready for transmission or storage. Each instance of
+ * JSONStringer can produce one JSON text.
+ *
+ * A JSONStringer instance provides a value
method for appending
+ * values to the
+ * text, and a key
+ * method for adding keys before values in objects. There are array
+ * and endArray
methods that make and bound array values, and
+ * object
and endObject
methods which make and bound
+ * object values. All of these methods return the JSONWriter instance,
+ * permitting cascade style. For example,
+ * myString = new JSONStringer()
+ * .object()
+ * .key("JSON")
+ * .value("Hello, World!")
+ * .endObject()
+ * .toString(); which produces the string
+ * {"JSON":"Hello, World!"}
+ *
+ * The first method called must be array
or object
.
+ * There are no methods for adding commas or colons. JSONStringer adds them for
+ * you. Objects and arrays can be nested up to 200 levels deep.
+ *
+ * This can sometimes be easier than using a JSONObject to build a string.
+ * @author JSON.org
+ * @version 2015-12-09
+ */
+public class JSONStringer extends JSONWriter {
+ /**
+ * Make a fresh JSONStringer. It can be used to build one JSON text.
+ */
+ public JSONStringer() {
+ super(new StringWriter());
+ }
+
+ /**
+ * Return the JSON text. This method is used to obtain the product of the
+ * JSONStringer instance. It will return null
if there was a
+ * problem in the construction of the JSON text (such as the calls to
+ * array
were not properly balanced with calls to
+ * endArray
).
+ * @return The JSON text.
+ */
+ @Override
+ public String toString() {
+ return this.mode == 'd' ? this.writer.toString() : null;
+ }
+}
diff --git a/JSONTokener.java b/src/main/java/org/json/JSONTokener.java
similarity index 73%
rename from JSONTokener.java
rename to src/main/java/org/json/JSONTokener.java
index 36bce45c2..05a6e34c1 100644
--- a/JSONTokener.java
+++ b/src/main/java/org/json/JSONTokener.java
@@ -1,34 +1,10 @@
package org.json;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.StringReader;
+import java.io.*;
+import java.nio.charset.Charset;
/*
-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.
*/
/**
@@ -56,13 +32,27 @@ public class JSONTokener {
/** the number of characters read in the previous line. */
private long characterPreviousLine;
+ // access to this object is required for strict mode checking
+ private JSONParserConfiguration jsonParserConfiguration;
/**
* Construct a JSONTokener from a Reader. The caller must close the Reader.
*
- * @param reader A reader.
+ * @param reader the source.
*/
public JSONTokener(Reader reader) {
+ this(reader, new JSONParserConfiguration());
+ }
+
+ /**
+ * Construct a JSONTokener from a Reader with a given JSONParserConfiguration. The caller must close the Reader.
+ *
+ * @param reader the source.
+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
+ *
+ */
+ public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguration) {
+ this.jsonParserConfiguration = jsonParserConfiguration;
this.reader = reader.markSupported()
? reader
: new BufferedReader(reader);
@@ -75,25 +65,60 @@ public JSONTokener(Reader reader) {
this.line = 1;
}
-
/**
* Construct a JSONTokener from an InputStream. The caller must close the input stream.
* @param inputStream The source.
*/
public JSONTokener(InputStream inputStream) {
- this(new InputStreamReader(inputStream));
+ this(inputStream, new JSONParserConfiguration());
+ }
+
+ /**
+ * Construct a JSONTokener from an InputStream. The caller must close the input stream.
+ * @param inputStream The source.
+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
+ */
+ public JSONTokener(InputStream inputStream, JSONParserConfiguration jsonParserConfiguration) {
+ this(new InputStreamReader(inputStream, Charset.forName("UTF-8")), jsonParserConfiguration);
}
/**
* Construct a JSONTokener from a string.
*
- * @param s A source string.
+ * @param source A source string.
+ */
+ public JSONTokener(String source) {
+ this(new StringReader(source));
+ }
+
+ /**
+ * Construct a JSONTokener from an InputStream. The caller must close the input stream.
+ * @param source The source.
+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
*/
- public JSONTokener(String s) {
- this(new StringReader(s));
+ public JSONTokener(String source, JSONParserConfiguration jsonParserConfiguration) {
+ this(new StringReader(source), jsonParserConfiguration);
}
+ /**
+ * Getter
+ * @return jsonParserConfiguration
+ */
+ public JSONParserConfiguration getJsonParserConfiguration() {
+ return jsonParserConfiguration;
+ }
+
+ /**
+ * Setter
+ * @param jsonParserConfiguration new value for jsonParserConfiguration
+ *
+ * @deprecated method should not be used
+ */
+ @Deprecated
+ public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
+ this.jsonParserConfiguration = jsonParserConfiguration;
+ }
/**
* Back up one character. This provides a sort of lookahead capability,
@@ -145,7 +170,7 @@ public static int dehexchar(char c) {
/**
* Checks if the end of the input has been reached.
- *
+ *
* @return true if at the end of the file and we didn't step back
*/
public boolean end() {
@@ -210,6 +235,12 @@ public char next() throws JSONException {
return this.previous;
}
+ /**
+ * Get the last character read from the input or '\0' if nothing has been read yet.
+ * @return the last character read from the input.
+ */
+ protected char getPrevious() { return this.previous;}
+
/**
* Increments the internal indexes according to the previous character
* read and the character passed as the current character.
@@ -317,7 +348,8 @@ public String nextString(char quote) throws JSONException {
case 0:
case '\n':
case '\r':
- throw this.syntaxError("Unterminated string");
+ throw this.syntaxError("Unterminated string. " +
+ "Character with int code " + (int) c + " is not allowed within a quoted string.");
case '\\':
c = this.next();
switch (c) {
@@ -337,10 +369,12 @@ public String nextString(char quote) throws JSONException {
sb.append('\r');
break;
case 'u':
+ String next = this.next(4);
try {
- sb.append((char)Integer.parseInt(this.next(4), 16));
+ sb.append((char)Integer.parseInt(next, 16));
} catch (NumberFormatException e) {
- throw this.syntaxError("Illegal escape.", e);
+ throw this.syntaxError("Illegal escape. " +
+ "\\u must be followed by a 4 digit hexadecimal number. \\" + next + " is not valid.", e);
}
break;
case '"':
@@ -350,7 +384,7 @@ public String nextString(char quote) throws JSONException {
sb.append(c);
break;
default:
- throw this.syntaxError("Illegal escape.");
+ throw this.syntaxError("Illegal escape. Escape sequence \\" + c + " is not valid.");
}
break;
default:
@@ -420,18 +454,38 @@ public String nextTo(String delimiters) throws JSONException {
*/
public Object nextValue() throws JSONException {
char c = this.nextClean();
+ switch (c) {
+ case '{':
+ this.back();
+ try {
+ return new JSONObject(this, jsonParserConfiguration);
+ } catch (StackOverflowError e) {
+ throw new JSONException("JSON Array or Object depth too large to process.", e);
+ }
+ case '[':
+ this.back();
+ try {
+ return new JSONArray(this, jsonParserConfiguration);
+ } catch (StackOverflowError e) {
+ throw new JSONException("JSON Array or Object depth too large to process.", e);
+ }
+ }
+ return nextSimpleValue(c);
+ }
+
+ Object nextSimpleValue(char c) {
String string;
+ // Strict mode only allows strings with explicit double quotes
+ if (jsonParserConfiguration != null &&
+ jsonParserConfiguration.isStrictMode() &&
+ c == '\'') {
+ throw this.syntaxError("Strict mode error: Single quoted strings are not allowed");
+ }
switch (c) {
case '"':
case '\'':
return this.nextString(c);
- case '{':
- this.back();
- return new JSONObject(this);
- case '[':
- this.back();
- return new JSONArray(this);
}
/*
@@ -448,13 +502,32 @@ public Object nextValue() throws JSONException {
sb.append(c);
c = this.next();
}
- this.back();
+ if (!this.eof) {
+ this.back();
+ }
string = sb.toString().trim();
if ("".equals(string)) {
throw this.syntaxError("Missing value");
}
- return JSONObject.stringToValue(string);
+ Object obj = JSONObject.stringToValue(string);
+ // if obj is a boolean, look at string
+ if (jsonParserConfiguration != null &&
+ jsonParserConfiguration.isStrictMode()) {
+ if (obj instanceof Boolean && !"true".equals(string) && !"false".equals(string)) {
+ // Strict mode only allows lowercase true or false
+ throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase boolean", obj));
+ }
+ else if (obj == JSONObject.NULL && !"null".equals(string)) {
+ // Strint mode only allows lowercase null
+ throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase null", obj));
+ }
+ else if (obj instanceof String) {
+ // Strict mode only allows strings with explicit double quotes
+ throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
+ }
+ }
+ return obj;
}
@@ -526,4 +599,15 @@ public String toString() {
return " at " + this.index + " [character " + this.character + " line " +
this.line + "]";
}
+
+ /**
+ * Closes the underlying reader, releasing any resources associated with it.
+ *
+ * @throws IOException If an I/O error occurs while closing the reader.
+ */
+ public void close() throws IOException {
+ if(reader!=null){
+ reader.close();
+ }
+ }
}
diff --git a/JSONWriter.java b/src/main/java/org/json/JSONWriter.java
similarity index 66%
rename from JSONWriter.java
rename to src/main/java/org/json/JSONWriter.java
index 549f93ee6..11f4a5c7e 100644
--- a/JSONWriter.java
+++ b/src/main/java/org/json/JSONWriter.java
@@ -1,326 +1,394 @@
-package org.json;
-
-import java.io.IOException;
-
-/*
-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.
-*/
-
-/**
- * JSONWriter provides a quick and convenient way of producing JSON text.
- * The texts produced strictly conform to JSON syntax rules. No whitespace is
- * added, so the results are ready for transmission or storage. Each instance of
- * JSONWriter can produce one JSON text.
- *
- * A JSONWriter instance provides a value
method for appending
- * values to the
- * text, and a key
- * method for adding keys before values in objects. There are array
- * and endArray
methods that make and bound array values, and
- * object
and endObject
methods which make and bound
- * object values. All of these methods return the JSONWriter instance,
- * permitting a cascade style. For example,
- * new JSONWriter(myWriter)
- * .object()
- * .key("JSON")
- * .value("Hello, World!")
- * .endObject(); which writes
- * {"JSON":"Hello, World!"}
- *
- * The first method called must be array
or object
.
- * There are no methods for adding commas or colons. JSONWriter adds them for
- * you. Objects and arrays can be nested up to 200 levels deep.
- *
- * This can sometimes be easier than using a JSONObject to build a string.
- * @author JSON.org
- * @version 2016-08-08
- */
-public class JSONWriter {
- private static final int maxdepth = 200;
-
- /**
- * The comma flag determines if a comma should be output before the next
- * value.
- */
- private boolean comma;
-
- /**
- * The current mode. Values:
- * 'a' (array),
- * 'd' (done),
- * 'i' (initial),
- * 'k' (key),
- * 'o' (object).
- */
- protected char mode;
-
- /**
- * The object/array stack.
- */
- private final JSONObject stack[];
-
- /**
- * The stack top index. A value of 0 indicates that the stack is empty.
- */
- private int top;
-
- /**
- * The writer that will receive the output.
- */
- protected Appendable writer;
-
- /**
- * Make a fresh JSONWriter. It can be used to build one JSON text.
- */
- public JSONWriter(Appendable w) {
- this.comma = false;
- this.mode = 'i';
- this.stack = new JSONObject[maxdepth];
- this.top = 0;
- this.writer = w;
- }
-
- /**
- * Append a value.
- * @param string A string value.
- * @return this
- * @throws JSONException If the value is out of sequence.
- */
- private JSONWriter append(String string) throws JSONException {
- if (string == null) {
- throw new JSONException("Null pointer");
- }
- if (this.mode == 'o' || this.mode == 'a') {
- try {
- if (this.comma && this.mode == 'a') {
- this.writer.append(',');
- }
- this.writer.append(string);
- } catch (IOException e) {
- throw new JSONException(e);
- }
- if (this.mode == 'o') {
- this.mode = 'k';
- }
- this.comma = true;
- return this;
- }
- throw new JSONException("Value out of sequence.");
- }
-
- /**
- * Begin appending a new array. All values until the balancing
- * endArray
will be appended to this array. The
- * endArray
method must be called to mark the array's end.
- * @return this
- * @throws JSONException If the nesting is too deep, or if the object is
- * started in the wrong place (for example as a key or after the end of the
- * outermost array or object).
- */
- public JSONWriter array() throws JSONException {
- if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
- this.push(null);
- this.append("[");
- this.comma = false;
- return this;
- }
- throw new JSONException("Misplaced array.");
- }
-
- /**
- * End something.
- * @param m Mode
- * @param c Closing character
- * @return this
- * @throws JSONException If unbalanced.
- */
- private JSONWriter end(char m, char c) throws JSONException {
- if (this.mode != m) {
- throw new JSONException(m == 'a'
- ? "Misplaced endArray."
- : "Misplaced endObject.");
- }
- this.pop(m);
- try {
- this.writer.append(c);
- } catch (IOException e) {
- throw new JSONException(e);
- }
- this.comma = true;
- return this;
- }
-
- /**
- * End an array. This method most be called to balance calls to
- * array
.
- * @return this
- * @throws JSONException If incorrectly nested.
- */
- public JSONWriter endArray() throws JSONException {
- return this.end('a', ']');
- }
-
- /**
- * End an object. This method most be called to balance calls to
- * object
.
- * @return this
- * @throws JSONException If incorrectly nested.
- */
- public JSONWriter endObject() throws JSONException {
- return this.end('k', '}');
- }
-
- /**
- * Append a key. The key will be associated with the next value. In an
- * object, every value must be preceded by a key.
- * @param string A key string.
- * @return this
- * @throws JSONException If the key is out of place. For example, keys
- * do not belong in arrays or if the key is null.
- */
- public JSONWriter key(String string) throws JSONException {
- if (string == null) {
- throw new JSONException("Null key.");
- }
- if (this.mode == 'k') {
- try {
- this.stack[this.top - 1].putOnce(string, Boolean.TRUE);
- if (this.comma) {
- this.writer.append(',');
- }
- this.writer.append(JSONObject.quote(string));
- this.writer.append(':');
- this.comma = false;
- this.mode = 'o';
- return this;
- } catch (IOException e) {
- throw new JSONException(e);
- }
- }
- throw new JSONException("Misplaced key.");
- }
-
-
- /**
- * Begin appending a new object. All keys and values until the balancing
- * endObject
will be appended to this object. The
- * endObject
method must be called to mark the object's end.
- * @return this
- * @throws JSONException If the nesting is too deep, or if the object is
- * started in the wrong place (for example as a key or after the end of the
- * outermost array or object).
- */
- public JSONWriter object() throws JSONException {
- if (this.mode == 'i') {
- this.mode = 'o';
- }
- if (this.mode == 'o' || this.mode == 'a') {
- this.append("{");
- this.push(new JSONObject());
- this.comma = false;
- return this;
- }
- throw new JSONException("Misplaced object.");
-
- }
-
-
- /**
- * Pop an array or object scope.
- * @param c The scope to close.
- * @throws JSONException If nesting is wrong.
- */
- private void pop(char c) throws JSONException {
- if (this.top <= 0) {
- throw new JSONException("Nesting error.");
- }
- char m = this.stack[this.top - 1] == null ? 'a' : 'k';
- if (m != c) {
- throw new JSONException("Nesting error.");
- }
- this.top -= 1;
- this.mode = this.top == 0
- ? 'd'
- : this.stack[this.top - 1] == null
- ? 'a'
- : 'k';
- }
-
- /**
- * Push an array or object scope.
- * @param jo The scope to open.
- * @throws JSONException If nesting is too deep.
- */
- private void push(JSONObject jo) throws JSONException {
- if (this.top >= maxdepth) {
- throw new JSONException("Nesting too deep.");
- }
- this.stack[this.top] = jo;
- this.mode = jo == null ? 'a' : 'k';
- this.top += 1;
- }
-
-
- /**
- * Append either the value true
or the value
- * false
.
- * @param b A boolean.
- * @return this
- * @throws JSONException
- */
- public JSONWriter value(boolean b) throws JSONException {
- return this.append(b ? "true" : "false");
- }
-
- /**
- * Append a double value.
- * @param d A double.
- * @return this
- * @throws JSONException If the number is not finite.
- */
- public JSONWriter value(double d) throws JSONException {
- return this.value(new Double(d));
- }
-
- /**
- * Append a long value.
- * @param l A long.
- * @return this
- * @throws JSONException
- */
- public JSONWriter value(long l) throws JSONException {
- return this.append(Long.toString(l));
- }
-
-
- /**
- * Append an object value.
- * @param object The object to append. It can be null, or a Boolean, Number,
- * String, JSONObject, or JSONArray, or an object that implements JSONString.
- * @return this
- * @throws JSONException If the value is out of sequence.
- */
- public JSONWriter value(Object object) throws JSONException {
- return this.append(JSONObject.valueToString(object));
- }
-}
+package org.json;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+
+/*
+Public Domain.
+*/
+
+/**
+ * JSONWriter provides a quick and convenient way of producing JSON text.
+ * The texts produced strictly conform to JSON syntax rules. No whitespace is
+ * added, so the results are ready for transmission or storage. Each instance of
+ * JSONWriter can produce one JSON text.
+ *
+ * A JSONWriter instance provides a value
method for appending
+ * values to the
+ * text, and a key
+ * method for adding keys before values in objects. There are array
+ * and endArray
methods that make and bound array values, and
+ * object
and endObject
methods which make and bound
+ * object values. All of these methods return the JSONWriter instance,
+ * permitting a cascade style. For example,
+ * new JSONWriter(myWriter)
+ * .object()
+ * .key("JSON")
+ * .value("Hello, World!")
+ * .endObject(); which writes
+ * {"JSON":"Hello, World!"}
+ *
+ * The first method called must be array
or object
.
+ * There are no methods for adding commas or colons. JSONWriter adds them for
+ * you. Objects and arrays can be nested up to 200 levels deep.
+ *
+ * This can sometimes be easier than using a JSONObject to build a string.
+ * @author JSON.org
+ * @version 2016-08-08
+ */
+public class JSONWriter {
+ private static final int maxdepth = 200;
+
+ /**
+ * The comma flag determines if a comma should be output before the next
+ * value.
+ */
+ private boolean comma;
+
+ /**
+ * The current mode. Values:
+ * 'a' (array),
+ * 'd' (done),
+ * 'i' (initial),
+ * 'k' (key),
+ * 'o' (object).
+ */
+ protected char mode;
+
+ /**
+ * The object/array stack.
+ */
+ private final JSONObject stack[];
+
+ /**
+ * The stack top index. A value of 0 indicates that the stack is empty.
+ */
+ private int top;
+
+ /**
+ * The writer that will receive the output.
+ */
+ protected Appendable writer;
+
+ /**
+ * Make a fresh JSONWriter. It can be used to build one JSON text.
+ * @param w an appendable object
+ */
+ public JSONWriter(Appendable w) {
+ this.comma = false;
+ this.mode = 'i';
+ this.stack = new JSONObject[maxdepth];
+ this.top = 0;
+ this.writer = w;
+ }
+
+ /**
+ * Append a value.
+ * @param string A string value.
+ * @return this
+ * @throws JSONException If the value is out of sequence.
+ */
+ private JSONWriter append(String string) throws JSONException {
+ if (string == null) {
+ throw new JSONException("Null pointer");
+ }
+ if (this.mode == 'o' || this.mode == 'a') {
+ try {
+ if (this.comma && this.mode == 'a') {
+ this.writer.append(',');
+ }
+ this.writer.append(string);
+ } catch (IOException e) {
+ // Android as of API 25 does not support this exception constructor
+ // however we won't worry about it. If an exception is happening here
+ // it will just throw a "Method not found" exception instead.
+ throw new JSONException(e);
+ }
+ if (this.mode == 'o') {
+ this.mode = 'k';
+ }
+ this.comma = true;
+ return this;
+ }
+ throw new JSONException("Value out of sequence.");
+ }
+
+ /**
+ * Begin appending a new array. All values until the balancing
+ * endArray
will be appended to this array. The
+ * endArray
method must be called to mark the array's end.
+ * @return this
+ * @throws JSONException If the nesting is too deep, or if the object is
+ * started in the wrong place (for example as a key or after the end of the
+ * outermost array or object).
+ */
+ public JSONWriter array() throws JSONException {
+ if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
+ this.push(null);
+ this.append("[");
+ this.comma = false;
+ return this;
+ }
+ throw new JSONException("Misplaced array.");
+ }
+
+ /**
+ * End something.
+ * @param m Mode
+ * @param c Closing character
+ * @return this
+ * @throws JSONException If unbalanced.
+ */
+ private JSONWriter end(char m, char c) throws JSONException {
+ if (this.mode != m) {
+ throw new JSONException(m == 'a'
+ ? "Misplaced endArray."
+ : "Misplaced endObject.");
+ }
+ this.pop(m);
+ try {
+ this.writer.append(c);
+ } catch (IOException e) {
+ // Android as of API 25 does not support this exception constructor
+ // however we won't worry about it. If an exception is happening here
+ // it will just throw a "Method not found" exception instead.
+ throw new JSONException(e);
+ }
+ this.comma = true;
+ return this;
+ }
+
+ /**
+ * End an array. This method most be called to balance calls to
+ * array
.
+ * @return this
+ * @throws JSONException If incorrectly nested.
+ */
+ public JSONWriter endArray() throws JSONException {
+ return this.end('a', ']');
+ }
+
+ /**
+ * End an object. This method most be called to balance calls to
+ * object
.
+ * @return this
+ * @throws JSONException If incorrectly nested.
+ */
+ public JSONWriter endObject() throws JSONException {
+ return this.end('k', '}');
+ }
+
+ /**
+ * Append a key. The key will be associated with the next value. In an
+ * object, every value must be preceded by a key.
+ * @param string A key string.
+ * @return this
+ * @throws JSONException If the key is out of place. For example, keys
+ * do not belong in arrays or if the key is null.
+ */
+ public JSONWriter key(String string) throws JSONException {
+ if (string == null) {
+ throw new JSONException("Null key.");
+ }
+ if (this.mode == 'k') {
+ try {
+ JSONObject topObject = this.stack[this.top - 1];
+ // don't use the built in putOnce method to maintain Android support
+ if(topObject.has(string)) {
+ throw new JSONException("Duplicate key \"" + string + "\"");
+ }
+ topObject.put(string, true);
+ if (this.comma) {
+ this.writer.append(',');
+ }
+ this.writer.append(JSONObject.quote(string));
+ this.writer.append(':');
+ this.comma = false;
+ this.mode = 'o';
+ return this;
+ } catch (IOException e) {
+ // Android as of API 25 does not support this exception constructor
+ // however we won't worry about it. If an exception is happening here
+ // it will just throw a "Method not found" exception instead.
+ throw new JSONException(e);
+ }
+ }
+ throw new JSONException("Misplaced key.");
+ }
+
+
+ /**
+ * Begin appending a new object. All keys and values until the balancing
+ * endObject
will be appended to this object. The
+ * endObject
method must be called to mark the object's end.
+ * @return this
+ * @throws JSONException If the nesting is too deep, or if the object is
+ * started in the wrong place (for example as a key or after the end of the
+ * outermost array or object).
+ */
+ public JSONWriter object() throws JSONException {
+ if (this.mode == 'i') {
+ this.mode = 'o';
+ }
+ if (this.mode == 'o' || this.mode == 'a') {
+ this.append("{");
+ this.push(new JSONObject());
+ this.comma = false;
+ return this;
+ }
+ throw new JSONException("Misplaced object.");
+
+ }
+
+
+ /**
+ * Pop an array or object scope.
+ * @param c The scope to close.
+ * @throws JSONException If nesting is wrong.
+ */
+ private void pop(char c) throws JSONException {
+ if (this.top <= 0) {
+ throw new JSONException("Nesting error.");
+ }
+ char m = this.stack[this.top - 1] == null ? 'a' : 'k';
+ if (m != c) {
+ throw new JSONException("Nesting error.");
+ }
+ this.top -= 1;
+ this.mode = this.top == 0
+ ? 'd'
+ : this.stack[this.top - 1] == null
+ ? 'a'
+ : 'k';
+ }
+
+ /**
+ * Push an array or object scope.
+ * @param jo The scope to open.
+ * @throws JSONException If nesting is too deep.
+ */
+ private void push(JSONObject jo) throws JSONException {
+ if (this.top >= maxdepth) {
+ throw new JSONException("Nesting too deep.");
+ }
+ this.stack[this.top] = jo;
+ this.mode = jo == null ? 'a' : 'k';
+ this.top += 1;
+ }
+
+ /**
+ * Make a JSON text of an Object value. If the object has an
+ * value.toJSONString() method, then that method will be used to produce the
+ * JSON text. The method is required to produce a strictly conforming text.
+ * If the object does not contain a toJSONString method (which is the most
+ * common case), then a text will be produced by other means. If the value
+ * is an array or Collection, then a JSONArray will be made from it and its
+ * toJSONString method will be called. If the value is a MAP, then a
+ * JSONObject will be made from it and its toJSONString method will be
+ * called. Otherwise, the value's toString method will be called, and the
+ * result will be quoted.
+ *
+ *
+ * Warning: This method assumes that the data structure is acyclical.
+ *
+ * @param value
+ * The value to be serialized.
+ * @return a printable, displayable, transmittable representation of the
+ * object, beginning with {
(left
+ * brace) and ending with }
(right
+ * brace) .
+ * @throws JSONException
+ * If the value is or contains an invalid number.
+ */
+ public static String valueToString(Object value) throws JSONException {
+ if (value == null || value.equals(null)) {
+ return "null";
+ }
+ if (value instanceof JSONString) {
+ String object;
+ try {
+ object = ((JSONString) value).toJSONString();
+ } catch (Exception e) {
+ throw new JSONException(e);
+ }
+ if (object != null) {
+ return object;
+ }
+ throw new JSONException("Bad value from toJSONString: " + object);
+ }
+ if (value instanceof Number) {
+ // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
+ final String numberAsString = JSONObject.numberToString((Number) value);
+ if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) {
+ // Close enough to a JSON number that we will return it unquoted
+ return numberAsString;
+ }
+ // The Number value is not a valid JSON number.
+ // Instead we will quote it as a string
+ return JSONObject.quote(numberAsString);
+ }
+ if (value instanceof Boolean || value instanceof JSONObject
+ || value instanceof JSONArray) {
+ return value.toString();
+ }
+ if (value instanceof Map) {
+ Map, ?> map = (Map, ?>) value;
+ return new JSONObject(map).toString();
+ }
+ if (value instanceof Collection) {
+ Collection> coll = (Collection>) value;
+ return new JSONArray(coll).toString();
+ }
+ if (value.getClass().isArray()) {
+ return new JSONArray(value).toString();
+ }
+ if(value instanceof Enum>){
+ return JSONObject.quote(((Enum>)value).name());
+ }
+ return JSONObject.quote(value.toString());
+ }
+
+ /**
+ * Append either the value true
or the value
+ * false
.
+ * @param b A boolean.
+ * @return this
+ * @throws JSONException if a called function has an error
+ */
+ public JSONWriter value(boolean b) throws JSONException {
+ return this.append(b ? "true" : "false");
+ }
+
+ /**
+ * Append a double value.
+ * @param d A double.
+ * @return this
+ * @throws JSONException If the number is not finite.
+ */
+ public JSONWriter value(double d) throws JSONException {
+ return this.value(Double.valueOf(d));
+ }
+
+ /**
+ * Append a long value.
+ * @param l A long.
+ * @return this
+ * @throws JSONException if a called function has an error
+ */
+ public JSONWriter value(long l) throws JSONException {
+ return this.append(Long.toString(l));
+ }
+
+
+ /**
+ * Append an object value.
+ * @param object The object to append. It can be null, or a Boolean, Number,
+ * String, JSONObject, or JSONArray, or an object that implements JSONString.
+ * @return this
+ * @throws JSONException If the value is out of sequence.
+ */
+ public JSONWriter value(Object object) throws JSONException {
+ return this.append(valueToString(object));
+ }
+}
diff --git a/src/main/java/org/json/ParserConfiguration.java b/src/main/java/org/json/ParserConfiguration.java
new file mode 100644
index 000000000..06cc44366
--- /dev/null
+++ b/src/main/java/org/json/ParserConfiguration.java
@@ -0,0 +1,126 @@
+package org.json;
+/*
+Public Domain.
+*/
+
+/**
+ * Configuration base object for parsers. The configuration is immutable.
+ */
+@SuppressWarnings({""})
+public class ParserConfiguration {
+ /**
+ * Used to indicate there's no defined limit to the maximum nesting depth when parsing a document.
+ */
+ public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1;
+
+ /**
+ * The default maximum nesting depth when parsing a document.
+ */
+ public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;
+
+ /**
+ * Specifies if values should be kept as strings (true
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string).
+ */
+ protected boolean keepStrings;
+
+ /**
+ * The maximum nesting depth when parsing an object.
+ */
+ protected int maxNestingDepth;
+
+ /**
+ * Constructs a new ParserConfiguration with default settings.
+ */
+ public ParserConfiguration() {
+ this.keepStrings = false;
+ this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
+ }
+
+ /**
+ * Constructs a new ParserConfiguration with the specified settings.
+ *
+ * @param keepStrings A boolean indicating whether to preserve strings during parsing.
+ * @param maxNestingDepth An integer representing the maximum allowed nesting depth.
+ */
+ protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
+ this.keepStrings = keepStrings;
+ this.maxNestingDepth = maxNestingDepth;
+ }
+
+ /**
+ * Provides a new instance of the same configuration.
+ */
+ @Override
+ protected ParserConfiguration clone() {
+ // future modifications to this method should always ensure a "deep"
+ // clone in the case of collections. i.e. if a Map is added as a configuration
+ // item, a new map instance should be created and if possible each value in the
+ // map should be cloned as well. If the values of the map are known to also
+ // be immutable, then a shallow clone of the map is acceptable.
+ return new ParserConfiguration(
+ this.keepStrings,
+ this.maxNestingDepth
+ );
+ }
+
+ /**
+ * When parsing the XML into JSONML, specifies if values should be kept as strings (true
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string).
+ *
+ * @return The keepStrings
configuration value.
+ */
+ public boolean isKeepStrings() {
+ return this.keepStrings;
+ }
+
+ /**
+ * When parsing the XML into JSONML, specifies if values should be kept as strings (true
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string)
+ *
+ * @param newVal new value to use for the keepStrings
configuration option.
+ * @param the type of the configuration object
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ @SuppressWarnings("unchecked")
+ public T withKeepStrings(final boolean newVal) {
+ T newConfig = (T) this.clone();
+ newConfig.keepStrings = newVal;
+ return newConfig;
+ }
+
+ /**
+ * The maximum nesting depth that the parser will descend before throwing an exception
+ * when parsing an object (e.g. Map, Collection) into JSON-related objects.
+ *
+ * @return the maximum nesting depth set for this configuration
+ */
+ public int getMaxNestingDepth() {
+ return maxNestingDepth;
+ }
+
+ /**
+ * Defines the maximum nesting depth that the parser will descend before throwing an exception
+ * when parsing an object (e.g. Map, Collection) into JSON-related objects.
+ * The default max nesting depth is 512, which means the parser will throw a JsonException if
+ * the maximum depth is reached.
+ * Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
+ * which means the parses will go as deep as the maximum call stack size allows.
+ *
+ * @param maxNestingDepth the maximum nesting depth allowed to the XML parser
+ * @param the type of the configuration object
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ @SuppressWarnings("unchecked")
+ public T withMaxNestingDepth(int maxNestingDepth) {
+ T newConfig = (T) this.clone();
+
+ if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) {
+ newConfig.maxNestingDepth = maxNestingDepth;
+ } else {
+ newConfig.maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH;
+ }
+
+ return newConfig;
+ }
+}
diff --git a/src/main/java/org/json/Property.java b/src/main/java/org/json/Property.java
new file mode 100644
index 000000000..ba6c56967
--- /dev/null
+++ b/src/main/java/org/json/Property.java
@@ -0,0 +1,62 @@
+package org.json;
+
+/*
+Public Domain.
+*/
+
+import java.util.Enumeration;
+import java.util.Properties;
+
+/**
+ * Converts a Property file data into JSONObject and back.
+ * @author JSON.org
+ * @version 2015-05-05
+ */
+public class Property {
+
+ /**
+ * Constructs a new Property object.
+ */
+ public Property() {
+ }
+
+ /**
+ * Converts a property file object into a JSONObject. The property file object is a table of name value pairs.
+ * @param properties java.util.Properties
+ * @return JSONObject
+ * @throws JSONException if a called function has an error
+ */
+ public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
+ // can't use the new constructor for Android support
+ // JSONObject jo = new JSONObject(properties == null ? 0 : properties.size());
+ JSONObject jo = new JSONObject();
+ if (properties != null && !properties.isEmpty()) {
+ Enumeration> enumProperties = properties.propertyNames();
+ while(enumProperties.hasMoreElements()) {
+ String name = (String)enumProperties.nextElement();
+ jo.put(name, properties.getProperty(name));
+ }
+ }
+ return jo;
+ }
+
+ /**
+ * Converts the JSONObject into a property file object.
+ * @param jo JSONObject
+ * @return java.util.Properties
+ * @throws JSONException if a called function has an error
+ */
+ public static Properties toProperties(JSONObject jo) throws JSONException {
+ Properties properties = new Properties();
+ if (jo != null) {
+ // Don't use the new entrySet API to maintain Android support
+ for (final String key : jo.keySet()) {
+ Object value = jo.opt(key);
+ if (!JSONObject.NULL.equals(value)) {
+ properties.put(key, value.toString());
+ }
+ }
+ }
+ return properties;
+ }
+}
diff --git a/src/main/java/org/json/StringBuilderWriter.java b/src/main/java/org/json/StringBuilderWriter.java
new file mode 100644
index 000000000..4aaa4903f
--- /dev/null
+++ b/src/main/java/org/json/StringBuilderWriter.java
@@ -0,0 +1,92 @@
+package org.json;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Performance optimised alternative for {@link java.io.StringWriter}
+ * using internally a {@link StringBuilder} instead of a {@link StringBuffer}.
+ */
+public class StringBuilderWriter extends Writer {
+ private final StringBuilder builder;
+
+ /**
+ * Create a new string builder writer using the default initial string-builder buffer size.
+ */
+ public StringBuilderWriter() {
+ builder = new StringBuilder();
+ lock = builder;
+ }
+
+ /**
+ * Create a new string builder writer using the specified initial string-builder buffer size.
+ *
+ * @param initialSize The number of {@code char} values that will fit into this buffer
+ * before it is automatically expanded
+ *
+ * @throws IllegalArgumentException If {@code initialSize} is negative
+ */
+ public StringBuilderWriter(int initialSize) {
+ builder = new StringBuilder(initialSize);
+ lock = builder;
+ }
+
+ @Override
+ public void write(int c) {
+ builder.append((char) c);
+ }
+
+ @Override
+ public void write(char[] cbuf, int offset, int length) {
+ if ((offset < 0) || (offset > cbuf.length) || (length < 0) ||
+ ((offset + length) > cbuf.length) || ((offset + length) < 0)) {
+ throw new IndexOutOfBoundsException();
+ } else if (length == 0) {
+ return;
+ }
+ builder.append(cbuf, offset, length);
+ }
+
+ @Override
+ public void write(String str) {
+ builder.append(str);
+ }
+
+ @Override
+ public void write(String str, int offset, int length) {
+ builder.append(str, offset, offset + length);
+ }
+
+ @Override
+ public StringBuilderWriter append(CharSequence csq) {
+ write(String.valueOf(csq));
+ return this;
+ }
+
+ @Override
+ public StringBuilderWriter append(CharSequence csq, int start, int end) {
+ if (csq == null) {
+ csq = "null";
+ }
+ return append(csq.subSequence(start, end));
+ }
+
+ @Override
+ public StringBuilderWriter append(char c) {
+ write(c);
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return builder.toString();
+ }
+
+ @Override
+ public void flush() {
+ }
+
+ @Override
+ public void close() throws IOException {
+ }
+}
diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java
new file mode 100644
index 000000000..3eb948c77
--- /dev/null
+++ b/src/main/java/org/json/XML.java
@@ -0,0 +1,1135 @@
+package org.json;
+
+/*
+Public Domain.
+*/
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Iterator;
+
+/**
+ * This provides static methods to convert an XML text into a JSONObject, and to
+ * covert a JSONObject into an XML text.
+ *
+ * @author JSON.org
+ * @version 2016-08-10
+ */
+@SuppressWarnings("boxing")
+public class XML {
+
+ /**
+ * Constructs a new XML object.
+ */
+ public XML() {
+ }
+
+ /** The Character '&'. */
+ public static final Character AMP = '&';
+
+ /** The Character '''. */
+ public static final Character APOS = '\'';
+
+ /** The Character '!'. */
+ public static final Character BANG = '!';
+
+ /** The Character '='. */
+ public static final Character EQ = '=';
+
+ /** The Character {@code '>'. } */
+ public static final Character GT = '>';
+
+ /** The Character '<'. */
+ public static final Character LT = '<';
+
+ /** The Character '?'. */
+ public static final Character QUEST = '?';
+
+ /** The Character '"'. */
+ public static final Character QUOT = '"';
+
+ /** The Character '/'. */
+ public static final Character SLASH = '/';
+
+ /**
+ * Null attribute name
+ */
+ public static final String NULL_ATTR = "xsi:nil";
+
+ /**
+ * Represents the XML attribute name for specifying type information.
+ */
+ public static final String TYPE_ATTR = "xsi:type";
+
+ /**
+ * Creates an iterator for navigating Code Points in a string instead of
+ * characters. Once Java7 support is dropped, this can be replaced with
+ *
+ * string.codePoints()
+ *
+ * which is available in Java8 and above.
+ *
+ * @see http://stackoverflow.com/a/21791059/6030888
+ */
+ private static Iterable codePointIterator(final String string) {
+ return new Iterable() {
+ @Override
+ public Iterator iterator() {
+ return new Iterator() {
+ private int nextIndex = 0;
+ private int length = string.length();
+
+ @Override
+ public boolean hasNext() {
+ return this.nextIndex < this.length;
+ }
+
+ @Override
+ public Integer next() {
+ int result = string.codePointAt(this.nextIndex);
+ this.nextIndex += Character.charCount(result);
+ return result;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+ };
+ }
+
+ /**
+ * Replace special characters with XML escapes:
+ *
+ * {@code
+ * & (ampersand) is replaced by &
+ * < (less than) is replaced by <
+ * > (greater than) is replaced by >
+ * " (double quote) is replaced by "
+ * ' (single quote / apostrophe) is replaced by '
+ * }
+ *
+ * @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);
+ }
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * @param cp code point to test
+ * @return true if the code point is not valid for an XML
+ */
+ private static boolean mustEscape(int cp) {
+ /* Valid range from https://www.w3.org/TR/REC-xml/#charsets
+ *
+ * #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
+ *
+ * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
+ */
+ // isISOControl is true when (cp >= 0 && cp <= 0x1F) || (cp >= 0x7F && cp <= 0x9F)
+ // all ISO control characters are out of range except tabs and new lines
+ return (Character.isISOControl(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)
+ )
+ ;
+ }
+
+ /**
+ * Removes XML escapes from the string.
+ *
+ * @param string
+ * string to remove escapes from
+ * @return string with converted entities
+ */
+ public static String unescape(String string) {
+ StringBuilder sb = new StringBuilder(string.length());
+ for (int i = 0, length = string.length(); i < length; i++) {
+ char c = string.charAt(i);
+ if (c == '&') {
+ final int semic = string.indexOf(';', i);
+ if (semic > i) {
+ final String entity = string.substring(i + 1, semic);
+ sb.append(XMLTokener.unescapeEntity(entity));
+ // skip past the entity we just parsed.
+ i += entity.length() + 1;
+ } else {
+ // this shouldn't happen in most cases since the parser
+ // errors on unclosed entries.
+ sb.append(c);
+ }
+ } else {
+ // not part of an entity
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Throw an exception if the string contains whitespace. Whitespace is not
+ * allowed in tagNames and attributes.
+ *
+ * @param string
+ * A string.
+ * @throws JSONException Thrown if the string contains whitespace or is empty.
+ */
+ public static void noSpace(String string) throws JSONException {
+ int i, length = string.length();
+ if (length == 0) {
+ throw new JSONException("Empty string.");
+ }
+ for (i = 0; i < length; i += 1) {
+ if (Character.isWhitespace(string.charAt(i))) {
+ throw new JSONException("'" + string
+ + "' contains a space character.");
+ }
+ }
+ }
+
+ /**
+ * 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 config
+ * The XML parser configuration.
+ * @param currentNestingDepth
+ * The current nesting depth.
+ * @return true if the close tag is processed.
+ * @throws JSONException Thrown if any parsing error occurs.
+ */
+ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, int currentNestingDepth)
+ 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) {
+ Object obj = stringToValue((String) token);
+ if (obj instanceof Boolean) {
+ jsonObject.accumulate(string,
+ config.isKeepBooleanAsString()
+ ? ((String) token)
+ : obj);
+ } else if (obj instanceof Number) {
+ jsonObject.accumulate(string,
+ config.isKeepNumberAsString()
+ ? ((String) token)
+ : obj);
+ } else {
+ jsonObject.accumulate(string, 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 {
+ Object obj = stringToValue((String) token);
+ if (obj instanceof Boolean) {
+ jsonObject.accumulate(config.getcDataTagName(),
+ config.isKeepBooleanAsString()
+ ? ((String) token)
+ : obj);
+ } else if (obj instanceof Number) {
+ jsonObject.accumulate(config.getcDataTagName(),
+ config.isKeepNumberAsString()
+ ? ((String) token)
+ : obj);
+ } else if (obj == JSONObject.NULL) {
+ jsonObject.accumulate(config.getcDataTagName(),
+ config.isKeepStrings() ? ((String) token) : obj);
+ } else {
+ jsonObject.accumulate(config.getcDataTagName(), stringToValue((String) token));
+ }
+ }
+ }
+
+ } else if (token == LT) {
+ // Nested element
+ if (currentNestingDepth == config.getMaxNestingDepth()) {
+ throw x.syntaxError("Maximum nesting depth of " + config.getMaxNestingDepth() + " reached");
+ }
+
+ if (parse(x, jsonObject, tagName, config, currentNestingDepth + 1)) {
+ 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 {
+ if (!config.shouldTrimWhiteSpace()) {
+ removeEmpty(jsonObject, config);
+ }
+ context.accumulate(tagName, jsonObject);
+ }
+ }
+
+ return false;
+ }
+ }
+ }
+ } else {
+ throw x.syntaxError("Misshaped tag");
+ }
+ }
+ }
+ }
+ /**
+ * This method removes any JSON entry which has the key set by XMLParserConfiguration.cDataTagName
+ * and contains whitespace as this is caused by whitespace between tags. See test XMLTest.testNestedWithWhitespaceTrimmingDisabled.
+ * @param jsonObject JSONObject which may require deletion
+ * @param config The XMLParserConfiguration which includes the cDataTagName
+ */
+ private static void removeEmpty(final JSONObject jsonObject, final XMLParserConfiguration config) {
+ if (jsonObject.has(config.getcDataTagName())) {
+ final Object s = jsonObject.get(config.getcDataTagName());
+ if (s instanceof String) {
+ if (isStringAllWhiteSpace(s.toString())) {
+ jsonObject.remove(config.getcDataTagName());
+ }
+ }
+ else if (s instanceof JSONArray) {
+ final JSONArray sArray = (JSONArray) s;
+ for (int k = sArray.length()-1; k >= 0; k--){
+ final Object eachString = sArray.get(k);
+ if (eachString instanceof String) {
+ String s1 = (String) eachString;
+ if (isStringAllWhiteSpace(s1)) {
+ sArray.remove(k);
+ }
+ }
+ }
+ if (sArray.isEmpty()) {
+ jsonObject.remove(config.getcDataTagName());
+ }
+ }
+ }
+ }
+
+ private static boolean isStringAllWhiteSpace(final String s) {
+ for (int k = 0; k= '0' && initial <= '9') || initial == '-') {
+ // decimal representation
+ if (isDecimalNotation(val)) {
+ // Use a BigDecimal all the time so we keep the original
+ // representation. BigDecimal doesn't support -0.0, ensure we
+ // keep that by forcing a decimal.
+ try {
+ BigDecimal bd = new BigDecimal(val);
+ if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
+ return Double.valueOf(-0.0);
+ }
+ return bd;
+ } catch (NumberFormatException retryAsDouble) {
+ // 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.");
+ }
+ return d;
+ } catch (NumberFormatException ignore) {
+ 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) {
+ char at1 = val.charAt(1);
+ 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.");
+ }
+ }
+ // 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){
+ return Integer.valueOf(bi.intValue());
+ }
+ if(bi.bitLength() <= 63){
+ return Long.valueOf(bi.longValue());
+ }
+ return bi;
+ }
+ throw new NumberFormatException("val ["+val+"] is not a valid number.");
+ }
+
+ /**
+ * direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support.
+ */
+ private static boolean isDecimalNotation(final String val) {
+ return val.indexOf('.') > -1 || val.indexOf('e') > -1
+ || val.indexOf('E') > -1 || "-0".equals(val);
+ }
+
+ /**
+ * This method tries to convert the given string value to the target object
+ * @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) {
+ return typeConverter.convert(string);
+ }
+ return stringToValue(string);
+ }
+
+ /**
+ * This method is the same as {@link JSONObject#stringToValue(String)}.
+ *
+ * @param string String to convert
+ * @return JSON value of this string or the string
+ */
+ // To maintain compatibility with the Android API, this method is a direct copy of
+ // the one in JSONObject. Changes made here should be reflected there.
+ // This method should not make calls out of the XML object.
+ public static Object stringToValue(String string) {
+ if ("".equals(string)) {
+ return string;
+ }
+
+ // check JSON key words true/false/null
+ if ("true".equalsIgnoreCase(string)) {
+ return Boolean.TRUE;
+ }
+ if ("false".equalsIgnoreCase(string)) {
+ return Boolean.FALSE;
+ }
+ if ("null".equalsIgnoreCase(string)) {
+ return JSONObject.NULL;
+ }
+
+ /*
+ * If it might be a number, try converting it. If a number cannot be
+ * produced, then the value will just be a string.
+ */
+
+ char initial = string.charAt(0);
+ if ((initial >= '0' && initial <= '9') || initial == '-') {
+ try {
+ return stringToNumber(string);
+ } catch (Exception ignore) {
+ }
+ }
+ return string;
+ }
+
+ /**
+ * 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.
+ *
+ * @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
+ */
+ 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
+ * 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.
+ *
+ * @param reader The XML source reader.
+ * @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) throws JSONException {
+ return toJSONObject(reader, XMLParserConfiguration.ORIGINAL);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML 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 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
+ * @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) {
+ return toJSONObject(reader, XMLParserConfiguration.KEEP_STRINGS);
+ }
+ return toJSONObject(reader, XMLParserConfiguration.ORIGINAL);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML 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 numbers 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 depending
+ * on how flag is set.
+ * All booleans are converted as strings, for true, false will not be coerced to
+ * booleans but will instead be the exact value as seen in the XML document depending
+ * on how flag is set.
+ *
+ * @param reader The XML source reader.
+ * @param keepNumberAsString If true, then numeric values will not be coerced into
+ * numeric values and will instead be left as strings
+ * @param keepBooleanAsString If true, then boolean values will not be coerced into
+ * * 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 keepNumberAsString, boolean keepBooleanAsString) throws JSONException {
+ XMLParserConfiguration xmlParserConfiguration = new XMLParserConfiguration();
+ if(keepNumberAsString) {
+ xmlParserConfiguration = xmlParserConfiguration.withKeepNumberAsString(keepNumberAsString);
+ }
+ if(keepBooleanAsString) {
+ xmlParserConfiguration = xmlParserConfiguration.withKeepBooleanAsString(keepBooleanAsString);
+ }
+ return toJSONObject(reader, xmlParserConfiguration);
+ }
+
+ /**
+ * Convert a well-formed (but not necessarily valid) XML 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 reader The XML source reader.
+ * @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
+ */
+ public static JSONObject toJSONObject(Reader reader, XMLParserConfiguration config) throws JSONException {
+ JSONObject jo = new JSONObject();
+ XMLTokener x = new XMLTokener(reader, config);
+ while (x.more()) {
+ x.skipPast("<");
+ if(x.more()) {
+ parse(x, jo, null, config, 0);
+ }
+ }
+ 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
+ * 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 numbers 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 depending
+ * on how flag is set.
+ * All booleans are converted as strings, for true, false will not be coerced to
+ * booleans but will instead be the exact value as seen in the XML document depending
+ * on how flag is set.
+ *
+ * @param string
+ * The source string.
+ * @param keepNumberAsString If true, then numeric values will not be coerced into
+ * numeric values and will instead be left as strings
+ * @param keepBooleanAsString If true, then boolean values will not be coerced into
+ * 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 keepNumberAsString, boolean keepBooleanAsString) throws JSONException {
+ return toJSONObject(new StringReader(string), keepNumberAsString, keepBooleanAsString);
+ }
+
+ /**
+ * 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 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
+ */
+ public static JSONObject toJSONObject(String string, XMLParserConfiguration config) throws JSONException {
+ return toJSONObject(new StringReader(string), config);
+ }
+
+ /**
+ * Convert a JSONObject into a well-formed, element-normal XML string.
+ *
+ * @param object
+ * A JSONObject.
+ * @return A string.
+ * @throws JSONException Thrown if there is an error parsing the string
+ */
+ public static String toString(Object object) throws JSONException {
+ return toString(object, null, XMLParserConfiguration.ORIGINAL);
+ }
+
+ /**
+ * Convert a JSONObject into a well-formed, element-normal XML string.
+ *
+ * @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
+ */
+ public static String toString(final Object object, final String tagName) {
+ return toString(object, tagName, XMLParserConfiguration.ORIGINAL);
+ }
+
+ /**
+ * 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.
+ * @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;
+ String string;
+
+ if (object instanceof JSONObject) {
+
+ // 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.
+ // don't use the new entrySet accessor to maintain Android Support
+ jo = (JSONObject) object;
+ for (final String key : jo.keySet()) {
+ Object value = jo.opt(key);
+ if (value == null) {
+ value = "";
+ } else if (value.getClass().isArray()) {
+ value = new JSONArray(value);
+ }
+
+ // Emit content in body
+ if (key.equals(config.getcDataTagName())) {
+ if (value instanceof JSONArray) {
+ 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++) {
+ if (i > 0) {
+ sb.append('\n');
+ }
+ Object val = ja.opt(i);
+ sb.append(escape(val.toString()));
+ }
+ } else {
+ sb.append(escape(value.toString()));
+ }
+
+ // Emit an array of similar keys
+
+ } else if (value instanceof JSONArray) {
+ 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++) {
+ Object val = ja.opt(i);
+ if (val instanceof JSONArray) {
+ sb.append('<');
+ sb.append(key);
+ sb.append('>');
+ sb.append(toString(val, null, config, indentFactor, indent));
+ sb.append("");
+ sb.append(key);
+ sb.append('>');
+ } else {
+ sb.append(toString(val, key, config, indentFactor, indent));
+ }
+ }
+ } else if ("".equals(value)) {
+ if (config.isCloseEmptyTag()){
+ sb.append(indent(indent));
+ sb.append('<');
+ sb.append(key);
+ sb.append(">");
+ sb.append("");
+ sb.append(key);
+ sb.append(">");
+ if (indentFactor > 0) {
+ sb.append("\n");
+ }
+ }else {
+ 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, 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()) {
+ 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++) {
+ 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, indentFactor, indent));
+ }
+ return sb.toString();
+ }
+
+
+ string = (object == null) ? "null" : escape(object.toString());
+ String indentationSuffix = (indentFactor > 0) ? "\n" : "";
+ if(tagName == null){
+ return indent(indent) + "\"" + string + "\"" + indentationSuffix;
+ } else if(string.length() == 0){
+ return indent(indent) + "<" + tagName + "/>" + indentationSuffix;
+ } else {
+ return indent(indent) + "<" + tagName
+ + ">" + string + "" + tagName + ">" + indentationSuffix;
+ }
+ }
+
+ /**
+ * 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();
+ }
+}
diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java
new file mode 100644
index 000000000..de84b90cb
--- /dev/null
+++ b/src/main/java/org/json/XMLParserConfiguration.java
@@ -0,0 +1,444 @@
+package org.json;
+/*
+Public Domain.
+*/
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+
+/**
+ * Configuration object for the XML parser. The configuration is immutable.
+ * @author AylwardJ
+ */
+@SuppressWarnings({""})
+public class XMLParserConfiguration extends ParserConfiguration {
+
+ /**
+ * The default maximum nesting depth when parsing a XML document to JSON.
+ */
+// public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512; // We could override
+
+ /**
+ * Allow user to control how numbers are parsed
+ */
+ private boolean keepNumberAsString;
+
+ /**
+ * Allow user to control how booleans are parsed
+ */
+ private boolean keepBooleanAsString;
+
+ /** Original Configuration of the XML Parser. */
+ public static final XMLParserConfiguration ORIGINAL
+ = new XMLParserConfiguration();
+ /** Original configuration of the XML Parser except that values are kept as strings. */
+ public static final XMLParserConfiguration KEEP_STRINGS
+ = new XMLParserConfiguration().withKeepStrings(true);
+
+ /**
+ * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
+ * been the value "content" but can be changed. Use null
to indicate no CDATA
+ * processing.
+ */
+ private String cDataTagName;
+
+ /**
+ * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
+ * should be kept as attribute(false
), or they should be converted to
+ * null
(true
)
+ */
+ private boolean convertNilAttributeToNull;
+
+ /**
+ * When creating an XML from JSON Object, an empty tag by default will self-close.
+ * If it has to be closed explicitly, with empty content between start and end tag,
+ * this flag is to be turned on.
+ */
+ private boolean closeEmptyTag;
+
+ /**
+ * This will allow type conversion for values in XML if xsi:type attribute is defined
+ */
+ private Map> xsiTypeMap;
+
+ /**
+ * When parsing the XML into JSON, specifies the tags whose values should be converted
+ * to arrays
+ */
+ private Set forceList;
+
+
+ /**
+ * Flag to indicate whether white space should be trimmed when parsing XML.
+ * The default behaviour is to trim white space. When this is set to false, inputting XML
+ * with tags that are the same as the value of cDataTagName is unsupported. It is recommended to set cDataTagName
+ * to a distinct value in this case.
+ */
+ private boolean shouldTrimWhiteSpace;
+
+ /**
+ * Default parser configuration. Does not keep strings (tries to implicitly convert
+ * values), and the CDATA Tag Name is "content". Trims whitespace.
+ */
+ public XMLParserConfiguration () {
+ super();
+ this.cDataTagName = "content";
+ this.convertNilAttributeToNull = false;
+ this.xsiTypeMap = Collections.emptyMap();
+ this.forceList = Collections.emptySet();
+ this.shouldTrimWhiteSpace = true;
+ }
+
+ /**
+ * Configure the parser string processing and use the default CDATA Tag Name as "content".
+ * @param keepStrings true
to parse all values as string.
+ * false
to try and convert XML string values into a JSON value.
+ * @deprecated This constructor has been deprecated in favor of using the new builder
+ * pattern for the configuration.
+ * This constructor may be removed in a future release.
+ */
+ @Deprecated
+ public XMLParserConfiguration (final boolean keepStrings) {
+ this(keepStrings, "content", false);
+ }
+
+ /**
+ * Configure the parser string processing to try and convert XML values to JSON values and
+ * use the passed CDATA Tag Name the processing value. Pass null
to
+ * disable CDATA processing
+ * @param cDataTagName null
to disable CDATA processing. Any other value
+ * to use that value as the JSONObject key name to process as CDATA.
+ * @deprecated This constructor has been deprecated in favor of using the new builder
+ * pattern for the configuration.
+ * This constructor may be removed in a future release.
+ */
+ @Deprecated
+ public XMLParserConfiguration (final String cDataTagName) {
+ this(false, cDataTagName, false);
+ }
+
+ /**
+ * Configure the parser to use custom settings.
+ * @param keepStrings true
to parse all values as string.
+ * false
to try and convert XML string values into a JSON value.
+ * @param cDataTagName null
to disable CDATA processing. Any other value
+ * to use that value as the JSONObject key name to process as CDATA.
+ * @deprecated This constructor has been deprecated in favor of using the new builder
+ * pattern for the configuration.
+ * This constructor may be removed in a future release.
+ */
+ @Deprecated
+ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
+ super(keepStrings, DEFAULT_MAXIMUM_NESTING_DEPTH);
+ this.cDataTagName = cDataTagName;
+ this.convertNilAttributeToNull = false;
+ }
+
+ /**
+ * Configure the parser to use custom settings.
+ * @param keepStrings true
to parse all values as string.
+ * false
to try and convert XML string values into a JSON value.
+ * @param cDataTagName null
to disable CDATA processing. Any other value
+ * to use that value as the JSONObject key name to process as CDATA.
+ * @param convertNilAttributeToNull true
to parse values with attribute xsi:nil="true" as null.
+ * false
to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
+ * @deprecated This constructor has been deprecated in favor of using the new builder
+ * pattern for the configuration.
+ * This constructor may be removed or marked private in a future release.
+ */
+ @Deprecated
+ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
+ super(false, DEFAULT_MAXIMUM_NESTING_DEPTH);
+ this.keepNumberAsString = keepStrings;
+ this.keepBooleanAsString = keepStrings;
+ this.cDataTagName = cDataTagName;
+ this.convertNilAttributeToNull = convertNilAttributeToNull;
+ }
+
+ /**
+ * Configure the parser to use custom settings.
+ * @param keepStrings true
to parse all values as string.
+ * false
to try and convert XML string values into a JSON value.
+ * @param cDataTagName null
to disable CDATA processing. Any other value
+ * to use that value as the JSONObject key name to process as CDATA.
+ * @param convertNilAttributeToNull true
to parse values with attribute xsi:nil="true" as null.
+ * false
to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
+ * @param xsiTypeMap new HashMap>()
to parse values with attribute
+ * xsi:type="integer" as integer, xsi:type="string" as string
+ * @param forceList new HashSet()
to parse the provided tags' values as arrays
+ * @param maxNestingDepth int
to limit the nesting depth
+ * @param closeEmptyTag boolean
to turn on explicit end tag for tag with empty value
+ */
+ private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
+ final boolean convertNilAttributeToNull, final Map> xsiTypeMap, final Set forceList,
+ final int maxNestingDepth, final boolean closeEmptyTag, final boolean keepNumberAsString, final boolean keepBooleanAsString) {
+ super(false, maxNestingDepth);
+ this.keepNumberAsString = keepNumberAsString;
+ this.keepBooleanAsString = keepBooleanAsString;
+ this.cDataTagName = cDataTagName;
+ this.convertNilAttributeToNull = convertNilAttributeToNull;
+ this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
+ this.forceList = Collections.unmodifiableSet(forceList);
+ this.closeEmptyTag = closeEmptyTag;
+ }
+
+ /**
+ * Provides a new instance of the same configuration.
+ */
+ @Override
+ protected XMLParserConfiguration clone() {
+ // future modifications to this method should always ensure a "deep"
+ // clone in the case of collections. i.e. if a Map is added as a configuration
+ // item, a new map instance should be created and if possible each value in the
+ // map should be cloned as well. If the values of the map are known to also
+ // be immutable, then a shallow clone of the map is acceptable.
+ final XMLParserConfiguration config = new XMLParserConfiguration(
+ this.keepStrings,
+ this.cDataTagName,
+ this.convertNilAttributeToNull,
+ this.xsiTypeMap,
+ this.forceList,
+ this.maxNestingDepth,
+ this.closeEmptyTag,
+ this.keepNumberAsString,
+ this.keepBooleanAsString
+ );
+ config.shouldTrimWhiteSpace = this.shouldTrimWhiteSpace;
+ return config;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies if values should be kept as strings (true
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string)
+ *
+ * @param newVal
+ * new value to use for the keepStrings
configuration option.
+ *
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public XMLParserConfiguration withKeepStrings(final boolean newVal) {
+ XMLParserConfiguration newConfig = this.clone();
+ newConfig.keepStrings = newVal;
+ newConfig.keepNumberAsString = newVal;
+ newConfig.keepBooleanAsString = newVal;
+ return newConfig;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies if numbers should be kept as strings (1
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string)
+ *
+ * @param newVal
+ * new value to use for the keepNumberAsString
configuration option.
+ *
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public XMLParserConfiguration withKeepNumberAsString(final boolean newVal) {
+ XMLParserConfiguration newConfig = this.clone();
+ newConfig.keepNumberAsString = newVal;
+ newConfig.keepStrings = newConfig.keepBooleanAsString && newConfig.keepNumberAsString;
+ return newConfig;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies if booleans should be kept as strings (true
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string)
+ *
+ * @param newVal
+ * new value to use for the withKeepBooleanAsString
configuration option.
+ *
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public XMLParserConfiguration withKeepBooleanAsString(final boolean newVal) {
+ XMLParserConfiguration newConfig = this.clone();
+ newConfig.keepBooleanAsString = newVal;
+ newConfig.keepStrings = newConfig.keepBooleanAsString && newConfig.keepNumberAsString;
+ return newConfig;
+ }
+
+ /**
+ * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
+ * been the value "content" but can be changed. Use null
to indicate no CDATA
+ * processing.
+ *
+ * @return The cDataTagName
configuration value.
+ */
+ public String getcDataTagName() {
+ return this.cDataTagName;
+ }
+
+ /**
+ * When parsing the XML into JSONML, specifies if numbers should be kept as strings (true
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string).
+ *
+ * @return The keepStrings
configuration value.
+ */
+ public boolean isKeepNumberAsString() {
+ return this.keepNumberAsString;
+ }
+
+ /**
+ * When parsing the XML into JSONML, specifies if booleans should be kept as strings (true
), or if
+ * they should try to be guessed into JSON values (numeric, boolean, string).
+ *
+ * @return The keepStrings
configuration value.
+ */
+ public boolean isKeepBooleanAsString() {
+ return this.keepBooleanAsString;
+ }
+
+ /**
+ * The name of the key in a JSON Object that indicates a CDATA section. Historically this has
+ * been the value "content" but can be changed. Use null
to indicate no CDATA
+ * processing.
+ *
+ * @param newVal
+ * new value to use for the cDataTagName
configuration option.
+ *
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public XMLParserConfiguration withcDataTagName(final String newVal) {
+ XMLParserConfiguration newConfig = this.clone();
+ newConfig.cDataTagName = newVal;
+ return newConfig;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
+ * should be kept as attribute(false
), or they should be converted to
+ * null
(true
)
+ *
+ * @return The convertNilAttributeToNull
configuration value.
+ */
+ public boolean isConvertNilAttributeToNull() {
+ return this.convertNilAttributeToNull;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
+ * should be kept as attribute(false
), or they should be converted to
+ * null
(true
)
+ *
+ * @param newVal
+ * new value to use for the convertNilAttributeToNull
configuration option.
+ *
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal) {
+ XMLParserConfiguration newConfig = this.clone();
+ newConfig.convertNilAttributeToNull = newVal;
+ return newConfig;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies that the values with attribute xsi:type
+ * will be converted to target type defined to client in this configuration
+ * {@code Map>} to parse values with attribute
+ * xsi:type="integer" as integer, xsi:type="string" as string
+ * @return xsiTypeMap
unmodifiable configuration map.
+ */
+ public Map> getXsiTypeMap() {
+ return this.xsiTypeMap;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies that the values with attribute xsi:type
+ * will be converted to target type defined to client in this configuration
+ * {@code Map>} to parse values with attribute
+ * xsi:type="integer" as integer, xsi:type="string" as string
+ * @param xsiTypeMap {@code new HashMap>()} to parse values with attribute
+ * xsi:type="integer" as integer, xsi:type="string" as string
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public XMLParserConfiguration withXsiTypeMap(final Map> xsiTypeMap) {
+ XMLParserConfiguration newConfig = this.clone();
+ Map> cloneXsiTypeMap = new HashMap>(xsiTypeMap);
+ newConfig.xsiTypeMap = Collections.unmodifiableMap(cloneXsiTypeMap);
+ return newConfig;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies that tags that will be converted to arrays
+ * in this configuration {@code Set} to parse the provided tags' values as arrays
+ * @return forceList
unmodifiable configuration set.
+ */
+ public Set getForceList() {
+ return this.forceList;
+ }
+
+ /**
+ * When parsing the XML into JSON, specifies that tags that will be converted to arrays
+ * in this configuration {@code Set} to parse the provided tags' values as arrays
+ * @param forceList {@code new HashSet()} to parse the provided tags' values as arrays
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ public XMLParserConfiguration withForceList(final Set forceList) {
+ XMLParserConfiguration newConfig = this.clone();
+ Set cloneForceList = new HashSet(forceList);
+ newConfig.forceList = Collections.unmodifiableSet(cloneForceList);
+ return newConfig;
+ }
+
+ /**
+ * Defines the maximum nesting depth that the parser will descend before throwing an exception
+ * when parsing the XML into JSON. The default max nesting depth is 512, which means the parser
+ * will throw a JsonException if the maximum depth is reached.
+ * Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
+ * which means the parses will go as deep as the maximum call stack size allows.
+ * @param maxNestingDepth the maximum nesting depth allowed to the XML parser
+ * @return The existing configuration will not be modified. A new configuration is returned.
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
+ return super.withMaxNestingDepth(maxNestingDepth);
+ }
+
+ /**
+ * To enable explicit end tag with empty value.
+ * @param closeEmptyTag new value for the closeEmptyTag property
+ * @return same instance of configuration with empty tag config updated
+ */
+ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){
+ XMLParserConfiguration clonedConfiguration = this.clone();
+ clonedConfiguration.closeEmptyTag = closeEmptyTag;
+ return clonedConfiguration;
+ }
+
+ /**
+ * Sets whether whitespace should be trimmed inside of tags. *NOTE* Do not use this if
+ * you expect your XML tags to have names that are the same as cDataTagName as this is unsupported.
+ * cDataTagName should be set to a distinct value in these cases.
+ * @param shouldTrimWhiteSpace boolean to set trimming on or off. Off is default.
+ * @return same instance of configuration with empty tag config updated
+ */
+ public XMLParserConfiguration withShouldTrimWhitespace(boolean shouldTrimWhiteSpace){
+ XMLParserConfiguration clonedConfiguration = this.clone();
+ clonedConfiguration.shouldTrimWhiteSpace = shouldTrimWhiteSpace;
+ return clonedConfiguration;
+ }
+
+ /**
+ * Checks if the parser should automatically close empty XML tags.
+ *
+ * @return {@code true} if empty XML tags should be automatically closed, {@code false} otherwise.
+ */
+ public boolean isCloseEmptyTag() {
+ return this.closeEmptyTag;
+ }
+
+ /**
+ * Checks if the parser should trim white spaces from XML content.
+ *
+ * @return {@code true} if white spaces should be trimmed, {@code false} otherwise.
+ */
+ public boolean shouldTrimWhiteSpace() {
+ return this.shouldTrimWhiteSpace;
+ }
+}
diff --git a/XMLTokener.java b/src/main/java/org/json/XMLTokener.java
similarity index 82%
rename from XMLTokener.java
rename to src/main/java/org/json/XMLTokener.java
index fb54da389..bc18b31c9 100644
--- a/XMLTokener.java
+++ b/src/main/java/org/json/XMLTokener.java
@@ -1,29 +1,11 @@
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;
+
/**
* The XMLTokener extends the JSONTokener to provide additional methods
* for the parsing of XML texts.
@@ -38,6 +20,8 @@ public class XMLTokener extends JSONTokener {
*/
public static final java.util.HashMap entity;
+ private XMLParserConfiguration configuration = XMLParserConfiguration.ORIGINAL;
+
static {
entity = new java.util.HashMap(8);
entity.put("amp", XML.AMP);
@@ -47,6 +31,14 @@ public class XMLTokener extends JSONTokener {
entity.put("quot", XML.QUOT);
}
+ /**
+ * Construct an XMLTokener from a Reader.
+ * @param r A source reader.
+ */
+ public XMLTokener(Reader r) {
+ super(r);
+ }
+
/**
* Construct an XMLTokener from a string.
* @param s A source string.
@@ -55,6 +47,16 @@ public XMLTokener(String s) {
super(s);
}
+ /**
+ * Construct an XMLTokener from a Reader and an XMLParserConfiguration.
+ * @param r A source reader.
+ * @param configuration the configuration that can be used to set certain flags
+ */
+ public XMLTokener(Reader r, XMLParserConfiguration configuration) {
+ super(r);
+ this.configuration = configuration;
+ }
+
/**
* Get the text in the CDATA block.
* @return The string up to the ]]>
.
@@ -80,19 +82,20 @@ public String nextCDATA() throws JSONException {
/**
* Get the next XML outer token, trimming whitespace. There are two kinds
- * of tokens: the '<' character which begins a markup tag, and the content
+ * of tokens: the {@code '<' } character which begins a markup
+ * tag, and the content
* text between markup tags.
*
- * @return A string, or a '<' Character, or null if there is no more
- * source text.
- * @throws JSONException
+ * @return A string, or a {@code '<' } Character, or null if
+ * there is no more source text.
+ * @throws JSONException if a called function has an error
*/
public Object nextContent() throws JSONException {
char c;
StringBuilder sb;
do {
c = next();
- } while (Character.isWhitespace(c));
+ } while (Character.isWhitespace(c) && configuration.shouldTrimWhiteSpace());
if (c == 0) {
return null;
}
@@ -106,7 +109,9 @@ public Object nextContent() throws JSONException {
}
if (c == '<') {
back();
- return sb.toString().trim();
+ if (configuration.shouldTrimWhiteSpace()) {
+ return sb.toString().trim();
+ } else return sb.toString();
}
if (c == '&') {
sb.append(nextEntity(c));
@@ -119,13 +124,15 @@ public Object nextContent() throws JSONException {
/**
+ * {@code
* Return the next entity. These entities are translated to Characters:
- * & ' > < "
.
+ * & ' > < ".
+ * }
* @param ampersand An ampersand character.
* @return A Character or an entity String if the entity is not recognized.
* @throws JSONException If missing ';' in XML entity.
*/
- public Object nextEntity(char ampersand) throws JSONException {
+ public Object nextEntity(@SuppressWarnings("unused") char ampersand) throws JSONException {
StringBuilder sb = new StringBuilder();
for (;;) {
char c = next();
@@ -142,7 +149,7 @@ public Object nextEntity(char ampersand) throws JSONException {
}
/**
- * Unescapes an XML entity encoding;
+ * Unescape an XML entity encoding;
* @param e entity (only the actual entity value, not the preceding & or ending ;
* @return
*/
@@ -154,7 +161,7 @@ static String unescapeEntity(String e) {
// if our entity is an encoded unicode point, parse it.
if (e.charAt(0) == '#') {
int cp;
- if (e.charAt(1) == 'x') {
+ if (e.charAt(1) == 'x' || e.charAt(1) == 'X') {
// hex encoded unicode
cp = Integer.parseInt(e.substring(2), 16);
} else {
@@ -173,11 +180,14 @@ static String unescapeEntity(String e) {
/**
+ * {@code
* Returns the next XML meta token. This is used for skipping over
* and ...?> structures.
- * @return Syntax characters (< > / = ! ?
) are returned as
+ * }
+ * @return {@code Syntax characters (< > / = ! ?) are returned as
* Character, and strings and names are returned as Boolean. We don't care
* what the values actually are.
+ * }
* @throws JSONException If a string is not properly closed or if the XML
* is badly structured.
*/
@@ -222,6 +232,7 @@ public Object nextMeta() throws JSONException {
}
switch (c) {
case 0:
+ throw syntaxError("Unterminated string");
case '<':
case '>':
case '/':
@@ -239,10 +250,12 @@ public Object nextMeta() throws JSONException {
/**
+ * {@code
* Get the next XML Token. These tokens are found inside of angle
- * brackets. It may be one of these characters: / > = ! ?
or it
+ * brackets. It may be one of these characters: / > = ! ? or it
* may be a string wrapped in single quotes or double quotes, or it may be a
* name.
+ * }
* @return a String or a Character.
* @throws JSONException If the XML is not well formed.
*/
@@ -326,9 +339,11 @@ public Object nextToken() throws JSONException {
* Skip characters until past the requested string.
* If it is not found, we are left at the end of the source with a result of false.
* @param to A string to skip past.
- * @throws JSONException
*/
- public boolean skipPast(String to) throws JSONException {
+ // The Android implementation of JSONTokener has a public method of public void skipPast(String to)
+ // even though ours does not have that method, to have API compatibility, our method in the subclass
+ // should match.
+ public void skipPast(String to) {
boolean b;
char c;
int i;
@@ -345,7 +360,7 @@ public boolean skipPast(String to) throws JSONException {
for (i = 0; i < length; i += 1) {
c = next();
if (c == 0) {
- return false;
+ return;
}
circle[i] = c;
}
@@ -372,14 +387,14 @@ public boolean skipPast(String to) throws JSONException {
/* If we exit the loop with b intact, then victory is ours. */
if (b) {
- return true;
+ return;
}
/* Get the next character. If there isn't one, then defeat is ours. */
c = next();
if (c == 0) {
- return false;
+ return;
}
/*
* Shove the character in the circle buffer and advance the
diff --git a/src/main/java/org/json/XMLXsiTypeConverter.java b/src/main/java/org/json/XMLXsiTypeConverter.java
new file mode 100644
index 000000000..ea6739d34
--- /dev/null
+++ b/src/main/java/org/json/XMLXsiTypeConverter.java
@@ -0,0 +1,53 @@
+package org.json;
+/*
+Public Domain.
+*/
+
+/**
+ * Type conversion configuration interface to be used with xsi:type attributes.
+ *
+ * XML Sample
+ * {@code
+ *
+ * 12345
+ * 54321
+ *
+ * }
+ * JSON Output
+ * {@code
+ * {
+ * "root" : {
+ * "asString" : "12345",
+ * "asInt": 54321
+ * }
+ * }
+ * }
+ *
+ * Usage
+ * {@code
+ * Map> xsiTypeMap = new HashMap>();
+ * xsiTypeMap.put("string", new XMLXsiTypeConverter() {
+ * @Override public String convert(final String value) {
+ * return value;
+ * }
+ * });
+ * xsiTypeMap.put("integer", new XMLXsiTypeConverter() {
+ * @Override public Integer convert(final String value) {
+ * return Integer.valueOf(value);
+ * }
+ * });
+ * }
+ *
+ * @author kumar529
+ * @param return type of convert method
+ */
+public interface XMLXsiTypeConverter {
+
+ /**
+ * Converts an XML xsi:type attribute value to the specified type {@code T}.
+ *
+ * @param value The string representation of the XML xsi:type attribute value to be converted.
+ * @return An object of type {@code T} representing the converted value.
+ */
+ T convert(String value);
+}
diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java
new file mode 100644
index 000000000..e5eb9eda8
--- /dev/null
+++ b/src/test/java/org/json/junit/CDLTest.java
@@ -0,0 +1,392 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONArray;
+import org.json.CDL;
+
+/**
+ * Tests for CDL.java.
+ * CDL provides an application level API, but it is not used by the
+ * reference app. To test it, strings will be converted to JSON-Java classes
+ * and then converted back.
+ */
+public class CDLTest {
+
+ /**
+ * String of lines where the column names are in the first row,
+ * and all subsequent rows are values. All keys and values should be legal.
+ */
+ private static final String LINES = "Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" +
+ "val1, val2, val3, val4, val5, val6, val7\n" +
+ "1, 2, 3, 4\t, 5, 6, 7\n" +
+ "true, false, true, true, false, false, false\n" +
+ "0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
+ "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"va'l6\", val7\n";
+
+
+ /**
+ * CDL.toJSONArray() adds all values as strings, with no filtering or
+ * conversions. For testing, this means that the expected JSONObject
+ * values all must be quoted in the cases where the JSONObject parsing
+ * might normally convert the value into a non-string.
+ */
+ private static final String EXPECTED_LINES =
+ "[ " +
+ "{" +
+ "\"Col 1\":\"val1\", " +
+ "\"Col 2\":\"val2\", " +
+ "\"Col 3\":\"val3\", " +
+ "\"Col 4\":\"val4\", " +
+ "\"Col 5\":\"val5\", " +
+ "\"Col 6\":\"val6\", " +
+ "\"Col 7\":\"val7\"" +
+ "}, " +
+ " {" +
+ "\"Col 1\":\"1\", " +
+ "\"Col 2\":\"2\", " +
+ "\"Col 3\":\"3\", " +
+ "\"Col 4\":\"4\", " +
+ "\"Col 5\":\"5\", " +
+ "\"Col 6\":\"6\", " +
+ "\"Col 7\":\"7\"" +
+ "}, " +
+ " {" +
+ "\"Col 1\":\"true\", " +
+ "\"Col 2\":\"false\", " +
+ "\"Col 3\":\"true\", " +
+ "\"Col 4\":\"true\", " +
+ "\"Col 5\":\"false\", " +
+ "\"Col 6\":\"false\", " +
+ "\"Col 7\":\"false\"" +
+ "}, " +
+ "{" +
+ "\"Col 1\":\"0.23\", " +
+ "\"Col 2\":\"57.42\", " +
+ "\"Col 3\":\"5e27\", " +
+ "\"Col 4\":\"-234.879\", " +
+ "\"Col 5\":\"2.34e5\", " +
+ "\"Col 6\":\"0.0\", " +
+ "\"Col 7\":\"9e-3\"" +
+ "}, " +
+ "{" +
+ "\"Col 1\":\"va\tl1\", " +
+ "\"Col 2\":\"v\bal2\", " +
+ "\"Col 3\":\"val3\", " +
+ "\"Col 4\":\"val\f4\", " +
+ "\"Col 5\":\"val5\", " +
+ "\"Col 6\":\"va'l6\", " +
+ "\"Col 7\":\"val7\"" +
+ "}" +
+ "]";
+
+ /**
+ * Attempts to create a JSONArray from a null string.
+ * Expect a NullPointerException.
+ */
+ @Test(expected=NullPointerException.class)
+ public void exceptionOnNullString() {
+ String nullStr = null;
+ CDL.toJSONArray(nullStr);
+ }
+
+ /**
+ * Attempts to create a JSONArray from a string with unbalanced quotes
+ * in column title line. Expects a JSONException.
+ */
+ @Test
+ public void unbalancedQuoteInName() {
+ String badLine = "Col1, \"Col2\nVal1, Val2";
+ try {
+ CDL.toJSONArray(badLine);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Missing close quote '\"'. at 12 [character 0 line 2]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempts to create a JSONArray from a string with unbalanced quotes
+ * in value line. Expects a JSONException.
+ */
+ @Test
+ public void unbalancedQuoteInValue() {
+ String badLine = "Col1, Col2\n\"Val1, Val2";
+ try {
+ CDL.toJSONArray(badLine);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Missing close quote '\"'. at 22 [character 11 line 2]",
+ e.getMessage());
+
+ }
+ }
+
+ /**
+ * Attempts to create a JSONArray from a string with null char
+ * in column title line. Expects a JSONException.
+ */
+ @Test
+ public void nullInName() {
+ String badLine = "C\0ol1, Col2\nVal1, Val2";
+ try {
+ CDL.toJSONArray(badLine);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Bad character 'o' (111). at 2 [character 3 line 1]",
+ e.getMessage());
+
+ }
+ }
+
+ /**
+ * Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote.
+ * Expects a JSONException.
+ */
+ @Test
+ public void unbalancedEscapedQuote(){
+ String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
+ try {
+ CDL.toJSONArray(badLine);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Missing close quote '\"'. at 26 [character 15 line 2]",
+ e.getMessage());
+
+ }
+ }
+
+ /**
+ * Csv parsing skip last row if last field of this row is empty #943
+ */
+ @Test
+ public void csvParsingCatchesLastRow(){
+ String data = "Field 1,Field 2,Field 3\n" +
+ "value11,value12,\n" +
+ "value21,value22,";
+
+ JSONArray jsonArray = CDL.toJSONArray(data);
+
+ JSONArray expectedJsonArray = new JSONArray();
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("Field 1", "value11");
+ jsonObject.put("Field 2", "value12");
+ jsonObject.put("Field 3", "");
+ expectedJsonArray.put(jsonObject);
+
+ jsonObject = new JSONObject();
+ jsonObject.put("Field 1", "value21");
+ jsonObject.put("Field 2", "value22");
+ jsonObject.put("Field 3", "");
+ expectedJsonArray.put(jsonObject);
+
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+ }
+
+ /**
+ * Assert that there is no error for a single escaped quote within a properly embedded quote.
+ */
+ @Test
+ public void singleEscapedQuote(){
+ String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
+ JSONArray jsonArray = CDL.toJSONArray(singleEscape);
+
+ String cdlStr = CDL.toString(jsonArray);
+ assertTrue(cdlStr.contains("Col1"));
+ assertTrue(cdlStr.contains("Col2"));
+ assertTrue(cdlStr.contains("Val1"));
+ assertTrue(cdlStr.contains("\"Val2"));
+ }
+
+ /**
+ * Assert that there is no error for a single escaped quote within a properly
+ * embedded quote when not the last value.
+ */
+ @Test
+ public void singleEscapedQuoteMiddleString(){
+ String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"\nVal 3,Val 4";
+ JSONArray jsonArray = CDL.toJSONArray(singleEscape);
+
+ String cdlStr = CDL.toString(jsonArray);
+ assertTrue(cdlStr.contains("Col1"));
+ assertTrue(cdlStr.contains("Col2"));
+ assertTrue(cdlStr.contains("Val1"));
+ assertTrue(cdlStr.contains("\"Val2"));
+ }
+
+ /**
+ * Attempt to create a JSONArray with an escape quote and no enclosing quotes.
+ * Expects a JSONException.
+ */
+ @Test
+ public void badEscapedQuote(){
+ String badLine = "Col1, Col2\nVal1, \"\"Val2";
+
+ try {
+ CDL.toJSONArray(badLine);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ //System.out.println("Message" + e.getMessage());
+ assertEquals("Expecting an exception message",
+ "Bad character 'V' (86). at 20 [character 9 line 2]",
+ e.getMessage());
+
+ }
+
+ }
+
+ /**
+ * call toString with a null array
+ */
+ @Test(expected=NullPointerException.class)
+ public void nullJSONArrayToString() {
+ CDL.toString((JSONArray)null);
+ }
+
+ /**
+ * Create a JSONArray from an empty string
+ */
+ @Test
+ public void emptyString() {
+ String emptyStr = "";
+ JSONArray jsonArray = CDL.toJSONArray(emptyStr);
+ assertNull("CDL should return null when the input string is empty", jsonArray);
+ }
+
+ /**
+ * Create a JSONArray with only 1 row
+ */
+ @Test
+ public void onlyColumnNames() {
+ String columnNameStr = "col1, col2, col3";
+ JSONArray jsonArray = CDL.toJSONArray(columnNameStr);
+ assertNull("CDL should return null when only 1 row is given",
+ jsonArray);
+ }
+
+ /**
+ * Create a JSONArray from string containing only whitespace and commas
+ */
+ @Test
+ public void emptyLinesToJSONArray() {
+ String str = " , , , \n , , , ";
+ JSONArray jsonArray = CDL.toJSONArray(str);
+ assertNull("JSONArray should be null for no content",
+ jsonArray);
+ }
+
+ /**
+ * call toString with a null array
+ */
+ @Test
+ public void emptyJSONArrayToString() {
+ JSONArray jsonArray = new JSONArray();
+ String str = CDL.toString(jsonArray);
+ assertNull("CDL should return null for toString(null)",
+ str);
+ }
+
+ /**
+ * call toString with a null arrays for names and values
+ */
+ @Test
+ public void nullJSONArraysToString() {
+ String str = CDL.toString(null, null);
+ assertNull("CDL should return null for toString(null)",
+ str);
+ }
+
+ /**
+ * Given a JSONArray that was not built by CDL, some chars may be
+ * found that would otherwise be filtered out by CDL.
+ */
+ @Test
+ public void checkSpecialChars() {
+ JSONArray jsonArray = new JSONArray();
+ JSONObject jsonObject = new JSONObject();
+ jsonArray.put(jsonObject);
+ // \r will be filtered from name
+ jsonObject.put("Col \r1", "V1");
+ // \r will be filtered from value
+ jsonObject.put("Col 2", "V2\r");
+ assertEquals("expected length should be 1", 1, jsonArray.length());
+ String cdlStr = CDL.toString(jsonArray);
+ jsonObject = jsonArray.getJSONObject(0);
+ assertTrue(cdlStr.contains("\"Col 1\""));
+ assertTrue(cdlStr.contains("Col 2"));
+ assertTrue(cdlStr.contains("V1"));
+ assertTrue(cdlStr.contains("\"V2\""));
+ }
+
+ /**
+ * Create a JSONArray from a string of lines
+ */
+ @Test
+ public void textToJSONArray() {
+ JSONArray jsonArray = CDL.toJSONArray(LINES);
+ JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+ }
+ @Test
+ public void textToJSONArrayPipeDelimited() {
+ char delimiter = '|';
+ JSONArray jsonArray = CDL.toJSONArray(LINES.replaceAll(",", String.valueOf(delimiter)), delimiter);
+ JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+ }
+
+ /**
+ * Create a JSONArray from a JSONArray of titles and a
+ * string of value lines
+ */
+ @Test
+ public void jsonArrayToJSONArray() {
+ String nameArrayStr = "[\"Col1\", \"Col2\"]";
+ String values = "V1, V2";
+ JSONArray nameJSONArray = new JSONArray(nameArrayStr);
+ JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
+ JSONArray expectedJsonArray = new JSONArray("[{\"Col1\":\"V1\",\"Col2\":\"V2\"}]");
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+ }
+
+ /**
+ * Create a JSONArray from a string of lines,
+ * then convert to string and then back to JSONArray
+ */
+ @Test
+ public void textToJSONArrayAndBackToString() {
+ JSONArray jsonArray = CDL.toJSONArray(LINES);
+ String jsonStr = CDL.toString(jsonArray);
+ JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
+ JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
+ Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
+ }
+
+ /**
+ * Create a JSONArray from a string of lines,
+ * then convert to string and then back to JSONArray
+ * with a custom delimiter
+ */
+ @Test
+ public void textToJSONArrayAndBackToStringCustomDelimiter() {
+ JSONArray jsonArray = CDL.toJSONArray(LINES, ',');
+ String jsonStr = CDL.toString(jsonArray, ';');
+ JSONArray finalJsonArray = CDL.toJSONArray(jsonStr, ';');
+ JSONArray expectedJsonArray = new JSONArray(EXPECTED_LINES);
+ Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/json/junit/CookieListTest.java b/src/test/java/org/json/junit/CookieListTest.java
new file mode 100644
index 000000000..0af96401b
--- /dev/null
+++ b/src/test/java/org/json/junit/CookieListTest.java
@@ -0,0 +1,190 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.json.*;
+import org.junit.Test;
+
+import com.jayway.jsonpath.*;
+
+/**
+ * HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
+ *
+ * A cookie list is a JSONObject whose members are presumed to be cookie
+ * name/value pairs. Entries are unescaped while being added, and escaped in
+ * the toString() output.
+ * Unescaping means to convert %hh hex strings to the ascii equivalent
+ * and converting '+' to ' '.
+ * Escaping converts '+', '%', '=', ';' and ascii control chars to %hh hex strings.
+ *
+ * CookieList should not be considered as just a list of Cookie objects:
+ * - CookieList stores a cookie name/value pair as a single entry; Cookie stores
+ * it as 2 entries (key="name" and key="value").
+ * - CookieList requires multiple name/value pairs as input; Cookie allows the
+ * 'secure' name with no associated value
+ * - CookieList has no special handling for attribute name/value pairs.
+ */
+public class CookieListTest {
+
+ /**
+ * Attempts to create a CookieList from a null string.
+ * Expects a NullPointerException.
+ */
+ @Test(expected=NullPointerException.class)
+ public void nullCookieListException() {
+ String cookieStr = null;
+ CookieList.toJSONObject(cookieStr);
+ }
+
+ /**
+ * Attempts to create a CookieList from a malformed string.
+ * Expects a JSONException.
+ */
+ @Test
+ public void malFormedCookieListException() {
+ String cookieStr = "thisCookieHasNoEqualsChar";
+ try {
+ CookieList.toJSONObject(cookieStr);
+ fail("should throw an exception");
+ } catch (JSONException e) {
+ /**
+ * Not sure of the missing char, but full string compare fails
+ */
+ assertEquals("Expecting an exception message",
+ "Expected '=' and instead saw '' at 25 [character 26 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Creates a CookieList from an empty string.
+ */
+ @Test
+ public void emptyStringCookieList() {
+ String cookieStr = "";
+ JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
+ assertTrue(jsonObject.isEmpty());
+ }
+
+ /**
+ * CookieList with the simplest cookie - a name/value pair with no delimiter.
+ */
+ @Test
+ public void simpleCookieList() {
+ String cookieStr = "SID=31d4d96e407aad42";
+ JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
+ }
+
+ /**
+ * CookieList with a single a cookie which has a name/value pair and delimiter.
+ */
+ @Test
+ public void simpleCookieListWithDelimiter() {
+ String cookieStr = "SID=31d4d96e407aad42;";
+ JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
+ }
+
+ /**
+ * CookieList with multiple cookies consisting of name/value pairs
+ * with delimiters.
+ */
+ @Test
+ public void multiPartCookieList() {
+ String cookieStr =
+ "name1=myCookieValue1; "+
+ " name2=myCookieValue2;"+
+ "name3=myCookieValue3;"+
+ " name4=myCookieValue4; "+
+ "name5=myCookieValue5;"+
+ " name6=myCookieValue6;";
+ JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
+ assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
+ assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
+ assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
+ assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
+ assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
+ }
+
+ /**
+ * CookieList from a JSONObject with valid key and null value
+ */
+ @Test
+ public void convertCookieListWithNullValueToString() {
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("key", JSONObject.NULL);
+ String cookieToStr = CookieList.toString(jsonObject);
+ assertTrue("toString() should be empty", "".equals(cookieToStr));
+ }
+
+ /**
+ * CookieList with multiple entries converted to a JSON document.
+ */
+ @Test
+ public void convertCookieListToString() {
+ String cookieStr =
+ "name1=myCookieValue1; "+
+ " name2=myCookieValue2;"+
+ "name3=myCookieValue3;"+
+ " name4=myCookieValue4; "+
+ "name5=myCookieValue5;"+
+ " name6=myCookieValue6;";
+ JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
+ // exercise CookieList.toString()
+ String cookieListString = CookieList.toString(jsonObject);
+ // have to convert it back for validation
+ jsonObject = CookieList.toJSONObject(cookieListString);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
+ assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
+ assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
+ assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
+ assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
+ assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
+ }
+
+ /**
+ * CookieList with multiple entries and some '+' chars and URL-encoded
+ * values converted to a JSON document.
+ */
+ @Test
+ public void convertEncodedCookieListToString() {
+ String cookieStr =
+ "name1=myCookieValue1; "+
+ " name2=my+Cookie+Value+2;"+
+ "name3=my%2BCookie%26Value%3B3%3D;"+
+ " name4=my%25CookieValue4; "+
+ "name5=myCookieValue5;"+
+ " name6=myCookieValue6;";
+ JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("Expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
+ assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(jsonObject.query("/name2")));
+ assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(jsonObject.query("/name3")));
+ assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(jsonObject.query("/name4")));
+ assertTrue("expected my%CookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
+ assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
+ }
+}
diff --git a/src/test/java/org/json/junit/CookieTest.java b/src/test/java/org/json/junit/CookieTest.java
new file mode 100644
index 000000000..edd8a7eeb
--- /dev/null
+++ b/src/test/java/org/json/junit/CookieTest.java
@@ -0,0 +1,242 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+
+import org.json.*;
+import org.junit.Test;
+
+
+/**
+ * HTTP cookie specification: RFC6265
+ *
+ * At its most basic, a cookie is a name=value pair. The value may be subdivided
+ * into other cookies, but that is not tested here. The cookie may also include
+ * certain named attributes, delimited by semicolons.
+ *
+ * The Cookie.toString() method emits certain attributes if present: expires,
+ * domain, path, secure. All but secure are name-value pairs. Other attributes
+ * are not included in the toString() output.
+ *
+ * A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values.
+ */
+public class CookieTest {
+
+ /**
+ * Attempts to create a JSONObject from a null string.
+ * Expects a NullPointerException.
+ */
+ @Test(expected=NullPointerException.class)
+ public void nullCookieException() {
+ String cookieStr = null;
+ Cookie.toJSONObject(cookieStr);
+ }
+
+ /**
+ * Attempts to create a JSONObject from a cookie string with
+ * no '=' char.
+ * Expects a JSONException.
+ */
+ @Test
+ public void malFormedNameValueException() {
+ String cookieStr = "thisCookieHasNoEqualsChar";
+ try {
+ Cookie.toJSONObject(cookieStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Expected '=' and instead saw '' at 25 [character 26 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempts to create a JSONObject from a cookie string
+ * with embedded ';' char.
+ * Expects a JSONException.
+ */
+ @Test
+ public void booleanAttribute() {
+ String cookieStr = "this=Cookie;myAttribute";
+ JSONObject jo = Cookie.toJSONObject(cookieStr);
+ assertTrue("has key 'name'", jo.has("name"));
+ assertTrue("has key 'value'", jo.has("value"));
+ assertTrue("has key 'myAttribute'", jo.has("myattribute"));
+ }
+
+ /**
+ * Attempts to create a JSONObject from an empty cookie string.
+ * Note: Cookie throws an exception, but CookieList does not.
+ * Expects a JSONException
+ */
+ @Test
+ public void emptyStringCookieException() {
+ String cookieStr = "";
+ try {
+ Cookie.toJSONObject(cookieStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Cookies must have a 'name'",
+ e.getMessage());
+ }
+ }
+ /**
+ *
+ * Attempts to create a JSONObject from an cookie string where the name is blank.
+ * Note: Cookie throws an exception, but CookieList does not.
+ * Expects a JSONException
+ */
+ @Test
+ public void emptyNameCookieException() {
+ String cookieStr = " = value ";
+ try {
+ Cookie.toJSONObject(cookieStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Cookies must have a 'name'",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Cookie from a simple name/value pair with no delimiter
+ */
+ @Test
+ public void simpleCookie() {
+ String cookieStr = "SID=31d4d96e407aad42";
+ String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}";
+ JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Store a cookie with all of the supported attributes in a
+ * JSONObject. The secure attribute, which has no value, is treated
+ * as a boolean.
+ */
+ @Test
+ public void multiPartCookie() {
+ String cookieStr =
+ "PH=deleted; "+
+ " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
+ "path=/; "+
+ " domain=.yahoo.com;"+
+ "secure";
+ String expectedCookieStr =
+ "{"+
+ "\"name\":\"PH\","+
+ "\"value\":\"deleted\","+
+ "\"path\":\"/\","+
+ "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
+ "\"domain\":\".yahoo.com\","+
+ "\"secure\":true"+
+ "}";
+ JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Cookie.toString() will emit the non-standard "thiswont=beIncluded"
+ * attribute, and the attribute is still stored in the JSONObject.
+ * This test confirms both behaviors.
+ */
+ @Test
+ public void convertCookieToString() {
+ String cookieStr =
+ "PH=deleted; "+
+ " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
+ "path=/; "+
+ " domain=.yahoo.com;"+
+ "thisWont=beIncluded;"+
+ "secure";
+ String expectedCookieStr =
+ "{\"thiswont\":\"beIncluded\","+
+ "\"path\":\"/\","+
+ "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
+ "\"domain\":\".yahoo.com\","+
+ "\"name\":\"PH\","+
+ "\"secure\":true,"+
+ "\"value\":\"deleted\"}";
+ // Add the nonstandard attribute to the expected cookie string
+ String expectedDirectCompareCookieStr = expectedCookieStr;
+ // convert all strings into JSONObjects
+ JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
+ JSONObject expectedDirectCompareJsonObject =
+ new JSONObject(expectedDirectCompareCookieStr);
+ // emit the string
+ String cookieToStr = Cookie.toString(jsonObject);
+ // create a final JSONObject from the string
+ JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr);
+ // JSONObject should contain the nonstandard string
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedDirectCompareJsonObject);
+ // JSONObject -> string -> JSONObject should not contain the nonstandard string
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+
+ /**
+ * A string may be URL-encoded when converting to JSONObject.
+ * If found, '+' is converted to ' ', and %hh hex strings are converted
+ * to their ascii char equivalents. This test confirms the decoding
+ * behavior.
+ */
+ @Test
+ public void convertEncodedCookieToString() {
+ String cookieStr =
+ "PH=deleted; "+
+ " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+
+ "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+
+ " domain=.yahoo.com;"+
+ "secure";
+ String expectedCookieStr =
+ "{\"path\":\"/+this/is&/a/spec;segment=\","+
+ "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
+ "\"domain\":\".yahoo.com\","+
+ "\"name\":\"PH\","+
+ "\"secure\":true,"+
+ "\"value\":\"deleted\"}";
+ JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
+ String cookieToStr = Cookie.toString(jsonObject);
+ JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+
+ /**
+ * A public API method performs a URL encoding for selected chars
+ * in a string. Control chars, '+', '%', '=', ';' are all encoded
+ * as %hh hex strings. The string is also trimmed.
+ * This test confirms that behavior.
+ */
+ @Test
+ public void escapeString() {
+ String str = " +%\r\n\t\b%=;;; ";
+ String expectedStr = "%2b%25%0d%0a%09%08%25%3d%3b%3b%3b";
+ String actualStr = Cookie.escape(str);
+ assertTrue("expect escape() to encode correctly. Actual: " +actualStr+
+ " expected: " +expectedStr, expectedStr.equals(actualStr));
+ }
+
+ /**
+ * A public API method performs URL decoding for strings.
+ * '+' is converted to space and %hh hex strings are converted to
+ * their ascii equivalent values. The string is not trimmed.
+ * This test confirms that behavior.
+ */
+ @Test
+ public void unescapeString() {
+ String str = " +%2b%25%0d%0a%09%08%25%3d%3b%3b%3b+ ";
+ String expectedStr = " +%\r\n\t\b%=;;; ";
+ String actualStr = Cookie.unescape(str);
+ assertTrue("expect unescape() to decode correctly. Actual: " +actualStr+
+ " expected: " +expectedStr, expectedStr.equals(actualStr));
+ }
+}
diff --git a/src/test/java/org/json/junit/EnumTest.java b/src/test/java/org/json/junit/EnumTest.java
new file mode 100644
index 000000000..1496a636a
--- /dev/null
+++ b/src/test/java/org/json/junit/EnumTest.java
@@ -0,0 +1,433 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Map;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.json.junit.data.MyEnum;
+import org.json.junit.data.MyEnumClass;
+import org.json.junit.data.MyEnumField;
+import org.junit.Test;
+
+import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.JsonPath;
+
+/**
+ * Enums are not explicitly supported in JSON-Java. But because enums act like
+ * classes, all required behavior is already be present in some form.
+ * These tests explore how enum serialization works with JSON-Java.
+ */
+public class EnumTest {
+
+ /**
+ * To serialize an enum by its getters, use the JSONObject Object constructor.
+ * The JSONObject ctor handles enum like any other bean. A JSONobject
+ * is created whose entries are the getter name/value pairs.
+ */
+ @Test
+ public void jsonObjectFromEnum() {
+ // If there are no getters then the object is empty.
+ MyEnum myEnum = MyEnum.VAL2;
+ JSONObject jsonObject = new JSONObject(myEnum);
+ assertTrue("simple enum has no getters", jsonObject.isEmpty());
+
+ // enum with a getters should create a non-empty object
+ MyEnumField myEnumField = MyEnumField.VAL2;
+ jsonObject = new JSONObject(myEnumField);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider()
+ .parse(jsonObject.toString());
+ assertTrue("expecting 2 items in top level object", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expecting val 2", "val 2".equals(jsonObject.query("/value")));
+ assertTrue("expecting 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
+
+ /**
+ * class which contains enum instances. Each enum should be stored
+ * in its own JSONObject
+ */
+ MyEnumClass myEnumClass = new MyEnumClass();
+ myEnumClass.setMyEnum(MyEnum.VAL1);
+ myEnumClass.setMyEnumField(MyEnumField.VAL3);
+ jsonObject = new JSONObject(myEnumClass);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected 2 myEnumField items", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
+ assertTrue("expected 0 myEnum items", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
+
+ assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField")));
+ assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum")));
+ }
+
+ /**
+ * To serialize an enum by its set of allowed values, use getNames()
+ * and the JSONObject Object with names constructor.
+ */
+ @Test
+ public void jsonObjectFromEnumWithNames() {
+ String [] names;
+ JSONObject jsonObject;
+
+ MyEnum myEnum = MyEnum.VAL1;
+ names = JSONObject.getNames(myEnum);
+ // The values will be MyEnum fields
+ jsonObject = new JSONObject(myEnum, names);
+
+ // validate JSON object
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
+ assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
+ assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
+
+ MyEnumField myEnumField = MyEnumField.VAL3;
+ names = JSONObject.getNames(myEnumField);
+ // The values will be MyEnmField fields
+ jsonObject = new JSONObject(myEnumField, names);
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
+ assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
+ assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
+ }
+
+ /**
+ * Verify that enums are handled consistently between JSONArray and JSONObject
+ */
+ @Test
+ public void verifyEnumConsistency(){
+ JSONObject jo = new JSONObject();
+
+ jo.put("value", MyEnumField.VAL2);
+ String expected="{\"value\":\"VAL2\"}";
+ String actual = jo.toString();
+ assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+
+ jo.accumulate("value", MyEnumField.VAL1);
+ expected="{\"value\":[\"VAL2\",\"VAL1\"]}";
+ actual = jo.toString();
+ assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+
+ jo.remove("value");
+ jo.append("value", MyEnumField.VAL1);
+ expected="{\"value\":[\"VAL1\"]}";
+ actual = jo.toString();
+ assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+
+ jo.put("value", EnumSet.of(MyEnumField.VAL2));
+ expected="{\"value\":[\"VAL2\"]}";
+ actual = jo.toString();
+ assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+
+ JSONArray ja = new JSONArray();
+ ja.put(MyEnumField.VAL2);
+ jo.put("value", ja);
+ actual = jo.toString();
+ assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+
+ jo.put("value", new MyEnumField[]{MyEnumField.VAL2});
+ actual = jo.toString();
+ assertTrue("Expected "+expected+" but actual was "+actual, expected.equals(actual));
+
+ }
+
+ /**
+ * To serialize by assigned value, use the put() methods. The value
+ * will be stored as a enum type.
+ */
+ @Test
+ public void enumPut() {
+ JSONObject jsonObject = new JSONObject();
+ MyEnum myEnum = MyEnum.VAL2;
+ jsonObject.put("myEnum", myEnum);
+ MyEnumField myEnumField = MyEnumField.VAL1;
+ jsonObject.putOnce("myEnumField", myEnumField);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level objects", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
+ assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/myEnumField")));
+
+ JSONArray jsonArray = new JSONArray();
+ jsonArray.put(myEnum);
+ jsonArray.put(1, myEnumField);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 2 top level objects", ((List>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
+ assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonArray.query("/1")));
+
+ /**
+ * Leaving these tests because they exercise get, opt, and remove
+ */
+ assertTrue("expecting myEnum value", MyEnum.VAL2.equals(jsonArray.get(0)));
+ assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.opt(1)));
+ assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
+ }
+
+ /**
+ * The default action of valueToString() is to call object.toString().
+ * For enums, this means the assigned value will be returned as a string.
+ */
+ @Test
+ public void enumValueToString() {
+ String expectedStr1 = "\"VAL1\"";
+ String expectedStr2 = "\"VAL1\"";
+ MyEnum myEnum = MyEnum.VAL1;
+ MyEnumField myEnumField = MyEnumField.VAL1;
+ MyEnumClass myEnumClass = new MyEnumClass();
+
+ String str1 = JSONObject.valueToString(myEnum);
+ assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1,
+ str1.equals(expectedStr1));
+ String str2 = JSONObject.valueToString(myEnumField);
+ assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2,
+ str2.equals(expectedStr2));
+
+ /**
+ * However, an enum within another class will not be rendered
+ * unless that class overrides default toString()
+ */
+ String expectedStr3 = "\"org.json.junit.data.MyEnumClass@";
+ myEnumClass.setMyEnum(MyEnum.VAL1);
+ myEnumClass.setMyEnumField(MyEnumField.VAL1);
+ String str3 = JSONObject.valueToString(myEnumClass);
+ assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3,
+ str3.startsWith(expectedStr3));
+ }
+
+ /**
+ * In whatever form the enum was added to the JSONObject or JSONArray,
+ * json[Object|Array].toString should serialize it in a reasonable way.
+ */
+ @Test
+ public void enumToString() {
+ MyEnum myEnum = MyEnum.VAL2;
+ JSONObject jsonObject = new JSONObject(myEnum);
+ String expectedStr = "{}";
+ assertTrue("myEnum toString() should be empty", expectedStr.equals(jsonObject.toString()));
+
+ MyEnumField myEnumField = MyEnumField.VAL2;
+ jsonObject = new JSONObject(myEnumField);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
+
+ MyEnumClass myEnumClass = new MyEnumClass();
+ myEnumClass.setMyEnum(MyEnum.VAL1);
+ myEnumClass.setMyEnumField(MyEnumField.VAL3);
+ jsonObject = new JSONObject(myEnumClass);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
+ assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
+
+ String [] names = JSONObject.getNames(myEnum);
+ jsonObject = new JSONObject(myEnum, names);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
+ assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
+ assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
+
+ names = JSONObject.getNames(myEnumField);
+ jsonObject = new JSONObject(myEnumField, names);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
+ assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
+ assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
+
+ expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
+ jsonObject = new JSONObject();
+ jsonObject.putOpt("myEnum", myEnum);
+ jsonObject.putOnce("myEnumField", myEnumField);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
+ assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/myEnumField")));
+
+ JSONArray jsonArray = new JSONArray();
+ jsonArray.put(myEnum);
+ jsonArray.put(1, myEnumField);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 2 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
+ assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonArray.query("/1")));
+ }
+
+ /**
+ * Wrap should handle enums exactly as a value type like Integer, Boolean, or String.
+ */
+ @Test
+ public void wrap() {
+ assertTrue("simple enum has no getters", JSONObject.wrap(MyEnum.VAL2) instanceof MyEnum);
+
+ MyEnumField myEnumField = MyEnumField.VAL2;
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("enum",myEnumField);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 1 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/enum")));
+
+ MyEnumClass myEnumClass = new MyEnumClass();
+ myEnumClass.setMyEnum(MyEnum.VAL1);
+ myEnumClass.setMyEnumField(MyEnumField.VAL3);
+ jsonObject = (JSONObject)JSONObject.wrap(myEnumClass);
+
+ // validate JSON content
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected VAL3", "VAL3".equals((JsonPath.read(doc, "$.myEnumField"))));
+ assertTrue("expected VAL1", "VAL1".equals((JsonPath.read(doc, "$.myEnum"))));
+
+ assertTrue("expecting MyEnumField.VAL3", MyEnumField.VAL3.equals(jsonObject.query("/myEnumField")));
+ assertTrue("expecting MyEnum.VAL1", MyEnum.VAL1.equals(jsonObject.query("/myEnum")));
+ }
+
+ /**
+ * It was determined that some API methods should be added to
+ * support enums:
+ * JSONObject.getEnum(class, key)
+ * JSONObject.optEnum(class, key)
+ * JSONObject.optEnum(class, key, default)
+ * JSONArray.getEnum(class, index)
+ * JSONArray.optEnum(class, index)
+ * JSONArray.optEnum(class, index, default)
+ *
+ * Exercise these enum API methods on JSONObject and JSONArray
+ */
+ @Test
+ public void enumAPI() {
+ MyEnumClass myEnumClass = new MyEnumClass();
+ myEnumClass.setMyEnum(MyEnum.VAL1);
+ MyEnumField myEnumField = MyEnumField.VAL2;
+
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("strKey", "value");
+ jsonObject.put("strKey2", "VAL1");
+ jsonObject.put("enumKey", myEnumField);
+ jsonObject.put("enumClassKey", myEnumClass);
+
+ // get a plain old enum
+ MyEnumField actualEnum = jsonObject.getEnum(MyEnumField.class, "enumKey");
+ assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2);
+
+ // try to get the wrong value
+ try {
+ actualEnum = jsonObject.getEnum(MyEnumField.class, "strKey");
+ assertTrue("should throw an exception for wrong key", false);
+ } catch (Exception ignored) {}
+
+ // get a class that contains an enum
+ MyEnumClass actualEnumClass = (MyEnumClass)jsonObject.get("enumClassKey");
+ assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+
+ // opt a plain old enum
+ actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey");
+ assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+
+ // opt the wrong value
+ actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey");
+ assertTrue("opt null", actualEnum == null);
+
+ // opt a class that contains an enum
+ actualEnumClass = (MyEnumClass)jsonObject.opt("enumClassKey");
+ assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+
+ // opt with default a plain old enum
+ actualEnum = jsonObject.optEnum(MyEnumField.class, "enumKey", null);
+ assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+
+ // opt with default the wrong value
+ actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey", null);
+ assertNull("opt null", actualEnum);
+
+ // opt with default the string value
+ actualEnum = jsonObject.optEnum(MyEnumField.class, "strKey2", null);
+ assertEquals(MyEnumField.VAL1, actualEnum);
+
+ // opt with default an index that does not exist
+ actualEnum = jsonObject.optEnum(MyEnumField.class, "noKey", null);
+ assertNull("opt null", actualEnum);
+
+ assertNull("Expected Null when the enum class is null",
+ jsonObject.optEnum(null, "enumKey"));
+
+ /**
+ * Exercise the proposed enum API methods on JSONArray
+ */
+ JSONArray jsonArray = new JSONArray();
+ jsonArray.put("value");
+ jsonArray.put(myEnumField);
+ jsonArray.put(myEnumClass);
+
+ // get a plain old enum
+ actualEnum = jsonArray.getEnum(MyEnumField.class, 1);
+ assertTrue("get myEnumField", actualEnum == MyEnumField.VAL2);
+
+ // try to get the wrong value
+ try {
+ actualEnum = jsonArray.getEnum(MyEnumField.class, 0);
+ assertTrue("should throw an exception for wrong index", false);
+ } catch (Exception ignored) {}
+
+ // get a class that contains an enum
+ actualEnumClass = (MyEnumClass)jsonArray.get(2);
+ assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+
+ // opt a plain old enum
+ actualEnum = jsonArray.optEnum(MyEnumField.class, 1);
+ assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+
+ // opt the wrong value
+ actualEnum = jsonArray.optEnum(MyEnumField.class, 0);
+ assertTrue("opt null", actualEnum == null);
+
+ // opt a class that contains an enum
+ actualEnumClass = (MyEnumClass)jsonArray.opt(2);
+ assertTrue("get enum", actualEnumClass.getMyEnum() == MyEnum.VAL1);
+
+ // opt with default a plain old enum
+ actualEnum = jsonArray.optEnum(MyEnumField.class, 1, null);
+ assertTrue("opt myEnumField", actualEnum == MyEnumField.VAL2);
+
+ // opt with default the wrong value
+ actualEnum = jsonArray.optEnum(MyEnumField.class, 0, null);
+ assertTrue("opt null", actualEnum == null);
+
+ // opt with default an index that does not exist
+ actualEnum = jsonArray.optEnum(MyEnumField.class, 3, null);
+ assertTrue("opt null", actualEnum == null);
+
+ }
+}
diff --git a/src/test/java/org/json/junit/HTTPTest.java b/src/test/java/org/json/junit/HTTPTest.java
new file mode 100644
index 000000000..703d5ad2f
--- /dev/null
+++ b/src/test/java/org/json/junit/HTTPTest.java
@@ -0,0 +1,200 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+
+import org.json.*;
+import org.junit.Test;
+
+
+/**
+ * Unit tests for JSON-Java HTTP.java. See RFC7230.
+ */
+public class HTTPTest {
+
+ /**
+ * Attempt to call HTTP.toJSONObject() with a null string
+ * Expects a NUllPointerException.
+ */
+ @Test(expected=NullPointerException.class)
+ public void nullHTTPException() {
+ String httpStr = null;
+ HTTP.toJSONObject(httpStr);
+ }
+
+ /**
+ * Attempt to call HTTP.toJSONObject() with a string containing
+ * an empty object. Expects a JSONException.
+ */
+ @Test
+ public void notEnoughHTTPException() {
+ String httpStr = "{}";
+ JSONObject jsonObject = new JSONObject(httpStr);
+ try {
+ HTTP.toString(jsonObject);
+ assertTrue("Expected to throw exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "Not enough material for an HTTP header.".equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Calling HTTP.toJSONObject() with an empty string will result in a
+ * populated JSONObject with keys but no values for Request-URI, Method,
+ * and HTTP-Version.
+ */
+ @Test
+ public void emptyStringHTTPRequest() {
+ String httpStr = "";
+ String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}";
+ JSONObject jsonObject = HTTP.toJSONObject(httpStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Call HTTP.toJSONObject() with a Request-URI, Method,
+ * and HTTP-Version.
+ */
+ @Test
+ public void simpleHTTPRequest() {
+ String httpStr = "GET /hello.txt HTTP/1.1";
+ String expectedHTTPStr =
+ "{\"Request-URI\":\"/hello.txt\",\"Method\":\"GET\",\"HTTP-Version\":\"HTTP/1.1\"}";
+ JSONObject jsonObject = HTTP.toJSONObject(httpStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Call HTTP.toJSONObject() with a response string containing a
+ * HTTP-Version, Status-Code, and Reason.
+ */
+ @Test
+ public void simpleHTTPResponse() {
+ String httpStr = "HTTP/1.1 200 OK";
+ String expectedHTTPStr =
+ "{\"HTTP-Version\":\"HTTP/1.1\",\"Status-Code\":\"200\",\"Reason-Phrase\":\"OK\"}";
+ JSONObject jsonObject = HTTP.toJSONObject(httpStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Call HTTP.toJSONObject() with a full request string including
+ * request headers.
+ */
+ @Test
+ public void extendedHTTPRequest() {
+ String httpStr =
+ "POST /enlighten/calais.asmx HTTP/1.1\n"+
+ "Host: api.opencalais.com\n"+
+ "Content-Type: text/xml; charset=utf-8\n"+
+ "Content-Length: 100\n"+
+ "SOAPAction: \"http://clearforest.com/Enlighten\"";
+ String expectedHTTPStr =
+ "{"+
+ "\"Request-URI\":\"/enlighten/calais.asmx\","+
+ "\"Host\":\"api.opencalais.com\","+
+ "\"Method\":\"POST\","+
+ "\"HTTP-Version\":\"HTTP/1.1\","+
+ "\"Content-Length\":\"100\","+
+ "\"Content-Type\":\"text/xml; charset=utf-8\"}";
+ JSONObject jsonObject = HTTP.toJSONObject(httpStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
+ /**
+ * Not too easy for JSONObject to parse a string with embedded quotes.
+ * For the sake of the test, add it here.
+ */
+ expectedJsonObject.put("SOAPAction","\"http://clearforest.com/Enlighten\"");
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Call HTTP.toJSONObject() with a full response string including
+ * response headers.
+ */
+ @Test
+ public void extendedHTTPResponse() {
+ String httpStr =
+ "HTTP/1.1 200 OK\n"+
+ "Content-Type: text/xml; charset=utf-8\n"+
+ "Content-Length: 100\n";
+ String expectedHTTPStr =
+ "{\"HTTP-Version\":\"HTTP/1.1\","+
+ "\"Status-Code\":\"200\","+
+ "\"Content-Length\":\"100\","+
+ "\"Reason-Phrase\":\"OK\","+
+ "\"Content-Type\":\"text/xml; charset=utf-8\"}";
+ JSONObject jsonObject = HTTP.toJSONObject(httpStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Call HTTP.toJSONObject() with a full POST request string including
+ * response headers, then convert it back into an HTTP string.
+ */
+ @Test
+ public void convertHTTPRequestToString() {
+ String httpStr =
+ "POST /enlighten/calais.asmx HTTP/1.1\n"+
+ "Host: api.opencalais.com\n"+
+ "Content-Type: text/xml; charset=utf-8\n"+
+ "Content-Length: 100";
+ String expectedHTTPStr =
+ "{"+
+ "\"Request-URI\":\"/enlighten/calais.asmx\","+
+ "\"Host\":\"api.opencalais.com\","+
+ "\"Method\":\"POST\","+
+ "\"HTTP-Version\":\"HTTP/1.1\","+
+ "\"Content-Length\":\"100\","+
+ "\"Content-Type\":\"text/xml; charset=utf-8\"}";
+ JSONObject jsonObject = HTTP.toJSONObject(httpStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
+ String httpToStr = HTTP.toString(jsonObject);
+ /**
+ * JSONObject objects to crlfs and any trailing chars.
+ * For the sake of the test, simplify the resulting string
+ */
+ httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
+ httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
+ JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Call HTTP.toJSONObject() with a full response string including
+ * response headers, then convert it back into an HTTP string.
+ */
+ @Test
+ public void convertHTTPResponseToString() {
+ String httpStr =
+ "HTTP/1.1 200 OK\n"+
+ "Content-Type: text/xml; charset=utf-8\n"+
+ "Content-Length: 100\n";
+ String expectedHTTPStr =
+ "{\"HTTP-Version\":\"HTTP/1.1\","+
+ "\"Status-Code\":\"200\","+
+ "\"Content-Length\":\"100\","+
+ "\"Reason-Phrase\":\"OK\","+
+ "\"Content-Type\":\"text/xml; charset=utf-8\"}";
+ JSONObject jsonObject = HTTP.toJSONObject(httpStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
+ String httpToStr = HTTP.toString(jsonObject);
+ /**
+ * JSONObject objects to crlfs and any trailing chars.
+ * For the sake of the test, simplify the resulting string
+ */
+ httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
+ httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
+ JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+}
diff --git a/src/test/java/org/json/junit/HTTPTokenerTest.java b/src/test/java/org/json/junit/HTTPTokenerTest.java
new file mode 100644
index 000000000..28dd40353
--- /dev/null
+++ b/src/test/java/org/json/junit/HTTPTokenerTest.java
@@ -0,0 +1,107 @@
+package org.json.junit;
+
+import org.json.HTTPTokener;
+import org.json.JSONException;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+/**
+ * Tests for JSON-Java HTTPTokener.java
+ */
+public class HTTPTokenerTest {
+
+ /**
+ * Test parsing a simple unquoted token.
+ */
+ @Test
+ public void parseSimpleToken() {
+ HTTPTokener tokener = new HTTPTokener("Content-Type");
+ String token = tokener.nextToken();
+ assertEquals("Content-Type", token);
+ }
+
+ /**
+ * Test parsing multiple tokens separated by whitespace.
+ */
+ @Test
+ public void parseMultipleTokens() {
+ HTTPTokener tokener = new HTTPTokener("Content-Type application/json");
+ String token1 = tokener.nextToken();
+ String token2 = tokener.nextToken();
+ assertEquals("Content-Type", token1);
+ assertEquals("application/json", token2);
+ }
+
+ /**
+ * Test parsing a double-quoted token.
+ */
+ @Test
+ public void parseDoubleQuotedToken() {
+ HTTPTokener tokener = new HTTPTokener("\"application/json\"");
+ String token = tokener.nextToken();
+ assertEquals("application/json", token);
+ }
+
+ /**
+ * Test parsing a single-quoted token.
+ */
+ @Test
+ public void parseSingleQuotedToken() {
+ HTTPTokener tokener = new HTTPTokener("'application/json'");
+ String token = tokener.nextToken();
+ assertEquals("application/json", token);
+ }
+
+ /**
+ * Test parsing a quoted token that includes spaces and semicolons.
+ */
+ @Test
+ public void parseQuotedTokenWithSpaces() {
+ HTTPTokener tokener = new HTTPTokener("\"text/html; charset=UTF-8\"");
+ String token = tokener.nextToken();
+ assertEquals("text/html; charset=UTF-8", token);
+ }
+
+ /**
+ * Test that unterminated quoted strings throw a JSONException.
+ */
+ @Test
+ public void throwExceptionOnUnterminatedString() {
+ HTTPTokener tokener = new HTTPTokener("\"incomplete");
+ JSONException exception = assertThrows(JSONException.class, tokener::nextToken);
+ assertTrue(exception.getMessage().contains("Unterminated string"));
+ }
+
+ /**
+ * Test behavior with empty input string.
+ */
+ @Test
+ public void parseEmptyInput() {
+ HTTPTokener tokener = new HTTPTokener("");
+ String token = tokener.nextToken();
+ assertEquals("", token);
+ }
+
+ /**
+ * Test behavior with input consisting only of whitespace.
+ */
+ @Test
+ public void parseWhitespaceOnly() {
+ HTTPTokener tokener = new HTTPTokener(" \t \n ");
+ String token = tokener.nextToken();
+ assertEquals("", token);
+ }
+
+ /**
+ * Test parsing tokens separated by multiple whitespace characters.
+ */
+ @Test
+ public void parseTokensWithMultipleWhitespace() {
+ HTTPTokener tokener = new HTTPTokener("GET /index.html");
+ String method = tokener.nextToken();
+ String path = tokener.nextToken();
+ assertEquals("GET", method);
+ assertEquals("/index.html", path);
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java
new file mode 100644
index 000000000..429620396
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONArrayTest.java
@@ -0,0 +1,1547 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONParserConfiguration;
+import org.json.JSONPointerException;
+import org.json.JSONString;
+import org.json.JSONTokener;
+import org.json.ParserConfiguration;
+import org.json.junit.data.MyJsonString;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.JsonPath;
+
+
+/**
+ * Tests for JSON-Java JSONArray.java
+ */
+public class JSONArrayTest {
+ private final 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\""+
+ "]";
+
+ /**
+ * Tests that the similar method is working as expected.
+ */
+ @Test
+ public void verifySimilar() {
+ final String string1 = "HasSameRef";
+ final String string2 = "HasDifferentRef";
+ JSONArray obj1 = new JSONArray()
+ .put("abc")
+ .put(string1)
+ .put(2);
+
+ JSONArray obj2 = new JSONArray()
+ .put("abc")
+ .put(string1)
+ .put(3);
+
+ JSONArray obj3 = new JSONArray()
+ .put("abc")
+ .put(new String(string1))
+ .put(2);
+
+ JSONArray obj4 = new JSONArray()
+ .put("abc")
+ .put(2.0)
+ .put(new String(string1));
+
+ JSONArray obj5 = new JSONArray()
+ .put("abc")
+ .put(2.0)
+ .put(new String(string2));
+
+ assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2));
+ assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3));
+ assertFalse("obj4-obj5 Should eval to false", obj4.similar(obj5));
+ }
+
+ /**
+ * Attempt to create a JSONArray with a null string.
+ * Expects a NullPointerException.
+ */
+ @Test(expected=NullPointerException.class)
+ public void nullException() {
+ String str = null;
+ assertNull("Should throw an exception", new JSONArray(str));
+ }
+
+ /**
+ * Attempt to create a JSONArray with an empty string.
+ * Expects a JSONException.
+ */
+ @Test
+ public void emptyStr() {
+ String str = "";
+ try {
+ assertNull("Should throw an exception", new JSONArray(str));
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "A JSONArray text must start with '[' at 0 [character 1 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempt to create a JSONArray with an unclosed array.
+ * Expects an exception
+ */
+ @Test
+ public void unclosedArray() {
+ try {
+ assertNull("Should throw an exception", new JSONArray("["));
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "Expected a ',' or ']' at 1 [character 2 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempt to create a JSONArray with an unclosed array.
+ * Expects an exception
+ */
+ @Test
+ public void unclosedArray2() {
+ try {
+ assertNull("Should throw an exception", new JSONArray("[\"test\""));
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "Expected a ',' or ']' at 7 [character 8 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempt to create a JSONArray with an unclosed array.
+ * Expects an exception
+ */
+ @Test
+ public void unclosedArray3() {
+ try {
+ assertNull("Should throw an exception", new JSONArray("[\"test\","));
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "Expected a ',' or ']' at 8 [character 9 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempt to create a JSONArray with a string as object that is
+ * not a JSON array doc.
+ * Expects a JSONException.
+ */
+ @Test
+ public void badObject() {
+ String str = "abc";
+ try {
+ assertNull("Should throw an exception", new JSONArray((Object)str));
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "JSONArray initial value should be a string or collection or array.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Verifies that the constructor has backwards compatibility with RAW types pre-java5.
+ */
+ @Test
+ public void verifyConstructor() {
+
+ final JSONArray expected = new JSONArray("[10]");
+
+ @SuppressWarnings("rawtypes")
+ Collection myRawC = Collections.singleton(Integer.valueOf(10));
+ JSONArray jaRaw = new JSONArray(myRawC);
+
+ Collection myCInt = Collections.singleton(Integer.valueOf(10));
+ JSONArray jaInt = new JSONArray(myCInt);
+
+ Collection myCObj = Collections.singleton((Object) Integer
+ .valueOf(10));
+ JSONArray jaObj = new JSONArray(myCObj);
+
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaRaw));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaInt));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaObj));
+ Util.checkJSONArrayMaps(expected);
+ Util.checkJSONArrayMaps(jaObj);
+ Util.checkJSONArrayMaps(jaRaw);
+ Util.checkJSONArrayMaps(jaInt);
+ }
+
+ @Test
+ public void jsonArrayByListWithNestedNullValue() {
+ List> list = new ArrayList>();
+ Map sub = new HashMap();
+ sub.put("nullKey", null);
+ list.add(sub);
+ JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
+ JSONArray jsonArray = new JSONArray(list, parserConfiguration);
+ JSONObject subObject = jsonArray.getJSONObject(0);
+ assertTrue(subObject.has("nullKey"));
+ assertEquals(JSONObject.NULL, subObject.get("nullKey"));
+ }
+
+ /**
+ * Tests consecutive calls to putAll with array and collection.
+ */
+ @Test
+ public void verifyPutAll() {
+ final JSONArray jsonArray = new JSONArray();
+
+ // array
+ int[] myInts = { 1, 2, 3, 4, 5 };
+ jsonArray.putAll(myInts);
+
+ assertEquals("int arrays lengths should be equal",
+ jsonArray.length(),
+ myInts.length);
+
+ for (int i = 0; i < myInts.length; i++) {
+ assertEquals("int arrays elements should be equal",
+ myInts[i],
+ jsonArray.getInt(i));
+ }
+
+ // collection
+ List myList = Arrays.asList("one", "two", "three", "four", "five");
+ jsonArray.putAll(myList);
+
+ int len = myInts.length + myList.size();
+
+ assertEquals("arrays lengths should be equal",
+ jsonArray.length(),
+ len);
+
+ // collection as object
+ @SuppressWarnings("RedundantCast")
+ Object myListAsObject = (Object) myList;
+ jsonArray.putAll(myListAsObject);
+
+ for (int i = 0; i < myList.size(); i++) {
+ assertEquals("collection elements should be equal",
+ myList.get(i),
+ jsonArray.getString(myInts.length + i));
+ }
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Verifies that the put Collection has backwards compatibility with RAW types pre-java5.
+ */
+ @Test
+ public void verifyPutCollection() {
+
+ final JSONArray expected = new JSONArray("[[10]]");
+
+ @SuppressWarnings("rawtypes")
+ Collection myRawC = Collections.singleton(Integer.valueOf(10));
+ JSONArray jaRaw = new JSONArray();
+ jaRaw.put(myRawC);
+
+ Collection myCObj = Collections.singleton((Object) Integer
+ .valueOf(10));
+ JSONArray jaObj = new JSONArray();
+ jaObj.put(myCObj);
+
+ Collection myCInt = Collections.singleton(Integer.valueOf(10));
+ JSONArray jaInt = new JSONArray();
+ jaInt.put(myCInt);
+
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaRaw));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaObj));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaInt));
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ jaRaw, jaObj, jaInt
+ )));
+ }
+
+
+ /**
+ * Verifies that the put Map has backwards compatibility with RAW types pre-java5.
+ */
+ @Test
+ public void verifyPutMap() {
+
+ final JSONArray expected = new JSONArray("[{\"myKey\":10}]");
+
+ @SuppressWarnings("rawtypes")
+ Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
+ JSONArray jaRaw = new JSONArray();
+ jaRaw.put(myRawC);
+
+ Map myCStrObj = Collections.singletonMap("myKey",
+ (Object) Integer.valueOf(10));
+ JSONArray jaStrObj = new JSONArray();
+ jaStrObj.put(myCStrObj);
+
+ Map myCStrInt = Collections.singletonMap("myKey",
+ Integer.valueOf(10));
+ JSONArray jaStrInt = new JSONArray();
+ jaStrInt.put(myCStrInt);
+
+ Map, ?> myCObjObj = Collections.singletonMap((Object) "myKey",
+ (Object) Integer.valueOf(10));
+ JSONArray jaObjObj = new JSONArray();
+ jaObjObj.put(myCObjObj);
+
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaRaw));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaStrObj));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaStrInt));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaObjObj));
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ expected, jaRaw, jaStrObj, jaStrInt, jaObjObj
+ )));
+ }
+
+ /**
+ * Create a JSONArray doc with a variety of different elements.
+ * Confirm that the values can be accessed via the get[type]() API methods
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void getArrayValues() {
+ JSONArray jsonArray = new JSONArray(this.arrayStr);
+ // booleans
+ assertTrue("Array true",
+ true == jsonArray.getBoolean(0));
+ assertTrue("Array false",
+ false == jsonArray.getBoolean(1));
+ assertTrue("Array string true",
+ true == jsonArray.getBoolean(2));
+ assertTrue("Array string false",
+ false == jsonArray.getBoolean(3));
+ // strings
+ assertTrue("Array value string",
+ "hello".equals(jsonArray.getString(4)));
+ // doubles
+ assertTrue("Array double",
+ Double.valueOf(23.45e-4).equals(jsonArray.getDouble(5)));
+ assertTrue("Array string double",
+ Double.valueOf(23.45).equals(jsonArray.getDouble(6)));
+ assertTrue("Array double can be float",
+ Float.valueOf(23.45e-4f).equals(jsonArray.getFloat(5)));
+ // ints
+ assertTrue("Array value int",
+ Integer.valueOf(42).equals(jsonArray.getInt(7)));
+ assertTrue("Array value string int",
+ Integer.valueOf(43).equals(jsonArray.getInt(8)));
+ // nested objects
+ JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
+ assertTrue("Array value JSONArray", nestedJsonArray != null);
+ JSONObject nestedJsonObject = jsonArray.getJSONObject(10);
+ assertTrue("Array value JSONObject", nestedJsonObject != null);
+ // longs
+ assertTrue("Array value long",
+ Long.valueOf(0).equals(jsonArray.getLong(11)));
+ assertTrue("Array value string long",
+ Long.valueOf(-1).equals(jsonArray.getLong(12)));
+
+ assertTrue("Array value null", jsonArray.isNull(-1));
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Create a JSONArray doc with a variety of different elements.
+ * Confirm that attempting to get the wrong types via the get[type]()
+ * API methods result in JSONExceptions
+ */
+ @Test
+ public void failedGetArrayValues() {
+ JSONArray jsonArray = new JSONArray(this.arrayStr);
+ try {
+ jsonArray.getBoolean(4);
+ assertTrue("expected getBoolean to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[4] is not a boolean (class java.lang.String : hello).",e.getMessage());
+ }
+ try {
+ jsonArray.get(-1);
+ assertTrue("expected get to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[-1] not found.",e.getMessage());
+ }
+ try {
+ jsonArray.getDouble(4);
+ assertTrue("expected getDouble to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[4] is not a double (class java.lang.String : hello).",e.getMessage());
+ }
+ try {
+ jsonArray.getInt(4);
+ assertTrue("expected getInt to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[4] is not a int (class java.lang.String : hello).",e.getMessage());
+ }
+ try {
+ jsonArray.getJSONArray(4);
+ assertTrue("expected getJSONArray to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[4] is not a JSONArray (class java.lang.String : hello).",e.getMessage());
+ }
+ try {
+ jsonArray.getJSONObject(4);
+ assertTrue("expected getJSONObject to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[4] is not a JSONObject (class java.lang.String : hello).",e.getMessage());
+ }
+ try {
+ jsonArray.getLong(4);
+ assertTrue("expected getLong to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[4] is not a long (class java.lang.String : hello).",e.getMessage());
+ }
+ try {
+ jsonArray.getString(5);
+ assertTrue("expected getString to fail", false);
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray[5] is not a String (class java.math.BigDecimal : 0.002345).",e.getMessage());
+ }
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * The JSON parser is permissive of unambiguous unquoted keys and values.
+ * Such JSON text should be allowed, even if it does not strictly conform
+ * to the spec. However, after being parsed, toString() should emit strictly
+ * conforming JSON text.
+ */
+ @Test
+ public void unquotedText() {
+ String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
+ List expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
+
+ // Test should fail if default strictMode is true, pass if false
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
+ if (jsonParserConfiguration.isStrictMode()) {
+ try {
+ JSONArray jsonArray = new JSONArray(str);
+ assertEquals("Expected to throw exception due to invalid string", true, false);
+ } catch (JSONException e) { }
+ } else {
+ JSONArray jsonArray = new JSONArray(str);
+ assertEquals(expected, jsonArray.toList());
+ }
+ }
+
+ /**
+ * Exercise JSONArray.join() by converting a JSONArray into a
+ * comma-separated string. Since this is very nearly a JSON document,
+ * array braces are added to the beginning and end prior to validation.
+ */
+ @Test
+ public void join() {
+ JSONArray jsonArray = new JSONArray(this.arrayStr);
+ String joinStr = jsonArray.join(",");
+
+ // validate JSON
+ /**
+ * Don't need to remake the JSONArray to perform the parsing
+ */
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse("["+joinStr+"]");
+ assertTrue("expected 13 items in top level object", ((List>)(JsonPath.read(doc, "$"))).size() == 13);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
+ assertTrue("expected \"true\"", "true".equals(jsonArray.query("/2")));
+ assertTrue("expected \"false\"", "false".equals(jsonArray.query("/3")));
+ assertTrue("expected hello", "hello".equals(jsonArray.query("/4")));
+ assertTrue("expected 0.002345", BigDecimal.valueOf(0.002345).equals(jsonArray.query("/5")));
+ assertTrue("expected \"23.45\"", "23.45".equals(jsonArray.query("/6")));
+ assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/7")));
+ assertTrue("expected \"43\"", "43".equals(jsonArray.query("/8")));
+ assertTrue("expected 1 item in [9]", ((List>)(JsonPath.read(doc, "$[9]"))).size() == 1);
+ assertTrue("expected world", "world".equals(jsonArray.query("/9/0")));
+ assertTrue("expected 4 items in [10]", ((Map,?>)(JsonPath.read(doc, "$[10]"))).size() == 4);
+ assertTrue("expected value1", "value1".equals(jsonArray.query("/10/key1")));
+ assertTrue("expected value2", "value2".equals(jsonArray.query("/10/key2")));
+ assertTrue("expected value3", "value3".equals(jsonArray.query("/10/key3")));
+ assertTrue("expected value4", "value4".equals(jsonArray.query("/10/key4")));
+ assertTrue("expected 0", Integer.valueOf(0).equals(jsonArray.query("/11")));
+ assertTrue("expected \"-1\"", "-1".equals(jsonArray.query("/12")));
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Confirm the JSONArray.length() method
+ */
+ @Test
+ public void length() {
+ assertTrue("expected empty JSONArray length 0",
+ new JSONArray().length() == 0);
+ JSONArray jsonArray = new JSONArray(this.arrayStr);
+ assertTrue("expected JSONArray length 13. instead found "+jsonArray.length(), jsonArray.length() == 13);
+ JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
+ assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ jsonArray, nestedJsonArray
+ )));
+ }
+
+ /**
+ * Create a JSONArray doc with a variety of different elements.
+ * Confirm that the values can be accessed via the opt[type](index)
+ * and opt[type](index, default) API methods.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void opt() {
+ JSONArray jsonArray = new JSONArray(this.arrayStr);
+ assertTrue("Array opt value true",
+ Boolean.TRUE == jsonArray.opt(0));
+ assertTrue("Array opt value out of range",
+ null == jsonArray.opt(-1));
+
+ assertTrue("Array opt value out of range",
+ null == jsonArray.opt(jsonArray.length()));
+
+ assertTrue("Array opt boolean",
+ Boolean.TRUE == jsonArray.optBoolean(0));
+ assertTrue("Array opt boolean default",
+ Boolean.FALSE == jsonArray.optBoolean(-1, Boolean.FALSE));
+ assertTrue("Array opt boolean implicit default",
+ Boolean.FALSE == jsonArray.optBoolean(-1));
+
+ assertTrue("Array opt boolean object",
+ Boolean.TRUE.equals(jsonArray.optBooleanObject(0)));
+ assertTrue("Array opt boolean object default",
+ Boolean.FALSE.equals(jsonArray.optBooleanObject(-1, Boolean.FALSE)));
+ assertTrue("Array opt boolean object implicit default",
+ Boolean.FALSE.equals(jsonArray.optBooleanObject(-1)));
+
+ assertTrue("Array opt double",
+ Double.valueOf(23.45e-4).equals(jsonArray.optDouble(5)));
+ assertTrue("Array opt double default",
+ Double.valueOf(1).equals(jsonArray.optDouble(0, 1)));
+ assertTrue("Array opt double default implicit",
+ Double.valueOf(jsonArray.optDouble(99)).isNaN());
+
+ assertTrue("Array opt double object",
+ Double.valueOf(23.45e-4).equals(jsonArray.optDoubleObject(5)));
+ assertTrue("Array opt double object default",
+ Double.valueOf(1).equals(jsonArray.optDoubleObject(0, 1D)));
+ assertTrue("Array opt double object default implicit",
+ jsonArray.optDoubleObject(99).isNaN());
+
+ assertTrue("Array opt float",
+ Float.valueOf(Double.valueOf(23.45e-4).floatValue()).equals(jsonArray.optFloat(5)));
+ assertTrue("Array opt float default",
+ Float.valueOf(1).equals(jsonArray.optFloat(0, 1)));
+ assertTrue("Array opt float default implicit",
+ Float.valueOf(jsonArray.optFloat(99)).isNaN());
+
+ assertTrue("Array opt float object",
+ Float.valueOf(23.45e-4F).equals(jsonArray.optFloatObject(5)));
+ assertTrue("Array opt float object default",
+ Float.valueOf(1).equals(jsonArray.optFloatObject(0, 1F)));
+ assertTrue("Array opt float object default implicit",
+ jsonArray.optFloatObject(99).isNaN());
+
+ assertTrue("Array opt Number",
+ BigDecimal.valueOf(23.45e-4).equals(jsonArray.optNumber(5)));
+ assertTrue("Array opt Number default",
+ Double.valueOf(1).equals(jsonArray.optNumber(0, 1d)));
+ assertTrue("Array opt Number default implicit",
+ Double.valueOf(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN());
+
+ assertTrue("Array opt int",
+ Integer.valueOf(42).equals(jsonArray.optInt(7)));
+ assertTrue("Array opt int default",
+ Integer.valueOf(-1).equals(jsonArray.optInt(0, -1)));
+ assertTrue("Array opt int default implicit",
+ 0 == jsonArray.optInt(0));
+
+ assertTrue("Array opt int object",
+ Integer.valueOf(42).equals(jsonArray.optIntegerObject(7)));
+ assertTrue("Array opt int object default",
+ Integer.valueOf(-1).equals(jsonArray.optIntegerObject(0, -1)));
+ assertTrue("Array opt int object default implicit",
+ Integer.valueOf(0).equals(jsonArray.optIntegerObject(0)));
+
+ JSONArray nestedJsonArray = jsonArray.optJSONArray(9);
+ assertTrue("Array opt JSONArray", nestedJsonArray != null);
+ assertTrue("Array opt JSONArray null",
+ null == jsonArray.optJSONArray(99));
+ assertTrue("Array opt JSONArray default",
+ "value".equals(jsonArray.optJSONArray(99, new JSONArray("[\"value\"]")).getString(0)));
+
+ JSONObject nestedJsonObject = jsonArray.optJSONObject(10);
+ assertTrue("Array opt JSONObject", nestedJsonObject != null);
+ assertTrue("Array opt JSONObject null",
+ null == jsonArray.optJSONObject(99));
+ assertTrue("Array opt JSONObject default",
+ "value".equals(jsonArray.optJSONObject(99, new JSONObject("{\"key\":\"value\"}")).getString("key")));
+
+ assertTrue("Array opt long",
+ 0 == jsonArray.optLong(11));
+ assertTrue("Array opt long default",
+ -2 == jsonArray.optLong(-1, -2));
+ assertTrue("Array opt long default implicit",
+ 0 == jsonArray.optLong(-1));
+
+ assertTrue("Array opt long object",
+ Long.valueOf(0).equals(jsonArray.optLongObject(11)));
+ assertTrue("Array opt long object default",
+ Long.valueOf(-2).equals(jsonArray.optLongObject(-1, -2L)));
+ assertTrue("Array opt long object default implicit",
+ Long.valueOf(0).equals(jsonArray.optLongObject(-1)));
+
+ assertTrue("Array opt string",
+ "hello".equals(jsonArray.optString(4)));
+ assertTrue("Array opt string default implicit",
+ "".equals(jsonArray.optString(-1)));
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ jsonArray, nestedJsonArray
+ )));
+ Util.checkJSONObjectMaps(nestedJsonObject);
+ }
+
+ /**
+ * Verifies that the opt methods properly convert string values.
+ */
+ @Test
+ public void optStringConversion(){
+ JSONArray ja = new JSONArray("[\"123\",\"true\",\"false\"]");
+ assertTrue("unexpected optBoolean value",ja.optBoolean(1,false)==true);
+ assertTrue("unexpected optBooleanObject value",Boolean.valueOf(true).equals(ja.optBooleanObject(1,false)));
+ assertTrue("unexpected optBoolean value",ja.optBoolean(2,true)==false);
+ assertTrue("unexpected optBooleanObject value",Boolean.valueOf(false).equals(ja.optBooleanObject(2,true)));
+ assertTrue("unexpected optInt value",ja.optInt(0,0)==123);
+ assertTrue("unexpected optIntegerObject value",Integer.valueOf(123).equals(ja.optIntegerObject(0,0)));
+ assertTrue("unexpected optLong value",ja.optLong(0,0)==123);
+ assertTrue("unexpected optLongObject value",Long.valueOf(123).equals(ja.optLongObject(0,0L)));
+ assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0);
+ assertTrue("unexpected optDoubleObject value",Double.valueOf(123.0).equals(ja.optDoubleObject(0,0.0)));
+ assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
+ assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
+ Util.checkJSONArrayMaps(ja);
+ }
+
+ /**
+ * Exercise the JSONArray.put(value) method with various parameters
+ * and confirm the resulting JSONArray.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void put() {
+ JSONArray jsonArray = new JSONArray();
+
+ // index 0
+ jsonArray.put(true);
+ // 1
+ jsonArray.put(false);
+
+ String jsonArrayStr =
+ "["+
+ "\"hello\","+
+ "\"world\""+
+ "]";
+ // 2
+ jsonArray.put(new JSONArray(jsonArrayStr));
+
+ // 3
+ jsonArray.put(2.5);
+ // 4
+ jsonArray.put(1);
+ // 5
+ jsonArray.put(45L);
+
+ // 6
+ jsonArray.put("objectPut");
+
+ String jsonObjectStr =
+ "{"+
+ "\"key10\":\"val10\","+
+ "\"key20\":\"val20\","+
+ "\"key30\":\"val30\""+
+ "}";
+ JSONObject jsonObject = new JSONObject(jsonObjectStr);
+ // 7
+ jsonArray.put(jsonObject);
+
+ Map map = new HashMap();
+ map.put("k1", "v1");
+ // 8
+ jsonArray.put(map);
+
+ Collection collection = new ArrayList();
+ collection.add(1);
+ collection.add(2);
+ // 9
+ jsonArray.put(collection);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 10 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 10);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
+ assertTrue("expected 2 items in [2]", ((List>)(JsonPath.read(doc, "$[2]"))).size() == 2);
+ assertTrue("expected hello", "hello".equals(jsonArray.query("/2/0")));
+ assertTrue("expected world", "world".equals(jsonArray.query("/2/1")));
+ assertTrue("expected 2.5", Double.valueOf(2.5).equals(jsonArray.query("/3")));
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/4")));
+ assertTrue("expected 45", Long.valueOf(45).equals(jsonArray.query("/5")));
+ assertTrue("expected objectPut", "objectPut".equals(jsonArray.query("/6")));
+ assertTrue("expected 3 items in [7]", ((Map,?>)(JsonPath.read(doc, "$[7]"))).size() == 3);
+ assertTrue("expected val10", "val10".equals(jsonArray.query("/7/key10")));
+ assertTrue("expected val20", "val20".equals(jsonArray.query("/7/key20")));
+ assertTrue("expected val30", "val30".equals(jsonArray.query("/7/key30")));
+ assertTrue("expected 1 item in [8]", ((Map,?>)(JsonPath.read(doc, "$[8]"))).size() == 1);
+ assertTrue("expected v1", "v1".equals(jsonArray.query("/8/k1")));
+ assertTrue("expected 2 items in [9]", ((List>)(JsonPath.read(doc, "$[9]"))).size() == 2);
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1")));
+ Util.checkJSONArrayMaps(jsonArray);
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise the JSONArray.put(index, value) method with various parameters
+ * and confirm the resulting JSONArray.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void putIndex() {
+ JSONArray jsonArray = new JSONArray();
+
+ // 1
+ jsonArray.put(1, false);
+ // index 0
+ jsonArray.put(0, true);
+
+ String jsonArrayStr =
+ "["+
+ "\"hello\","+
+ "\"world\""+
+ "]";
+ // 2
+ jsonArray.put(2, new JSONArray(jsonArrayStr));
+
+ // 5
+ jsonArray.put(5, 45L);
+ // 4
+ jsonArray.put(4, 1);
+ // 3
+ jsonArray.put(3, 2.5);
+
+ // 6
+ jsonArray.put(6, "objectPut");
+
+ // 7 will be null
+
+ String jsonObjectStr =
+ "{"+
+ "\"key10\":\"val10\","+
+ "\"key20\":\"val20\","+
+ "\"key30\":\"val30\""+
+ "}";
+ JSONObject jsonObject = new JSONObject(jsonObjectStr);
+ jsonArray.put(8, jsonObject);
+ Collection collection = new ArrayList();
+ collection.add(1);
+ collection.add(2);
+ jsonArray.put(9,collection);
+
+ Map map = new HashMap();
+ map.put("k1", "v1");
+ jsonArray.put(10, map);
+ try {
+ jsonArray.put(-1, "abc");
+ assertTrue("put index < 0 should have thrown exception", false);
+ } catch(Exception ignored) {}
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 11 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 11);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
+ assertTrue("expected 2 items in [2]", ((List>)(JsonPath.read(doc, "$[2]"))).size() == 2);
+ assertTrue("expected hello", "hello".equals(jsonArray.query("/2/0")));
+ assertTrue("expected world", "world".equals(jsonArray.query("/2/1")));
+ assertTrue("expected 2.5", Double.valueOf(2.5).equals(jsonArray.query("/3")));
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/4")));
+ assertTrue("expected 45", Long.valueOf(45).equals(jsonArray.query("/5")));
+ assertTrue("expected objectPut", "objectPut".equals(jsonArray.query("/6")));
+ assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/7")));
+ assertTrue("expected 3 items in [8]", ((Map,?>)(JsonPath.read(doc, "$[8]"))).size() == 3);
+ assertTrue("expected val10", "val10".equals(jsonArray.query("/8/key10")));
+ assertTrue("expected val20", "val20".equals(jsonArray.query("/8/key20")));
+ assertTrue("expected val30", "val30".equals(jsonArray.query("/8/key30")));
+ assertTrue("expected 2 items in [9]", ((List>)(JsonPath.read(doc, "$[9]"))).size() == 2);
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1")));
+ assertTrue("expected 1 item in [10]", ((Map,?>)(JsonPath.read(doc, "$[10]"))).size() == 1);
+ assertTrue("expected v1", "v1".equals(jsonArray.query("/10/k1")));
+ Util.checkJSONObjectMaps(jsonObject);
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Exercise the JSONArray.remove(index) method
+ * and confirm the resulting JSONArray.
+ */
+ @Test
+ public void remove() {
+ String arrayStr1 =
+ "["+
+ "1"+
+ "]";
+ JSONArray jsonArray = new JSONArray(arrayStr1);
+ jsonArray.remove(0);
+ assertTrue("array should be empty", null == jsonArray.remove(5));
+ assertTrue("jsonArray should be empty", jsonArray.isEmpty());
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Exercise the JSONArray.similar() method with various parameters
+ * and confirm the results when not similar.
+ */
+ @Test
+ public void notSimilar() {
+ String arrayStr1 =
+ "["+
+ "1"+
+ "]";
+ JSONArray jsonArray = new JSONArray(arrayStr1);
+ JSONArray otherJsonArray = new JSONArray();
+ assertTrue("arrays lengths differ", !jsonArray.similar(otherJsonArray));
+
+ JSONObject jsonObject = new JSONObject("{\"k1\":\"v1\"}");
+ JSONObject otherJsonObject = new JSONObject();
+ jsonArray = new JSONArray();
+ jsonArray.put(jsonObject);
+ otherJsonArray = new JSONArray();
+ otherJsonArray.put(otherJsonObject);
+ assertTrue("arrays JSONObjects differ", !jsonArray.similar(otherJsonArray));
+
+ JSONArray nestedJsonArray = new JSONArray("[1, 2]");
+ JSONArray otherNestedJsonArray = new JSONArray();
+ jsonArray = new JSONArray();
+ jsonArray.put(nestedJsonArray);
+ otherJsonArray = new JSONArray();
+ otherJsonArray.put(otherNestedJsonArray);
+ assertTrue("arrays nested JSONArrays differ",
+ !jsonArray.similar(otherJsonArray));
+
+ jsonArray = new JSONArray();
+ jsonArray.put("hello");
+ otherJsonArray = new JSONArray();
+ otherJsonArray.put("world");
+ assertTrue("arrays values differ",
+ !jsonArray.similar(otherJsonArray));
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ jsonArray, otherJsonArray
+ )));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject, otherJsonObject
+ )));
+ }
+
+ /**
+ * Exercise JSONArray toString() method with various indent levels.
+ */
+ @Test
+ public void jsonArrayToStringIndent() {
+ String jsonArray0Str =
+ "[" +
+ "[1,2," +
+ "{\"key3\":true}" +
+ "]," +
+ "{\"key1\":\"val1\",\"key2\":" +
+ "{\"key2\":\"val2\"}" +
+ "}," +
+ "[" +
+ "[1,2.1]" +
+ "," +
+ "[null]" +
+ "]" +
+ "]";
+
+ String jsonArray1Strs [] =
+ {
+ "[",
+ " [",
+ " 1,",
+ " 2,",
+ " {\"key3\": true}",
+ " ],",
+ " {",
+ " \"key1\": \"val1\",",
+ " \"key2\": {\"key2\": \"val2\"}",
+ " },",
+ " [",
+ " [",
+ " 1,",
+ " 2.1",
+ " ],",
+ " [null]",
+ " ]",
+ "]"
+ };
+ String jsonArray4Strs [] =
+ {
+ "[",
+ " [",
+ " 1,",
+ " 2,",
+ " {\"key3\": true}",
+ " ],",
+ " {",
+ " \"key1\": \"val1\",",
+ " \"key2\": {\"key2\": \"val2\"}",
+ " },",
+ " [",
+ " [",
+ " 1,",
+ " 2.1",
+ " ],",
+ " [null]",
+ " ]",
+ "]"
+ };
+ JSONArray jsonArray = new JSONArray(jsonArray0Str);
+ String [] actualStrArray = jsonArray.toString().split("\\r?\\n");
+ assertEquals("Expected 1 line", 1, actualStrArray.length);
+ actualStrArray = jsonArray.toString(0).split("\\r?\\n");
+ assertEquals("Expected 1 line", 1, actualStrArray.length);
+
+ actualStrArray = jsonArray.toString(1).split("\\r?\\n");
+ assertEquals("Expected lines", jsonArray1Strs.length, actualStrArray.length);
+ List list = Arrays.asList(actualStrArray);
+ for (String s : jsonArray1Strs) {
+ list.contains(s);
+ }
+
+ actualStrArray = jsonArray.toString(4).split("\\r?\\n");
+ assertEquals("Expected lines", jsonArray1Strs.length, actualStrArray.length);
+ list = Arrays.asList(actualStrArray);
+ for (String s : jsonArray4Strs) {
+ list.contains(s);
+ }
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Convert an empty JSONArray to JSONObject
+ */
+ @Test
+ public void toJSONObject() {
+ JSONArray names = new JSONArray();
+ JSONArray jsonArray = new JSONArray();
+ assertTrue("toJSONObject should return null",
+ null == jsonArray.toJSONObject(names));
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ names, jsonArray
+ )));
+ }
+
+ /**
+ * Confirm the creation of a JSONArray from an array of ints
+ */
+ @Test
+ public void objectArrayVsIsArray() {
+ int[] myInts = { 1, 2, 3, 4, 5, 6, 7 };
+ Object myObject = myInts;
+ JSONArray jsonArray = new JSONArray(myObject);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 7 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 7);
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
+ assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
+ assertTrue("expected 4", Integer.valueOf(4).equals(jsonArray.query("/3")));
+ assertTrue("expected 5", Integer.valueOf(5).equals(jsonArray.query("/4")));
+ assertTrue("expected 6", Integer.valueOf(6).equals(jsonArray.query("/5")));
+ assertTrue("expected 7", Integer.valueOf(7).equals(jsonArray.query("/6")));
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Exercise the JSONArray iterator.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void iteratorTest() {
+ JSONArray jsonArray = new JSONArray(this.arrayStr);
+ Iterator it = jsonArray.iterator();
+ assertTrue("Array true",
+ Boolean.TRUE.equals(it.next()));
+ assertTrue("Array false",
+ Boolean.FALSE.equals(it.next()));
+ assertTrue("Array string true",
+ "true".equals(it.next()));
+ assertTrue("Array string false",
+ "false".equals(it.next()));
+ assertTrue("Array string",
+ "hello".equals(it.next()));
+
+ assertTrue("Array double [23.45e-4]",
+ new BigDecimal("0.002345").equals(it.next()));
+ assertTrue("Array string double",
+ Double.valueOf(23.45).equals(Double.parseDouble((String)it.next())));
+
+ assertTrue("Array value int",
+ Integer.valueOf(42).equals(it.next()));
+ assertTrue("Array value string int",
+ Integer.valueOf(43).equals(Integer.parseInt((String)it.next())));
+
+ JSONArray nestedJsonArray = (JSONArray)it.next();
+ assertTrue("Array value JSONArray", nestedJsonArray != null);
+
+ JSONObject nestedJsonObject = (JSONObject)it.next();
+ assertTrue("Array value JSONObject", nestedJsonObject != null);
+
+ assertTrue("Array value long",
+ Long.valueOf(0).equals(((Number) it.next()).longValue()));
+ assertTrue("Array value string long",
+ Long.valueOf(-1).equals(Long.parseLong((String) it.next())));
+ assertTrue("should be at end of array", !it.hasNext());
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ jsonArray, nestedJsonArray
+ )));
+ Util.checkJSONObjectMaps(nestedJsonObject);
+ }
+
+ @Test(expected = JSONPointerException.class)
+ public void queryWithNoResult() {
+ new JSONArray().query("/a/b");
+ }
+
+ @Test
+ public void optQueryWithNoResult() {
+ assertNull(new JSONArray().optQuery("/a/b"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void optQueryWithSyntaxError() {
+ new JSONArray().optQuery("invalid");
+ }
+
+
+ /**
+ * Exercise the JSONArray write() method
+ */
+ @Test
+ public void write() throws IOException {
+ String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
+ JSONArray jsonArray = new JSONArray(str);
+ String expectedStr = str;
+ StringWriter stringWriter = new StringWriter();
+ try {
+ jsonArray.write(stringWriter);
+ String actualStr = stringWriter.toString();
+ JSONArray finalArray = new JSONArray(actualStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
+ assertTrue("write() expected " + expectedStr +
+ " but found " + actualStr,
+ actualStr.startsWith("[\"value1\",\"value2\",{")
+ && actualStr.contains("\"key1\":1")
+ && actualStr.contains("\"key2\":2")
+ && actualStr.contains("\"key3\":3")
+ );
+ } finally {
+ stringWriter.close();
+ }
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Exercise the JSONArray write() method using Appendable.
+ */
+/*
+ @Test
+ public void writeAppendable() {
+ String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
+ JSONArray jsonArray = new JSONArray(str);
+ String expectedStr = str;
+ StringBuilder stringBuilder = new StringBuilder();
+ Appendable appendable = jsonArray.write(stringBuilder);
+ String actualStr = appendable.toString();
+ assertTrue("write() expected " + expectedStr +
+ " but found " + actualStr,
+ expectedStr.equals(actualStr));
+ }
+*/
+
+ /**
+ * Exercise the JSONArray write(Writer, int, int) method
+ */
+ @Test
+ public void write3Param() throws IOException {
+ String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
+ JSONArray jsonArray = new JSONArray(str0);
+ String expectedStr = str0;
+ StringWriter stringWriter = new StringWriter();
+ try {
+ String actualStr = jsonArray.write(stringWriter, 0, 0).toString();
+ JSONArray finalArray = new JSONArray(actualStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
+ assertTrue("write() expected " + expectedStr +
+ " but found " + actualStr,
+ actualStr.startsWith("[\"value1\",\"value2\",{")
+ && actualStr.contains("\"key1\":1")
+ && actualStr.contains("\"key2\":false")
+ && actualStr.contains("\"key3\":3.14")
+ );
+ } finally {
+ stringWriter.close();
+ }
+
+ stringWriter = new StringWriter();
+ try {
+ String actualStr = jsonArray.write(stringWriter, 2, 1).toString();
+ JSONArray finalArray = new JSONArray(actualStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
+ assertTrue("write() expected " + expectedStr +
+ " but found " + actualStr,
+ actualStr.startsWith("[\n" +
+ " \"value1\",\n" +
+ " \"value2\",\n" +
+ " {")
+ && actualStr.contains("\"key1\": 1")
+ && actualStr.contains("\"key2\": false")
+ && actualStr.contains("\"key3\": 3.14")
+ );
+ Util.checkJSONArrayMaps(finalArray);
+ } finally {
+ stringWriter.close();
+ }
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Exercise the JSONArray write(Appendable, int, int) method
+ */
+/*
+ @Test
+ public void write3ParamAppendable() {
+ String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
+ String str2 =
+ "[\n" +
+ " \"value1\",\n" +
+ " \"value2\",\n" +
+ " {\n" +
+ " \"key1\": 1,\n" +
+ " \"key2\": false,\n" +
+ " \"key3\": 3.14\n" +
+ " }\n" +
+ " ]";
+ JSONArray jsonArray = new JSONArray(str0);
+ String expectedStr = str0;
+ StringBuilder stringBuilder = new StringBuilder();
+ Appendable appendable = jsonArray.write(stringBuilder, 0, 0);
+ String actualStr = appendable.toString();
+ assertEquals(expectedStr, actualStr);
+
+ expectedStr = str2;
+ stringBuilder = new StringBuilder();
+ appendable = jsonArray.write(stringBuilder, 2, 1);
+ actualStr = appendable.toString();
+ assertEquals(expectedStr, actualStr);
+ }
+*/
+
+ /**
+ * Exercise JSONArray toString() method with various indent levels.
+ */
+ @Test
+ public void toList() {
+ String jsonArrayStr =
+ "[" +
+ "[1,2," +
+ "{\"key3\":true}" +
+ "]," +
+ "{\"key1\":\"val1\",\"key2\":" +
+ "{\"key2\":null}," +
+ "\"key3\":42,\"key4\":[]" +
+ "}," +
+ "[" +
+ "[\"value1\",2.1]" +
+ "," +
+ "[null]" +
+ "]" +
+ "]";
+
+ JSONArray jsonArray = new JSONArray(jsonArrayStr);
+ List> list = jsonArray.toList();
+
+ assertTrue("List should not be null", list != null);
+ assertTrue("List should have 3 elements", list.size() == 3);
+
+ List> val1List = (List>) list.get(0);
+ assertTrue("val1 should not be null", val1List != null);
+ assertTrue("val1 should have 3 elements", val1List.size() == 3);
+
+ assertTrue("val1 value 1 should be 1", val1List.get(0).equals(Integer.valueOf(1)));
+ assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2)));
+
+ Map,?> key1Value3Map = (Map,?>)val1List.get(2);
+ assertTrue("Map should not be null", key1Value3Map != null);
+ assertTrue("Map should have 1 element", key1Value3Map.size() == 1);
+ assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE));
+
+ Map,?> val2Map = (Map,?>) list.get(1);
+ assertTrue("val2 should not be null", val2Map != null);
+ assertTrue("val2 should have 4 elements", val2Map.size() == 4);
+ assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));
+ assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42)));
+
+ Map,?> val2Key2Map = (Map,?>)val2Map.get("key2");
+ assertTrue("val2 map key 2 should not be null", val2Key2Map != null);
+ assertTrue("val2 map key 2 should have an entry", val2Key2Map.containsKey("key2"));
+ assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null);
+
+ List> val2Key4List = (List>)val2Map.get("key4");
+ assertTrue("val2 map key 4 should not be null", val2Key4List != null);
+ assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty());
+
+ List> val3List = (List>) list.get(2);
+ assertTrue("val3 should not be null", val3List != null);
+ assertTrue("val3 should have 2 elements", val3List.size() == 2);
+
+ List> val3Val1List = (List>)val3List.get(0);
+ assertTrue("val3 list val 1 should not be null", val3Val1List != null);
+ assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2);
+ assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1"));
+ assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(new BigDecimal("2.1")));
+
+ List> val3Val2List = (List>)val3List.get(1);
+ assertTrue("val3 list val 2 should not be null", val3Val2List != null);
+ assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1);
+ assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null);
+
+ // assert that toList() is a deep copy
+ jsonArray.getJSONObject(1).put("key1", "still val1");
+ assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));
+
+ // assert that the new list is mutable
+ assertTrue("Removing an entry should succeed", list.remove(2) != null);
+ assertTrue("List should have 2 elements", list.size() == 2);
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Create a JSONArray with specified initial capacity.
+ * Expects an exception if the initial capacity is specified as a negative integer
+ */
+ @Test
+ public void testJSONArrayInt() {
+ assertNotNull(new JSONArray(0));
+ assertNotNull(new JSONArray(5));
+ // Check Size -> Even though the capacity of the JSONArray can be specified using a positive
+ // integer but the length of JSONArray always reflects upon the items added into it.
+ // assertEquals(0l, new JSONArray(10).length());
+ try {
+ assertNotNull("Should throw an exception", new JSONArray(-1));
+ } catch (JSONException e) {
+ assertEquals("Expected an exception message",
+ "JSONArray initial capacity cannot be negative.",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Verifies that the object constructor can properly handle any supported collection object.
+ */
+ @Test
+ @SuppressWarnings({ "unchecked", "boxing" })
+ public void testObjectConstructor() {
+ // should copy the array
+ Object o = new Object[] {2, "test2", true};
+ JSONArray a = new JSONArray(o);
+ assertNotNull("Should not error", a);
+ assertEquals("length", 3, a.length());
+
+ // should NOT copy the collection
+ // this is required for backwards compatibility
+ o = new ArrayList();
+ ((Collection)o).add(1);
+ ((Collection)o).add("test");
+ ((Collection)o).add(false);
+ try {
+ JSONArray a0 = new JSONArray(o);
+ assertNull("Should error", a0);
+ } catch (JSONException ex) {
+ }
+
+ // should NOT copy the JSONArray
+ // this is required for backwards compatibility
+ o = a;
+ try {
+ JSONArray a1 = new JSONArray(o);
+ assertNull("Should error", a1);
+ } catch (JSONException ex) {
+ }
+ Util.checkJSONArrayMaps(a);
+ }
+
+ /**
+ * Verifies that the JSONArray constructor properly copies the original.
+ */
+ @Test
+ public void testJSONArrayConstructor() {
+ // should copy the array
+ JSONArray a1 = new JSONArray("[2, \"test2\", true]");
+ JSONArray a2 = new JSONArray(a1);
+ assertNotNull("Should not error", a2);
+ assertEquals("length", a1.length(), a2.length());
+
+ for(int i = 0; i < a1.length(); i++) {
+ assertEquals("index " + i + " are equal", a1.get(i), a2.get(i));
+ }
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ a1, a2
+ )));
+ }
+
+ /**
+ * Verifies that the object constructor can properly handle any supported collection object.
+ */
+ @Test
+ public void testJSONArrayPutAll() {
+ // should copy the array
+ JSONArray a1 = new JSONArray("[2, \"test2\", true]");
+ JSONArray a2 = new JSONArray();
+ a2.putAll(a1);
+ assertNotNull("Should not error", a2);
+ assertEquals("length", a1.length(), a2.length());
+
+ for(int i = 0; i < a1.length(); i++) {
+ assertEquals("index " + i + " are equal", a1.get(i), a2.get(i));
+ }
+ Util.checkJSONArraysMaps(new ArrayList(Arrays.asList(
+ a1, a2
+ )));
+ }
+
+ /**
+ * Tests if calling JSONArray clear() method actually makes the JSONArray empty
+ */
+ @Test(expected = JSONException.class)
+ public void jsonArrayClearMethodTest() {
+ //Adds random stuff to the JSONArray
+ JSONArray jsonArray = new JSONArray();
+ jsonArray.put(123);
+ jsonArray.put("456");
+ jsonArray.put(new JSONArray());
+ jsonArray.clear(); //Clears the JSONArray
+ assertTrue("expected jsonArray.length() == 0", jsonArray.length() == 0); //Check if its length is 0
+ jsonArray.getInt(0); //Should throws org.json.JSONException: JSONArray[0] not found
+ Util.checkJSONArrayMaps(jsonArray);
+ }
+
+ /**
+ * Tests for stack overflow. See https://github.com/stleary/JSON-java/issues/654
+ */
+ @Ignore("This test relies on system constraints and may not always pass. See: https://github.com/stleary/JSON-java/issues/821")
+ @Test(expected = JSONException.class)
+ public void issue654StackOverflowInputWellFormed() {
+ //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes));
+ final InputStream resourceAsStream = JSONArrayTest.class.getClassLoader().getResourceAsStream("Issue654WellFormedArray.json");
+ JSONTokener tokener = new JSONTokener(resourceAsStream);
+ JSONArray json_input = new JSONArray(tokener);
+ assertNotNull(json_input);
+ fail("Excepected Exception due to stack overflow.");
+ 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));
+ }
+
+ @Test(expected = JSONException.class)
+ public void testRecursiveDepth() {
+ HashMap map = new HashMap<>();
+ map.put("t", map);
+ new JSONArray().put(map);
+ }
+
+ @Test(expected = JSONException.class)
+ public void testRecursiveDepthAtPosition() {
+ HashMap map = new HashMap<>();
+ map.put("t", map);
+ new JSONArray().put(0, map);
+ }
+
+ @Test(expected = JSONException.class)
+ public void testRecursiveDepthArray() {
+ ArrayList array = new ArrayList<>();
+ array.add(array);
+ new JSONArray(array);
+ }
+
+ @Test
+ public void testRecursiveDepthAtPositionDefaultObject() {
+ HashMap map = JSONObjectTest.buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
+ new JSONArray().put(0, map);
+ }
+
+ @Test
+ public void testRecursiveDepthAtPosition1000Object() {
+ HashMap map = JSONObjectTest.buildNestedMap(1000);
+ new JSONArray().put(0, map, new JSONParserConfiguration().withMaxNestingDepth(1000));
+ }
+
+ @Test(expected = JSONException.class)
+ public void testRecursiveDepthAtPosition1001Object() {
+ HashMap map = JSONObjectTest.buildNestedMap(1001);
+ new JSONArray().put(0, map);
+ }
+
+ @Test(expected = JSONException.class)
+ public void testRecursiveDepthArrayLimitedMaps() {
+ ArrayList array = new ArrayList<>();
+ array.add(array);
+ new JSONArray(array);
+ }
+
+ @Test
+ public void testRecursiveDepthArrayForDefaultLevels() {
+ ArrayList array = buildNestedArray(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
+ new JSONArray(array, new JSONParserConfiguration());
+ }
+
+ @Test
+ public void testRecursiveDepthArrayFor1000Levels() {
+ try {
+ ArrayList array = buildNestedArray(1000);
+ JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000);
+ new JSONArray(array, parserConfiguration);
+ } catch (StackOverflowError e) {
+ String javaVersion = System.getProperty("java.version");
+ if (javaVersion.startsWith("11.")) {
+ System.out.println(
+ "testRecursiveDepthArrayFor1000Levels() allowing intermittent stackoverflow, Java Version: "
+ + javaVersion);
+ } else {
+ String errorStr = "testRecursiveDepthArrayFor1000Levels() unexpected stackoverflow, Java Version: "
+ + javaVersion;
+ System.out.println(errorStr);
+ throw new RuntimeException(errorStr);
+ }
+ }
+ }
+
+ @Test(expected = JSONException.class)
+ public void testRecursiveDepthArrayFor1001Levels() {
+ ArrayList array = buildNestedArray(1001);
+ new JSONArray(array);
+ }
+
+ @Test
+ public void testStrictModeJSONTokener_expectException(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
+ JSONTokener tokener = new JSONTokener("[\"value\"]invalidCharacters", jsonParserConfiguration);
+
+ assertThrows(JSONException.class, () -> { new JSONArray(tokener); });
+ }
+
+ public static ArrayList buildNestedArray(int maxDepth) {
+ if (maxDepth <= 0) {
+ return new ArrayList<>();
+ }
+ ArrayList nestedArray = new ArrayList<>();
+ nestedArray.add(buildNestedArray(maxDepth - 1));
+ return nestedArray;
+ }
+}
diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java
new file mode 100644
index 000000000..5a360dd59
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONMLTest.java
@@ -0,0 +1,989 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+
+import org.json.*;
+import org.junit.Test;
+
+/**
+ * Tests for org.json.JSONML.java
+ *
+ * Certain inputs are expected to result in exceptions. These tests are
+ * executed first. JSONML provides an API to:
+ * Convert an XML string into a JSONArray or a JSONObject.
+ * Convert a JSONArray or JSONObject into an XML string.
+ * Both fromstring and tostring operations operations should be symmetrical
+ * within the limits of JSONML.
+ * It should be possible to perform the following operations, which should
+ * result in the original string being recovered, within the limits of the
+ * underlying classes:
+ * Convert a string -> JSONArray -> string -> JSONObject -> string
+ * Convert a string -> JSONObject -> string -> JSONArray -> string
+ *
+ */
+public class JSONMLTest {
+
+ /**
+ * Attempts to transform a null XML string to JSON.
+ * Expects a NullPointerException
+ */
+ @Test(expected=NullPointerException.class)
+ public void nullXMLException() {
+ String xmlStr = null;
+ JSONML.toJSONArray(xmlStr);
+ }
+
+ /**
+ * Attempts to transform an empty string to JSON.
+ * Expects a JSONException
+ */
+ @Test
+ public void emptyXMLException() {
+ String xmlStr = "";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Bad XML at 0 [character 1 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempts to call JSONML.toString() with a null JSONArray.
+ * Expects a NullPointerException.
+ */
+ @Test(expected=NullPointerException.class)
+ public void nullJSONXMLException() {
+ /**
+ * Tries to convert a null JSONArray to XML.
+ */
+ JSONArray jsonArray= null;
+ JSONML.toString(jsonArray);
+ }
+
+ /**
+ * Attempts to call JSONML.toString() with a null JSONArray.
+ * Expects a JSONException.
+ */
+ @Test
+ public void emptyJSONXMLException() {
+ /**
+ * Tries to convert an empty JSONArray to XML.
+ */
+ JSONArray jsonArray = new JSONArray();
+ try {
+ JSONML.toString(jsonArray);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "JSONArray[0] not found.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Attempts to transform an non-XML string to JSON.
+ * Expects a JSONException
+ */
+ @Test
+ public void nonXMLException() {
+ /**
+ * Attempts to transform a nonXML string to JSON
+ */
+ String xmlStr = "{ \"this is\": \"not xml\"}";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Bad XML at 23 [character 24 line 1]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempts to transform a JSON document with XML content that
+ * does not follow JSONML conventions (element name is not first value
+ * in a nested JSONArray) to a JSONArray then back to string.
+ * Expects a JSONException
+ */
+ @Test
+ public void emptyTagException() {
+ /**
+ * jsonArrayStr is used to build a JSONArray which is then
+ * turned into XML. For this transformation, all arrays represent
+ * elements and the first array entry is the name of the element.
+ * In this case, one of the arrays does not have a name
+ */
+ String jsonArrayStr =
+ "[\"addresses\","+
+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
+ // this array has no name
+ "["+
+ "[\"name\"],"+
+ "[\"nocontent\"],"+
+ "\">\""+
+ "]"+
+ "]";
+ JSONArray jsonArray = new JSONArray(jsonArrayStr);
+ try {
+ JSONML.toString(jsonArray);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONArray[0] is not a String (class org.json.JSONArray).",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Attempts to transform a JSON document with XML content that
+ * does not follow JSONML conventions (element tag has an embedded space)
+ * to a JSONArray then back to string. Expects a JSONException
+ */
+ @Test
+ public void spaceInTagException() {
+ /**
+ * jsonArrayStr is used to build a JSONArray which is then
+ * turned into XML. For this transformation, all arrays represent
+ * elements and the first array entry is the name of the element.
+ * In this case, one of the element names has an embedded space,
+ * which is not allowed.
+ */
+ String jsonArrayStr =
+ "[\"addresses\","+
+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
+ // this array has an invalid name
+ "[\"addr esses\","+
+ "[\"name\"],"+
+ "[\"nocontent\"],"+
+ "\">\""+
+ "]"+
+ "]";
+ JSONArray jsonArray = new JSONArray(jsonArrayStr);
+ try {
+ JSONML.toString(jsonArray);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "'addr esses' contains a space character.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Attempts to transform a malformed XML document
+ * (element tag has a frontslash) to a JSONArray.\
+ * Expects a JSONException
+ */
+ @Test
+ public void invalidSlashInTagException() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONArray.
+ * In this case, the XML is invalid because the 'name' element
+ * contains an invalid frontslash.
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " abc street \n"+
+ " \n"+
+ " ";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped tag at 176 [character 14 line 4]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Malformed XML text (invalid tagname) is transformed into a JSONArray.
+ * Expects a JSONException.
+ */
+ @Test
+ public void invalidBangInTagException() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped meta tag at 215 [character 12 line 7]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Malformed XML text (invalid tagname, no close bracket) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
+ public void invalidBangNoCloseInTagException() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONArray.
+ * In this case, the XML is invalid because an element
+ * starts with '!' and has no closing tag
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped meta tag at 214 [character 12 line 7]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Malformed XML text (tagname with no close bracket) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
+ public void noCloseStartTagException() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONArray.
+ * In this case, the XML is invalid because an element
+ * has no closing '>'.
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misplaced '<' at 194 [character 5 line 6]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Malformed XML text (endtag with no name) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
+ public void noCloseEndTagException() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONArray.
+ * In this case, the XML is invalid because an element
+ * has no name after the closing tag ''.
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " >\n"+
+ " ";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ assertTrue("Expecting an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expecting an exception message",
+ "Expected a closing name instead of '>'.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Malformed XML text (endtag with no close bracket) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
+ public void noCloseEndBraceException() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONArray.
+ * In this case, the XML is invalid because an element
+ * has '>' after the closing tag '' and name.
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misplaced '<' at 206 [character 1 line 7]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Malformed XML text (incomplete CDATA string) is transformed\
+ * into a JSONArray. Expects a JSONException.
+ */
+ @Test
+ public void invalidCDATABangInTagException() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONArray.
+ * In this case, the XML is invalid because an element
+ * does not have a complete CDATA string.
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " Joe Tester \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ JSONML.toJSONArray(xmlStr);
+ fail("Expecting an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Expected 'CDATA[' at 204 [character 11 line 5]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Convert an XML document into a JSONArray, then use JSONML.toString()
+ * to convert it into a string. This string is then converted back into
+ * a JSONArray. Both JSONArrays are compared against a control to
+ * confirm the contents.
+ */
+ @Test
+ public void toJSONArray() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONArray.
+ * Each element becomes a JSONArray:
+ * 1st entry = elementname
+ * 2nd entry = attributes object (if present)
+ * 3rd entry = content (if present)
+ * 4th entry = child element JSONArrays (if present)
+ * The result is compared against an expected JSONArray.
+ * The transformed JSONArray is then transformed back into a string
+ * which is used to create a final JSONArray, which is also compared
+ * against the expected JSONArray.
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ "\n"+
+ "myName \n"+
+ " >\n"+
+ " \n"+
+ " ";
+ String expectedStr =
+ "[\"addresses\","+
+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
+ "[\"address\","+
+ "{\"attr1\":\"attrValue1\",\"attr2\":\"attrValue2\",\"attr3\":\"attrValue3\"},"+
+ "[\"name\", {\"nameType\":\"mine\"},\"myName\"],"+
+ "[\"nocontent\"],"+
+ "\">\""+
+ "]"+
+ "]";
+ JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
+ JSONArray expectedJsonArray = new JSONArray(expectedStr);
+ String xmlToStr = JSONML.toString(jsonArray);
+ JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+ Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
+ }
+
+ /**
+ * Convert an XML document into a JSONObject. Use JSONML.toString() to
+ * convert it back into a string, and then re-convert it into a JSONObject.
+ * Both JSONObjects are compared against a control JSONObject to confirm
+ * the contents.
+ *
+ * Next convert the XML document into a JSONArray. Use JSONML.toString() to
+ * convert it back into a string, and then re-convert it into a JSONArray.
+ * Both JSONArrays are compared against a control JSONArray to confirm
+ * the contents.
+ *
+ * This test gives a comprehensive example of how the JSONML
+ * transformations work.
+ */
+ @Test
+ public void toJSONObjectToJSONArray() {
+ /**
+ * xmlStr contains XML text which is transformed into a JSONObject,
+ * restored to XML, transformed into a JSONArray, and then restored
+ * to XML again. Both JSONObject and JSONArray should contain the same
+ * information and should produce the same XML, allowing for non-ordered
+ * attributes.
+ *
+ * Transformation to JSONObject:
+ * The elementName is stored as a string where key="tagName"
+ * Attributes are simply stored as key/value pairs
+ * If the element has either content or child elements, they are stored
+ * in a jsonArray with key="childNodes".
+ *
+ * Transformation to JSONArray:
+ * 1st entry = elementname
+ * 2nd entry = attributes object (if present)
+ * 3rd entry = content (if present)
+ * 4th entry = child element JSONArrays (if present)
+ */
+ String xmlStr =
+ "\n"+
+ "\n"+
+ "\n"+
+ "Joe Tester \n"+
+ " \n"+
+ " \n"+
+ "true \n"+
+ "false \n"+
+ "null \n"+
+ "42 \n"+
+ "-23 \n"+
+ "-23.45 \n"+
+ "-23x.45 \n"+
+ "\n"+
+ "1 \n"+
+ "2 \n"+
+ "abc \n"+
+ "3 \n"+
+ "4.1 \n"+
+ "5.2 \n"+
+ " \n"+
+ " \n"+
+ " ";
+
+ String expectedJSONObjectStr =
+ "{"+
+ "\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
+ "\"childNodes\":["+
+ "{"+
+ "\"childNodes\":["+
+ "{"+
+ "\"childNodes\":[\"Joe Tester\"],"+
+ "\"nameType\":\"my name\","+
+ "\"tagName\":\"name\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[\"Baker street 5\"],"+
+ "\"tagName\":\"street\""+
+ "},"+
+ "{"+
+ "\"tagName\":\"NothingHere\","+
+ "\"except\":\"an attribute\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[true],"+
+ "\"tagName\":\"TrueValue\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[false],"+
+ "\"tagName\":\"FalseValue\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[null],"+
+ "\"tagName\":\"NullValue\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[42],"+
+ "\"tagName\":\"PositiveValue\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[-23],"+
+ "\"tagName\":\"NegativeValue\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[-23.45],"+
+ "\"tagName\":\"DoubleValue\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[\"-23x.45\"],"+
+ "\"tagName\":\"Nan\""+
+ "},"+
+ "{"+
+ "\"childNodes\":["+
+ "{"+
+ "\"childNodes\":[1],"+
+ "\"tagName\":\"value\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[2],"+
+ "\"tagName\":\"value\""+
+ "},"+
+ "{"+
+ "\"childNodes\":["+
+ "{"+
+ "\"childNodes\":[\"abc\"],"+
+ "\"svAttr\":\"svValue\","+
+ "\"tagName\":\"subValue\""+
+ "}"+
+ "],"+
+ "\"tagName\":\"value\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[3],"+
+ "\"tagName\":\"value\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[4.1],"+
+ "\"tagName\":\"value\""+
+ "},"+
+ "{"+
+ "\"childNodes\":[5.2],"+
+ "\"tagName\":\"value\""+
+ "}"+
+ "],"+
+ "\"tagName\":\"ArrayOfNum\""+
+ "}"+
+ "],"+
+ "\"addrType\":\"my address\","+
+ "\"tagName\":\"address\""+
+ "}"+
+ "],"+
+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+
+ "\"tagName\":\"addresses\""+
+ "}";
+
+ String expectedJSONArrayStr =
+ "["+
+ "\"addresses\","+
+ "{"+
+ "\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\""+
+ "},"+
+ "["+
+ "\"address\","+
+ "{"+
+ "\"addrType\":\"my address\""+
+ "},"+
+ "["+
+ "\"name\","+
+ "{"+
+ "\"nameType\":\"my name\""+
+ "},"+
+ "\"Joe Tester\""+
+ "],"+
+ "[\"street\",\"Baker street 5\"],"+
+ "["+
+ "\"NothingHere\","+
+ "{\"except\":\"an attribute\"}"+
+ "],"+
+ "[\"TrueValue\",true],"+
+ "[\"FalseValue\",false],"+
+ "[\"NullValue\",null],"+
+ "[\"PositiveValue\",42],"+
+ "[\"NegativeValue\",-23],"+
+ "[\"DoubleValue\",-23.45],"+
+ "[\"Nan\",\"-23x.45\"],"+
+ "["+
+ "\"ArrayOfNum\","+
+ "[\"value\",1],"+
+ "[\"value\",2],"+
+ "[\"value\","+
+ "["+
+ "\"subValue\","+
+ "{\"svAttr\":\"svValue\"},"+
+ "\"abc\""+
+ "]"+
+ "],"+
+ "[\"value\",3],"+
+ "[\"value\",4.1],"+
+ "[\"value\",5.2]"+
+ "]"+
+ "]"+
+ "]";
+
+ // make a JSONObject and make sure it looks as expected
+ JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedJSONObjectStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+
+ // restore the XML, then make another JSONObject and make sure it
+ // looks as expected
+ String jsonObjectXmlToStr = JSONML.toString(jsonObject);
+ JSONObject finalJsonObject = JSONML.toJSONObject(jsonObjectXmlToStr);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
+
+ // create a JSON array from the original string and make sure it
+ // looks as expected
+ JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
+ JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
+
+ // restore the XML, then make another JSONArray and make sure it
+ // looks as expected
+ String jsonArrayXmlToStr = JSONML.toString(jsonArray);
+ JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr);
+ Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
+
+ // lastly, confirm the restored JSONObject XML and JSONArray XML look
+ // reasonably similar
+ JSONObject jsonObjectFromObject = JSONML.toJSONObject(jsonObjectXmlToStr);
+ JSONObject jsonObjectFromArray = JSONML.toJSONObject(jsonArrayXmlToStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObjectFromObject, jsonObjectFromArray);
+ }
+
+ /**
+ * Convert an XML document which contains embedded comments into
+ * a JSONArray. Use JSONML.toString() to turn it into a string, then
+ * reconvert it into a JSONArray. Compare both JSONArrays to a control
+ * JSONArray to confirm the contents.
+ *
+ * This test shows how XML comments are handled.
+ */
+ @Test
+ public void commentsInXML() {
+
+ String xmlStr =
+ "\n"+
+ "\n"+
+ "\n"+
+ "\n"+
+ "\n"+
+ "Joe Tester \n"+
+ "\n"+
+ "Baker street 5 \n"+
+ " \n"+
+ " ";
+ String expectedStr =
+ "[\"addresses\","+
+ "[\"address\","+
+ "[\"name\",\"Joe Tester\"],"+
+ "[\"street\",\"Baker street 5\"]"+
+ "]"+
+ "]";
+ JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
+ JSONArray expectedJsonArray = new JSONArray(expectedStr);
+ String xmlToStr = JSONML.toString(jsonArray);
+ JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+ Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
+ }
+
+ /**
+ * JSON string with lost leading zero and converted "True" to true. See test
+ * result in comment below.
+ */
+ @Test
+ public void testToJSONArray_jsonOutput() {
+ final String originalXml = "01 1 00 0 True ";
+ final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",1],[\"id\",\"00\"],[\"id\",0],[\"item\",{\"id\":\"01\"}],[\"title\",true]]";
+ final JSONArray actualJsonOutput = JSONML.toJSONArray(originalXml, false);
+ assertEquals(expectedJsonString, actualJsonOutput.toString());
+ }
+
+ /**
+ * JSON string cannot be reverted to original xml when type guessing is used.
+ */
+ @Test
+ public void testToJSONArray_reversibility() {
+ final String originalXml = "01 1 00 0 True ";
+ final String revertedXml = JSONML.toString(JSONML.toJSONArray(originalXml, false));
+ assertNotEquals(revertedXml, originalXml);
+ }
+
+ /**
+ * JSON string cannot be reverted to original xml when type guessing is used.
+ * When we force all the values as string, the original text comes back.
+ */
+ @Test
+ public void testToJSONArray_reversibility2() {
+ final String originalXml = "01 1 00 0 True ";
+ final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";
+ final JSONArray json = JSONML.toJSONArray(originalXml,true);
+ assertEquals(expectedJsonString, json.toString());
+
+ final String reverseXml = JSONML.toString(json);
+ assertEquals(originalXml, reverseXml);
+ }
+
+ /**
+ * JSON can be reverted to original xml.
+ */
+ @Test
+ public void testToJSONArray_reversibility3() {
+ final String originalXml = "400
402
";
+ final JSONArray jsonArray = JSONML.toJSONArray(originalXml, false);
+ final String revertedXml = JSONML.toString(jsonArray);
+ assertEquals(revertedXml, originalXml);
+ }
+
+ /**
+ * JSON string cannot be reverted to original xml. See test result in
+ * comment below.
+ */
+ @Test
+ public void testToJSONObject_reversibility() {
+ final String originalXml = "400
402
";
+ final JSONObject originalObject=JSONML.toJSONObject(originalXml,false);
+ final String originalJson = originalObject.toString();
+ final String xml = JSONML.toString(originalObject);
+ final JSONObject revertedObject = JSONML.toJSONObject(xml, false);
+ final String newJson = revertedObject.toString();
+ assertTrue("JSON Objects are not similar", originalObject.similar(revertedObject));
+ assertTrue("JSON Strings are not similar", new JSONObject(originalJson).similar(new JSONObject(newJson)));
+ }
+
+// these tests do not pass for the following reasons:
+// 1. Our XML parser does not handle generic HTML entities, only valid XML entities. Hence
+// or other HTML specific entities would fail on reversability
+// 2. Our JSON implementation for storing the XML attributes uses the standard unordered map.
+// This means that can not be reversed reliably.
+//
+// /**
+// * Test texts taken from jsonml.org. Currently our implementation FAILS this conversion but shouldn't.
+// * Technically JsonML should be able to transform any valid xhtml document, but ours only supports
+// * standard XML entities, not HTML entities.
+// */
+// @Test
+// public void testAttributeConversionReversabilityHTML() {
+// final String originalXml = "
#5D28D1 Example text here #AF44EF 127310656 #AAD034 ©
";
+// final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"\u00A0\",[\"span\",{ \"style\" : \"background-color:maroon\" },\"\u00A9\"],\"\u00A0\"]]]";
+// final JSONArray json = JSONML.toJSONArray(originalXml,true);
+// final String actualJsonString = json.toString();
+//
+// final String reverseXml = JSONML.toString(json);
+// assertNotEquals(originalXml, reverseXml);
+//
+// assertNotEquals(expectedJsonString, actualJsonString);
+// }
+//
+// /**
+// * Test texts taken from jsonml.org but modified to have XML entities only.
+// */
+// @Test
+// public void testAttributeConversionReversabilityXML() {
+// final String originalXml = "#5D28D1 Example text here #AF44EF 127310656 #AAD034 &> <
";
+// final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"&\",[\"span\",{ \"style\" : \"background-color:maroon\" },\">\"],\"<\"]]]";
+// final JSONArray jsonML = JSONML.toJSONArray(originalXml,true);
+// final String actualJsonString = jsonML.toString();
+//
+// final String reverseXml = JSONML.toString(jsonML);
+// // currently not equal because the hashing of the attribute objects makes the attribute
+// // order not happen the same way twice
+// assertEquals(originalXml, reverseXml);
+//
+// assertEquals(expectedJsonString, actualJsonString);
+// }
+
+ @Test (timeout = 6000)
+ public void testIssue484InfinteLoop1() {
+ try {
+ JSONML.toJSONObject("??*^M??|?CglR^F??`??>?w??PIlr^E??D^X^]?$?-^R?o??O?*??{OD?^FY??`2a????NM?b^Tq?:O?>S$^K?J?^FB.gUK?m^H??zE??^??!v]?^A???^[^A??^U?c??????h???s???g^Z???`?q^Dbi??:^QZl?)?}1^??k?0??:$V?$?Ovs(}J??^V????2;^QgQ?^_^A?^D?^U?Tg?K?`?h%c?hmGA??w??PIlr??D?$?-?o??O?*??{OD?Y??`2a????NM?bq?:O?>S$?J?B.gUK?m\b??zE???!v]???????c??????h???s???g???`?qbi??:Zl?)?}1^??k?0??:$V?$?Ovs(}J??????2;gQ????Tg?K?`?h%c?hmGA?");
+
+ final int maxNestingDepth = 42;
+
+ try {
+ JSONML.toJSONArray(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
+
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
+ e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
+ }
+ }
+
+
+ @Test
+ public void testToJSONArrayMaxNestingDepthIsRespectedWithValidXML() {
+ final String perfectlyFineXML = "\n" +
+ " \n" +
+ " sonoo \n" +
+ " 56000 \n" +
+ " true \n" +
+ " \n" +
+ " \n";
+
+ final int maxNestingDepth = 1;
+
+ try {
+ JSONML.toJSONArray(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
+
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
+ e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
+ }
+ }
+
+ @Test
+ public void testToJSONArrayMaxNestingDepthWithValidFittingXML() {
+ final String perfectlyFineXML = "\n" +
+ " \n" +
+ " sonoo \n" +
+ " 56000 \n" +
+ " true \n" +
+ " \n" +
+ " \n";
+
+ final int maxNestingDepth = 3;
+
+ try {
+ JSONML.toJSONArray(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ fail("XML document should be parsed as its maximum depth fits the maxNestingDepth " +
+ "parameter of the JSONMLParserConfiguration used");
+ }
+ }
+
+
+ @Test
+ public void testToJSONObjectMaxDefaultNestingDepthIsRespected() {
+ final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", "");
+
+ try {
+ JSONML.toJSONObject(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL);
+
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
+ e.getMessage().startsWith("Maximum nesting depth of " + JSONMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH));
+ }
+ }
+
+ @Test
+ public void testToJSONObjectUnlimitedNestingDepthIsPossible() {
+ int actualDepth = JSONMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH +10;
+ final String deeperThanDefaultMax = new String(new char[actualDepth]).replace("\0", " ") +
+ "value" +
+ new String(new char[actualDepth]).replace("\0", " ");
+
+ try {
+ JSONML.toJSONObject(deeperThanDefaultMax, JSONMLParserConfiguration.ORIGINAL
+ .withMaxNestingDepth(JSONMLParserConfiguration.UNDEFINED_MAXIMUM_NESTING_DEPTH));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ fail("XML document should be parsed beyond the default maximum depth if maxNestingDepth " +
+ "parameter is set to -1 in JSONMLParserConfiguration");
+ }
+ }
+
+
+ @Test
+ public void testToJSONObjectMaxNestingDepthOf42IsRespected() {
+ final String wayTooLongMalformedXML = new String(new char[6000]).replace("\0", "");
+
+ final int maxNestingDepth = 42;
+
+ try {
+ JSONML.toJSONObject(wayTooLongMalformedXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
+
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
+ e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
+ }
+ }
+
+ @Test
+ public void testToJSONObjectMaxNestingDepthIsRespectedWithValidXML() {
+ final String perfectlyFineXML = "\n" +
+ " \n" +
+ " sonoo \n" +
+ " 56000 \n" +
+ " true \n" +
+ " \n" +
+ " \n";
+
+ final int maxNestingDepth = 1;
+
+ try {
+ JSONML.toJSONObject(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
+
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertTrue("Wrong throwable thrown: not expecting message <" + e.getMessage() + ">",
+ e.getMessage().startsWith("Maximum nesting depth of " + maxNestingDepth));
+ }
+ }
+
+ @Test
+ public void testToJSONObjectMaxNestingDepthWithValidFittingXML() {
+ final String perfectlyFineXML = "\n" +
+ " \n" +
+ " sonoo \n" +
+ " 56000 \n" +
+ " true \n" +
+ " \n" +
+ " \n";
+
+ final int maxNestingDepth = 3;
+
+ try {
+ JSONML.toJSONObject(perfectlyFineXML, JSONMLParserConfiguration.ORIGINAL.withMaxNestingDepth(maxNestingDepth));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ fail("XML document should be parsed as its maximum depth fits the maxNestingDepth " +
+ "parameter of the JSONMLParserConfiguration used");
+ }
+ }
+
+}
diff --git a/src/test/java/org/json/junit/JSONObjectLocaleTest.java b/src/test/java/org/json/junit/JSONObjectLocaleTest.java
new file mode 100755
index 000000000..1cdaf743d
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONObjectLocaleTest.java
@@ -0,0 +1,60 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.json.*;
+import org.json.junit.data.MyLocaleBean;
+import org.junit.*;
+
+/**
+ * Note: This file is saved as UTF-8. Do not save as ASCII or the tests will
+ * fail.
+ *
+ */
+public class JSONObjectLocaleTest {
+ /**
+ * JSONObject built from a bean with locale-specific keys.
+ * In the Turkish alphabet, there are 2 versions of the letter "i".
+ * 'eh' I ı (dotless i)
+ * 'ee' İ i (dotted i)
+ * A problem can occur when parsing the public get methods for a bean.
+ * If the method starts with getI... then the key name will be lowercased
+ * to 'i' in English, and 'ı' in Turkish.
+ * We want the keys to be consistent regardless of locale, so JSON-Java
+ * lowercase operations are made to be locale-neutral by specifying
+ * Locale.ROOT. This causes 'I' to be universally lowercased to 'i'
+ * regardless of the locale currently in effect.
+ */
+ @Test
+ public void jsonObjectByLocaleBean() {
+
+ MyLocaleBean myLocaleBean = new MyLocaleBean();
+
+ /**
+ * This is just the control case which happens when the locale.ROOT
+ * lowercasing behavior is the same as the current locale.
+ */
+ Locale.setDefault(new Locale("en"));
+ JSONObject jsonen = new JSONObject(myLocaleBean);
+ assertEquals("expected size 2, found: " +jsonen.length(), 2, jsonen.length());
+ assertEquals("expected jsonen[i] == beanI", "beanI", jsonen.getString("i"));
+ assertEquals("expected jsonen[id] == beanId", "beanId", jsonen.getString("id"));
+
+ /**
+ * Without the JSON-Java change, these keys would be stored internally as
+ * starting with the letter, 'ı' (dotless i), since the lowercasing of
+ * the getI and getId keys would be specific to the Turkish locale.
+ */
+ Locale.setDefault(new Locale("tr"));
+ JSONObject jsontr = new JSONObject(myLocaleBean);
+ assertEquals("expected size 2, found: " +jsontr.length(), 2, jsontr.length());
+ assertEquals("expected jsontr[i] == beanI", "beanI", jsontr.getString("i"));
+ assertEquals("expected jsontr[id] == beanId", "beanId", jsontr.getString("id"));
+ }
+}
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 000000000..0f2af2902
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONObjectNumberTest.java
@@ -0,0 +1,146 @@
+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 data() {
+ return Arrays.asList(new Object[][]{
+ {"{\"value\":50}", 1},
+ {"{\"value\":50.0}", 1},
+ {"{\"value\":5e1}", 1},
+ {"{\"value\":5E1}", 1},
+ {"{\"value\":5e1}", 1},
+ {"{\"value\":\"50\"}", 1},
+ {"{\"value\":-50}", -1},
+ {"{\"value\":-50.0}", -1},
+ {"{\"value\":-5e1}", -1},
+ {"{\"value\":-5E1}", -1},
+ {"{\"value\":-5e1}", -1},
+ {"{\"value\":\"-50\"}", -1}
+ // JSON does not support octal or hex numbers;
+ // see https://stackoverflow.com/a/52671839/6323312
+ // "{value:062}", // octal 50
+ // "{value:0x32}" // hex 50
+ });
+ }
+
+ public JSONObjectNumberTest(String objectString, int resultIsNegative) {
+ this.objectString = objectString;
+ this.value *= resultIsNegative;
+ }
+
+ private JSONObject object;
+
+ @Before
+ public void setJsonObject() {
+ object = new JSONObject(objectString);
+ }
+
+ @Test
+ public void testGetNumber() {
+ assertEquals(value.intValue(), object.getNumber("value").intValue());
+ }
+
+ @Test
+ public void testGetBigDecimal() {
+ assertTrue(BigDecimal.valueOf(value).compareTo(object.getBigDecimal("value")) == 0);
+ }
+
+ @Test
+ public void testGetBigInteger() {
+ assertEquals(BigInteger.valueOf(value), object.getBigInteger("value"));
+ }
+
+ @Test
+ public void testGetFloat() {
+ assertEquals(value.floatValue(), object.getFloat("value"), 0.0f);
+ }
+
+ @Test
+ public void testGetDouble() {
+ assertEquals(value.doubleValue(), object.getDouble("value"), 0.0d);
+ }
+
+ @Test
+ public void testGetInt() {
+ assertEquals(value.intValue(), object.getInt("value"));
+ }
+
+ @Test
+ public void testGetLong() {
+ assertEquals(value.longValue(), object.getLong("value"));
+ }
+
+ @Test
+ public void testOptNumber() {
+ assertEquals(value.intValue(), object.optNumber("value").intValue());
+ }
+
+ @Test
+ public void testOptBigDecimal() {
+ assertTrue(BigDecimal.valueOf(value).compareTo(object.optBigDecimal("value", null)) == 0);
+ }
+
+ @Test
+ public void testOptBigInteger() {
+ assertEquals(BigInteger.valueOf(value), object.optBigInteger("value", null));
+ }
+
+ @Test
+ public void testOptFloat() {
+ assertEquals(value.floatValue(), object.optFloat("value"), 0.0f);
+ }
+
+ @Test
+ public void testOptFloatObject() {
+ assertEquals((Float) value.floatValue(), object.optFloatObject("value"), 0.0f);
+ }
+
+ @Test
+ public void testOptDouble() {
+ assertEquals(value.doubleValue(), object.optDouble("value"), 0.0d);
+ }
+
+ @Test
+ public void testOptDoubleObject() {
+ assertEquals((Double) value.doubleValue(), object.optDoubleObject("value"), 0.0d);
+ }
+
+ @Test
+ public void testOptInt() {
+ assertEquals(value.intValue(), object.optInt("value"));
+ }
+
+ @Test
+ public void testOptIntegerObject() {
+ assertEquals((Integer) value.intValue(), object.optIntegerObject("value"));
+ }
+
+ @Test
+ public void testOptLong() {
+ assertEquals(value.longValue(), object.optLong("value"));
+ }
+
+ @Test
+ public void testOptLongObject() {
+ assertEquals((Long) value.longValue(), object.optLongObject("value"));
+ }
+}
diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java
new file mode 100644
index 000000000..3c3436846
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONObjectTest.java
@@ -0,0 +1,4097 @@
+package org.json.junit;
+
+/*
+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.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.regex.Pattern;
+
+import org.json.CDL;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONPointerException;
+import org.json.JSONParserConfiguration;
+import org.json.JSONString;
+import org.json.JSONTokener;
+import org.json.ParserConfiguration;
+import org.json.XML;
+import org.json.junit.data.BrokenToString;
+import org.json.junit.data.ExceptionalBean;
+import org.json.junit.data.Fraction;
+import org.json.junit.data.GenericBean;
+import org.json.junit.data.GenericBeanInt;
+import org.json.junit.data.MyBean;
+import org.json.junit.data.MyBeanCustomName;
+import org.json.junit.data.MyBeanCustomNameSubClass;
+import org.json.junit.data.MyBigNumberBean;
+import org.json.junit.data.MyEnum;
+import org.json.junit.data.MyEnumField;
+import org.json.junit.data.MyJsonString;
+import org.json.junit.data.MyNumber;
+import org.json.junit.data.MyNumberContainer;
+import org.json.junit.data.MyPublicClass;
+import org.json.junit.data.RecursiveBean;
+import org.json.junit.data.RecursiveBeanEquals;
+import org.json.junit.data.Singleton;
+import org.json.junit.data.SingletonEnum;
+import org.json.junit.data.WeirdList;
+import org.junit.After;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.JsonPath;
+
+/**
+ * JSONObject, along with JSONArray, are the central classes of the reference app.
+ * All of the other classes interact with them, and JSON functionality would
+ * otherwise be impossible.
+ */
+public class JSONObjectTest {
+
+ /**
+ * Regular Expression Pattern that matches JSON Numbers. This is primarily used for
+ * output to guarantee that we are always writing valid JSON.
+ */
+ static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?");
+
+ @After
+ public void tearDown() {
+ SingletonEnum.getInstance().setSomeInt(0);
+ SingletonEnum.getInstance().setSomeString(null);
+ Singleton.getInstance().setSomeInt(0);
+ Singleton.getInstance().setSomeString(null);
+ }
+
+ /**
+ * Tests that the similar method is working as expected.
+ */
+ @Test
+ public void verifySimilar() {
+ final String string1 = "HasSameRef";
+ final String string2 = "HasDifferentRef";
+ JSONObject obj1 = new JSONObject()
+ .put("key1", "abc")
+ .put("key2", 2)
+ .put("key3", string1);
+
+ JSONObject obj2 = new JSONObject()
+ .put("key1", "abc")
+ .put("key2", 3)
+ .put("key3", string1);
+
+ JSONObject obj3 = new JSONObject()
+ .put("key1", "abc")
+ .put("key2", 2)
+ .put("key3", new String(string1));
+
+ JSONObject obj4 = new JSONObject()
+ .put("key1", "abc")
+ .put("key2", 2.0)
+ .put("key3", new String(string1));
+
+ JSONObject obj5 = new JSONObject()
+ .put("key1", "abc")
+ .put("key2", 2.0)
+ .put("key3", new String(string2));
+
+ assertFalse("obj1-obj2 Should eval to false", obj1.similar(obj2));
+ assertTrue("obj1-obj3 Should eval to true", obj1.similar(obj3));
+ assertTrue("obj1-obj4 Should eval to true", obj1.similar(obj4));
+ assertFalse("obj1-obj5 Should eval to false", obj1.similar(obj5));
+ // verify that a double and big decimal are "similar"
+ assertTrue("should eval to true",new JSONObject().put("a",1.1d).similar(new JSONObject("{\"a\":1.1}")));
+ // Confirm #618 is fixed (compare should not exit early if similar numbers are found)
+ // Note that this test may not work if the JSONObject map entry order changes
+ JSONObject first = new JSONObject("{\"a\": 1, \"b\": 2, \"c\": 3}");
+ JSONObject second = new JSONObject("{\"a\": 1, \"b\": 2.0, \"c\": 4}");
+ assertFalse("first-second should eval to false", first.similar(second));
+ List jsonObjects = new ArrayList(
+ Arrays.asList(obj1, obj2, obj3, obj4, obj5)
+ );
+ Util.checkJSONObjectsMaps(jsonObjects);
+ }
+
+ @Test
+ public void timeNumberParsing() {
+ // test data to use
+ final String[] testData = new String[] {
+ null,
+ "",
+ "100",
+ "-100",
+ "abc123",
+ "012345",
+ "100.5e199",
+ "-100.5e199",
+ "DEADBEEF",
+ "0xDEADBEEF",
+ "1234567890.1234567890",
+ "-1234567890.1234567890",
+ "adloghakuidghauiehgauioehgdkjfb nsruoh aeu noerty384 nkljfgh "
+ + "395h tdfn kdz8yt3 4hkls gn.ey85 4hzfhnz.o8y5a84 onvklt "
+ + "yh389thub nkz8y49lihv al4itlaithknty8hnbl"
+ // long (in length) number sequences with invalid data at the end of the
+ // string offer very poor performance for the REGEX.
+ ,"123467890123467890123467890123467890123467890123467890123467"
+ + "8901234678901234678901234678901234678901234678901234678"
+ + "9012346789012346789012346789012346789012346789012346789"
+ + "0a"
+ };
+ final int testDataLength = testData.length;
+ /**
+ * Changed to 1000 for faster test runs
+ */
+ // final int iterations = 1000000;
+ final int iterations = 1000;
+
+ // 10 million iterations 1,000,000 * 10 (currently 100,000)
+ long startTime = System.nanoTime();
+ for(int i = 0; i < iterations; i++) {
+ for(int j = 0; j < testDataLength; j++) {
+ try {
+ BigDecimal v1 = new BigDecimal(testData[j]);
+ v1.signum();
+ } catch(Exception ignore) {
+ //do nothing
+ }
+ }
+ }
+ final long elapsedNano1 = System.nanoTime() - startTime;
+ System.out.println("new BigDecimal(testData[]) : " + elapsedNano1 / 1000000 + " ms");
+
+ startTime = System.nanoTime();
+ for(int i = 0; i < iterations; i++) {
+ for(int j = 0; j < testDataLength; j++) {
+ try {
+ boolean v2 = NUMBER_PATTERN.matcher(testData[j]).matches();
+ assert v2 == !!v2;
+ } catch(Exception ignore) {
+ //do nothing
+ }
+ }
+ }
+ final long elapsedNano2 = System.nanoTime() - startTime;
+ System.out.println("NUMBER_PATTERN.matcher(testData[]).matches() : " + elapsedNano2 / 1000000 + " ms");
+ // don't assert normally as the testing is machine dependent.
+ // assertTrue("Expected Pattern matching to be faster than BigDecimal constructor",elapsedNano2)(JsonPath.read(doc, "$"))).size() == 4);
+ assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey")));
+ assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey")));
+ assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey")));
+ assertTrue("expected \"doubleKey\":-23.45e67", new BigDecimal("-23.45e67").equals(jsonObjectByName.query("/doubleKey")));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(jsonObject, jsonObjectByName)));
+ }
+
+ /**
+ * JSONObjects can be built from a Map.
+ * In this test the map is null.
+ * the JSONObject(JsonTokener) ctor is not tested directly since it already
+ * has full coverage from other tests.
+ */
+ @Test
+ public void jsonObjectByNullMap() {
+ Map map = null;
+ JSONObject jsonObject = new JSONObject(map);
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * JSONObjects can be built from a Map.
+ * In this test all of the map entries are valid JSON types.
+ */
+ @Test
+ public void jsonObjectByMap() {
+ Map map = new HashMap();
+ map.put("trueKey", Boolean.valueOf(true));
+ map.put("falseKey", Boolean.valueOf(false));
+ map.put("stringKey", "hello world!");
+ map.put("escapeStringKey", "h\be\tllo w\u1234orld!");
+ map.put("intKey", Long.valueOf(42));
+ map.put("doubleKey", Double.valueOf(-23.45e67));
+ JSONObject jsonObject = new JSONObject(map);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
+ assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
+ assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey")));
+ assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
+ assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Verifies that the constructor has backwards compatability with RAW types pre-java5.
+ */
+ @Test
+ public void verifyConstructor() {
+
+ final JSONObject expected = new JSONObject("{\"myKey\":10}");
+
+ @SuppressWarnings("rawtypes")
+ Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
+ JSONObject jaRaw = new JSONObject(myRawC);
+
+ Map myCStrObj = Collections.singletonMap("myKey",
+ (Object) Integer.valueOf(10));
+ JSONObject jaStrObj = new JSONObject(myCStrObj);
+
+ Map myCStrInt = Collections.singletonMap("myKey",
+ Integer.valueOf(10));
+ JSONObject jaStrInt = new JSONObject(myCStrInt);
+
+ Map, ?> myCObjObj = Collections.singletonMap((Object) "myKey",
+ (Object) Integer.valueOf(10));
+ JSONObject jaObjObj = new JSONObject(myCObjObj);
+
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaRaw));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaStrObj));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaStrInt));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaObjObj));
+ Util.checkJSONObjectsMaps(new ArrayList(
+ Arrays.asList(jaRaw, jaStrObj, jaStrInt, jaObjObj))
+ );
+ }
+
+ /**
+ * Tests Number serialization.
+ */
+ @Test
+ public void verifyNumberOutput(){
+ /**
+ * MyNumberContainer is a POJO, so call JSONObject(bean),
+ * which builds a map of getter names/values
+ * The only getter is getMyNumber (key=myNumber),
+ * whose return value is MyNumber. MyNumber extends Number,
+ * but is not recognized as such by wrap() per current
+ * implementation, so wrap() returns the default new JSONObject(bean).
+ * The only getter is getNumber (key=number), whose return value is
+ * BigDecimal(42).
+ */
+ JSONObject jsonObject0 = new JSONObject(new MyNumberContainer());
+ String actual = jsonObject0.toString();
+ String expected = "{\"myNumber\":{\"number\":42}}";
+ assertEquals("Equal", expected , actual);
+
+ /**
+ * JSONObject.put() handles objects differently than the
+ * bean constructor. Where the bean ctor wraps objects before
+ * placing them in the map, put() inserts the object without wrapping.
+ * In this case, a MyNumber instance is the value.
+ * The MyNumber.toString() method is responsible for
+ * returning a reasonable value: the string '42'.
+ */
+ JSONObject jsonObject1 = new JSONObject();
+ jsonObject1.put("myNumber", new MyNumber());
+ actual = jsonObject1.toString();
+ expected = "{\"myNumber\":42}";
+ assertEquals("Equal", expected , actual);
+
+ /**
+ * Calls the JSONObject(Map) ctor, which calls wrap() for values.
+ * AtomicInteger is a Number, but is not recognized by wrap(), per
+ * current implementation. However, the type is
+ * 'java.util.concurrent.atomic', so due to the 'java' prefix,
+ * wrap() inserts the value as a string. That is why 42 comes back
+ * wrapped in quotes.
+ */
+ JSONObject jsonObject2 = new JSONObject(Collections.singletonMap("myNumber", new AtomicInteger(42)));
+ actual = jsonObject2.toString();
+ expected = "{\"myNumber\":\"42\"}";
+ assertEquals("Equal", expected , actual);
+
+ /**
+ * JSONObject.put() inserts the AtomicInteger directly into the
+ * map not calling wrap(). In toString()->write()->writeValue(),
+ * AtomicInteger is recognized as a Number, and converted via
+ * numberToString() into the unquoted string '42'.
+ */
+ JSONObject jsonObject3 = new JSONObject();
+ jsonObject3.put("myNumber", new AtomicInteger(42));
+ actual = jsonObject3.toString();
+ expected = "{\"myNumber\":42}";
+ assertEquals("Equal", expected , actual);
+
+ /**
+ * Calls the JSONObject(Map) ctor, which calls wrap() for values.
+ * Fraction is a Number, but is not recognized by wrap(), per
+ * current implementation. As a POJO, Fraction is handled as a
+ * bean and inserted into a contained JSONObject. It has 2 getters,
+ * for numerator and denominator.
+ */
+ JSONObject jsonObject4 = new JSONObject(Collections.singletonMap("myNumber", new Fraction(4,2)));
+ assertEquals(1, jsonObject4.length());
+ assertEquals(2, ((JSONObject)(jsonObject4.get("myNumber"))).length());
+ assertEquals("Numerator", BigInteger.valueOf(4) , jsonObject4.query("/myNumber/numerator"));
+ assertEquals("Denominator", BigInteger.valueOf(2) , jsonObject4.query("/myNumber/denominator"));
+
+ /**
+ * JSONObject.put() inserts the Fraction directly into the
+ * map not calling wrap(). In toString()->write()->writeValue(),
+ * Fraction is recognized as a Number, and converted via
+ * numberToString() into the unquoted string '4/2'. But the
+ * BigDecimal sanity check fails, so writeValue() defaults
+ * to returning a safe JSON quoted string. Pretty slick!
+ */
+ JSONObject jsonObject5 = new JSONObject();
+ jsonObject5.put("myNumber", new Fraction(4,2));
+ actual = jsonObject5.toString();
+ expected = "{\"myNumber\":\"4/2\"}"; // valid JSON, bug fixed
+ assertEquals("Equal", expected , actual);
+
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject0, jsonObject1, jsonObject2, jsonObject3, jsonObject4, jsonObject5
+ )));
+ }
+
+ /**
+ * Verifies that the put Collection has backwards compatibility with RAW types pre-java5.
+ */
+ @Test
+ public void verifyPutCollection() {
+
+ final JSONObject expected = new JSONObject("{\"myCollection\":[10]}");
+
+ @SuppressWarnings("rawtypes")
+ Collection myRawC = Collections.singleton(Integer.valueOf(10));
+ JSONObject jaRaw = new JSONObject();
+ jaRaw.put("myCollection", myRawC);
+
+ Collection myCObj = Collections.singleton((Object) Integer
+ .valueOf(10));
+ JSONObject jaObj = new JSONObject();
+ jaObj.put("myCollection", myCObj);
+
+ Collection myCInt = Collections.singleton(Integer
+ .valueOf(10));
+ JSONObject jaInt = new JSONObject();
+ jaInt.put("myCollection", myCInt);
+
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaRaw));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaObj));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaInt));
+
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jaRaw, jaObj, jaInt
+ )));
+ }
+
+
+ /**
+ * Verifies that the put Map has backwards compatibility with RAW types pre-java5.
+ */
+ @Test
+ public void verifyPutMap() {
+
+ final JSONObject expected = new JSONObject("{\"myMap\":{\"myKey\":10}}");
+
+ @SuppressWarnings("rawtypes")
+ Map myRawC = Collections.singletonMap("myKey", Integer.valueOf(10));
+ JSONObject jaRaw = new JSONObject();
+ jaRaw.put("myMap", myRawC);
+
+ Map myCStrObj = Collections.singletonMap("myKey",
+ (Object) Integer.valueOf(10));
+ JSONObject jaStrObj = new JSONObject();
+ jaStrObj.put("myMap", myCStrObj);
+
+ Map myCStrInt = Collections.singletonMap("myKey",
+ Integer.valueOf(10));
+ JSONObject jaStrInt = new JSONObject();
+ jaStrInt.put("myMap", myCStrInt);
+
+ Map, ?> myCObjObj = Collections.singletonMap((Object) "myKey",
+ (Object) Integer.valueOf(10));
+ JSONObject jaObjObj = new JSONObject();
+ jaObjObj.put("myMap", myCObjObj);
+
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaRaw));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaStrObj));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaStrInt));
+ assertTrue(
+ "The RAW Collection should give me the same as the Typed Collection",
+ expected.similar(jaObjObj));
+
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jaRaw, jaStrObj, jaStrInt, jaStrObj
+ )));
+ }
+
+
+ /**
+ * JSONObjects can be built from a Map.
+ * In this test the map entries are not valid JSON types.
+ * The actual conversion is kind of interesting.
+ */
+ @Test
+ public void jsonObjectByMapWithUnsupportedValues() {
+ Map jsonMap = new HashMap();
+ // Just insert some random objects
+ jsonMap.put("key1", new CDL());
+ jsonMap.put("key2", new Exception());
+
+ JSONObject jsonObject = new JSONObject(jsonMap);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected 0 key1 items", ((Map,?>)(JsonPath.read(doc, "$.key1"))).size() == 0);
+ assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(jsonObject.query("/key2")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * JSONObjects can be built from a Map.
+ * In this test one of the map values is null
+ */
+ @Test
+ public void jsonObjectByMapWithNullValue() {
+ Map map = new HashMap();
+ map.put("trueKey", Boolean.valueOf(true));
+ map.put("falseKey", Boolean.valueOf(false));
+ map.put("stringKey", "hello world!");
+ map.put("nullKey", null);
+ map.put("escapeStringKey", "h\be\tllo w\u1234orld!");
+ map.put("intKey", Long.valueOf(42));
+ map.put("doubleKey", Double.valueOf(-23.45e67));
+ JSONObject jsonObject = new JSONObject(map);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
+ assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
+ assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey")));
+ assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
+ assertTrue("expected \"intKey\":42", Long.valueOf("42").equals(jsonObject.query("/intKey")));
+ assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ @Test
+ public void jsonObjectByMapWithNullValueAndParserConfiguration() {
+ Map map = new HashMap();
+ map.put("nullKey", null);
+
+ // by default, null values are ignored
+ JSONObject obj1 = new JSONObject(map);
+ assertTrue("expected null value to be ignored by default", obj1.isEmpty());
+
+ // if configured, null values are written as such into the JSONObject.
+ JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
+ JSONObject obj2 = new JSONObject(map, parserConfiguration);
+ assertFalse("expected null value to accepted when configured", obj2.isEmpty());
+ assertTrue(obj2.has("nullKey"));
+ assertEquals(JSONObject.NULL, obj2.get("nullKey"));
+ }
+
+ @Test
+ public void jsonObjectByMapWithNestedNullValueAndParserConfiguration() {
+ Map map = new HashMap();
+ Map nestedMap = new HashMap();
+ nestedMap.put("nullKey", null);
+ map.put("nestedMap", nestedMap);
+ List> nestedList = new ArrayList>();
+ nestedList.add(nestedMap);
+ map.put("nestedList", nestedList);
+
+ JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withUseNativeNulls(true);
+ JSONObject jsonObject = new JSONObject(map, parserConfiguration);
+
+ JSONObject nestedObject = jsonObject.getJSONObject("nestedMap");
+ assertTrue(nestedObject.has("nullKey"));
+ assertEquals(JSONObject.NULL, nestedObject.get("nullKey"));
+
+ JSONArray nestedArray = jsonObject.getJSONArray("nestedList");
+ assertEquals(1, nestedArray.length());
+ assertTrue(nestedArray.getJSONObject(0).has("nullKey"));
+ assertEquals(JSONObject.NULL, nestedArray.getJSONObject(0).get("nullKey"));
+ }
+
+ /**
+ * JSONObject built from a bean. In this case all but one of the
+ * bean getters return valid JSON types
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void jsonObjectByBean1() {
+ /**
+ * Default access classes have to be mocked since JSONObject, which is
+ * not in the same package, cannot call MyBean methods by reflection.
+ */
+ MyBean myBean = mock(MyBean.class);
+ when(myBean.getDoubleKey()).thenReturn(-23.45e7);
+ when(myBean.getIntKey()).thenReturn(42);
+ when(myBean.getStringKey()).thenReturn("hello world!");
+ when(myBean.getEscapeStringKey()).thenReturn("h\be\tllo w\u1234orld!");
+ when(myBean.isTrueKey()).thenReturn(true);
+ when(myBean.isFalseKey()).thenReturn(false);
+ when(myBean.getStringReaderKey()).thenReturn(
+ new StringReader("") {
+ });
+
+ JSONObject jsonObject = new JSONObject(myBean);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 8 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 8);
+ assertTrue("expected 0 items in stringReaderKey", ((Map, ?>) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
+ assertTrue("expected hello world!","hello world!".equals(jsonObject.query("/stringKey")));
+ assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
+ assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey")));
+ assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey")));
+ // sorry, mockito artifact
+ assertTrue("expected 2 mockitoInterceptor items", ((Map,?>)(JsonPath.read(doc, "$.mockitoInterceptor"))).size() == 2);
+ assertTrue("expected 0 mockitoInterceptor.serializationSupport items",
+ ((Map,?>)(JsonPath.read(doc, "$.mockitoInterceptor.serializationSupport"))).size() == 0);
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * JSONObject built from a bean that has custom field names.
+ */
+ @Test
+ public void jsonObjectByBean2() {
+ JSONObject jsonObject = new JSONObject(new MyBeanCustomName());
+ assertNotNull(jsonObject);
+ assertEquals("Wrong number of keys found:",
+ 5,
+ jsonObject.keySet().size());
+ assertFalse("Normal field name (someString) processing did not work",
+ jsonObject.has("someString"));
+ assertFalse("Normal field name (myDouble) processing did not work",
+ jsonObject.has("myDouble"));
+ assertFalse("Normal field name (someFloat) processing did not work",
+ jsonObject.has("someFloat"));
+ assertFalse("Ignored field not found!",
+ jsonObject.has("ignoredInt"));
+ // getSomeInt() has no user-defined annotation
+ assertTrue("Normal field name (someInt) should have been found",
+ jsonObject.has("someInt"));
+ // the user-defined annotation does not replace any value, so someLong should be found
+ assertTrue("Normal field name (someLong) should have been found",
+ jsonObject.has("someLong"));
+ // myStringField replaces someString property name via user-defined annotation
+ assertTrue("Overridden String field name (myStringField) should have been found",
+ jsonObject.has("myStringField"));
+ // weird name replaces myDouble property name via user-defined annotation
+ assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) should have been found",
+ jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!"));
+ // InterfaceField replaces someFloat property name via user-defined annotation
+ assertTrue("Overridden String field name (InterfaceField) should have been found",
+ jsonObject.has("InterfaceField"));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * JSONObject built from a bean that has custom field names inherited from a parent class.
+ */
+ @Test
+ public void jsonObjectByBean3() {
+ JSONObject jsonObject = new JSONObject(new MyBeanCustomNameSubClass());
+ assertNotNull(jsonObject);
+ assertEquals("Wrong number of keys found:",
+ 7,
+ jsonObject.keySet().size());
+ assertFalse("Normal int field name (someInt) found, but was overridden",
+ jsonObject.has("someInt"));
+ assertFalse("Normal field name (myDouble) processing did not work",
+ jsonObject.has("myDouble"));
+ // myDouble was replaced by weird name, and then replaced again by AMoreNormalName via user-defined annotation
+ assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) should not be FOUND!",
+ jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!"));
+ assertFalse("Normal field name (someFloat) found, but was overridden",
+ jsonObject.has("someFloat"));
+ assertFalse("Ignored field found! but was overridden",
+ jsonObject.has("ignoredInt"));
+ // shouldNotBeJSON property name was first ignored, then replaced by ShouldBeIgnored via user-defined annotations
+ assertFalse("Ignored field at the same level as forced name should not have been found",
+ jsonObject.has("ShouldBeIgnored"));
+ // able property name was replaced by Getable via user-defined annotation
+ assertFalse("Normally ignored field (able) with explicit property name should not have been found",
+ jsonObject.has("able"));
+ // property name someInt was replaced by newIntFieldName via user-defined annotation
+ assertTrue("Overridden int field name (newIntFieldName) should have been found",
+ jsonObject.has("newIntFieldName"));
+ // property name someLong was not replaced via user-defined annotation
+ assertTrue("Normal field name (someLong) should have been found",
+ jsonObject.has("someLong"));
+ // property name someString was replaced by myStringField via user-defined annotation
+ assertTrue("Overridden String field name (myStringField) should have been found",
+ jsonObject.has("myStringField"));
+ // property name myDouble was replaced by a weird name, followed by AMoreNormalName via user-defined annotations
+ assertTrue("Overridden double field name (AMoreNormalName) should have been found",
+ jsonObject.has("AMoreNormalName"));
+ // property name someFloat was replaced by InterfaceField via user-defined annotation
+ assertTrue("Overridden String field name (InterfaceField) should have been found",
+ jsonObject.has("InterfaceField"));
+ // property name ignoredInt was replaced by none, followed by forcedInt via user-defined annotations
+ assertTrue("Forced field should have been found!",
+ jsonObject.has("forcedInt"));
+ // property name able was replaced by Getable via user-defined annotation
+ assertTrue("Overridden boolean field name (Getable) should have been found",
+ jsonObject.has("Getable"));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * A bean is also an object. But in order to test the JSONObject
+ * ctor that takes an object and a list of names,
+ * this particular bean needs some public
+ * data members, which have been added to the class.
+ */
+ @Test
+ public void jsonObjectByObjectAndNames() {
+ String[] keys = {"publicString", "publicInt"};
+ // just need a class that has public data members
+ MyPublicClass myPublicClass = new MyPublicClass();
+ JSONObject jsonObject = new JSONObject(myPublicClass, keys);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected \"publicString\":\"abc\"", "abc".equals(jsonObject.query("/publicString")));
+ assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(jsonObject.query("/publicInt")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise the JSONObject from resource bundle functionality.
+ * The test resource bundle is uncomplicated, but provides adequate test coverage.
+ */
+ @Test
+ public void jsonObjectByResourceBundle() {
+ JSONObject jsonObject = new
+ JSONObject("org.json.junit.data.StringsResourceBundle",
+ Locale.getDefault());
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 2 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 2);
+ assertTrue("expected 2 greetings items", ((Map,?>)(JsonPath.read(doc, "$.greetings"))).size() == 2);
+ assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(jsonObject.query("/greetings/hello")));
+ assertTrue("expected \"world\":\"World!\"", "World!".equals(jsonObject.query("/greetings/world")));
+ assertTrue("expected 2 farewells items", ((Map,?>)(JsonPath.read(doc, "$.farewells"))).size() == 2);
+ assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later")));
+ assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise the JSONObject.accumulate() method
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void jsonObjectAccumulate() {
+
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.accumulate("myArray", true);
+ jsonObject.accumulate("myArray", false);
+ jsonObject.accumulate("myArray", "hello world!");
+ jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!");
+ jsonObject.accumulate("myArray", 42);
+ jsonObject.accumulate("myArray", -23.45e7);
+ // include an unsupported object for coverage
+ try {
+ jsonObject.accumulate("myArray", Double.NaN);
+ fail("Expected exception");
+ } catch (JSONException ignored) {}
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 6 myArray items", ((List>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1")));
+ assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2")));
+ assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3")));
+ assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4")));
+ assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise the JSONObject append() functionality
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void jsonObjectAppend() {
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.append("myArray", true);
+ jsonObject.append("myArray", false);
+ jsonObject.append("myArray", "hello world!");
+ jsonObject.append("myArray", "h\be\tllo w\u1234orld!");
+ jsonObject.append("myArray", 42);
+ jsonObject.append("myArray", -23.45e7);
+ // include an unsupported object for coverage
+ try {
+ jsonObject.append("myArray", Double.NaN);
+ fail("Expected exception");
+ } catch (JSONException ignored) {}
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 6 myArray items", ((List>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1")));
+ assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2")));
+ assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3")));
+ assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4")));
+ assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise the JSONObject doubleToString() method
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void jsonObjectDoubleToString() {
+ String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" };
+ Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67,
+ Double.NaN, Double.NEGATIVE_INFINITY };
+ for (int i = 0; i < expectedStrs.length; ++i) {
+ String actualStr = JSONObject.doubleToString(doubles[i]);
+ assertTrue("value expected ["+expectedStrs[i]+
+ "] found ["+actualStr+ "]",
+ expectedStrs[i].equals(actualStr));
+ }
+ }
+
+ /**
+ * Exercise some JSONObject get[type] and opt[type] methods
+ */
+ @Test
+ public void jsonObjectValues() {
+ String str =
+ "{"+
+ "\"trueKey\":true,"+
+ "\"falseKey\":false,"+
+ "\"trueStrKey\":\"true\","+
+ "\"falseStrKey\":\"false\","+
+ "\"stringKey\":\"hello world!\","+
+ "\"intKey\":42,"+
+ "\"intStrKey\":\"43\","+
+ "\"longKey\":1234567890123456789,"+
+ "\"longStrKey\":\"987654321098765432\","+
+ "\"doubleKey\":-23.45e7,"+
+ "\"doubleStrKey\":\"00001.000\","+
+ "\"BigDecimalStrKey\":\"19007199254740993.35481234487103587486413587843213584\","+
+ "\"negZeroKey\":-0.0,"+
+ "\"negZeroStrKey\":\"-0.0\","+
+ "\"arrayKey\":[0,1,2],"+
+ "\"objectKey\":{\"myKey\":\"myVal\"}"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ assertTrue("trueKey should be true", jsonObject.getBoolean("trueKey"));
+ assertTrue("opt trueKey should be true", jsonObject.optBoolean("trueKey"));
+ assertTrue("opt trueKey should be true", jsonObject.optBooleanObject("trueKey"));
+ assertTrue("falseKey should be false", !jsonObject.getBoolean("falseKey"));
+ assertTrue("trueStrKey should be true", jsonObject.getBoolean("trueStrKey"));
+ assertTrue("trueStrKey should be true", jsonObject.optBoolean("trueStrKey"));
+ assertTrue("trueStrKey should be true", jsonObject.optBooleanObject("trueStrKey"));
+ assertTrue("falseStrKey should be false", !jsonObject.getBoolean("falseStrKey"));
+ assertTrue("stringKey should be string",
+ jsonObject.getString("stringKey").equals("hello world!"));
+ assertTrue("doubleKey should be double",
+ jsonObject.getDouble("doubleKey") == -23.45e7);
+ assertTrue("doubleStrKey should be double",
+ jsonObject.getDouble("doubleStrKey") == 1);
+ assertTrue("doubleKey can be float",
+ jsonObject.getFloat("doubleKey") == -23.45e7f);
+ assertTrue("doubleStrKey can be float",
+ jsonObject.getFloat("doubleStrKey") == 1f);
+ assertTrue("opt doubleKey should be double",
+ jsonObject.optDouble("doubleKey") == -23.45e7);
+ assertTrue("opt doubleKey with Default should be double",
+ jsonObject.optDouble("doubleStrKey", Double.NaN) == 1);
+ assertTrue("opt doubleKey should be Double",
+ Double.valueOf(-23.45e7).equals(jsonObject.optDoubleObject("doubleKey")));
+ assertTrue("opt doubleKey with Default should be Double",
+ Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", Double.NaN)));
+ assertTrue("opt negZeroKey should be a Double",
+ jsonObject.opt("negZeroKey") instanceof Double);
+ assertTrue("get negZeroKey should be a Double",
+ jsonObject.get("negZeroKey") instanceof Double);
+ assertTrue("optNumber negZeroKey should return Double",
+ jsonObject.optNumber("negZeroKey") instanceof Double);
+ assertTrue("optNumber negZeroStrKey should return Double",
+ jsonObject.optNumber("negZeroStrKey") instanceof Double);
+ assertTrue("opt negZeroKey should be double",
+ Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0);
+ assertTrue("opt negZeroStrKey with Default should be double",
+ Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0);
+ assertTrue("opt negZeroKey should be Double",
+ Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroKey")));
+ assertTrue("opt negZeroStrKey with Default should be Double",
+ Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroStrKey")));
+ assertTrue("optNumber negZeroKey should be -0.0",
+ Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0);
+ assertTrue("optNumber negZeroStrKey should be -0.0",
+ Double.compare(jsonObject.optNumber("negZeroStrKey").doubleValue(), -0.0d) == 0);
+ assertTrue("optFloat doubleKey should be float",
+ jsonObject.optFloat("doubleKey") == -23.45e7f);
+ assertTrue("optFloat doubleKey with Default should be float",
+ jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f);
+ assertTrue("optFloat doubleKey should be Float",
+ Float.valueOf(-23.45e7f).equals(jsonObject.optFloatObject("doubleKey")));
+ assertTrue("optFloat doubleKey with Default should be Float",
+ Float.valueOf(1f).equals(jsonObject.optFloatObject("doubleStrKey", Float.NaN)));
+ assertTrue("intKey should be int",
+ jsonObject.optInt("intKey") == 42);
+ assertTrue("opt intKey should be int",
+ jsonObject.optInt("intKey", 0) == 42);
+ assertTrue("intKey should be Integer",
+ Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey")));
+ assertTrue("opt intKey should be Integer",
+ Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey", 0)));
+ assertTrue("opt intKey with default should be int",
+ jsonObject.getInt("intKey") == 42);
+ assertTrue("intStrKey should be int",
+ jsonObject.getInt("intStrKey") == 43);
+ assertTrue("longKey should be long",
+ jsonObject.getLong("longKey") == 1234567890123456789L);
+ assertTrue("opt longKey should be long",
+ jsonObject.optLong("longKey") == 1234567890123456789L);
+ assertTrue("opt longKey with default should be long",
+ jsonObject.optLong("longKey", 0) == 1234567890123456789L);
+ assertTrue("opt longKey should be Long",
+ Long.valueOf(1234567890123456789L).equals(jsonObject.optLongObject("longKey")));
+ assertTrue("opt longKey with default should be Long",
+ Long.valueOf(1234567890123456789L).equals(jsonObject.optLongObject("longKey", 0L)));
+ assertTrue("longStrKey should be long",
+ jsonObject.getLong("longStrKey") == 987654321098765432L);
+ assertTrue("optNumber int should return Integer",
+ jsonObject.optNumber("intKey") instanceof Integer);
+ assertTrue("optNumber long should return Long",
+ jsonObject.optNumber("longKey") instanceof Long);
+ assertTrue("optNumber double should return BigDecimal",
+ jsonObject.optNumber("doubleKey") instanceof BigDecimal);
+ assertTrue("optNumber Str int should return Integer",
+ jsonObject.optNumber("intStrKey") instanceof Integer);
+ assertTrue("optNumber Str long should return Long",
+ jsonObject.optNumber("longStrKey") instanceof Long);
+ assertTrue("optNumber Str double should return BigDecimal",
+ jsonObject.optNumber("doubleStrKey") instanceof BigDecimal);
+ assertTrue("optNumber BigDecimalStrKey should return BigDecimal",
+ jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal);
+ assertTrue("xKey should not exist",
+ jsonObject.isNull("xKey"));
+ assertTrue("stringKey should exist",
+ jsonObject.has("stringKey"));
+ assertTrue("opt stringKey should string",
+ jsonObject.optString("stringKey").equals("hello world!"));
+ assertTrue("opt stringKey with default should string",
+ jsonObject.optString("stringKey", "not found").equals("hello world!"));
+ JSONArray jsonArray = jsonObject.getJSONArray("arrayKey");
+ assertTrue("arrayKey should be JSONArray",
+ jsonArray.getInt(0) == 0 &&
+ jsonArray.getInt(1) == 1 &&
+ jsonArray.getInt(2) == 2);
+ jsonArray = jsonObject.optJSONArray("arrayKey");
+ assertTrue("opt arrayKey should be JSONArray",
+ jsonArray.getInt(0) == 0 &&
+ jsonArray.getInt(1) == 1 &&
+ jsonArray.getInt(2) == 2);
+ JSONObject jsonObjectInner = jsonObject.getJSONObject("objectKey");
+ assertTrue("objectKey should be JSONObject",
+ jsonObjectInner.get("myKey").equals("myVal"));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Check whether JSONObject handles large or high precision numbers correctly
+ */
+ @Test
+ public void stringToValueNumbersTest() {
+ assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double);
+ assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double);
+ assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String);
+ assertTrue( "0.2 should be a BigDecimal!",
+ JSONObject.stringToValue( "0.2" ) instanceof BigDecimal );
+ assertTrue( "Doubles should be BigDecimal, even when incorrectly converting floats!",
+ JSONObject.stringToValue( Double.valueOf( "0.2f" ).toString() ) instanceof BigDecimal );
+ /**
+ * This test documents a need for BigDecimal conversion.
+ */
+ Object obj = JSONObject.stringToValue( "299792.457999999984" );
+ assertTrue( "does not evaluate to 299792.457999999984 BigDecimal!",
+ obj.equals(new BigDecimal("299792.457999999984")) );
+ assertTrue( "1 should be an Integer!",
+ JSONObject.stringToValue( "1" ) instanceof Integer );
+ assertTrue( "Integer.MAX_VALUE should still be an Integer!",
+ JSONObject.stringToValue( Integer.valueOf( Integer.MAX_VALUE ).toString() ) instanceof Integer );
+ assertTrue( "Large integers should be a Long!",
+ JSONObject.stringToValue( Long.valueOf(((long)Integer.MAX_VALUE) + 1 ) .toString() ) instanceof Long );
+ assertTrue( "Long.MAX_VALUE should still be an Integer!",
+ JSONObject.stringToValue( Long.valueOf( Long.MAX_VALUE ).toString() ) instanceof Long );
+
+ String str = new BigInteger( Long.valueOf( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString();
+ assertTrue( "Really large integers currently evaluate to BigInteger",
+ JSONObject.stringToValue(str).equals(new BigInteger("9223372036854775808")));
+ }
+
+ /**
+ * This test documents numeric values which could be numerically
+ * handled as BigDecimal or BigInteger. It helps determine what outputs
+ * will change if those types are supported.
+ */
+ @Test
+ public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() {
+ // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects
+ String str =
+ "{"+
+ "\"numberWithDecimals\":299792.457999999984,"+
+ "\"largeNumber\":12345678901234567890,"+
+ "\"preciseNumber\":0.2000000000000000111,"+
+ "\"largeExponent\":-23.45e2327"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ // Comes back as a double, but loses precision
+ assertTrue( "numberWithDecimals currently evaluates to double 299792.458",
+ jsonObject.get( "numberWithDecimals" ).equals( new BigDecimal( "299792.457999999984" ) ) );
+ Object obj = jsonObject.get( "largeNumber" );
+ assertTrue("largeNumber currently evaluates to BigInteger",
+ new BigInteger("12345678901234567890").equals(obj));
+ // comes back as a double but loses precision
+ assertEquals( "preciseNumber currently evaluates to double 0.2",
+ 0.2, jsonObject.getDouble( "preciseNumber" ), 0.0);
+ obj = jsonObject.get( "largeExponent" );
+ assertTrue("largeExponent should evaluate as a BigDecimal",
+ new BigDecimal("-23.45e2327").equals(obj));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * This test documents how JSON-Java handles invalid numeric input.
+ */
+ @Test
+ public void jsonInvalidNumberValues() {
+ // Number-notations supported by Java and invalid as JSON
+ String str =
+ "{" +
+ "\"hexNumber\":-0x123," +
+ "\"tooManyZeros\":00," +
+ "\"negativeInfinite\":-Infinity," +
+ "\"negativeNaN\":-NaN," +
+ "\"negativeFraction\":-.01," +
+ "\"tooManyZerosFraction\":00.001," +
+ "\"negativeHexFloat\":-0x1.fffp1," +
+ "\"hexFloat\":0x1.0P-1074," +
+ "\"floatIdentifier\":0.1f," +
+ "\"doubleIdentifier\":0.1d" +
+ "}";
+
+ // Test should fail if default strictMode is true, pass if false
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
+ if (jsonParserConfiguration.isStrictMode()) {
+ try {
+ JSONObject jsonObject = new JSONObject(str);
+ assertEquals("Expected to throw exception due to invalid string", true, false);
+ } catch (JSONException e) { }
+ } else {
+ JSONObject jsonObject = new JSONObject(str);
+ Object obj;
+ obj = jsonObject.get("hexNumber");
+ assertFalse("hexNumber must not be a number (should throw exception!?)",
+ obj instanceof Number);
+ assertTrue("hexNumber currently evaluates to string",
+ obj.equals("-0x123"));
+ assertTrue("tooManyZeros currently evaluates to string",
+ jsonObject.get("tooManyZeros").equals("00"));
+ obj = jsonObject.get("negativeInfinite");
+ assertTrue("negativeInfinite currently evaluates to string",
+ obj.equals("-Infinity"));
+ obj = jsonObject.get("negativeNaN");
+ assertTrue("negativeNaN currently evaluates to string",
+ obj.equals("-NaN"));
+ assertTrue("negativeFraction currently evaluates to double -0.01",
+ jsonObject.get("negativeFraction").equals(BigDecimal.valueOf(-0.01)));
+ assertTrue("tooManyZerosFraction currently evaluates to double 0.001",
+ jsonObject.optLong("tooManyZerosFraction") == 0);
+ assertTrue("negativeHexFloat currently evaluates to double -3.99951171875",
+ jsonObject.get("negativeHexFloat").equals(Double.valueOf(-3.99951171875)));
+ assertTrue("hexFloat currently evaluates to double 4.9E-324",
+ jsonObject.get("hexFloat").equals(Double.valueOf(4.9E-324)));
+ assertTrue("floatIdentifier currently evaluates to double 0.1",
+ jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1)));
+ assertTrue("doubleIdentifier currently evaluates to double 0.1",
+ jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+ }
+
+ /**
+ * Tests how JSONObject get[type] handles incorrect types
+ */
+ @Test
+ public void jsonObjectNonAndWrongValues() {
+ String str =
+ "{"+
+ "\"trueKey\":true,"+
+ "\"falseKey\":false,"+
+ "\"trueStrKey\":\"true\","+
+ "\"falseStrKey\":\"false\","+
+ "\"stringKey\":\"hello world!\","+
+ "\"intKey\":42,"+
+ "\"intStrKey\":\"43\","+
+ "\"longKey\":1234567890123456789,"+
+ "\"longStrKey\":\"987654321098765432\","+
+ "\"doubleKey\":-23.45e7,"+
+ "\"doubleStrKey\":\"00001.000\","+
+ "\"arrayKey\":[0,1,2],"+
+ "\"objectKey\":{\"myKey\":\"myVal\"}"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ try {
+ jsonObject.getBoolean("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.", e.getMessage());
+ }
+ try {
+ jsonObject.getBoolean("stringKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"stringKey\"] is not a Boolean (class java.lang.String : hello world!).",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getString("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getString("trueKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"trueKey\"] is not a string (class java.lang.Boolean : true).",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getDouble("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getDouble("stringKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"stringKey\"] is not a double (class java.lang.String : hello world!).",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getFloat("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getFloat("stringKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"stringKey\"] is not a float (class java.lang.String : hello world!).",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getInt("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getInt("stringKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"stringKey\"] is not a int (class java.lang.String : hello world!).",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getLong("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getLong("stringKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"stringKey\"] is not a long (class java.lang.String : hello world!).",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getJSONArray("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getJSONArray("stringKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"stringKey\"] is not a JSONArray (class java.lang.String : hello world!).",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getJSONObject("nonKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"nonKey\"] not found.",
+ e.getMessage());
+ }
+ try {
+ jsonObject.getJSONObject("stringKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"stringKey\"] is not a JSONObject (class java.lang.String : hello world!).",
+ e.getMessage());
+ }
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * This test documents an unexpected numeric behavior.
+ * A double that ends with .0 is parsed, serialized, then
+ * parsed again. On the second parse, it has become an int.
+ */
+ @Test
+ public void unexpectedDoubleToIntConversion() {
+ String key30 = "key30";
+ String key31 = "key31";
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put(key30, Double.valueOf(3.0));
+ jsonObject.put(key31, Double.valueOf(3.1));
+
+ assertTrue("3.0 should remain a double",
+ jsonObject.getDouble(key30) == 3);
+ assertTrue("3.1 should remain a double",
+ jsonObject.getDouble(key31) == 3.1);
+
+ // turns 3.0 into 3.
+ String serializedString = jsonObject.toString();
+ JSONObject deserialized = new JSONObject(serializedString);
+ assertTrue("3.0 is now an int", deserialized.get(key30) instanceof Integer);
+ assertTrue("3.0 can still be interpreted as a double",
+ deserialized.getDouble(key30) == 3.0);
+ assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1);
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Document behaviors of big numbers. Includes both JSONObject
+ * and JSONArray tests
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void bigNumberOperations() {
+ /**
+ * JSONObject tries to parse BigInteger as a bean, but it only has
+ * one getter, getLowestBitSet(). The value is lost and an unhelpful
+ * value is stored. This should be fixed.
+ */
+ BigInteger bigInteger = new BigInteger("123456789012345678901234567890");
+ JSONObject jsonObject0 = new JSONObject(bigInteger);
+ Object obj = jsonObject0.get("lowestSetBit");
+ assertTrue("JSONObject only has 1 value", jsonObject0.length() == 1);
+ assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet",
+ obj instanceof Integer);
+ assertTrue("this bigInteger lowestBitSet happens to be 1",
+ obj.equals(1));
+
+ /**
+ * JSONObject tries to parse BigDecimal as a bean, but it has
+ * no getters, The value is lost and no value is stored.
+ * This should be fixed.
+ */
+ BigDecimal bigDecimal = new BigDecimal(
+ "123456789012345678901234567890.12345678901234567890123456789");
+ JSONObject jsonObject1 = new JSONObject(bigDecimal);
+ assertTrue("large bigDecimal is not stored", jsonObject1.isEmpty());
+
+ /**
+ * JSONObject put(String, Object) method stores and serializes
+ * bigInt and bigDec correctly. Nothing needs to change.
+ */
+ JSONObject jsonObject2 = new JSONObject();
+ jsonObject2.put("bigInt", bigInteger);
+ assertTrue("jsonObject.put() handles bigInt correctly",
+ jsonObject2.get("bigInt").equals(bigInteger));
+ assertTrue("jsonObject.getBigInteger() handles bigInt correctly",
+ jsonObject2.getBigInteger("bigInt").equals(bigInteger));
+ assertTrue("jsonObject.optBigInteger() handles bigInt correctly",
+ jsonObject2.optBigInteger("bigInt", BigInteger.ONE).equals(bigInteger));
+ assertTrue("jsonObject serializes bigInt correctly",
+ jsonObject2.toString().equals("{\"bigInt\":123456789012345678901234567890}"));
+ assertTrue("BigInteger as BigDecimal",
+ jsonObject2.getBigDecimal("bigInt").equals(new BigDecimal(bigInteger)));
+
+
+ JSONObject jsonObject3 = new JSONObject();
+ jsonObject3.put("bigDec", bigDecimal);
+ assertTrue("jsonObject.put() handles bigDec correctly",
+ jsonObject3.get("bigDec").equals(bigDecimal));
+ assertTrue("jsonObject.getBigDecimal() handles bigDec correctly",
+ jsonObject3.getBigDecimal("bigDec").equals(bigDecimal));
+ assertTrue("jsonObject.optBigDecimal() handles bigDec correctly",
+ jsonObject3.optBigDecimal("bigDec", BigDecimal.ONE).equals(bigDecimal));
+ assertTrue("jsonObject serializes bigDec correctly",
+ jsonObject3.toString().equals(
+ "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}"));
+
+ assertTrue("BigDecimal as BigInteger",
+ jsonObject3.getBigInteger("bigDec").equals(bigDecimal.toBigInteger()));
+ /**
+ * exercise some exceptions
+ */
+ try {
+ // bigInt key does not exist
+ jsonObject3.getBigDecimal("bigInt");
+ fail("expected an exeption");
+ } catch (JSONException ignored) {}
+ obj = jsonObject3.optBigDecimal("bigInt", BigDecimal.ONE);
+ assertTrue("expected BigDecimal", obj.equals(BigDecimal.ONE));
+ jsonObject3.put("stringKey", "abc");
+ try {
+ jsonObject3.getBigDecimal("stringKey");
+ fail("expected an exeption");
+ } catch (JSONException ignored) {}
+ obj = jsonObject3.optBigInteger("bigDec", BigInteger.ONE);
+ assertTrue("expected BigInteger", obj instanceof BigInteger);
+ assertEquals(bigDecimal.toBigInteger(), obj);
+
+ /**
+ * JSONObject.numberToString() works correctly, nothing to change.
+ */
+ String str = JSONObject.numberToString(bigInteger);
+ assertTrue("numberToString() handles bigInteger correctly",
+ str.equals("123456789012345678901234567890"));
+ str = JSONObject.numberToString(bigDecimal);
+ assertTrue("numberToString() handles bigDecimal correctly",
+ str.equals("123456789012345678901234567890.12345678901234567890123456789"));
+
+ /**
+ * JSONObject.stringToValue() turns bigInt into an accurate string,
+ * and rounds bigDec. This incorrect, but users may have come to
+ * expect this behavior. Change would be marginally better, but
+ * might inconvenience users.
+ */
+ obj = JSONObject.stringToValue(bigInteger.toString());
+ assertTrue("stringToValue() turns bigInteger string into Number",
+ obj instanceof Number);
+ obj = JSONObject.stringToValue(bigDecimal.toString());
+ assertTrue("stringToValue() changes bigDecimal Number",
+ obj instanceof Number);
+
+ /**
+ * wrap() vs put() big number behavior is now the same.
+ */
+ // bigInt map ctor
+ Map map = new HashMap();
+ map.put("bigInt", bigInteger);
+ JSONObject jsonObject4 = new JSONObject(map);
+ String actualFromMapStr = jsonObject4.toString();
+ assertTrue("bigInt in map (or array or bean) is a string",
+ actualFromMapStr.equals(
+ "{\"bigInt\":123456789012345678901234567890}"));
+ // bigInt put
+ JSONObject jsonObject5 = new JSONObject();
+ jsonObject5.put("bigInt", bigInteger);
+ String actualFromPutStr = jsonObject5.toString();
+ assertTrue("bigInt from put is a number",
+ actualFromPutStr.equals(
+ "{\"bigInt\":123456789012345678901234567890}"));
+ // bigDec map ctor
+ map = new HashMap();
+ map.put("bigDec", bigDecimal);
+ JSONObject jsonObject6 = new JSONObject(map);
+ actualFromMapStr = jsonObject6.toString();
+ assertTrue("bigDec in map (or array or bean) is a bigDec",
+ actualFromMapStr.equals(
+ "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}"));
+ // bigDec put
+ JSONObject jsonObject7 = new JSONObject();
+ jsonObject7.put("bigDec", bigDecimal);
+ actualFromPutStr = jsonObject7.toString();
+ assertTrue("bigDec from put is a number",
+ actualFromPutStr.equals(
+ "{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}"));
+ // bigInt,bigDec put
+ JSONArray jsonArray0 = new JSONArray();
+ jsonArray0.put(bigInteger);
+ jsonArray0.put(bigDecimal);
+ actualFromPutStr = jsonArray0.toString();
+ assertTrue("bigInt, bigDec from put is a number",
+ actualFromPutStr.equals(
+ "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]"));
+ assertTrue("getBigInt is bigInt", jsonArray0.getBigInteger(0).equals(bigInteger));
+ assertTrue("getBigDec is bigDec", jsonArray0.getBigDecimal(1).equals(bigDecimal));
+ assertTrue("optBigInt is bigInt", jsonArray0.optBigInteger(0, BigInteger.ONE).equals(bigInteger));
+ assertTrue("optBigDec is bigDec", jsonArray0.optBigDecimal(1, BigDecimal.ONE).equals(bigDecimal));
+ jsonArray0.put(Boolean.TRUE);
+ try {
+ jsonArray0.getBigInteger(2);
+ fail("should not be able to get big int");
+ } catch (Exception ignored) {}
+ try {
+ jsonArray0.getBigDecimal(2);
+ fail("should not be able to get big dec");
+ } catch (Exception ignored) {}
+ assertTrue("optBigInt is default", jsonArray0.optBigInteger(2, BigInteger.ONE).equals(BigInteger.ONE));
+ assertTrue("optBigDec is default", jsonArray0.optBigDecimal(2, BigDecimal.ONE).equals(BigDecimal.ONE));
+
+ // bigInt,bigDec list ctor
+ List list = new ArrayList();
+ list.add(bigInteger);
+ list.add(bigDecimal);
+ JSONArray jsonArray1 = new JSONArray(list);
+ String actualFromListStr = jsonArray1.toString();
+ assertTrue("bigInt, bigDec in list is a bigInt, bigDec",
+ actualFromListStr.equals(
+ "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]"));
+ // bigInt bean ctor
+ MyBigNumberBean myBigNumberBean = mock(MyBigNumberBean.class);
+ when(myBigNumberBean.getBigInteger()).thenReturn(new BigInteger("123456789012345678901234567890"));
+ JSONObject jsonObject8 = new JSONObject(myBigNumberBean);
+ String actualFromBeanStr = jsonObject8.toString();
+ // can't do a full string compare because mockery adds an extra key/value
+ assertTrue("bigInt from bean ctor is a bigInt",
+ actualFromBeanStr.contains("123456789012345678901234567890"));
+ // bigDec bean ctor
+ myBigNumberBean = mock(MyBigNumberBean.class);
+ when(myBigNumberBean.getBigDecimal()).thenReturn(new BigDecimal("123456789012345678901234567890.12345678901234567890123456789"));
+ jsonObject8 = new JSONObject(myBigNumberBean);
+ actualFromBeanStr = jsonObject8.toString();
+ // can't do a full string compare because mockery adds an extra key/value
+ assertTrue("bigDec from bean ctor is a bigDec",
+ actualFromBeanStr.contains("123456789012345678901234567890.12345678901234567890123456789"));
+ // bigInt,bigDec wrap()
+ obj = JSONObject.wrap(bigInteger);
+ assertTrue("wrap() returns big num",obj.equals(bigInteger));
+ obj = JSONObject.wrap(bigDecimal);
+ assertTrue("wrap() returns string",obj.equals(bigDecimal));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject0, jsonObject1, jsonObject2, jsonObject3, jsonObject4,
+ jsonObject5, jsonObject6, jsonObject7, jsonObject8
+ )));
+ Util.checkJSONArrayMaps(jsonArray0, jsonObject0.getMapType());
+ Util.checkJSONArrayMaps(jsonArray1, jsonObject0.getMapType());
+ }
+
+ /**
+ * The purpose for the static method getNames() methods are not clear.
+ * This method is not called from within JSON-Java. Most likely
+ * uses are to prep names arrays for:
+ * JSONObject(JSONObject jo, String[] names)
+ * JSONObject(Object object, String names[]),
+ */
+ @Test
+ public void jsonObjectNames() {
+
+ // getNames() from null JSONObject
+ assertTrue("null names from null Object",
+ null == JSONObject.getNames((Object)null));
+
+ // getNames() from object with no fields
+ assertTrue("null names from Object with no fields",
+ null == JSONObject.getNames(new MyJsonString()));
+
+ // getNames from new JSONOjbect
+ JSONObject jsonObject0 = new JSONObject();
+ String [] names = JSONObject.getNames(jsonObject0);
+ assertTrue("names should be null", names == null);
+
+
+ // getNames() from empty JSONObject
+ String emptyStr = "{}";
+ JSONObject jsonObject1 = new JSONObject(emptyStr);
+ assertTrue("empty JSONObject should have null names",
+ null == JSONObject.getNames(jsonObject1));
+
+ // getNames() from JSONObject
+ String str =
+ "{"+
+ "\"trueKey\":true,"+
+ "\"falseKey\":false,"+
+ "\"stringKey\":\"hello world!\""+
+ "}";
+ JSONObject jsonObject2 = new JSONObject(str);
+ names = JSONObject.getNames(jsonObject2);
+ JSONArray jsonArray0 = new JSONArray(names);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider()
+ .parse(jsonArray0.toString());
+ List> docList = JsonPath.read(doc, "$");
+ assertTrue("expected 3 items", docList.size() == 3);
+ assertTrue(
+ "expected to find trueKey",
+ ((List>) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1);
+ assertTrue(
+ "expected to find falseKey",
+ ((List>) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1);
+ assertTrue(
+ "expected to find stringKey",
+ ((List>) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1);
+
+ /**
+ * getNames() from an enum with properties has an interesting result.
+ * It returns the enum values, not the selected enum properties
+ */
+ MyEnumField myEnumField = MyEnumField.VAL1;
+ names = JSONObject.getNames(myEnumField);
+
+ // validate JSON
+ JSONArray jsonArray1 = new JSONArray(names);
+ doc = Configuration.defaultConfiguration().jsonProvider()
+ .parse(jsonArray1.toString());
+ docList = JsonPath.read(doc, "$");
+ assertTrue("expected 3 items", docList.size() == 3);
+ assertTrue(
+ "expected to find VAL1",
+ ((List>) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1);
+ assertTrue(
+ "expected to find VAL2",
+ ((List>) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1);
+ assertTrue(
+ "expected to find VAL3",
+ ((List>) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1);
+
+ /**
+ * A bean is also an object. But in order to test the static
+ * method getNames(), this particular bean needs some public
+ * data members.
+ */
+ MyPublicClass myPublicClass = new MyPublicClass();
+ names = JSONObject.getNames(myPublicClass);
+
+ // validate JSON
+ JSONArray jsonArray2 = new JSONArray(names);
+ doc = Configuration.defaultConfiguration().jsonProvider()
+ .parse(jsonArray2.toString());
+ docList = JsonPath.read(doc, "$");
+ assertTrue("expected 2 items", docList.size() == 2);
+ assertTrue(
+ "expected to find publicString",
+ ((List>) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1);
+ assertTrue(
+ "expected to find publicInt",
+ ((List>) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1);
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject0, jsonObject1, jsonObject2
+ )));
+ Util.checkJSONArrayMaps(jsonArray0, jsonObject0.getMapType());
+ Util.checkJSONArrayMaps(jsonArray1, jsonObject0.getMapType());
+ Util.checkJSONArrayMaps(jsonArray2, jsonObject0.getMapType());
+ }
+
+ /**
+ * Populate a JSONArray from an empty JSONObject names() method.
+ * It should be empty.
+ */
+ @Test
+ public void emptyJsonObjectNamesToJsonAray() {
+ JSONObject jsonObject = new JSONObject();
+ JSONArray jsonArray = jsonObject.names();
+ assertTrue("jsonArray should be null", jsonArray == null);
+ Util.checkJSONObjectMaps(jsonObject);
+ Util.checkJSONArrayMaps(jsonArray, jsonObject.getMapType());
+ }
+
+ /**
+ * Populate a JSONArray from a JSONObject names() method.
+ * Confirm that it contains the expected names.
+ */
+ @Test
+ public void jsonObjectNamesToJsonAray() {
+ String str =
+ "{"+
+ "\"trueKey\":true,"+
+ "\"falseKey\":false,"+
+ "\"stringKey\":\"hello world!\""+
+ "}";
+
+ JSONObject jsonObject = new JSONObject(str);
+ JSONArray jsonArray = jsonObject.names();
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 3 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected to find trueKey", ((List>) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1);
+ assertTrue("expected to find falseKey", ((List>) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1);
+ assertTrue("expected to find stringKey", ((List>) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1);
+ Util.checkJSONObjectMaps(jsonObject);
+ Util.checkJSONArrayMaps(jsonArray, jsonObject.getMapType());
+ }
+
+ /**
+ * Exercise the JSONObject increment() method.
+ */
+ @SuppressWarnings("cast")
+ @Test
+ public void jsonObjectIncrement() {
+ String str =
+ "{"+
+ "\"keyLong\":9999999991,"+
+ "\"keyDouble\":1.1"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ jsonObject.increment("keyInt");
+ jsonObject.increment("keyInt");
+ jsonObject.increment("keyLong");
+ jsonObject.increment("keyDouble");
+ jsonObject.increment("keyInt");
+ jsonObject.increment("keyLong");
+ jsonObject.increment("keyDouble");
+ /**
+ * JSONObject constructor won't handle these types correctly, but
+ * adding them via put works.
+ */
+ jsonObject.put("keyFloat", 1.1f);
+ jsonObject.put("keyBigInt", new BigInteger("123456789123456789123456789123456780"));
+ jsonObject.put("keyBigDec", new BigDecimal("123456789123456789123456789123456780.1"));
+ jsonObject.increment("keyFloat");
+ jsonObject.increment("keyFloat");
+ jsonObject.increment("keyBigInt");
+ jsonObject.increment("keyBigDec");
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 6 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt")));
+ assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong")));
+ assertTrue("expected 3.1", BigDecimal.valueOf(3.1).equals(jsonObject.query("/keyDouble")));
+ assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt")));
+ assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec")));
+
+ /**
+ * Should work the same way on any platform! @see https://docs.oracle
+ * .com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 This is the
+ * effect of a float to double conversion and is inherent to the
+ * shortcomings of the IEEE 754 format, when converting 32-bit into
+ * double-precision 64-bit. Java type-casts float to double. A 32 bit
+ * float is type-casted to 64 bit double by simply appending zero-bits
+ * to the mantissa (and extended the signed exponent by 3 bits.) and
+ * there is no way to obtain more information than it is stored in the
+ * 32-bits float.
+ *
+ * Like 1/3 cannot be represented as base10 number because it is
+ * periodically, 1/5 (for example) cannot be represented as base2 number
+ * since it is periodically in base2 (take a look at
+ * http://www.h-schmidt.net/FloatConverter/) The same happens to 3.1,
+ * that decimal number (base10 representation) is periodic in base2
+ * representation, therefore appending zero-bits is inaccurate. Only
+ * repeating the periodically occurring bits (0110) would be a proper
+ * conversion. However one cannot detect from a 32 bit IEE754
+ * representation which bits would "repeat infinitely", since the
+ * missing bits would not fit into the 32 bit float, i.e. the
+ * information needed simply is not there!
+ */
+ assertEquals(Float.valueOf(3.1f), jsonObject.query("/keyFloat"));
+
+ /**
+ * float f = 3.1f; double df = (double) f; double d = 3.1d;
+ * System.out.println
+ * (Integer.toBinaryString(Float.floatToRawIntBits(f)));
+ * System.out.println
+ * (Long.toBinaryString(Double.doubleToRawLongBits(df)));
+ * System.out.println
+ * (Long.toBinaryString(Double.doubleToRawLongBits(d)));
+ *
+ * - Float:
+ * seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
+ * 1000000010001100110011001100110
+ * - Double
+ * seeeeeeeeeeemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
+ * 10000000 10001100110011001100110
+ * 100000000001000110011001100110011000000000000000000000000000000
+ * 100000000001000110011001100110011001100110011001100110011001101
+ */
+
+ /**
+ * Examples of well documented but probably unexpected behavior in
+ * java / with 32-bit float to 64-bit float conversion.
+ */
+ assertFalse("Document unexpected behaviour with explicit type-casting float as double!", (double)0.2f == 0.2d );
+ assertFalse("Document unexpected behaviour with implicit type-cast!", 0.2f == 0.2d );
+ Double d1 = Double.valueOf( 1.1f );
+ Double d2 = Double.valueOf( "1.1f" );
+ assertFalse( "Document implicit type cast from float to double before calling Double(double d) constructor", d1.equals( d2 ) );
+
+ assertTrue( "Correctly converting float to double via base10 (string) representation!", Double.valueOf( 3.1d ).equals( Double.valueOf( Float.valueOf( 3.1f ).toString() ) ) );
+
+ // Pinpointing the not so obvious "buggy" conversion from float to double in JSONObject
+ JSONObject jo = new JSONObject();
+ jo.put( "bug", 3.1f ); // will call put( String key, double value ) with implicit and "buggy" type-cast from float to double
+ assertFalse( "The java-compiler did add some zero bits for you to the mantissa (unexpected, but well documented)", jo.get( "bug" ).equals( Double.valueOf( 3.1d ) ) );
+
+ JSONObject inc = new JSONObject();
+ inc.put( "bug", Float.valueOf( 3.1f ) ); // This will put in instance of Float into JSONObject, i.e. call put( String key, Object value )
+ assertTrue( "Everything is ok here!", inc.get( "bug" ) instanceof Float );
+ inc.increment( "bug" ); // after adding 1, increment will call put( String key, double value ) with implicit and "buggy" type-cast from float to double!
+ // this.put(key, (Float) value + 1);
+ // 1. The (Object)value will be typecasted to (Float)value since it is an instanceof Float actually nothing is done.
+ // 2. Float instance will be autoboxed into float because the + operator will work on primitives not Objects!
+ // 3. A float+float operation will be performed and results into a float primitive.
+ // 4. There is no method that matches the signature put( String key, float value), java-compiler will choose the method
+ // put( String key, double value) and does an implicit type-cast(!) by appending zero-bits to the mantissa
+ assertTrue( "JSONObject increment converts Float to Double", jo.get( "bug" ) instanceof Float );
+ // correct implementation (with change of behavior) would be:
+ // this.put(key, new Float((Float) value + 1));
+ // Probably it would be better to deprecate the method and remove some day, while convenient processing the "payload" is not
+ // really in the scope of a JSON-library (IMHO.)
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject, inc
+ )));
+ }
+
+ /**
+ * Exercise JSONObject numberToString() method
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void jsonObjectNumberToString() {
+ String str;
+ Double dVal;
+ Integer iVal = 1;
+ str = JSONObject.numberToString(iVal);
+ assertTrue("expected "+iVal+" actual "+str, iVal.toString().equals(str));
+ dVal = 12.34;
+ str = JSONObject.numberToString(dVal);
+ assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str));
+ dVal = 12.34e27;
+ str = JSONObject.numberToString(dVal);
+ assertTrue("expected "+dVal+" actual "+str, dVal.toString().equals(str));
+ // trailing .0 is truncated, so it doesn't quite match toString()
+ dVal = 5000000.0000000;
+ str = JSONObject.numberToString(dVal);
+ assertTrue("expected 5000000 actual "+str, str.equals("5000000"));
+ }
+
+ /**
+ * Exercise JSONObject put() and similar() methods
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void jsonObjectPut() {
+ String expectedStr =
+ "{"+
+ "\"trueKey\":true,"+
+ "\"falseKey\":false,"+
+ "\"arrayKey\":[0,1,2],"+
+ "\"objectKey\":{"+
+ "\"myKey1\":\"myVal1\","+
+ "\"myKey2\":\"myVal2\","+
+ "\"myKey3\":\"myVal3\","+
+ "\"myKey4\":\"myVal4\""+
+ "}"+
+ "}";
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("trueKey", true);
+ jsonObject.put("falseKey", false);
+ Integer [] intArray = { 0, 1, 2 };
+ jsonObject.put("arrayKey", Arrays.asList(intArray));
+ Map myMap = new HashMap();
+ myMap.put("myKey1", "myVal1");
+ myMap.put("myKey2", "myVal2");
+ myMap.put("myKey3", "myVal3");
+ myMap.put("myKey4", "myVal4");
+ jsonObject.put("objectKey", myMap);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 4 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 4);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
+ assertTrue("expected 3 arrayKey items", ((List>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
+ assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0")));
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2")));
+ assertTrue("expected 4 objectKey items", ((Map,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
+ assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1")));
+ assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2")));
+ assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3")));
+ assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4")));
+
+ jsonObject.remove("trueKey");
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ assertTrue("unequal jsonObjects should not be similar",
+ !jsonObject.similar(expectedJsonObject));
+ assertTrue("jsonObject should not be similar to jsonArray",
+ !jsonObject.similar(new JSONArray()));
+
+ String aCompareValueStr = "{\"a\":\"aval\",\"b\":true}";
+ String bCompareValueStr = "{\"a\":\"notAval\",\"b\":true}";
+ JSONObject aCompareValueJsonObject = new JSONObject(aCompareValueStr);
+ JSONObject bCompareValueJsonObject = new JSONObject(bCompareValueStr);
+ assertTrue("different values should not be similar",
+ !aCompareValueJsonObject.similar(bCompareValueJsonObject));
+
+ String aCompareObjectStr = "{\"a\":\"aval\",\"b\":{}}";
+ String bCompareObjectStr = "{\"a\":\"aval\",\"b\":true}";
+ JSONObject aCompareObjectJsonObject = new JSONObject(aCompareObjectStr);
+ JSONObject bCompareObjectJsonObject = new JSONObject(bCompareObjectStr);
+ assertTrue("different nested JSONObjects should not be similar",
+ !aCompareObjectJsonObject.similar(bCompareObjectJsonObject));
+
+ String aCompareArrayStr = "{\"a\":\"aval\",\"b\":[]}";
+ String bCompareArrayStr = "{\"a\":\"aval\",\"b\":true}";
+ JSONObject aCompareArrayJsonObject = new JSONObject(aCompareArrayStr);
+ JSONObject bCompareArrayJsonObject = new JSONObject(bCompareArrayStr);
+ assertTrue("different nested JSONArrays should not be similar",
+ !aCompareArrayJsonObject.similar(bCompareArrayJsonObject));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject, expectedJsonObject, aCompareValueJsonObject,
+ aCompareArrayJsonObject, aCompareObjectJsonObject, aCompareArrayJsonObject,
+ bCompareValueJsonObject, bCompareArrayJsonObject, bCompareObjectJsonObject,
+ bCompareArrayJsonObject
+ )));
+ }
+
+ /**
+ * Exercise JSONObject toString() method
+ */
+ @Test
+ public void jsonObjectToString() {
+ String str =
+ "{"+
+ "\"trueKey\":true,"+
+ "\"falseKey\":false,"+
+ "\"arrayKey\":[0,1,2],"+
+ "\"objectKey\":{"+
+ "\"myKey1\":\"myVal1\","+
+ "\"myKey2\":\"myVal2\","+
+ "\"myKey3\":\"myVal3\","+
+ "\"myKey4\":\"myVal4\""+
+ "}"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 4 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 4);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
+ assertTrue("expected 3 arrayKey items", ((List>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
+ assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0")));
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2")));
+ assertTrue("expected 4 objectKey items", ((Map,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
+ assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1")));
+ assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2")));
+ assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3")));
+ assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise JSONObject toString() method with various indent levels.
+ */
+ @Test
+ public void jsonObjectToStringIndent() {
+ String jsonObject0Str =
+ "{"+
+ "\"key1\":" +
+ "[1,2," +
+ "{\"key3\":true}" +
+ "],"+
+ "\"key2\":" +
+ "{\"key1\":\"val1\",\"key2\":" +
+ "{\"key2\":\"val2\"}" +
+ "},"+
+ "\"key3\":" +
+ "[" +
+ "[1,2.1]" +
+ "," +
+ "[null]" +
+ "]"+
+ "}";
+
+ String jsonObject1Str =
+ "{\n" +
+ " \"key1\": [\n" +
+ " 1,\n" +
+ " 2,\n" +
+ " {\"key3\": true}\n" +
+ " ],\n" +
+ " \"key2\": {\n" +
+ " \"key1\": \"val1\",\n" +
+ " \"key2\": {\"key2\": \"val2\"}\n" +
+ " },\n" +
+ " \"key3\": [\n" +
+ " [\n" +
+ " 1,\n" +
+ " 2.1\n" +
+ " ],\n" +
+ " [null]\n" +
+ " ]\n" +
+ "}";
+ String jsonObject4Str =
+ "{\n" +
+ " \"key1\": [\n" +
+ " 1,\n" +
+ " 2,\n" +
+ " {\"key3\": true}\n" +
+ " ],\n" +
+ " \"key2\": {\n" +
+ " \"key1\": \"val1\",\n" +
+ " \"key2\": {\"key2\": \"val2\"}\n" +
+ " },\n" +
+ " \"key3\": [\n" +
+ " [\n" +
+ " 1,\n" +
+ " 2.1\n" +
+ " ],\n" +
+ " [null]\n" +
+ " ]\n" +
+ "}";
+ JSONObject jsonObject = new JSONObject(jsonObject0Str);
+ // contents are tested in other methods, in this case just validate the spacing by
+ // checking length
+ assertEquals("toString() length",jsonObject0Str.length(), jsonObject.toString().length());
+ assertEquals("toString(0) length",jsonObject0Str.length(), jsonObject.toString(0).length());
+ assertEquals("toString(1) length",jsonObject1Str.length(), jsonObject.toString(1).length());
+ assertEquals("toString(4) length",jsonObject4Str.length(), jsonObject.toString(4).length());
+
+ JSONObject jo = new JSONObject().put("TABLE", new JSONObject().put("yhoo", new JSONObject()));
+ assertEquals("toString(2)","{\"TABLE\": {\"yhoo\": {}}}", jo.toString(2));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject, jo
+ )));
+ }
+
+ /**
+ * Explores how JSONObject handles maps. Insert a string/string map
+ * as a value in a JSONObject. It will remain a map. Convert the
+ * JSONObject to string, then create a new JSONObject from the string.
+ * In the new JSONObject, the value will be stored as a nested JSONObject.
+ * Confirm that map and nested JSONObject have the same contents.
+ */
+ @Test
+ public void jsonObjectToStringSuppressWarningOnCastToMap() {
+ JSONObject jsonObject = new JSONObject();
+ Map map = new HashMap<>();
+ map.put("abc", "def");
+ jsonObject.put("key", map);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 1 key item", ((Map,?>)(JsonPath.read(doc, "$.key"))).size() == 1);
+ assertTrue("expected def", "def".equals(jsonObject.query("/key/abc")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Explores how JSONObject handles collections. Insert a string collection
+ * as a value in a JSONObject. It will remain a collection. Convert the
+ * JSONObject to string, then create a new JSONObject from the string.
+ * In the new JSONObject, the value will be stored as a nested JSONArray.
+ * Confirm that collection and nested JSONArray have the same contents.
+ */
+ @Test
+ public void jsonObjectToStringSuppressWarningOnCastToCollection() {
+ JSONObject jsonObject = new JSONObject();
+ Collection collection = new ArrayList();
+ collection.add("abc");
+ // ArrayList will be added as an object
+ jsonObject.put("key", collection);
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected 1 key item", ((List>)(JsonPath.read(doc, "$.key"))).size() == 1);
+ assertTrue("expected abc", "abc".equals(jsonObject.query("/key/0")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercises the JSONObject.valueToString() method for various types
+ */
+ @Test
+ public void valueToString() {
+
+ assertTrue("null valueToString() incorrect",
+ "null".equals(JSONObject.valueToString(null)));
+ MyJsonString jsonString = new MyJsonString();
+ assertTrue("jsonstring valueToString() incorrect",
+ "my string".equals(JSONObject.valueToString(jsonString)));
+ assertTrue("boolean valueToString() incorrect",
+ "true".equals(JSONObject.valueToString(Boolean.TRUE)));
+ assertTrue("non-numeric double",
+ "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY)));
+ String jsonObjectStr =
+ "{"+
+ "\"key1\":\"val1\","+
+ "\"key2\":\"val2\","+
+ "\"key3\":\"val3\""+
+ "}";
+ JSONObject jsonObject = new JSONObject(jsonObjectStr);
+ assertTrue("jsonObject valueToString() incorrect",
+ new JSONObject(JSONObject.valueToString(jsonObject))
+ .similar(new JSONObject(jsonObject.toString()))
+ );
+ String jsonArrayStr =
+ "[1,2,3]";
+ JSONArray jsonArray = new JSONArray(jsonArrayStr);
+ assertTrue("jsonArray valueToString() incorrect",
+ JSONObject.valueToString(jsonArray).equals(jsonArray.toString()));
+ Map map = new HashMap();
+ map.put("key1", "val1");
+ map.put("key2", "val2");
+ map.put("key3", "val3");
+ assertTrue("map valueToString() incorrect",
+ new JSONObject(jsonObject.toString())
+ .similar(new JSONObject(JSONObject.valueToString(map))));
+ Collection collection = new ArrayList();
+ collection.add(Integer.valueOf(1));
+ collection.add(Integer.valueOf(2));
+ collection.add(Integer.valueOf(3));
+ assertTrue("collection valueToString() expected: "+
+ jsonArray.toString()+ " actual: "+
+ JSONObject.valueToString(collection),
+ jsonArray.toString().equals(JSONObject.valueToString(collection)));
+ Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) };
+ assertTrue("array valueToString() incorrect",
+ jsonArray.toString().equals(JSONObject.valueToString(array)));
+ Util.checkJSONObjectMaps(jsonObject);
+ Util.checkJSONArrayMaps(jsonArray, jsonObject.getMapType());
+ }
+
+ /**
+ * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed.
+ * The following code was throwing a ClassCastException in the
+ * JSONObject(Map) constructor
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void valueToStringConfirmException() {
+ Map myMap = new HashMap();
+ myMap.put(1, "myValue");
+ // this is the test, it should not throw an exception
+ String str = JSONObject.valueToString(myMap);
+ // confirm result, just in case
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str);
+ assertTrue("expected 1 top level item", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 1);
+ assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1")));
+ }
+
+ /**
+ * Exercise the JSONObject wrap() method. Sometimes wrap() will change
+ * the object being wrapped, other times not. The purpose of wrap() is
+ * to ensure the value is packaged in a way that is compatible with how
+ * a JSONObject value or JSONArray value is supposed to be stored.
+ */
+ @Test
+ public void wrapObject() {
+ // wrap(null) returns NULL
+ assertTrue("null wrap() incorrect",
+ JSONObject.NULL == JSONObject.wrap(null));
+
+ // wrap(Integer) returns Integer
+ Integer in = Integer.valueOf(1);
+ assertTrue("Integer wrap() incorrect",
+ in == JSONObject.wrap(in));
+
+ /**
+ * This test is to document the preferred behavior if BigDecimal is
+ * supported. Previously bd returned as a string, since it
+ * is recognized as being a Java package class. Now with explicit
+ * support for big numbers, it remains a BigDecimal
+ */
+ Object bdWrap = JSONObject.wrap(BigDecimal.ONE);
+ assertTrue("BigDecimal.ONE evaluates to ONE",
+ bdWrap.equals(BigDecimal.ONE));
+
+ // wrap JSONObject returns JSONObject
+ String jsonObjectStr =
+ "{"+
+ "\"key1\":\"val1\","+
+ "\"key2\":\"val2\","+
+ "\"key3\":\"val3\""+
+ "}";
+ JSONObject jsonObject = new JSONObject(jsonObjectStr);
+ assertTrue("JSONObject wrap() incorrect",
+ jsonObject == JSONObject.wrap(jsonObject));
+
+ // wrap collection returns JSONArray
+ Collection collection = new ArrayList();
+ collection.add(Integer.valueOf(1));
+ collection.add(Integer.valueOf(2));
+ collection.add(Integer.valueOf(3));
+ JSONArray jsonArray = (JSONArray) (JSONObject.wrap(collection));
+
+ // validate JSON
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 3 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
+ assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
+
+ // wrap Array returns JSONArray
+ Integer[] array = { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) };
+ JSONArray integerArrayJsonArray = (JSONArray)(JSONObject.wrap(array));
+
+ // validate JSON
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 3 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
+ assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
+
+ // validate JSON
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString());
+ assertTrue("expected 3 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
+ assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
+
+ // wrap map returns JSONObject
+ Map map = new HashMap();
+ map.put("key1", "val1");
+ map.put("key2", "val2");
+ map.put("key3", "val3");
+ JSONObject mapJsonObject = (JSONObject) (JSONObject.wrap(map));
+
+ // validate JSON
+ doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString());
+ assertTrue("expected 3 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 3);
+ assertTrue("expected val1", "val1".equals(mapJsonObject.query("/key1")));
+ assertTrue("expected val2", "val2".equals(mapJsonObject.query("/key2")));
+ assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3")));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObject, mapJsonObject
+ )));
+ Util.checkJSONArrayMaps(jsonArray, jsonObject.getMapType());
+ Util.checkJSONArrayMaps(integerArrayJsonArray, jsonObject.getMapType());
+ }
+
+
+ /**
+ * RFC 7159 defines control characters to be U+0000 through U+001F. This test verifies that the parser is checking for these in expected ways.
+ */
+ @Test
+ public void jsonObjectParseControlCharacters(){
+ for(int i = 0;i<=0x001f;i++){
+ final String charString = String.valueOf((char)i);
+ final String source = "{\"key\":\""+charString+"\"}";
+ try {
+ JSONObject jo = new JSONObject(source);
+ assertTrue("Expected "+charString+"("+i+") in the JSON Object but did not find it.",charString.equals(jo.getString("key")));
+ Util.checkJSONObjectMaps(jo);
+ } catch (JSONException ex) {
+ assertTrue("Only \\0 (U+0000), \\n (U+000A), and \\r (U+000D) should cause an error. Instead "+charString+"("+i+") caused an error",
+ i=='\0' || i=='\n' || i=='\r'
+ );
+ }
+ }
+ }
+
+ @Test
+ public void jsonObjectParseControlCharacterEOFAssertExceptionMessage(){
+ char c = '\0';
+ final String source = "{\"key\":\"" + c + "\"}";
+ try {
+ JSONObject jo = new JSONObject(source);
+ fail("JSONException should be thrown");
+ } catch (JSONException ex) {
+ assertEquals("Unterminated string. " + "Character with int code 0" +
+ " is not allowed within a quoted string. at 8 [character 9 line 1]", ex.getMessage());
+ }
+ }
+
+ @Test
+ public void jsonObjectParseControlCharacterNewLineAssertExceptionMessage(){
+ char[] chars = {'\n', '\r'};
+ for( char c : chars) {
+ final String source = "{\"key\":\"" + c + "\"}";
+ try {
+ JSONObject jo = new JSONObject(source);
+ fail("JSONException should be thrown");
+ } catch (JSONException ex) {
+ assertEquals("Unterminated string. " + "Character with int code " + (int) c +
+ " is not allowed within a quoted string. at 9 [character 0 line 2]", ex.getMessage());
+ }
+ }
+ }
+
+ @Test
+ public void jsonObjectParseUTF8EncodingAssertExceptionMessage(){
+ String c = "\\u123x";
+ final String source = "{\"key\":\"" + c + "\"}";
+ try {
+ JSONObject jo = new JSONObject(source);
+ fail("JSONException should be thrown");
+ } catch (JSONException ex) {
+ assertEquals("Illegal escape. \\u must be followed by a 4 digit hexadecimal number. " +
+ "\\123x is not valid. at 14 [character 15 line 1]", ex.getMessage());
+ }
+ }
+
+ @Test
+ public void jsonObjectParseIllegalEscapeAssertExceptionMessage(){
+ String c = "\\x";
+ final String source = "{\"key\":\"" + c + "\"}";
+ try {
+ JSONObject jo = new JSONObject(source);
+ fail("JSONException should be thrown");
+ } catch (JSONException ex) {
+ assertEquals("Illegal escape. Escape sequence " + c + " is not valid." +
+ " at 10 [character 11 line 1]", ex.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorTrailingCurlyBrace () {
+ try {
+ // does not end with '}'
+ String str = "{";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "A JSONObject text must end with '}' at 1 [character 2 line 1]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorInitialCurlyBrace() {
+ try {
+ // does not start with '{'
+ String str = "abc";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "A JSONObject text must begin with '{' at 1 [character 2 line 1]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorNoColon() {
+ try {
+ // key with no ':'
+ String str = "{\"myKey\" = true}";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Expected a ':' after a key at 10 [character 11 line 1]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorNoCommaSeparator() {
+ try {
+ // entries with no ',' separator
+ String str = "{\"myKey\":true \"myOtherKey\":false}";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Expected a ',' or '}' at 15 [character 16 line 1]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorKeyIsNestedMap() {
+ try {
+ // key is a nested map
+ String str = "{{\"foo\": \"bar\"}: \"baz\"}";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Missing value at 1 [character 2 line 1]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorKeyIsNestedArrayWithMap() {
+ try {
+ // key is a nested array containing a map
+ String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Missing value at 9 [character 10 line 1]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorKeyContainsCurlyBrace() {
+ try {
+ // key contains }
+ String str = "{foo}: 2}";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+// assertEquals("Expecting an exception message",
+// "Expected a ':' after a key at 5 [character 6 line 1]",
+// e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorKeyContainsSquareBrace() {
+ try {
+ // key contains ]
+ String str = "{foo]: 2}";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+// assertEquals("Expecting an exception message",
+// "Expected a ':' after a key at 5 [character 6 line 1]",
+// e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorKeyContainsBinaryZero() {
+ try {
+ // \0 after ,
+ String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
+ assertNull("Expected an exception", new JSONObject(str));
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "A JSONObject text must end with '}' at 15 [character 16 line 1]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorAppendToWrongValue() {
+ try {
+ // append to wrong value
+ String str = "{\"myKey\":true, \"myOtherKey\":false}";
+ JSONObject jsonObject = new JSONObject(str);
+ jsonObject.append("myKey", "hello");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "JSONObject[\"myKey\"] is not a JSONArray (null).",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorIncrementWrongValue() {
+ try {
+ // increment wrong value
+ String str = "{\"myKey\":true, \"myOtherKey\":false}";
+ JSONObject jsonObject = new JSONObject(str);
+ jsonObject.increment("myKey");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Unable to increment [\"myKey\"].",
+ e.getMessage());
+ }
+ }
+ @Test
+ public void parsingErrorInvalidKey() {
+ try {
+ // invalid key
+ String str = "{\"myKey\":true, \"myOtherKey\":false}";
+ JSONObject jsonObject = new JSONObject(str);
+ jsonObject.get(null);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Null key.",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorNumberToString() {
+ try {
+ // invalid numberToString()
+ JSONObject.numberToString((Number) null);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Null pointer",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorPutOnceDuplicateKey() {
+ try {
+ // multiple putOnce key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.putOnce("hello", "world");
+ jsonObject.putOnce("hello", "world!");
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertTrue("", true);
+ }
+ }
+
+ @Test
+ public void parsingErrorInvalidDouble() {
+ try {
+ // test validity of invalid double
+ JSONObject.testValidity(Double.NaN);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertTrue("", true);
+ }
+ }
+
+ @Test
+ public void parsingErrorInvalidFloat() {
+ try {
+ // test validity of invalid float
+ JSONObject.testValidity(Float.NEGATIVE_INFINITY);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertTrue("", true);
+ }
+ }
+
+ @Test
+ public void parsingErrorDuplicateKeyException() {
+ try {
+ // test exception message when including a duplicate key (level 0)
+ String str = "{\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\":\"value-02\",\n"
+ + " \"attr03\":\"value-03\",\n"
+ + " \"attr03\":\"value-04\"\n"
+ + "}";
+ new JSONObject(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr03\" at 90 [character 13 line 5]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorNestedDuplicateKeyException() {
+ try {
+ // test exception message when including a duplicate key (level 0) holding an object
+ String str = "{\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\":\"value-02\",\n"
+ + " \"attr03\":\"value-03\",\n"
+ + " \"attr03\": {"
+ + " \"attr04-01\":\"value-04-01\",n"
+ + " \"attr04-02\":\"value-04-02\",n"
+ + " \"attr04-03\":\"value-04-03\"n"
+ + " }\n"
+ + "}";
+ new JSONObject(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr03\" at 90 [character 13 line 5]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorNestedDuplicateKeyWithArrayException() {
+ try {
+ // test exception message when including a duplicate key (level 0) holding an array
+ String str = "{\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\":\"value-02\",\n"
+ + " \"attr03\":\"value-03\",\n"
+ + " \"attr03\": [\n"
+ + " {"
+ + " \"attr04-01\":\"value-04-01\",n"
+ + " \"attr04-02\":\"value-04-02\",n"
+ + " \"attr04-03\":\"value-04-03\"n"
+ + " }\n"
+ + " ]\n"
+ + "}";
+ new JSONObject(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr03\" at 90 [character 13 line 5]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorDuplicateKeyWithinNestedDictExceptionMessage() {
+ try {
+ // test exception message when including a duplicate key (level 1)
+ String str = "{\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\":\"value-02\",\n"
+ + " \"attr03\":\"value-03\",\n"
+ + " \"attr04\": {\n"
+ + " \"attr04-01\":\"value04-01\",\n"
+ + " \"attr04-02\":\"value04-02\",\n"
+ + " \"attr04-03\":\"value04-03\",\n"
+ + " \"attr04-03\":\"value04-04\"\n"
+ + " }\n"
+ + "}";
+ new JSONObject(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorDuplicateKeyDoubleNestedDictExceptionMessage() {
+ try {
+ // test exception message when including a duplicate key (level 1) holding an
+ // object
+ String str = "{\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\":\"value-02\",\n"
+ + " \"attr03\":\"value-03\",\n"
+ + " \"attr04\": {\n"
+ + " \"attr04-01\":\"value04-01\",\n"
+ + " \"attr04-02\":\"value04-02\",\n"
+ + " \"attr04-03\":\"value04-03\",\n"
+ + " \"attr04-03\": {\n"
+ + " \"attr04-04-01\":\"value04-04-01\",\n"
+ + " \"attr04-04-02\":\"value04-04-02\",\n"
+ + " \"attr04-04-03\":\"value04-04-03\",\n"
+ + " }\n"
+ + " }\n"
+ + "}";
+ new JSONObject(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorDuplicateKeyNestedWithArrayExceptionMessage() {
+ try {
+ // test exception message when including a duplicate key (level 1) holding an
+ // array
+ String str = "{\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\":\"value-02\",\n"
+ + " \"attr03\":\"value-03\",\n"
+ + " \"attr04\": {\n"
+ + " \"attr04-01\":\"value04-01\",\n"
+ + " \"attr04-02\":\"value04-02\",\n"
+ + " \"attr04-03\":\"value04-03\",\n"
+ + " \"attr04-03\": [\n"
+ + " {\n"
+ + " \"attr04-04-01\":\"value04-04-01\",\n"
+ + " \"attr04-04-02\":\"value04-04-02\",\n"
+ + " \"attr04-04-03\":\"value04-04-03\",\n"
+ + " }\n"
+ + " ]\n"
+ + " }\n"
+ + "}";
+ new JSONObject(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorDuplicateKeyWithinArrayExceptionMessage() {
+ try {
+ // test exception message when including a duplicate key in object (level 0)
+ // within an array
+ String str = "[\n"
+ + " {\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\":\"value-02\"\n"
+ + " },\n"
+ + " {\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr01\":\"value-02\"\n"
+ + " }\n"
+ + "]";
+ new JSONArray(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr01\" at 124 [character 17 line 8]",
+ e.getMessage());
+ }
+ }
+
+ @Test
+ public void parsingErrorDuplicateKeyDoubleNestedWithinArrayExceptionMessage() {
+ try {
+ // test exception message when including a duplicate key in object (level 1)
+ // within an array
+ String str = "[\n"
+ + " {\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\": {\n"
+ + " \"attr02-01\":\"value-02-01\",\n"
+ + " \"attr02-02\":\"value-02-02\"\n"
+ + " }\n"
+ + " },\n"
+ + " {\n"
+ + " \"attr01\":\"value-01\",\n"
+ + " \"attr02\": {\n"
+ + " \"attr02-01\":\"value-02-01\",\n"
+ + " \"attr02-01\":\"value-02-02\"\n"
+ + " }\n"
+ + " }\n"
+ + "]";
+ new JSONArray(str);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Expecting an expection message",
+ "Duplicate key \"attr02-01\" at 269 [character 24 line 13]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Confirm behavior when putOnce() is called with null parameters
+ */
+ @Test
+ public void jsonObjectPutOnceNull() {
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.putOnce(null, null);
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ jsonObject.putOnce("", null);
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ jsonObject.putOnce(null, "");
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise JSONObject opt(key, default) method.
+ */
+ @Test
+ public void jsonObjectOptDefault() {
+
+ String str = "{\"myKey\": \"myval\", \"hiKey\": null}";
+ JSONObject jsonObject = new JSONObject(str);
+
+ assertTrue("optBigDecimal() should return default BigDecimal",
+ BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0);
+ assertTrue("optBigInteger() should return default BigInteger",
+ BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0);
+ assertTrue("optBoolean() should return default boolean",
+ jsonObject.optBoolean("myKey", true));
+ assertTrue("optBooleanObject() should return default Boolean",
+ jsonObject.optBooleanObject("myKey", true));
+ assertTrue("optInt() should return default int",
+ 42 == jsonObject.optInt("myKey", 42));
+ assertTrue("optIntegerObject() should return default Integer",
+ Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42)));
+ assertTrue("optEnum() should return default Enum",
+ MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1)));
+ assertTrue("optJSONArray() should return null ",
+ null==jsonObject.optJSONArray("myKey"));
+ assertTrue("optJSONArray() should return default JSONArray",
+ "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0)));
+ assertTrue("optJSONObject() should return default JSONObject ",
+ jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue"));
+ assertTrue("optLong() should return default long",
+ 42l == jsonObject.optLong("myKey", 42l));
+ assertTrue("optLongObject() should return default Long",
+ Long.valueOf(42l).equals(jsonObject.optLongObject("myKey", 42l)));
+ assertTrue("optDouble() should return default double",
+ 42.3d == jsonObject.optDouble("myKey", 42.3d));
+ assertTrue("optDoubleObject() should return default Double",
+ Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d)));
+ assertTrue("optFloat() should return default float",
+ 42.3f == jsonObject.optFloat("myKey", 42.3f));
+ assertTrue("optFloatObject() should return default Float",
+ Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f)));
+ assertTrue("optNumber() should return default Number",
+ 42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
+ assertTrue("optString() should return default string",
+ "hi".equals(jsonObject.optString("hiKey", "hi")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise JSONObject opt(key, default) method when the key doesn't exist.
+ */
+ @Test
+ public void jsonObjectOptNoKey() {
+
+ JSONObject jsonObject = new JSONObject();
+
+ assertNull(jsonObject.opt(null));
+
+ assertTrue("optBigDecimal() should return default BigDecimal",
+ BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0);
+ assertTrue("optBigInteger() should return default BigInteger",
+ BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0);
+ assertTrue("optBoolean() should return default boolean",
+ jsonObject.optBoolean("myKey", true));
+ assertTrue("optBooleanObject() should return default Boolean",
+ jsonObject.optBooleanObject("myKey", true));
+ assertTrue("optInt() should return default int",
+ 42 == jsonObject.optInt("myKey", 42));
+ assertTrue("optIntegerObject() should return default Integer",
+ Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42)));
+ assertTrue("optEnum() should return default Enum",
+ MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1)));
+ assertTrue("optJSONArray() should return default JSONArray",
+ "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0)));
+ assertTrue("optJSONArray() should return null ",
+ null==jsonObject.optJSONArray("myKey"));
+ assertTrue("optJSONObject() should return default JSONObject ",
+ jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue"));
+ assertTrue("optLong() should return default long",
+ 42l == jsonObject.optLong("myKey", 42l));
+ assertTrue("optLongObject() should return default Long",
+ Long.valueOf(42l).equals(jsonObject.optLongObject("myKey", 42l)));
+ assertTrue("optDouble() should return default double",
+ 42.3d == jsonObject.optDouble("myKey", 42.3d));
+ assertTrue("optDoubleObject() should return default Double",
+ Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d)));
+ assertTrue("optFloat() should return default float",
+ 42.3f == jsonObject.optFloat("myKey", 42.3f));
+ assertTrue("optFloatObject() should return default Float",
+ Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f)));
+ assertTrue("optNumber() should return default Number",
+ 42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
+ assertTrue("optString() should return default string",
+ "hi".equals(jsonObject.optString("hiKey", "hi")));
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Verifies that the opt methods properly convert string values.
+ */
+ @Test
+ public void jsonObjectOptStringConversion() {
+ JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}");
+ assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true);
+ assertTrue("unexpected optBooleanObject value",Boolean.valueOf(true).equals(jo.optBooleanObject("true",false)));
+ assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false);
+ assertTrue("unexpected optBooleanObject value",Boolean.valueOf(false).equals(jo.optBooleanObject("false",true)));
+ assertTrue("unexpected optInt value",jo.optInt("int",0)==123);
+ assertTrue("unexpected optIntegerObject value",Integer.valueOf(123).equals(jo.optIntegerObject("int",0)));
+ assertTrue("unexpected optLong value",jo.optLong("int",0)==123l);
+ assertTrue("unexpected optLongObject value",Long.valueOf(123l).equals(jo.optLongObject("int",0L)));
+ assertTrue("unexpected optDouble value",jo.optDouble("int",0.0d)==123.0d);
+ assertTrue("unexpected optDoubleObject value",Double.valueOf(123.0d).equals(jo.optDoubleObject("int",0.0d)));
+ assertTrue("unexpected optFloat value",jo.optFloat("int",0.0f)==123.0f);
+ assertTrue("unexpected optFloatObject value",Float.valueOf(123.0f).equals(jo.optFloatObject("int",0.0f)));
+ assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
+ assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
+ assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
+ assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l);
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ /**
+ * Verifies that the opt methods properly convert string values to numbers and coerce them consistently.
+ */
+ @Test
+ public void jsonObjectOptCoercion() {
+ JSONObject jo = new JSONObject("{\"largeNumberStr\":\"19007199254740993.35481234487103587486413587843213584\"}");
+ // currently the parser doesn't recognize BigDecimal, to we have to put it manually
+ jo.put("largeNumber", new BigDecimal("19007199254740993.35481234487103587486413587843213584"));
+
+ // Test type coercion from larger to smaller
+ assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumber",null));
+ assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumber",null));
+ assertEquals(1.9007199254740992E16, jo.optDouble("largeNumber"),0.0);
+ assertEquals(1.9007199254740992E16, jo.optDoubleObject("largeNumber"),0.0);
+ assertEquals(1.90071995E16f, jo.optFloat("largeNumber"),0.0f);
+ assertEquals(1.90071995E16f, jo.optFloatObject("largeNumber"),0.0f);
+ assertEquals(19007199254740993l, jo.optLong("largeNumber"));
+ assertEquals(Long.valueOf(19007199254740993l), jo.optLongObject("largeNumber"));
+ assertEquals(1874919425, jo.optInt("largeNumber"));
+ assertEquals(Integer.valueOf(1874919425), jo.optIntegerObject("largeNumber"));
+
+ // conversion from a string
+ assertEquals(new BigDecimal("19007199254740993.35481234487103587486413587843213584"), jo.optBigDecimal("largeNumberStr",null));
+ assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumberStr",null));
+ assertEquals(1.9007199254740992E16, jo.optDouble("largeNumberStr"),0.0);
+ assertEquals(1.9007199254740992E16, jo.optDoubleObject("largeNumberStr"),0.0);
+ assertEquals(1.90071995E16f, jo.optFloat("largeNumberStr"),0.0f);
+ assertEquals(1.90071995E16f, jo.optFloatObject("largeNumberStr"),0.0f);
+ assertEquals(19007199254740993l, jo.optLong("largeNumberStr"));
+ assertEquals(Long.valueOf(19007199254740993l), jo.optLongObject("largeNumberStr"));
+ assertEquals(1874919425, jo.optInt("largeNumberStr"));
+ assertEquals(Integer.valueOf(1874919425), jo.optIntegerObject("largeNumberStr"));
+
+ // the integer portion of the actual value is larger than a double can hold.
+ assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber"));
+ assertNotEquals(Long.valueOf((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optLongObject("largeNumber"));
+ assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumber"));
+ assertNotEquals(Integer.valueOf((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optIntegerObject("largeNumber"));
+ assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumberStr"));
+ assertNotEquals(Long.valueOf((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optLongObject("largeNumberStr"));
+ assertNotEquals((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optInt("largeNumberStr"));
+ assertNotEquals(Integer.valueOf((int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584")), jo.optIntegerObject("largeNumberStr"));
+ assertEquals(19007199254740992l, (long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
+ assertEquals(2147483647, (int)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"));
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ /**
+ * Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently.
+ */
+ @Test
+ public void jsonObjectOptBigDecimal() {
+ JSONObject jo = new JSONObject().put("int", 123).put("long", 654L)
+ .put("float", 1.234f).put("double", 2.345d)
+ .put("bigInteger", new BigInteger("1234"))
+ .put("bigDecimal", new BigDecimal("1234.56789"))
+ .put("nullVal", JSONObject.NULL);
+
+ assertEquals(new BigDecimal("123"),jo.optBigDecimal("int", null));
+ assertEquals(new BigDecimal("654"),jo.optBigDecimal("long", null));
+ assertEquals(new BigDecimal(1.234f),jo.optBigDecimal("float", null));
+ assertEquals(new BigDecimal(2.345d),jo.optBigDecimal("double", null));
+ assertEquals(new BigDecimal("1234"),jo.optBigDecimal("bigInteger", null));
+ assertEquals(new BigDecimal("1234.56789"),jo.optBigDecimal("bigDecimal", null));
+ assertNull(jo.optBigDecimal("nullVal", null));
+ assertEquals(jo.optBigDecimal("float", null),jo.getBigDecimal("float"));
+ assertEquals(jo.optBigDecimal("double", null),jo.getBigDecimal("double"));
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ /**
+ * Verifies that the optBigDecimal method properly converts values to BigDecimal and coerce them consistently.
+ */
+ @Test
+ public void jsonObjectOptBigInteger() {
+ JSONObject jo = new JSONObject().put("int", 123).put("long", 654L)
+ .put("float", 1.234f).put("double", 2.345d)
+ .put("bigInteger", new BigInteger("1234"))
+ .put("bigDecimal", new BigDecimal("1234.56789"))
+ .put("nullVal", JSONObject.NULL);
+
+ assertEquals(new BigInteger("123"),jo.optBigInteger("int", null));
+ assertEquals(new BigInteger("654"),jo.optBigInteger("long", null));
+ assertEquals(new BigInteger("1"),jo.optBigInteger("float", null));
+ assertEquals(new BigInteger("2"),jo.optBigInteger("double", null));
+ assertEquals(new BigInteger("1234"),jo.optBigInteger("bigInteger", null));
+ assertEquals(new BigInteger("1234"),jo.optBigInteger("bigDecimal", null));
+ assertNull(jo.optBigDecimal("nullVal", null));
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ /**
+ * Confirm behavior when JSONObject put(key, null object) is called
+ */
+ @Test
+ public void jsonObjectputNull() {
+
+ // put null should remove the item.
+ String str = "{\"myKey\": \"myval\"}";
+ JSONObject jsonObjectRemove = new JSONObject(str);
+ jsonObjectRemove.remove("myKey");
+ assertTrue("jsonObject should be empty", jsonObjectRemove.isEmpty());
+
+ JSONObject jsonObjectPutNull = new JSONObject(str);
+ jsonObjectPutNull.put("myKey", (Object) null);
+ assertTrue("jsonObject should be empty", jsonObjectPutNull.isEmpty());
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObjectRemove, jsonObjectPutNull
+ )));
+ }
+
+ /**
+ * Exercise JSONObject quote() method
+ * This purpose of quote() is to ensure that for strings with embedded
+ * quotes, the quotes are properly escaped.
+ */
+ @Test
+ public void jsonObjectQuote() {
+ String str;
+ str = "";
+ String quotedStr;
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped quotes, found "+quotedStr,
+ "\"\"".equals(quotedStr));
+ str = "\"\"";
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped quotes, found "+quotedStr,
+ "\"\\\"\\\"\"".equals(quotedStr));
+ str = "";
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped frontslash, found "+quotedStr,
+ "\"<\\/\"".equals(quotedStr));
+ str = "AB\bC";
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped backspace, found "+quotedStr,
+ "\"AB\\bC\"".equals(quotedStr));
+ str = "ABC\n";
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped newline, found "+quotedStr,
+ "\"ABC\\n\"".equals(quotedStr));
+ str = "AB\fC";
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped formfeed, found "+quotedStr,
+ "\"AB\\fC\"".equals(quotedStr));
+ str = "\r";
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped return, found "+quotedStr,
+ "\"\\r\"".equals(quotedStr));
+ str = "\u1234\u0088";
+ quotedStr = JSONObject.quote(str);
+ assertTrue("quote() expected escaped unicode, found "+quotedStr,
+ "\"\u1234\\u0088\"".equals(quotedStr));
+ }
+
+ /**
+ * Confirm behavior when JSONObject stringToValue() is called for an
+ * empty string
+ */
+ @Test
+ public void stringToValue() {
+ String str = "";
+ String valueStr = (String)(JSONObject.stringToValue(str));
+ assertTrue("stringToValue() expected empty String, found "+valueStr,
+ "".equals(valueStr));
+ }
+
+ /**
+ * Confirm behavior when toJSONArray is called with a null value
+ */
+ @Test
+ public void toJSONArray() {
+ assertTrue("toJSONArray() with null names should be null",
+ null == new JSONObject().toJSONArray(null));
+ }
+
+ /**
+ * Exercise the JSONObject write() method
+ */
+ @Test
+ public void write() throws IOException {
+ String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
+ String expectedStr = str;
+ JSONObject jsonObject = new JSONObject(str);
+ StringWriter stringWriter = new StringWriter();
+ try {
+ String actualStr = jsonObject.write(stringWriter).toString();
+ // key order may change. verify length and individual key content
+ assertEquals("length", expectedStr.length(), actualStr.length());
+ assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
+ assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]"));
+ } finally {
+ stringWriter.close();
+ }
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Confirms that exceptions thrown when writing values are wrapped properly.
+ */
+ @Test
+ public void testJSONWriterException() {
+ final JSONObject jsonObject = new JSONObject();
+
+ jsonObject.put("someKey",new BrokenToString());
+
+ // test single element JSONObject
+ StringWriter writer = new StringWriter();
+ try {
+ jsonObject.write(writer).toString();
+ fail("Expected an exception, got a String value");
+ } catch (JSONException e) {
+ assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
+ } catch(Exception e) {
+ fail("Expected JSONException");
+ } finally {
+ try {
+ writer.close();
+ } catch (Exception e) {}
+ }
+
+ //test multiElement
+ jsonObject.put("somethingElse", "a value");
+
+ writer = new StringWriter();
+ try {
+ jsonObject.write(writer).toString();
+ fail("Expected an exception, got a String value");
+ } catch (JSONException e) {
+ assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
+ } catch(Exception e) {
+ fail("Expected JSONException");
+ } finally {
+ try {
+ writer.close();
+ } catch (Exception e) {}
+ }
+
+ // test a more complex object
+ writer = new StringWriter();
+ try {
+ new JSONObject()
+ .put("somethingElse", "a value")
+ .put("someKey", new JSONArray()
+ .put(new JSONObject().put("key1", new BrokenToString())))
+ .write(writer).toString();
+ fail("Expected an exception, got a String value");
+ } catch (JSONException e) {
+ assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
+ } catch(Exception e) {
+ fail("Expected JSONException");
+ } finally {
+ try {
+ writer.close();
+ } catch (Exception e) {}
+ }
+
+ // test a more slightly complex object
+ writer = new StringWriter();
+ try {
+ new JSONObject()
+ .put("somethingElse", "a value")
+ .put("someKey", new JSONArray()
+ .put(new JSONObject().put("key1", new BrokenToString()))
+ .put(12345)
+ )
+ .write(writer).toString();
+ fail("Expected an exception, got a String value");
+ } catch (JSONException e) {
+ assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
+ } catch(Exception e) {
+ fail("Expected JSONException");
+ } finally {
+ try {
+ writer.close();
+ } catch (Exception e) {}
+ }
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+
+ /**
+ * Exercise the JSONObject write() method
+ */
+/*
+ @Test
+ public void writeAppendable() {
+ String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
+ String expectedStr = str;
+ JSONObject jsonObject = new JSONObject(str);
+ StringBuilder stringBuilder = new StringBuilder();
+ Appendable appendable = jsonObject.write(stringBuilder);
+ String actualStr = appendable.toString();
+ assertTrue("write() expected " +expectedStr+
+ " but found " +actualStr,
+ expectedStr.equals(actualStr));
+ }
+*/
+
+ /**
+ * Exercise the JSONObject write(Writer, int, int) method
+ */
+ @Test
+ public void write3Param() throws IOException {
+ String str0 = "{\"key1\":\"value1\",\"key2\":[1,false,3.14]}";
+ String str2 =
+ "{\n" +
+ " \"key1\": \"value1\",\n" +
+ " \"key2\": [\n" +
+ " 1,\n" +
+ " false,\n" +
+ " 3.14\n" +
+ " ]\n" +
+ " }";
+ JSONObject jsonObject = new JSONObject(str0);
+ StringWriter stringWriter = new StringWriter();
+ try {
+ String actualStr = jsonObject.write(stringWriter,0,0).toString();
+
+ assertEquals("length", str0.length(), actualStr.length());
+ assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
+ assertTrue("key2", actualStr.contains("\"key2\":[1,false,3.14]"));
+ } finally {
+ try {
+ stringWriter.close();
+ } catch (Exception e) {}
+ }
+
+ stringWriter = new StringWriter();
+ try {
+ String actualStr = jsonObject.write(stringWriter,2,1).toString();
+
+ assertEquals("length", str2.length(), actualStr.length());
+ assertTrue("key1", actualStr.contains(" \"key1\": \"value1\""));
+ assertTrue("key2", actualStr.contains(" \"key2\": [\n" +
+ " 1,\n" +
+ " false,\n" +
+ " 3.14\n" +
+ " ]")
+ );
+ } finally {
+ try {
+ stringWriter.close();
+ } catch (Exception e) {}
+ }
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Exercise the JSONObject write(Appendable, int, int) method
+ */
+/*
+ @Test
+ public void write3ParamAppendable() {
+ String str0 = "{\"key1\":\"value1\",\"key2\":[1,false,3.14]}";
+ String str2 =
+ "{\n" +
+ " \"key1\": \"value1\",\n" +
+ " \"key2\": [\n" +
+ " 1,\n" +
+ " false,\n" +
+ " 3.14\n" +
+ " ]\n" +
+ " }";
+ JSONObject jsonObject = new JSONObject(str0);
+ String expectedStr = str0;
+ StringBuilder stringBuilder = new StringBuilder();
+ Appendable appendable = jsonObject.write(stringBuilder,0,0);
+ String actualStr = appendable.toString();
+ assertEquals(expectedStr, actualStr);
+
+ expectedStr = str2;
+ stringBuilder = new StringBuilder();
+ appendable = jsonObject.write(stringBuilder,2,1);
+ actualStr = appendable.toString();
+ assertEquals(expectedStr, actualStr);
+ }
+*/
+
+ /**
+ * Exercise the JSONObject equals() method
+ */
+ @Test
+ public void equals() {
+ String str = "{\"key\":\"value\"}";
+ JSONObject aJsonObject = new JSONObject(str);
+ assertTrue("Same JSONObject should be equal to itself",
+ aJsonObject.equals(aJsonObject));
+ Util.checkJSONObjectMaps(aJsonObject);
+ }
+
+ /**
+ * JSON null is not the same as Java null. This test examines the differences
+ * in how they are handled by JSON-java.
+ */
+ @Test
+ public void jsonObjectNullOperations() {
+ /**
+ * The Javadoc for JSONObject.NULL states:
+ * "JSONObject.NULL is equivalent to the value that JavaScript calls null,
+ * whilst Java's null is equivalent to the value that JavaScript calls
+ * undefined."
+ *
+ * Standard ECMA-262 6th Edition / June 2015 (included to help explain the javadoc):
+ * undefined value: primitive value used when a variable has not been assigned a value
+ * Undefined type: type whose sole value is the undefined value
+ * null value: primitive value that represents the intentional absence of any object value
+ * Null type: type whose sole value is the null value
+ * Java SE8 language spec (included to help explain the javadoc):
+ * The Kinds of Types and Values ...
+ * There is also a special null type, the type of the expression null, which has no name.
+ * Because the null type has no name, it is impossible to declare a variable of the null
+ * type or to cast to the null type. The null reference is the only possible value of an
+ * expression of null type. The null reference can always be assigned or cast to any reference type.
+ * In practice, the programmer can ignore the null type and just pretend that null is merely
+ * a special literal that can be of any reference type.
+ * Extensible Markup Language (XML) 1.0 Fifth Edition / 26 November 2008
+ * No mention of null
+ * ECMA-404 1st Edition / October 2013:
+ * JSON Text ...
+ * These are three literal name tokens: ...
+ * null
+ *
+ * There seems to be no best practice to follow, it's all about what we
+ * want the code to do.
+ */
+
+ // add JSONObject.NULL then convert to string in the manner of XML.toString()
+ JSONObject jsonObjectJONull = new JSONObject();
+ Object obj = JSONObject.NULL;
+ jsonObjectJONull.put("key", obj);
+ Object value = jsonObjectJONull.opt("key");
+ assertTrue("opt() JSONObject.NULL should find JSONObject.NULL",
+ obj.equals(value));
+ value = jsonObjectJONull.get("key");
+ assertTrue("get() JSONObject.NULL should find JSONObject.NULL",
+ obj.equals(value));
+ if (value == null) {
+ value = "";
+ }
+ String string = value instanceof String ? (String)value : null;
+ assertTrue("XML toString() should convert JSONObject.NULL to null",
+ string == null);
+
+ // now try it with null
+ JSONObject jsonObjectNull = new JSONObject();
+ obj = null;
+ jsonObjectNull.put("key", obj);
+ value = jsonObjectNull.opt("key");
+ assertNull("opt() null should find null", value);
+ // what is this trying to do? It appears to test absolutely nothing...
+// if (value == null) {
+// value = "";
+// }
+// string = value instanceof String ? (String)value : null;
+// assertTrue("should convert null to empty string", "".equals(string));
+ try {
+ value = jsonObjectNull.get("key");
+ fail("get() null should throw exception");
+ } catch (Exception ignored) {}
+
+ /**
+ * XML.toString() then goes on to do something with the value
+ * if the key val is "content", then value.toString() will be
+ * called. This will evaluate to "null" for JSONObject.NULL,
+ * and the empty string for null.
+ * But if the key is anything else, then JSONObject.NULL will be emitted
+ * as null and null will be emitted as ""
+ */
+ String sJONull = XML.toString(jsonObjectJONull);
+ assertTrue("JSONObject.NULL should emit a null value",
+ "null ".equals(sJONull));
+ String sNull = XML.toString(jsonObjectNull);
+ assertTrue("null should emit an empty string", "".equals(sNull));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jsonObjectJONull, jsonObjectNull
+ )));
+ }
+
+ @Test(expected = JSONPointerException.class)
+ public void queryWithNoResult() {
+ new JSONObject().query("/a/b");
+ }
+
+ @Test
+ public void optQueryWithNoResult() {
+ assertNull(new JSONObject().optQuery("/a/b"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void optQueryWithSyntaxError() {
+ new JSONObject().optQuery("invalid");
+ }
+
+ @Test(expected = JSONException.class)
+ public void invalidEscapeSequence() {
+ String json = "{ \"\\url\": \"value\" }";
+ assertNull("Expected an exception",new JSONObject(json));
+ }
+
+ /**
+ * Exercise JSONObject toMap() method.
+ */
+ @Test
+ public void toMap() {
+ String jsonObjectStr =
+ "{" +
+ "\"key1\":" +
+ "[1,2," +
+ "{\"key3\":true}" +
+ "]," +
+ "\"key2\":" +
+ "{\"key1\":\"val1\",\"key2\":" +
+ "{\"key2\":null}," +
+ "\"key3\":42" +
+ "}," +
+ "\"key3\":" +
+ "[" +
+ "[\"value1\",2.1]" +
+ "," +
+ "[null]" +
+ "]" +
+ "}";
+
+ JSONObject jsonObject = new JSONObject(jsonObjectStr);
+ Map,?> map = jsonObject.toMap();
+
+ assertTrue("Map should not be null", map != null);
+ assertTrue("Map should have 3 elements", map.size() == 3);
+
+ List> key1List = (List>)map.get("key1");
+ assertTrue("key1 should not be null", key1List != null);
+ assertTrue("key1 list should have 3 elements", key1List.size() == 3);
+ assertTrue("key1 value 1 should be 1", key1List.get(0).equals(Integer.valueOf(1)));
+ assertTrue("key1 value 2 should be 2", key1List.get(1).equals(Integer.valueOf(2)));
+
+ Map,?> key1Value3Map = (Map,?>)key1List.get(2);
+ assertTrue("Map should not be null", key1Value3Map != null);
+ assertTrue("Map should have 1 element", key1Value3Map.size() == 1);
+ assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE));
+
+ Map,?> key2Map = (Map,?>)map.get("key2");
+ assertTrue("key2 should not be null", key2Map != null);
+ assertTrue("key2 map should have 3 elements", key2Map.size() == 3);
+ assertTrue("key2 map key 1 should be val1", key2Map.get("key1").equals("val1"));
+ assertTrue("key2 map key 3 should be 42", key2Map.get("key3").equals(Integer.valueOf(42)));
+
+ Map,?> key2Val2Map = (Map,?>)key2Map.get("key2");
+ assertTrue("key2 map key 2 should not be null", key2Val2Map != null);
+ assertTrue("key2 map key 2 should have an entry", key2Val2Map.containsKey("key2"));
+ assertTrue("key2 map key 2 value should be null", key2Val2Map.get("key2") == null);
+
+ List> key3List = (List>)map.get("key3");
+ assertTrue("key3 should not be null", key3List != null);
+ assertTrue("key3 list should have 3 elements", key3List.size() == 2);
+
+ List> key3Val1List = (List>)key3List.get(0);
+ assertTrue("key3 list val 1 should not be null", key3Val1List != null);
+ assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2);
+ assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1"));
+ assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(new BigDecimal("2.1")));
+
+ List> key3Val2List = (List>)key3List.get(1);
+ assertTrue("key3 list val 2 should not be null", key3Val2List != null);
+ assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1);
+ assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null);
+
+ // Assert that toMap() is a deep copy
+ jsonObject.getJSONArray("key3").getJSONArray(0).put(0, "still value 1");
+ assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1"));
+
+ // assert that the new map is mutable
+ assertTrue("Removing a key should succeed", map.remove("key3") != null);
+ assertTrue("Map should have 2 elements", map.size() == 2);
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * test that validates a singleton can be serialized as a bean.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void testSingletonBean() {
+ final JSONObject jo = new JSONObject(Singleton.getInstance());
+ assertEquals(jo.keySet().toString(), 1, jo.length());
+ assertEquals(0, jo.get("someInt"));
+ assertEquals(null, jo.opt("someString"));
+
+ // Update the singleton values
+ Singleton.getInstance().setSomeInt(42);
+ Singleton.getInstance().setSomeString("Something");
+ final JSONObject jo2 = new JSONObject(Singleton.getInstance());
+ assertEquals(2, jo2.length());
+ assertEquals(42, jo2.get("someInt"));
+ assertEquals("Something", jo2.get("someString"));
+
+ // ensure our original jo hasn't changed.
+ assertEquals(0, jo.get("someInt"));
+ assertEquals(null, jo.opt("someString"));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jo, jo2
+ )));
+ }
+
+ /**
+ * test that validates a singleton can be serialized as a bean.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void testSingletonEnumBean() {
+ final JSONObject jo = new JSONObject(SingletonEnum.getInstance());
+ assertEquals(jo.keySet().toString(), 1, jo.length());
+ assertEquals(0, jo.get("someInt"));
+ assertEquals(null, jo.opt("someString"));
+
+ // Update the singleton values
+ SingletonEnum.getInstance().setSomeInt(42);
+ SingletonEnum.getInstance().setSomeString("Something");
+ final JSONObject jo2 = new JSONObject(SingletonEnum.getInstance());
+ assertEquals(2, jo2.length());
+ assertEquals(42, jo2.get("someInt"));
+ assertEquals("Something", jo2.get("someString"));
+
+ // ensure our original jo hasn't changed.
+ assertEquals(0, jo.get("someInt"));
+ assertEquals(null, jo.opt("someString"));
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ jo, jo2
+ )));
+ }
+
+ /**
+ * Test to validate that a generic class can be serialized as a bean.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void testGenericBean() {
+ GenericBean bean = new GenericBean<>(42);
+ final JSONObject jo = new JSONObject(bean);
+ assertEquals(jo.keySet().toString(), 8, jo.length());
+ assertEquals(42, jo.get("genericValue"));
+ assertEquals("Expected the getter to only be called once",
+ 1, bean.genericGetCounter);
+ assertEquals(0, bean.genericSetCounter);
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ /**
+ * Test to validate that a generic class can be serialized as a bean.
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void testGenericIntBean() {
+ GenericBeanInt bean = new GenericBeanInt(42);
+ final JSONObject jo = new JSONObject(bean);
+ assertEquals(jo.keySet().toString(), 10, jo.length());
+ assertEquals(42, jo.get("genericValue"));
+ assertEquals("Expected the getter to only be called once",
+ 1, bean.genericGetCounter);
+ assertEquals(0, bean.genericSetCounter);
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ /**
+ * Test to verify key
limitations in the JSONObject bean serializer.
+ */
+ @Test
+ public void testWierdListBean() {
+ @SuppressWarnings("boxing")
+ WeirdList bean = new WeirdList(42, 43, 44);
+ final JSONObject jo = new JSONObject(bean);
+ // get() should have a key of 0 length
+ // get(int) should be ignored base on parameter count
+ // getInt(int) should also be ignored based on parameter count
+ // add(Integer) should be ignore as it doesn't start with get/is and also has a parameter
+ // getALL should be mapped
+ assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
+ 1, jo.length());
+ assertNotNull(jo.get("ALL"));
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ /**
+ * Sample test case from https://github.com/stleary/JSON-java/issues/531
+ * which verifies that no regression in double/BigDecimal support is present.
+ */
+ @Test
+ public void testObjectToBigDecimal() {
+ double value = 1412078745.01074;
+ Reader reader = new StringReader("[{\"value\": " + value + "}]");
+ JSONTokener tokener = new JSONTokener(reader);
+ JSONArray array = new JSONArray(tokener);
+ JSONObject jsonObject = array.getJSONObject(0);
+
+ BigDecimal current = jsonObject.getBigDecimal("value");
+ BigDecimal wantedValue = BigDecimal.valueOf(value);
+
+ assertEquals(current, wantedValue);
+ Util.checkJSONObjectMaps(jsonObject);
+ Util.checkJSONArrayMaps(array, jsonObject.getMapType());
+ }
+
+ /**
+ * Tests the exception portions of populateMap.
+ */
+ @Test
+ public void testExceptionalBean() {
+ ExceptionalBean bean = new ExceptionalBean();
+ final JSONObject jo = new JSONObject(bean);
+ assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
+ 1, jo.length());
+ assertTrue(jo.get("closeable") instanceof JSONObject);
+ assertTrue(jo.getJSONObject("closeable").has("string"));
+ Util.checkJSONObjectMaps(jo);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testPutNullBoolean() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, false);
+ fail("Expected an exception");
+ }
+ @Test(expected=NullPointerException.class)
+ public void testPutNullCollection() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, Collections.emptySet());
+ fail("Expected an exception");
+ }
+ @Test(expected=NullPointerException.class)
+ public void testPutNullDouble() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, 0.0d);
+ fail("Expected an exception");
+ }
+ @Test(expected=NullPointerException.class)
+ public void testPutNullFloat() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, 0.0f);
+ fail("Expected an exception");
+ }
+ @Test(expected=NullPointerException.class)
+ public void testPutNullInt() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, 0);
+ fail("Expected an exception");
+ }
+ @Test(expected=NullPointerException.class)
+ public void testPutNullLong() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, 0L);
+ fail("Expected an exception");
+ }
+ @Test(expected=NullPointerException.class)
+ public void testPutNullMap() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, Collections.emptyMap());
+ fail("Expected an exception");
+ }
+ @Test(expected=NullPointerException.class)
+ public void testPutNullObject() {
+ // null put key
+ JSONObject jsonObject = new JSONObject("{}");
+ jsonObject.put(null, new Object());
+ fail("Expected an exception");
+ }
+ @Test(expected=JSONException.class)
+ public void testSelfRecursiveObject() {
+ // A -> A ...
+ RecursiveBean ObjA = new RecursiveBean("ObjA");
+ ObjA.setRef(ObjA);
+ new JSONObject(ObjA);
+ fail("Expected an exception");
+ }
+ @Test(expected=JSONException.class)
+ public void testLongSelfRecursiveObject() {
+ // B -> A -> A ...
+ RecursiveBean ObjA = new RecursiveBean("ObjA");
+ RecursiveBean ObjB = new RecursiveBean("ObjB");
+ ObjB.setRef(ObjA);
+ ObjA.setRef(ObjA);
+ new JSONObject(ObjB);
+ fail("Expected an exception");
+ }
+ @Test(expected=JSONException.class)
+ public void testSimpleRecursiveObject() {
+ // B -> A -> B ...
+ RecursiveBean ObjA = new RecursiveBean("ObjA");
+ RecursiveBean ObjB = new RecursiveBean("ObjB");
+ ObjB.setRef(ObjA);
+ ObjA.setRef(ObjB);
+ new JSONObject(ObjA);
+ fail("Expected an exception");
+ }
+ @Test(expected=JSONException.class)
+ public void testLongRecursiveObject() {
+ // D -> C -> B -> A -> D ...
+ RecursiveBean ObjA = new RecursiveBean("ObjA");
+ RecursiveBean ObjB = new RecursiveBean("ObjB");
+ RecursiveBean ObjC = new RecursiveBean("ObjC");
+ RecursiveBean ObjD = new RecursiveBean("ObjD");
+ ObjC.setRef(ObjB);
+ ObjB.setRef(ObjA);
+ ObjD.setRef(ObjC);
+ ObjA.setRef(ObjD);
+ new JSONObject(ObjB);
+ fail("Expected an exception");
+ }
+ @Test(expected=JSONException.class)
+ public void testRepeatObjectRecursive() {
+ // C -> B -> A -> D -> C ...
+ // -> D -> C ...
+ RecursiveBean ObjA = new RecursiveBean("ObjA");
+ RecursiveBean ObjB = new RecursiveBean("ObjB");
+ RecursiveBean ObjC = new RecursiveBean("ObjC");
+ RecursiveBean ObjD = new RecursiveBean("ObjD");
+ ObjC.setRef(ObjB);
+ ObjB.setRef(ObjA);
+ ObjB.setRef2(ObjD);
+ ObjA.setRef(ObjD);
+ ObjD.setRef(ObjC);
+ new JSONObject(ObjC);
+ fail("Expected an exception");
+ }
+ @Test
+ public void testRepeatObjectNotRecursive() {
+ // C -> B -> A
+ // -> A
+ RecursiveBean ObjA = new RecursiveBean("ObjA");
+ RecursiveBean ObjB = new RecursiveBean("ObjB");
+ RecursiveBean ObjC = new RecursiveBean("ObjC");
+ ObjC.setRef(ObjA);
+ ObjB.setRef(ObjA);
+ ObjB.setRef2(ObjA);
+ JSONObject j0 = new JSONObject(ObjC);
+ JSONObject j1 = new JSONObject(ObjB);
+ JSONObject j2 = new JSONObject(ObjA);
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ j0, j1, j2
+ )));
+ }
+ @Test
+ public void testLongRepeatObjectNotRecursive() {
+ // C -> B -> A -> D -> E
+ // -> D -> E
+ RecursiveBean ObjA = new RecursiveBean("ObjA");
+ RecursiveBean ObjB = new RecursiveBean("ObjB");
+ RecursiveBean ObjC = new RecursiveBean("ObjC");
+ RecursiveBean ObjD = new RecursiveBean("ObjD");
+ RecursiveBean ObjE = new RecursiveBean("ObjE");
+ ObjC.setRef(ObjB);
+ ObjB.setRef(ObjA);
+ ObjB.setRef2(ObjD);
+ ObjA.setRef(ObjD);
+ ObjD.setRef(ObjE);
+ JSONObject j0 = new JSONObject(ObjC);
+ JSONObject j1 = new JSONObject(ObjB);
+ JSONObject j2 = new JSONObject(ObjA);
+ JSONObject j3 = new JSONObject(ObjD);
+ JSONObject j4 = new JSONObject(ObjE);
+ Util.checkJSONObjectsMaps(new ArrayList(Arrays.asList(
+ j0, j1, j2, j3, j4
+ )));
+ }
+ @Test(expected=JSONException.class)
+ public void testRecursiveEquals() {
+ RecursiveBeanEquals a = new RecursiveBeanEquals("same");
+ a.setRef(a);
+ JSONObject j0 = new JSONObject(a);
+ Util.checkJSONObjectMaps(j0);
+ }
+ @Test
+ public void testNotRecursiveEquals() {
+ RecursiveBeanEquals a = new RecursiveBeanEquals("same");
+ RecursiveBeanEquals b = new RecursiveBeanEquals("same");
+ RecursiveBeanEquals c = new RecursiveBeanEquals("same");
+ a.setRef(b);
+ b.setRef(c);
+ JSONObject j0 = new JSONObject(a);
+ Util.checkJSONObjectMaps(j0);
+ }
+
+
+ @Test
+ public void testIssue548ObjectWithEmptyJsonArray() {
+ JSONObject jsonObject = new JSONObject("{\"empty_json_array\": []}");
+ assertTrue("missing expected key 'empty_json_array'", jsonObject.has("empty_json_array"));
+ assertNotNull("'empty_json_array' should be an array", jsonObject.getJSONArray("empty_json_array"));
+ assertEquals("'empty_json_array' should have a length of 0", 0, jsonObject.getJSONArray("empty_json_array").length());
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Tests if calling JSONObject clear() method actually makes the JSONObject empty
+ */
+ @Test(expected = JSONException.class)
+ public void jsonObjectClearMethodTest() {
+ //Adds random stuff to the JSONObject
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("key1", 123);
+ jsonObject.put("key2", "456");
+ jsonObject.put("key3", new JSONObject());
+ jsonObject.clear(); //Clears the JSONObject
+ assertTrue("expected jsonObject.length() == 0", jsonObject.length() == 0); //Check if its length is 0
+ jsonObject.getInt("key1"); //Should throws org.json.JSONException: JSONObject["asd"] not found
+ Util.checkJSONObjectMaps(jsonObject);
+ }
+
+ /**
+ * Tests for stack overflow. See https://github.com/stleary/JSON-java/issues/654
+ */
+ @Test(expected = JSONException.class)
+ public void issue654StackOverflowInput() {
+ //String base64Bytes ="eyJHWiI6Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7ewl7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMCkwLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7CXt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7ewl7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMCkwLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7CXt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3sJe3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTApMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7ewl7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3sJe3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTApMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMCkwLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7CXt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7ewl7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMCkwLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7CXt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3sJe3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTApMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7ewl7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3sJe3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTApMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7ewl7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7c3t7e3t7e3vPAAAAAAAAAHt7e3t7e3t7e3t7e3t7e3t7e3t7e1ste3t7e3t7e3t7e3t7e3t7e3t7e3t7CXt7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3tbLTAtMCx7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e1stMC0wLHt7e3t7e3t7e3t7e3t7e3t7e88AAAAAAAAAe3t7e3t7e3t7e3t7e3t7e3t7e3t7Wy0wLTAse3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7f3syMv//e3t7e3t7e3t7e3t7e3sx//////8=";
+ //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes));
+ String input = "{\"GZ\":[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0)0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{{{{{{{{{{{{{{{{{{{{[-0-0,{{{{{{{{{{s{{{{{{{";
+ JSONObject json_input = new JSONObject(input);
+ assertNotNull(json_input);
+ fail("Excepected Exception.");
+ Util.checkJSONObjectMaps(json_input);
+ }
+
+ /**
+ * Tests for incorrect object/array nesting. See https://github.com/stleary/JSON-java/issues/654
+ */
+ @Test(expected = JSONException.class)
+ public void issue654IncorrectNestingNoKey1() {
+ JSONObject json_input = new JSONObject("{{\"a\":0}}");
+ assertNotNull(json_input);
+ fail("Expected Exception.");
+ }
+
+ /**
+ * Tests for incorrect object/array nesting. See https://github.com/stleary/JSON-java/issues/654
+ */
+ @Test(expected = JSONException.class)
+ public void issue654IncorrectNestingNoKey2() {
+ JSONObject json_input = new JSONObject("{[\"a\"]}");
+ assertNotNull(json_input);
+ fail("Excepected Exception.");
+ }
+
+ /**
+ * Tests for stack overflow. See https://github.com/stleary/JSON-java/issues/654
+ */
+ @Ignore("This test relies on system constraints and may not always pass. See: https://github.com/stleary/JSON-java/issues/821")
+ @Test(expected = JSONException.class)
+ public void issue654StackOverflowInputWellFormed() {
+ //String input = new String(java.util.Base64.getDecoder().decode(base64Bytes));
+ final InputStream resourceAsStream = JSONObjectTest.class.getClassLoader().getResourceAsStream("Issue654WellFormedObject.json");
+ JSONTokener tokener = new JSONTokener(resourceAsStream);
+ JSONObject json_input = new JSONObject(tokener);
+ assertNotNull(json_input);
+ fail("Excepected Exception due to stack overflow.");
+ }
+
+ @Test
+ public void testIssue682SimilarityOfJSONString() {
+ JSONObject jo1 = new JSONObject()
+ .put("a", new MyJsonString())
+ .put("b", 2);
+ JSONObject jo2 = new JSONObject()
+ .put("a", new MyJsonString())
+ .put("b", 2);
+ assertTrue(jo1.similar(jo2));
+
+ JSONObject jo3 = new JSONObject()
+ .put("a", new JSONString() {
+ @Override
+ public String toJSONString() {
+ return "\"different value\"";
+ }
+ })
+ .put("b", 2);
+ assertFalse(jo1.similar(jo3));
+ }
+
+ private static final Number[] NON_FINITE_NUMBERS = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN,
+ Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN };
+
+ @Test
+ public void issue713MapConstructorWithNonFiniteNumbers() {
+ for (Number nonFinite : NON_FINITE_NUMBERS) {
+ Map map = new HashMap<>();
+ map.put("a", nonFinite);
+
+ assertThrows(JSONException.class, () -> new JSONObject(map));
+ }
+ }
+
+ @Test
+ public void issue713BeanConstructorWithNonFiniteNumbers() {
+ for (Number nonFinite : NON_FINITE_NUMBERS) {
+ GenericBean bean = new GenericBean<>(nonFinite);
+ assertThrows(JSONException.class, () -> new JSONObject(bean));
+ }
+ }
+
+ @Test(expected = JSONException.class)
+ public void issue743SerializationMap() {
+ HashMap map = new HashMap<>();
+ map.put("t", map);
+ JSONObject object = new JSONObject(map);
+ String jsonString = object.toString();
+ }
+
+ @Test(expected = JSONException.class)
+ public void testCircularReferenceMultipleLevel() {
+ HashMap inside = new HashMap<>();
+ HashMap jsonObject = new HashMap<>();
+ inside.put("inside", jsonObject);
+ jsonObject.put("test", inside);
+ new JSONObject(jsonObject);
+ }
+
+ @Test
+ public void issue743SerializationMapWith512Objects() {
+ HashMap map = buildNestedMap(ParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
+ JSONObject object = new JSONObject(map);
+ String jsonString = object.toString();
+ }
+
+ @Test
+ public void issue743SerializationMapWith1000Objects() {
+ HashMap map = buildNestedMap(1000);
+ JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000);
+ JSONObject object = new JSONObject(map, parserConfiguration);
+ String jsonString = object.toString();
+ }
+
+ @Test(expected = JSONException.class)
+ public void issue743SerializationMapWith1001Objects() {
+ HashMap map = buildNestedMap(1001);
+ JSONObject object = new JSONObject(map);
+ String jsonString = object.toString();
+ }
+
+ @Test(expected = JSONException.class)
+ public void testCircleReferenceFirstLevel() {
+ Map jsonObject = new HashMap<>();
+
+ jsonObject.put("test", jsonObject);
+
+ new JSONObject(jsonObject, new JSONParserConfiguration());
+ }
+
+ @Test(expected = StackOverflowError.class)
+ public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() {
+ Map inside = new HashMap<>();
+
+ Map jsonObject = new HashMap<>();
+ inside.put("test", jsonObject);
+ jsonObject.put("test", inside);
+
+ new JSONObject(jsonObject, new JSONParserConfiguration().withMaxNestingDepth(99999));
+ }
+
+ @Test(expected = JSONException.class)
+ public void testCircleReferenceMultiplyLevel_configured_expectedJSONException() {
+ Map inside = new HashMap<>();
+
+ Map jsonObject = new HashMap<>();
+ inside.put("test", jsonObject);
+ jsonObject.put("test", inside);
+
+ new JSONObject(jsonObject, new JSONParserConfiguration());
+ }
+
+ @Test
+ public void testDifferentKeySameInstanceNotACircleReference() {
+ Map map1 = new HashMap<>();
+ Map map2 = new HashMap<>();
+
+ map1.put("test1", map2);
+ map1.put("test2", map2);
+
+ new JSONObject(map1);
+ }
+
+ @Test
+ public void clarifyCurrentBehavior() {
+ // Behavior documented in #653 optLong vs getLong inconsistencies
+ // This problem still exists.
+ // Internally, both number_1 and number_2 are stored as strings. This is reasonable since they are parsed as strings.
+ // However, getLong and optLong should return similar results
+ JSONObject json = new JSONObject("{\"number_1\":\"01234\", \"number_2\": \"332211\"}");
+ assertEquals(json.getLong("number_1"), 1234L);
+ assertEquals(json.optLong("number_1"), 0); //THIS VALUE IS NOT RETURNED AS A NUMBER
+ assertEquals(json.getLong("number_2"), 332211L);
+ assertEquals(json.optLong("number_2"), 332211L);
+
+ // Behavior documented in #826 JSONObject parsing 0-led numeric strings as ints
+ // After reverting the code, personId is stored as a string, and the behavior is as expected
+ String personId = "\"0123\"";
+ JSONObject j1 = new JSONObject("{\"personId\": " + personId + "}");
+ assertEquals(j1.getString("personId"), "0123");
+
+ // Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number.
+ // This example was mentioned in the same ticket
+ // After reverting the code, personId is stored as a string, and the behavior is as expected
+ JSONObject j2 = new JSONObject("{\"personId\":\"0123\"}");
+ assertEquals(j2.getString("personId"), "0123");
+
+ // Behavior uncovered while working on the code
+ // All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect
+ JSONObject j3 = new JSONObject("{ " +
+ "\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", " +
+ "\"hex4\": 00e0, \"hex5\": \"00f0\", \"hex6\": \"0011\" }");
+ assertEquals(j3.getString("hex1"), "010e4");
+ assertEquals(j3.getString("hex2"), "00f0");
+ assertEquals(j3.getString("hex3"), "0011");
+ assertEquals(j3.getLong("hex4"), 0, .1);
+ assertEquals(j3.getString("hex5"), "00f0");
+ assertEquals(j3.getString("hex6"), "0011");
+ }
+
+
+ @Test
+ public void testStrictModeJSONTokener_expectException(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
+ JSONTokener tokener = new JSONTokener("{\"key\":\"value\"}invalidCharacters", jsonParserConfiguration);
+
+ assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
+ }
+
+ @Test
+ public void test_strictModeWithMisCasedBooleanOrNullValue(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
+ try{
+ new JSONObject("{\"a\":True}", jsonParserConfiguration);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ // No action, expected outcome
+ }
+ try{
+ new JSONObject("{\"a\":TRUE}", jsonParserConfiguration);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ // No action, expected outcome
+ }
+ try{
+ new JSONObject("{\"a\":nUlL}", jsonParserConfiguration);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ // No action, expected outcome
+ }
+ }
+
+ @Test
+ public void test_strictModeWithInappropriateKey(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
+
+ // Parsing the following objects should fail
+ try{
+ new JSONObject("{true : 3}", jsonParserConfiguration);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ // No action, expected outcome
+ }
+ try{
+ new JSONObject("{TRUE : 3}", jsonParserConfiguration);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ // No action, expected outcome
+ }
+ try{
+ new JSONObject("{1 : 3}", jsonParserConfiguration);
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ // No action, expected outcome
+ }
+
+ }
+
+
+ /**
+ * Method to build nested map of max maxDepth
+ *
+ * @param maxDepth
+ * @return
+ */
+ public static HashMap buildNestedMap(int maxDepth) {
+ if (maxDepth <= 0) {
+ return new HashMap<>();
+ }
+ HashMap nestedMap = new HashMap<>();
+ nestedMap.put("t", buildNestedMap(maxDepth - 1));
+ return nestedMap;
+ }
+
+
+ /**
+ * Tests the behavior of the {@link JSONObject} when parsing a bean with null fields
+ * using a custom {@link JSONParserConfiguration} that enables the use of native nulls.
+ *
+ * This test ensures that uninitialized fields in the bean are serialized correctly
+ * into the resulting JSON object, and their keys are present in the JSON string output.
+ */
+ @Test
+ public void jsonObjectParseNullFieldsWithParserConfiguration() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
+ RecursiveBean bean = new RecursiveBean(null);
+ JSONObject jsonObject = new JSONObject(bean, jsonParserConfiguration.withUseNativeNulls(true));
+ assertTrue("name key should be present", jsonObject.has("name"));
+ assertTrue("ref key should be present", jsonObject.has("ref"));
+ assertTrue("ref2 key should be present", jsonObject.has("ref2"));
+ }
+
+ /**
+ * Tests the behavior of the {@link JSONObject} when parsing a bean with null fields
+ * without using a custom {@link JSONParserConfiguration}.
+ *
+ * This test ensures that uninitialized fields in the bean are not serialized
+ * into the resulting JSON object, and the object remains empty.
+ */
+ @Test
+ public void jsonObjectParseNullFieldsWithoutParserConfiguration() {
+ RecursiveBean bean = new RecursiveBean(null);
+ JSONObject jsonObject = new JSONObject(bean);
+ assertTrue("JSONObject should be empty", jsonObject.isEmpty());
+ }
+
+}
diff --git a/src/test/java/org/json/junit/JSONParserConfigurationTest.java b/src/test/java/org/json/junit/JSONParserConfigurationTest.java
new file mode 100644
index 000000000..926c49f41
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONParserConfigurationTest.java
@@ -0,0 +1,624 @@
+package org.json.junit;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONParserConfiguration;
+import org.json.JSONTokener;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class JSONParserConfigurationTest {
+ private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
+
+ @Test(expected = JSONException.class)
+ public void testThrowException() {
+ new JSONObject(TEST_SOURCE);
+ }
+
+ @Test
+ public void testOverwrite() {
+ JSONObject jsonObject = new JSONObject(TEST_SOURCE,
+ new JSONParserConfiguration().withOverwriteDuplicateKey(true));
+
+ assertEquals("duplicate key should be overwritten", "value2", jsonObject.getString("key"));
+ }
+
+ @Test
+ public void strictModeIsCloned(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true)
+ .withMaxNestingDepth(12);
+
+ assertTrue(jsonParserConfiguration.isStrictMode());
+ }
+
+ @Test
+ public void maxNestingDepthIsCloned(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withKeepStrings(true)
+ .withStrictMode(true);
+
+ assertTrue(jsonParserConfiguration.isKeepStrings());
+ }
+
+ @Test
+ public void useNativeNullsIsCloned() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withUseNativeNulls(true)
+ .withStrictMode(true);
+ assertTrue(jsonParserConfiguration.isUseNativeNulls());
+ }
+
+ @Test
+ public void verifyDuplicateKeyThenMaxDepth() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withOverwriteDuplicateKey(true)
+ .withMaxNestingDepth(42);
+
+ assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
+ assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
+ }
+
+ @Test
+ public void verifyMaxDepthThenDuplicateKey() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withMaxNestingDepth(42)
+ .withOverwriteDuplicateKey(true);
+
+ assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
+ assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
+ }
+
+ @Test
+ public void givenInvalidInput_testStrictModeTrue_shouldThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ List strictModeInputTestCases = getNonCompliantJSONArrayList();
+ // this is a lot easier to debug when things stop working
+ for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
+ String testCase = strictModeInputTestCases.get(i);
+ try {
+ JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
+ String s = jsonArray.toString();
+ String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
+ fail(msg);
+ } catch (Exception e) {
+ // its all good
+ }
+ }
+ }
+
+ @Test
+ public void givenInvalidInputObjects_testStrictModeTrue_shouldThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ List strictModeInputTestCases = getNonCompliantJSONObjectList();
+ // this is a lot easier to debug when things stop working
+ for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
+ String testCase = strictModeInputTestCases.get(i);
+ try {
+ JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
+ String s = jsonObject.toString();
+ String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
+ fail(msg);
+ } catch (Exception e) {
+ // its all good
+ }
+ }
+ }
+
+ @Test
+ public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "[]";
+ JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
+ assertEquals(testCase, jsonArray.toString());
+ }
+
+ @Test
+ public void givenEmptyObject_testStrictModeTrue_shouldNotThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "{}";
+ JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
+ assertEquals(testCase, jsonObject.toString());
+ }
+
+ @Test
+ public void givenValidNestedArray_testStrictModeTrue_shouldNotThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+
+ String testCase = "[[\"c\"], [10.2], [true, false, true]]";
+
+ JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
+ JSONArray arrayShouldContainStringAt0 = jsonArray.getJSONArray(0);
+ JSONArray arrayShouldContainNumberAt0 = jsonArray.getJSONArray(1);
+ JSONArray arrayShouldContainBooleanAt0 = jsonArray.getJSONArray(2);
+
+ assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
+ assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
+ assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
+ }
+
+ @Test
+ public void givenValidNestedObject_testStrictModeTrue_shouldNotThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+
+ String testCase = "{\"a0\":[\"c\"], \"a1\":[10.2], \"a2\":[true, false, true]}";
+
+ JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
+ JSONArray arrayShouldContainStringAt0 = jsonObject.getJSONArray("a0");
+ JSONArray arrayShouldContainNumberAt0 = jsonObject.getJSONArray("a1");
+ JSONArray arrayShouldContainBooleanAt0 = jsonObject.getJSONArray("a2");
+
+ assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
+ assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
+ assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
+ }
+
+ @Test
+ public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJsonException(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "[[]]";
+ JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
+ assertEquals(testCase, jsonArray.toString());
+ }
+
+ @Test
+ public void givenValidEmptyArrayInsideObject_testStrictModeTrue_shouldNotThrowJsonException(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "{\"a0\":[]}";
+ JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
+ assertEquals(testCase, jsonObject.toString());
+ }
+
+ @Test
+ public void givenValidEmptyArrayInsideArray_testStrictModeFalse_shouldNotThrowJsonException(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+ String testCase = "[[]]";
+ JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
+ assertEquals(testCase, jsonArray.toString());
+ }
+
+ @Test
+ public void givenValidEmptyArrayInsideObject_testStrictModeFalse_shouldNotThrowJsonException(){
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+ String testCase = "{\"a0\":[]}";
+ JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
+ assertEquals(testCase, jsonObject.toString());
+ }
+
+ @Test
+ public void givenInvalidStringArray_testStrictModeTrue_shouldThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "[badString]";
+ JSONException je = assertThrows(JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Value 'badString' is not surrounded by quotes at 10 [character 11 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidStringObject_testStrictModeTrue_shouldThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "{\"a0\":badString}";
+ JSONException je = assertThrows(JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Value 'badString' is not surrounded by quotes at 15 [character 16 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void allowNullArrayInStrictMode() {
+ String expected = "[null]";
+ JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
+ assertEquals(expected, jsonArray.toString());
+ }
+
+ @Test
+ public void allowNullObjectInStrictMode() {
+ String expected = "{\"a0\":null}";
+ JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
+ assertEquals(expected, jsonObject.toString());
+ }
+
+ @Test
+ public void shouldHandleNumericArray() {
+ String expected = "[10]";
+ JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
+ assertEquals(expected, jsonArray.toString());
+ }
+
+ @Test
+ public void shouldHandleNumericObject() {
+ String expected = "{\"a0\":10}";
+ JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
+ assertEquals(expected, jsonObject.toString());
+ }
+ @Test
+ public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
+ try (Stream lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
+ String compliantJsonArrayAsString = lines.collect(Collectors.joining());
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration);
+ }
+ }
+
+ @Test
+ public void givenCompliantJSONObjectFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
+ try (Stream lines = Files.lines(Paths.get("src/test/resources/compliantJsonObject.json"))) {
+ String compliantJsonObjectAsString = lines.collect(Collectors.joining());
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+
+ new JSONObject(compliantJsonObjectAsString, jsonParserConfiguration);
+ }
+ }
+
+ @Test
+ public void givenInvalidInputArrays_testStrictModeFalse_shouldNotThrowAnyException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+
+ List strictModeInputTestCases = getNonCompliantJSONArrayList();
+
+ // this is a lot easier to debug when things stop working
+ for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
+ String testCase = strictModeInputTestCases.get(i);
+ try {
+ JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
+ } catch (Exception e) {
+ System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
+ fail(String.format("Noncompliant array index: %d", i));
+ }
+ }
+ }
+
+ @Test
+ public void givenInvalidInputObjects_testStrictModeFalse_shouldNotThrowAnyException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+
+ List strictModeInputTestCases = getNonCompliantJSONObjectList();
+
+ // this is a lot easier to debug when things stop working
+ for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
+ String testCase = strictModeInputTestCases.get(i);
+ try {
+ JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
+ } catch (Exception e) {
+ System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
+ fail(String.format("Noncompliant array index: %d", i));
+ }
+ }
+ }
+
+ @Test
+ public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "[1,2];[3,4]";
+ JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
+ JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Unparsed characters found at end of input text at 6 [character 7 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "{\"a0\":[1,2];\"a1\":[3,4]}";
+ JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
+ JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Invalid character ';' found at 12 [character 13 line 1]", je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "[\"1\",\"2\"];[3,4]";
+ JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
+ JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Unparsed characters found at end of input text at 10 [character 11 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObjectWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "{\"a0\":[\"1\",\"2\"];\"a1\":[3,4]}";
+ JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
+ JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Invalid character ';' found at 16 [character 17 line 1]", je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "[{\"test\": implied}]";
+ JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
+ JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Value 'implied' is not surrounded by quotes at 17 [character 18 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+ String testCase = "{\"a0\":{\"test\": implied}]}";
+ JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
+ JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
+ assertEquals("Strict mode error: Value 'implied' is not surrounded by quotes at 22 [character 23 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+ String testCase = "[{\"test\": implied}]";
+ new JSONArray(testCase, jsonParserConfiguration);
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeFalse_shouldNotThrowAnyException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+ String testCase = "{\"a0\":{\"test\": implied}}";
+ new JSONObject(testCase, jsonParserConfiguration);
+ }
+
+ @Test
+ public void givenNonCompliantQuotesArray_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+
+ String testCaseOne = "[\"abc', \"test\"]";
+ String testCaseTwo = "['abc\", \"test\"]";
+ String testCaseThree = "['abc']";
+ String testCaseFour = "[{'testField': \"testValue\"}]";
+
+ JSONException jeOne = assertThrows(JSONException.class,
+ () -> new JSONArray(testCaseOne, jsonParserConfiguration));
+ JSONException jeTwo = assertThrows(JSONException.class,
+ () -> new JSONArray(testCaseTwo, jsonParserConfiguration));
+ JSONException jeThree = assertThrows(JSONException.class,
+ () -> new JSONArray(testCaseThree, jsonParserConfiguration));
+ JSONException jeFour = assertThrows(JSONException.class,
+ () -> new JSONArray(testCaseFour, jsonParserConfiguration));
+
+ assertEquals(
+ "Expected a ',' or ']' at 10 [character 11 line 1]",
+ jeOne.getMessage());
+ assertEquals(
+ "Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
+ jeTwo.getMessage());
+ assertEquals(
+ "Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
+ jeThree.getMessage());
+ assertEquals(
+ "Strict mode error: Single quoted strings are not allowed at 3 [character 4 line 1]",
+ jeFour.getMessage());
+ }
+
+ @Test
+ public void givenNonCompliantQuotesObject_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+
+ String testCaseOne = "{\"abc': \"test\"}";
+ String testCaseTwo = "{'abc\": \"test\"}";
+ String testCaseThree = "{\"a\":'abc'}";
+ String testCaseFour = "{'testField': \"testValue\"}";
+
+ JSONException jeOne = assertThrows(JSONException.class,
+ () -> new JSONObject(testCaseOne, jsonParserConfiguration));
+ JSONException jeTwo = assertThrows(JSONException.class,
+ () -> new JSONObject(testCaseTwo, jsonParserConfiguration));
+ JSONException jeThree = assertThrows(JSONException.class,
+ () -> new JSONObject(testCaseThree, jsonParserConfiguration));
+ JSONException jeFour = assertThrows(JSONException.class,
+ () -> new JSONObject(testCaseFour, jsonParserConfiguration));
+
+ assertEquals(
+ "Expected a ':' after a key at 10 [character 11 line 1]",
+ jeOne.getMessage());
+ assertEquals(
+ "Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
+ jeTwo.getMessage());
+ assertEquals(
+ "Strict mode error: Single quoted strings are not allowed at 6 [character 7 line 1]",
+ jeThree.getMessage());
+ assertEquals(
+ "Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
+ jeFour.getMessage());
+ }
+
+ @Test
+ public void givenUnbalancedQuotesArray_testStrictModeFalse_shouldThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+
+ String testCaseOne = "[\"abc', \"test\"]";
+ String testCaseTwo = "['abc\", \"test\"]";
+
+ JSONException jeOne = assertThrows(JSONException.class,
+ () -> new JSONArray(testCaseOne, jsonParserConfiguration));
+ JSONException jeTwo = assertThrows(JSONException.class,
+ () -> new JSONArray(testCaseTwo, jsonParserConfiguration));
+
+ assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage());
+ assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
+ }
+
+ @Test
+ public void givenUnbalancedQuotesObject_testStrictModeFalse_shouldThrowJsonException() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(false);
+
+ String testCaseOne = "{\"abc': \"test\"}";
+ String testCaseTwo = "{'abc\": \"test\"}";
+
+ JSONException jeOne = assertThrows(JSONException.class,
+ () -> new JSONObject(testCaseOne, jsonParserConfiguration));
+ JSONException jeTwo = assertThrows(JSONException.class,
+ () -> new JSONObject(testCaseTwo, jsonParserConfiguration));
+
+ assertEquals("Expected a ':' after a key at 10 [character 11 line 1]", jeOne.getMessage());
+ assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+
+ String testCase = "[{test: implied}]";
+ JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
+ JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
+
+ assertEquals("Strict mode error: Value 'test' is not surrounded by quotes at 6 [character 7 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
+ .withStrictMode(true);
+
+ String testCase = "{test: implied}";
+ JSONException je = assertThrows("expected non-compliant json but got instead: " + testCase,
+ JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
+
+ assertEquals("Strict mode error: Value 'test' is not surrounded by quotes at 5 [character 6 line 1]",
+ je.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingJSONTokener_shouldThrowJSONException() {
+ JSONException exception = assertThrows(JSONException.class, () -> {
+ new JSONObject(new JSONTokener("{\"key\":\"value\"} invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
+ });
+
+ assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeTrue_JSONObjectUsingString_shouldThrowJSONException() {
+ JSONException exception = assertThrows(JSONException.class, () -> {
+ new JSONObject("{\"key\":\"value\"} invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
+ });
+ assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]", exception.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingJSONTokener_shouldThrowJSONException() {
+ JSONException exception = assertThrows(JSONException.class, () -> {
+ new JSONArray(new JSONTokener("[\"value\"] invalid trailing text"), new JSONParserConfiguration().withStrictMode(true));
+ });
+
+ assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
+ }
+
+ @Test
+ public void givenInvalidInputObject_testStrictModeTrue_JSONArrayUsingString_shouldThrowJSONException() {
+ JSONException exception = assertThrows(JSONException.class, () -> {
+ new JSONArray("[\"value\"] invalid trailing text", new JSONParserConfiguration().withStrictMode(true));
+ });
+ assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]", exception.getMessage());
+ }
+
+ /**
+ * This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
+ * this class.
+ *
+ * @return List with JSON strings.
+ */
+ private List getNonCompliantJSONArrayList() {
+ return Arrays.asList(
+ "[1],",
+ "[1,]",
+ "[,]",
+ "[,,]",
+ "[[1],\"sa\",[2]]a",
+ "[1],\"dsa\": \"test\"",
+ "[[a]]",
+ "[]asdf",
+ "[]]",
+ "[]}",
+ "[][",
+ "[]{",
+ "[],",
+ "[]:",
+ "[],[",
+ "[],{",
+ "[1,2];[3,4]",
+ "[test]",
+ "[{'testSingleQuote': 'testSingleQuote'}]",
+ "[1, 2,3]:[4,5]",
+ "[{test: implied}]",
+ "[{\"test\": implied}]",
+ "[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]",
+ "[{test: \"implied\"}]");
+ }
+
+ /**
+ * This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
+ * this class.
+ *
+ * @return List with JSON strings.
+ */
+ private List getNonCompliantJSONObjectList() {
+ return Arrays.asList(
+ "{\"a\":1},",
+ "{\"a\":1,}",
+ "{\"a0\":[1],\"a1\":\"sa\",\"a2\":[2]}a",
+ "{\"a\":1},\"dsa\": \"test\"",
+ "{\"a\":[a]}",
+ "{}asdf",
+ "{}}",
+ "{}]",
+ "{}{",
+ "{}[",
+ "{},",
+ "{}:",
+ "{},{",
+ "{},[",
+ "{\"a0\":[1,2];\"a1\":[3,4]}",
+ "{\"a\":test}",
+ "{a:{'testSingleQuote': 'testSingleQuote'}}",
+ "{\"a0\":1, \"a1\":2,\"a2\":3}:{\"a3\":4,\"a4\":5}",
+ "{\"a\":{test: implied}}",
+ "{a:{\"test\": implied}}",
+ "{a:[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]}",
+ "{a:{test: \"implied\"}}"
+ );
+ }
+
+}
diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java
new file mode 100644
index 000000000..a420b297f
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONPointerTest.java
@@ -0,0 +1,398 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.InputStream;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.json.JSONPointer;
+import org.json.JSONPointerException;
+import org.json.JSONTokener;
+import org.junit.Test;
+
+public class JSONPointerTest {
+
+ private static final JSONObject document;
+ private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," +
+ "\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
+ "\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," +
+ "\"m~n\":8}";
+
+
+ static {
+ @SuppressWarnings("resource")
+ InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json");
+ if(resourceAsStream == null) {
+ throw new ExceptionInInitializerError("Unable to locate test file. Please check your development environment configuration");
+ }
+ document = new JSONObject(new JSONTokener(resourceAsStream));
+ }
+
+ private Object query(String pointer) {
+ return new JSONPointer(pointer).queryFrom(document);
+ }
+
+ @Test
+ public void emptyPointer() {
+ assertTrue(new JSONObject(EXPECTED_COMPLETE_DOCUMENT).similar(query("")));
+ }
+
+ @SuppressWarnings("unused")
+ @Test(expected = NullPointerException.class)
+ public void nullPointer() {
+ new JSONPointer((String) null);
+ }
+
+ @Test
+ public void objectPropertyQuery() {
+ assertEquals("[\"bar\",\"baz\"]", query("/foo").toString());
+ }
+
+ @Test
+ public void arrayIndexQuery() {
+ assertEquals("bar", query("/foo/0"));
+ }
+
+ @Test(expected = JSONPointerException.class)
+ public void stringPropOfArrayFailure() {
+ query("/foo/bar");
+ }
+
+ @Test
+ public void queryByEmptyKey() {
+ assertEquals(0, query("/"));
+ }
+
+ @Test
+ public void queryByEmptyKeySubObject() {
+ JSONObject json = new JSONObject("{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some" +
+ " other value\"}");
+ JSONObject obj = (JSONObject) query("/obj/");
+ assertTrue(json.similar(obj));
+ }
+
+ @Test
+ public void queryByEmptyKeySubObjectSubOject() {
+ assertEquals("empty key of an object with an empty key", query("/obj//"));
+ }
+
+ @Test
+ public void queryByEmptyKeySubObjectValue() {
+ assertEquals("Some other value", query("/obj//subKey"));
+ }
+
+ @Test
+ public void slashEscaping() {
+ assertEquals(1, query("/a~1b"));
+ }
+
+ @Test
+ public void tildeEscaping() {
+ assertEquals(8, query("/m~0n"));
+ }
+
+ /**
+ * We pass backslashes as-is
+ *
+ * @see rfc6901 section 3
+ */
+ @Test
+ public void backslashHandling() {
+ assertEquals(5, query("/i\\j"));
+ }
+
+ /**
+ * We pass quotations as-is
+ *
+ * @see rfc6901 section 3
+ */
+ @Test
+ public void quotationHandling() {
+ assertEquals(6, query("/k\"l"));
+ }
+
+ @Test
+ public void whitespaceKey() {
+ assertEquals(7, query("/ "));
+ }
+
+ @Test
+ public void uriFragmentNotation() {
+ assertEquals("[\"bar\",\"baz\"]", query("#/foo").toString());
+ }
+
+ @Test
+ public void uriFragmentNotationRoot() {
+ assertTrue(new JSONObject(EXPECTED_COMPLETE_DOCUMENT).similar(query("#")));
+ }
+
+ @Test
+ public void uriFragmentPercentHandling() {
+ assertEquals(2, query("#/c%25d"));
+ assertEquals(3, query("#/e%5Ef"));
+ assertEquals(4, query("#/g%7Ch"));
+ assertEquals(8, query("#/m~0n"));
+ }
+
+ @SuppressWarnings("unused")
+ @Test(expected = IllegalArgumentException.class)
+ public void syntaxError() {
+ new JSONPointer("key");
+ }
+
+ @Test(expected = JSONPointerException.class)
+ public void arrayIndexFailure() {
+ query("/foo/2");
+ }
+
+ @Test(expected = JSONPointerException.class)
+ public void primitiveFailure() {
+ query("/obj/key/failure");
+ }
+
+ @Test
+ public void builderTest() {
+ JSONPointer pointer = JSONPointer.builder()
+ .append("obj")
+ .append("other~key").append("another/key")
+ .append(0)
+ .build();
+ assertEquals("val", pointer.queryFrom(document));
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void nullToken() {
+ JSONPointer.builder().append(null);
+ }
+
+ @Test
+ public void toStringEscaping() {
+ JSONPointer pointer = JSONPointer.builder()
+ .append("obj")
+ .append("other~key").append("another/key")
+ .append("\"")
+ .append(0)
+ .build();
+ assertEquals("/obj/other~0key/another~1key/\"/0", pointer.toString());
+ }
+
+ @Test
+ public void emptyPointerToString() {
+ assertEquals("", new JSONPointer("").toString());
+ }
+
+ @Test
+ public void toURIFragment() {
+ assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment());
+ assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment());
+ assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment());
+ assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment());
+ }
+
+ @Test
+ public void tokenListIsCopiedInConstructor() {
+ JSONPointer.Builder b = JSONPointer.builder().append("key1");
+ JSONPointer jp1 = b.build();
+ b.append("key2");
+ JSONPointer jp2 = b.build();
+ if(jp1.toString().equals(jp2.toString())) {
+ fail("Oops, my pointers are sharing a backing array");
+ }
+ }
+
+ /**
+ * Coverage for JSONObject query(String)
+ */
+ @Test
+ public void queryFromJSONObject() {
+ String str = "{"+
+ "\"stringKey\":\"hello world!\","+
+ "\"arrayKey\":[0,1,2],"+
+ "\"objectKey\": {"+
+ "\"a\":\"aVal\","+
+ "\"b\":\"bVal\""+
+ "}"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ Object obj = jsonObject.query("/stringKey");
+ assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
+ obj = jsonObject.query("/arrayKey/1");
+ assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
+ obj = jsonObject.query("/objectKey/b");
+ assertTrue("Expected bVal", "bVal".equals(obj));
+ try {
+ obj = jsonObject.query("/a/b/c");
+ assertTrue("Expected JSONPointerException", false);
+ } catch (JSONPointerException e) {
+ assertTrue("Expected bad key/value exception",
+ "value [null] is not an array or object therefore its key b cannot be resolved".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Coverage for JSONObject query(JSONPointer)
+ */
+ @Test
+ public void queryFromJSONObjectUsingPointer() {
+ String str = "{"+
+ "\"stringKey\":\"hello world!\","+
+ "\"arrayKey\":[0,1,2],"+
+ "\"objectKey\": {"+
+ "\"a\":\"aVal\","+
+ "\"b\":\"bVal\""+
+ "}"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ Object obj = jsonObject.query(new JSONPointer("/stringKey"));
+ assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
+ obj = jsonObject.query(new JSONPointer("/arrayKey/1"));
+ assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
+ obj = jsonObject.query(new JSONPointer("/objectKey/b"));
+ assertTrue("Expected bVal", "bVal".equals(obj));
+ try {
+ obj = jsonObject.query(new JSONPointer("/a/b/c"));
+ assertTrue("Expected JSONPointerException", false);
+ } catch (JSONPointerException e) {
+ assertTrue("Expected bad key/value exception",
+ "value [null] is not an array or object therefore its key b cannot be resolved".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Coverage for JSONObject optQuery(JSONPointer)
+ */
+ @Test
+ public void optQueryFromJSONObjectUsingPointer() {
+ String str = "{"+
+ "\"stringKey\":\"hello world!\","+
+ "\"arrayKey\":[0,1,2],"+
+ "\"objectKey\": {"+
+ "\"a\":\"aVal\","+
+ "\"b\":\"bVal\""+
+ "}"+
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ Object obj = jsonObject.optQuery(new JSONPointer("/stringKey"));
+ assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
+ obj = jsonObject.optQuery(new JSONPointer("/arrayKey/1"));
+ assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
+ obj = jsonObject.optQuery(new JSONPointer("/objectKey/b"));
+ assertTrue("Expected bVal", "bVal".equals(obj));
+ obj = jsonObject.optQuery(new JSONPointer("/a/b/c"));
+ assertTrue("Expected null", obj == null);
+ }
+
+ /**
+ * Coverage for JSONArray query(String)
+ */
+ @Test
+ public void queryFromJSONArray() {
+ String str = "["+
+ "\"hello world!\","+
+ "[0,1,2],"+
+ "{"+
+ "\"a\":\"aVal\","+
+ "\"b\":\"bVal\""+
+ "}"+
+ "]";
+ JSONArray jsonArray = new JSONArray(str);
+ Object obj = jsonArray.query("/0");
+ assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
+ obj = jsonArray.query("/1/1");
+ assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
+ obj = jsonArray.query("/2/b");
+ assertTrue("Expected bVal", "bVal".equals(obj));
+ try {
+ obj = jsonArray.query("/a/b/c");
+ assertTrue("Expected JSONPointerException", false);
+ } catch (JSONPointerException e) {
+ assertTrue("Expected bad index exception",
+ "a is not an array index".equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Coverage for JSONArray query(JSONPointer)
+ */
+ @Test
+ public void queryFromJSONArrayUsingPointer() {
+ String str = "["+
+ "\"hello world!\","+
+ "[0,1,2],"+
+ "{"+
+ "\"a\":\"aVal\","+
+ "\"b\":\"bVal\""+
+ "}"+
+ "]";
+ JSONArray jsonArray = new JSONArray(str);
+ Object obj = jsonArray.query(new JSONPointer("/0"));
+ assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
+ obj = jsonArray.query(new JSONPointer("/1/1"));
+ assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
+ obj = jsonArray.query(new JSONPointer("/2/b"));
+ assertTrue("Expected bVal", "bVal".equals(obj));
+ try {
+ obj = jsonArray.query(new JSONPointer("/a/b/c"));
+ assertTrue("Expected JSONPointerException", false);
+ } catch (JSONPointerException e) {
+ assertTrue("Expected bad index exception",
+ "a is not an array index".equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Coverage for JSONArray optQuery(JSONPointer)
+ */
+ @Test
+ public void optQueryFromJSONArrayUsingPointer() {
+ String str = "["+
+ "\"hello world!\","+
+ "[0,1,2],"+
+ "{"+
+ "\"a\":\"aVal\","+
+ "\"b\":\"bVal\""+
+ "}"+
+ "]";
+ JSONArray jsonArray = new JSONArray(str);
+ Object obj = jsonArray.optQuery(new JSONPointer("/0"));
+ assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
+ obj = jsonArray.optQuery(new JSONPointer("/1/1"));
+ assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
+ obj = jsonArray.optQuery(new JSONPointer("/2/b"));
+ assertTrue("Expected bVal", "bVal".equals(obj));
+ obj = jsonArray.optQuery(new JSONPointer("/a/b/c"));
+ assertTrue("Expected null", obj == null);
+ }
+
+ /**
+ * When creating a jsonObject we need to parse escaped characters "\\\\"
+ * --> it's the string representation of "\\", so when query'ing via the JSONPointer
+ * we DON'T escape them
+ *
+ */
+ @Test
+ public void queryFromJSONObjectUsingPointer0() {
+ String str = "{"+
+ "\"string\\\\\\\\Key\":\"hello world!\","+
+
+ "\"\\\\\":\"slash test\"" +
+ "}";
+ JSONObject jsonObject = new JSONObject(str);
+ //Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
+ // as "\\" which means when querying, we need to use "\\"
+ Object twoBackslahObj = jsonObject.optQuery(new JSONPointer("/\\"));
+ assertEquals("slash test", twoBackslahObj);
+
+ Object fourBackslashObj = jsonObject.optQuery(new JSONPointer("/string\\\\Key"));
+ assertEquals("hello world!", fourBackslashObj);
+ }
+}
diff --git a/src/test/java/org/json/junit/JSONStringTest.java b/src/test/java/org/json/junit/JSONStringTest.java
new file mode 100644
index 000000000..235df1806
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONStringTest.java
@@ -0,0 +1,401 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+
+import java.io.StringWriter;
+import java.util.*;
+
+import org.json.*;
+import org.junit.Test;
+
+/**
+ * Tests for JSONString implementations, and the difference between
+ * {@link JSONObject#valueToString} and {@link JSONObject#writeValue}.
+ */
+public class JSONStringTest {
+
+ /**
+ * This tests the JSONObject.writeValue() method. We can't test directly
+ * due to it being a package-protected method. Instead, we can call
+ * JSONArray.write(), which delegates the writing of each entry to
+ * writeValue().
+ */
+ @Test
+ public void writeValues() throws Exception {
+ JSONArray jsonArray = new JSONArray();
+ jsonArray.put((Object)null);
+
+ StringWriter writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[null]".equals(output));
+
+ jsonArray = new JSONArray();
+ jsonArray.put(JSONObject.NULL);
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[null]".equals(output));
+
+ jsonArray = new JSONArray();
+ jsonArray.put(new JSONObject());
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[{}]".equals(output));
+
+ jsonArray = new JSONArray();
+ jsonArray.put(new JSONArray());
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[[]]".equals(output));
+
+ jsonArray = new JSONArray();
+ Map,?> singleMap = Collections.singletonMap("key1", "value1");
+ jsonArray.put((Object)singleMap);
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output));
+
+ jsonArray = new JSONArray();
+ List> singleList = Collections.singletonList("entry1");
+ jsonArray.put((Object)singleList);
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output));
+
+ jsonArray = new JSONArray();
+ int[] intArray = new int[] { 1, 2, 3 };
+ jsonArray.put(intArray);
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
+
+ jsonArray = new JSONArray();
+ jsonArray.put(24);
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[24]".equals(output));
+
+ jsonArray = new JSONArray();
+ jsonArray.put("string value");
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[\"string value\"]".equals(output));
+
+ jsonArray = new JSONArray();
+ jsonArray.put(true);
+ } finally {
+ writer.close();
+ }
+
+ writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[true]".equals(output));
+ } finally {
+ writer.close();
+ }
+
+ }
+
+ /**
+ * This tests the JSONObject.valueToString() method. These should be
+ * identical to the values above, except for the enclosing [ and ].
+ */
+ @SuppressWarnings("boxing")
+ @Test
+ public void valuesToString() throws Exception {
+
+ String output = JSONObject.valueToString(null);
+ assertTrue("String values should be equal", "null".equals(output));
+
+ output = JSONObject.valueToString(JSONObject.NULL);
+ assertTrue("String values should be equal", "null".equals(output));
+
+ output = JSONObject.valueToString(new JSONObject());
+ assertTrue("String values should be equal", "{}".equals(output));
+
+ output = JSONObject.valueToString(new JSONArray());
+ assertTrue("String values should be equal", "[]".equals(output));
+
+ Map,?> singleMap = Collections.singletonMap("key1", "value1");
+ output = JSONObject.valueToString(singleMap);
+ assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output));
+
+ List> singleList = Collections.singletonList("entry1");
+ output = JSONObject.valueToString(singleList);
+ assertTrue("String values should be equal", "[\"entry1\"]".equals(output));
+
+ int[] intArray = new int[] { 1, 2, 3 };
+ output = JSONObject.valueToString(intArray);
+ assertTrue("String values should be equal", "[1,2,3]".equals(output));
+
+ output = JSONObject.valueToString(24);
+ assertTrue("String values should be equal", "24".equals(output));
+
+ output = JSONObject.valueToString("string value");
+ assertTrue("String values should be equal", "\"string value\"".equals(output));
+
+ output = JSONObject.valueToString(true);
+ assertTrue("String values should be equal", "true".equals(output));
+
+ }
+
+ /**
+ * Test what happens when toJSONString() returns a well-formed JSON value.
+ * This is the usual case.
+ */
+ @Test
+ public void testJSONStringValue() throws Exception {
+ JSONStringValue jsonString = new JSONStringValue();
+ JSONArray jsonArray = new JSONArray();
+
+ jsonArray.put(jsonString);
+
+ StringWriter writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output));
+
+ output = JSONObject.valueToString(jsonString);
+ assertTrue("String values should be equal", "\"the JSON string value\"".equals(output));
+ } finally {
+ writer.close();
+ }
+ }
+
+ /**
+ * Test what happens when toJSONString() returns null. In one case,
+ * use the object's toString() method. In the other, throw a JSONException.
+ */
+ @Test
+ public void testJSONNullStringValue() throws Exception {
+ JSONNullStringValue jsonString = new JSONNullStringValue();
+ JSONArray jsonArray = new JSONArray();
+
+ jsonArray.put(jsonString);
+
+ StringWriter writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[\"the toString value\"]".equals(output));
+
+ // The only different between writeValue() and valueToString():
+ // in this case, valueToString throws a JSONException
+ try {
+ output = JSONObject.valueToString(jsonString);
+ fail("Expected an exception, got a String value");
+ } catch (Exception e) {
+ assertTrue("Expected JSONException", e instanceof JSONException);
+ assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage()));
+ }
+ } finally {
+ writer.close();
+ }
+ }
+
+ /**
+ * Test what happens when toJSONString() returns an exception. In both
+ * cases, a JSONException is thrown, with the cause and message set from
+ * the original exception.
+ */
+ @Test
+ public void testJSONStringExceptionValue() {
+ JSONStringExceptionValue jsonString = new JSONStringExceptionValue();
+ JSONArray jsonArray = new JSONArray();
+
+ jsonArray.put(jsonString);
+
+ StringWriter writer = new StringWriter();
+ try {
+ jsonArray.write(writer).toString();
+ fail("Expected an exception, got a String value");
+ } catch (JSONException e) {
+ assertEquals("Unable to write JSONArray value at index: 0", e.getMessage());
+ } catch(Exception e) {
+ fail("Expected JSONException");
+ } finally {
+ try {
+ writer.close();
+ } catch (Exception e){}
+ }
+
+ try {
+ JSONObject.valueToString(jsonString);
+ fail("Expected an exception, got a String value");
+ } catch (JSONException e) {
+ assertTrue("Exception message does not match", "the exception value".equals(e.getMessage()));
+ } catch(Exception e) {
+ fail("Expected JSONException");
+ }
+ }
+
+ /**
+ * Test what happens when a Java object's toString() returns a String value.
+ * This is the usual case.
+ */
+ @Test
+ public void testStringValue() throws Exception {
+ StringValue nonJsonString = new StringValue();
+ JSONArray jsonArray = new JSONArray();
+
+ jsonArray.put(nonJsonString);
+
+ StringWriter writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output));
+
+ output = JSONObject.valueToString(nonJsonString);
+ assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output));
+ } finally {
+ writer.close();
+ }
+ }
+
+ /**
+ * Test what happens when a Java object's toString() returns null.
+ * Defaults to empty string.
+ */
+ @Test
+ public void testNullStringValue() throws Exception {
+ NullStringValue nonJsonString = new NullStringValue();
+ JSONArray jsonArray = new JSONArray();
+
+ jsonArray.put(nonJsonString);
+
+ StringWriter writer = new StringWriter();
+ try {
+ String output = jsonArray.write(writer).toString();
+ assertTrue("String values should be equal", "[\"\"]".equals(output));
+
+ output = JSONObject.valueToString(nonJsonString);
+ assertTrue("String values should be equal", "\"\"".equals(output));
+ } finally {
+ writer.close();
+ }
+ }
+
+ @Test
+ public void testEnumJSONString() {
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("key", MyEnum.MY_ENUM);
+ assertEquals("{\"key\":\"myJsonString\"}", jsonObject.toString());
+ }
+
+ private enum MyEnum implements JSONString {
+ MY_ENUM;
+
+ @Override
+ public String toJSONString() {
+ return "\"myJsonString\"";
+ }
+ }
+
+ /**
+ * A JSONString that returns a valid JSON string value.
+ */
+ private static final class JSONStringValue implements JSONString {
+
+ @Override
+ public String toJSONString() {
+ return "\"the JSON string value\"";
+ }
+
+ @Override
+ public String toString() {
+ return "the toString value for JSONStringValue";
+ }
+ }
+
+ /**
+ * A JSONString that returns null when calling toJSONString().
+ */
+ private static final class JSONNullStringValue implements JSONString {
+
+ @Override
+ public String toJSONString() {
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return "the toString value";
+ }
+ }
+
+ /**
+ * A JSONString that throw an exception when calling toJSONString().
+ */
+ private static final class JSONStringExceptionValue implements JSONString {
+
+ @Override
+ public String toJSONString() {
+ throw new IllegalStateException("the exception value");
+ }
+
+ @Override
+ public String toString() {
+ return "the toString value for JSONStringExceptionValue";
+ }
+ }
+
+ public static final class StringValue {
+
+ @Override
+ public String toString() {
+ return "the toString value for StringValue";
+ }
+ }
+
+ public static final class NullStringValue {
+
+ @Override
+ public String toString() {
+ return null;
+ }
+ }
+}
diff --git a/src/test/java/org/json/junit/JSONStringerTest.java b/src/test/java/org/json/junit/JSONStringerTest.java
new file mode 100644
index 000000000..0ecb9d662
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONStringerTest.java
@@ -0,0 +1,357 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.*;
+
+import java.math.BigDecimal;
+import java.util.*;
+
+import org.json.*;
+import org.junit.Test;
+
+import com.jayway.jsonpath.*;
+
+
+/**
+ * Tests for JSON-Java JSONStringer and JSONWriter.
+ */
+public class JSONStringerTest {
+
+ /**
+ * Object with a null key.
+ * Expects a JSONException.
+ */
+ @Test
+ public void nullKeyException() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.object();
+ try {
+ jsonStringer.key(null);
+ assertTrue("Expected an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "Null key.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Add a key with no object.
+ * Expects a JSONException.
+ */
+ @Test
+ public void outOfSequenceException() {
+ JSONStringer jsonStringer = new JSONStringer();
+ try {
+ jsonStringer.key("hi");
+ assertTrue("Expected an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "Misplaced key.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Missplace an array.
+ * Expects a JSONException
+ */
+ @Test
+ public void missplacedArrayException() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.object().endObject();
+ try {
+ jsonStringer.array();
+ assertTrue("Expected an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "Misplaced array.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Missplace an endErray.
+ * Expects a JSONException
+ */
+ @Test
+ public void missplacedEndArrayException() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.object();
+ try {
+ jsonStringer.endArray();
+ assertTrue("Expected an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "Misplaced endArray.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Missplace an endObject.
+ * Expects a JSONException
+ */
+ @Test
+ public void missplacedEndObjectException() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.array();
+ try {
+ jsonStringer.endObject();
+ assertTrue("Expected an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "Misplaced endObject.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Missplace an object.
+ * Expects a JSONException.
+ */
+ @Test
+ public void missplacedObjectException() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.object().endObject();
+ try {
+ jsonStringer.object();
+ assertTrue("Expected an exception", false);
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "Misplaced object.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Exceeds implementation max nesting depth.
+ * Expects a JSONException
+ */
+ @Test
+ public void exceedNestDepthException() {
+ try {
+ JSONStringer s = new JSONStringer();
+ s.object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object();
+ s.key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object().
+ key("k").object().key("k").object().key("k").object().key("k").object().key("k").object();
+ fail("Expected an exception message");
+ } catch (JSONException e) {
+ assertTrue("Expected an exception message",
+ "Nesting too deep.".
+ equals(e.getMessage()));
+ }
+ }
+
+ /**
+ * Build a JSON doc using JSONString API calls,
+ * then convert to JSONObject
+ */
+ @Test
+ public void simpleObjectString() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.object();
+ 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.endObject();
+ String str = jsonStringer.toString();
+ JSONObject jsonObject = new JSONObject(str);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 7 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 7);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue")));
+ assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue")));
+ assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
+ assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
+ assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
+ assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
+ }
+
+ /**
+ * Build a JSON doc using JSONString API calls,
+ * then convert to JSONArray
+ */
+ @Test
+ public void simpleArrayString() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.array();
+ jsonStringer.value(true);
+ jsonStringer.value(false);
+ jsonStringer.value(null);
+ jsonStringer.value("hello world!");
+ jsonStringer.value(42);
+ jsonStringer.value(-23.45e67);
+ jsonStringer.endArray();
+ String str = jsonStringer.toString();
+ JSONArray jsonArray = new JSONArray(str);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
+ assertTrue("expected 6 top level items", ((List>)(JsonPath.read(doc, "$"))).size() == 6);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
+ assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/2")));
+ assertTrue("expected hello world!", "hello world!".equals(jsonArray.query("/3")));
+ assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/4")));
+ assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonArray.query("/5")));
+ }
+
+ /**
+ * Build a nested JSON doc using JSONString API calls, then convert to
+ * JSONObject. Will create a long cascade of output by reusing the
+ * returned values..
+ */
+ @Test
+ public void complexObjectString() {
+ JSONStringer jsonStringer = new JSONStringer();
+ jsonStringer.object().
+ key("trueValue").value(true).
+ key("falseValue").value(false).
+ key("nullValue").value(null).
+ key("stringValue").value("hello world!").
+ key("object2").object().
+ key("k1").value("v1").
+ key("k2").value("v2").
+ key("k3").value("v3").
+ key("array1").array().
+ value(1).
+ value(2).
+ object().
+ key("k4").value("v4").
+ key("k5").value("v5").
+ key("k6").value("v6").
+ key("array2").array().
+ value(5).
+ value(6).
+ value(7).
+ value(8).
+ endArray().
+ endObject().
+ value(3).
+ value(4).
+ endArray().
+ endObject().
+ key("complexStringValue").value("h\be\tllo w\u1234orld!").
+ key("intValue").value(42).
+ key("doubleValue").value(-23.45e67).
+ endObject();
+ String str = jsonStringer.toString();
+ JSONObject jsonObject = new JSONObject(str);
+
+ // validate JSON content
+ Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
+ assertTrue("expected 8 top level items", ((Map,?>)(JsonPath.read(doc, "$"))).size() == 8);
+ assertTrue("expected 4 object2 items", ((Map,?>)(JsonPath.read(doc, "$.object2"))).size() == 4);
+ assertTrue("expected 5 array1 items", ((List>)(JsonPath.read(doc, "$.object2.array1"))).size() == 5);
+ assertTrue("expected 4 array[2] items", ((Map,?>)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4);
+ assertTrue("expected 4 array1[2].array2 items", ((List>)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4);
+ assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue")));
+ assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue")));
+ assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue")));
+ assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
+ assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
+ assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
+ assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
+ assertTrue("expected v1", "v1".equals(jsonObject.query("/object2/k1")));
+ assertTrue("expected v2", "v2".equals(jsonObject.query("/object2/k2")));
+ assertTrue("expected v3", "v3".equals(jsonObject.query("/object2/k3")));
+ assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/object2/array1/0")));
+ assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/object2/array1/1")));
+ assertTrue("expected v4", "v4".equals(jsonObject.query("/object2/array1/2/k4")));
+ assertTrue("expected v5", "v5".equals(jsonObject.query("/object2/array1/2/k5")));
+ assertTrue("expected v6", "v6".equals(jsonObject.query("/object2/array1/2/k6")));
+ assertTrue("expected 5", Integer.valueOf(5).equals(jsonObject.query("/object2/array1/2/array2/0")));
+ assertTrue("expected 6", Integer.valueOf(6).equals(jsonObject.query("/object2/array1/2/array2/1")));
+ assertTrue("expected 7", Integer.valueOf(7).equals(jsonObject.query("/object2/array1/2/array2/2")));
+ assertTrue("expected 8", Integer.valueOf(8).equals(jsonObject.query("/object2/array1/2/array2/3")));
+ assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/object2/array1/3")));
+ assertTrue("expected 4", Integer.valueOf(4).equals(jsonObject.query("/object2/array1/4")));
+ }
+
+}
diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java
new file mode 100644
index 000000000..b0b45cb7c
--- /dev/null
+++ b/src/test/java/org/json/junit/JSONTokenerTest.java
@@ -0,0 +1,373 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.json.*;
+import org.junit.Test;
+
+/**
+ * Test specific to the {@link org.json.JSONTokener} class.
+ * @author John Aylward
+ *
+ */
+public class JSONTokenerTest {
+
+ /**
+ * verify that back() fails as expected.
+ * @throws IOException thrown if something unexpected happens.
+ */
+ @Test
+ public void verifyBackFailureZeroIndex() throws IOException {
+ Reader reader = new StringReader("some test string");
+ try {
+ final JSONTokener tokener = new JSONTokener(reader);
+ try {
+ // this should fail since the index is 0;
+ tokener.back();
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Stepping back two steps is not supported", e.getMessage());
+ } catch (Exception e) {
+ fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
+ }
+
+ } finally {
+ reader.close();
+ }
+ }
+ /**
+ * verify that back() fails as expected.
+ * @throws IOException thrown if something unexpected happens.
+ */
+ @Test
+ public void verifyBackFailureDoubleBack() throws IOException {
+ Reader reader = new StringReader("some test string");
+ try {
+ final JSONTokener tokener = new JSONTokener(reader);
+ tokener.next();
+ tokener.back();
+ try {
+ // this should fail since the index is 0;
+ tokener.back();
+ fail("Expected an exception");
+ } catch (JSONException e) {
+ assertEquals("Stepping back two steps is not supported", e.getMessage());
+ } catch (Exception e) {
+ fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
+ }
+ } finally {
+ reader.close();
+ }
+ }
+
+ @Test
+ public void testValid() {
+ checkValid("0",Number.class);
+ checkValid(" 0 ",Number.class);
+ checkValid("23",Number.class);
+ checkValid("23.5",Number.class);
+ checkValid(" 23.5 ",Number.class);
+ checkValid("null",null);
+ checkValid(" null ",null);
+ checkValid("true",Boolean.class);
+ checkValid(" true\n",Boolean.class);
+ checkValid("false",Boolean.class);
+ checkValid("\nfalse ",Boolean.class);
+ checkValid("{}",JSONObject.class);
+ checkValid(" {} ",JSONObject.class);
+ checkValid("{\"a\":1}",JSONObject.class);
+ checkValid(" {\"a\":1} ",JSONObject.class);
+ checkValid("[]",JSONArray.class);
+ checkValid(" [] ",JSONArray.class);
+ checkValid("[1,2]",JSONArray.class);
+ checkValid("\n\n[1,2]\n\n",JSONArray.class);
+
+ // Test should fail if default strictMode is true, pass if false
+ JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
+ if (jsonParserConfiguration.isStrictMode()) {
+ try {
+ checkValid("1 2", String.class);
+ assertEquals("Expected to throw exception due to invalid string", true, false);
+ } catch (JSONException e) { }
+ } else {
+ checkValid("1 2", String.class);
+ }
+ }
+
+ @Test
+ public void testErrors() {
+ // Check that stream can detect that a value is found after
+ // the first one
+ checkError(" { \"a\":1 } 4 ");
+ checkError("null \"a\"");
+ checkError("{} true");
+ }
+
+ private Object checkValid(String testStr, Class> aClass) {
+ Object result = nextValue(testStr);
+
+ // Check class of object returned
+ if( null == aClass ) {
+ if(JSONObject.NULL.equals(result)) {
+ // OK
+ } else {
+ throw new JSONException("Unexpected class: "+result.getClass().getSimpleName());
+ }
+ } else {
+ if( null == result ) {
+ throw new JSONException("Unexpected null result");
+ } else if(!aClass.isAssignableFrom(result.getClass()) ) {
+ throw new JSONException("Unexpected class: "+result.getClass().getSimpleName());
+ }
+ }
+
+ return result;
+ }
+
+ private void checkError(String testStr) {
+ try {
+ nextValue(testStr);
+
+ fail("Error should be triggered: (\""+testStr+"\")");
+ } catch (JSONException e) {
+ // OK
+ }
+ }
+
+ /**
+ * Verifies that JSONTokener can read a stream that contains a value. After
+ * the reading is done, check that the stream is left in the correct state
+ * by reading the characters after. All valid cases should reach end of stream.
+ * @param testStr
+ * @return
+ * @throws Exception
+ */
+ private Object nextValue(String testStr) throws JSONException {
+ StringReader sr = new StringReader(testStr);
+ try {
+ JSONTokener tokener = new JSONTokener(sr);
+
+ Object result = tokener.nextValue();
+
+ if( result == null ) {
+ throw new JSONException("Unable to find value token in JSON stream: ("+tokener+"): "+testStr);
+ }
+
+ char c = tokener.nextClean();
+ if( 0 != c ) {
+ throw new JSONException("Unexpected character found at end of JSON stream: "+c+ " ("+tokener+"): "+testStr);
+ }
+
+ return result;
+ } finally {
+ sr.close();
+ }
+
+ }
+
+ /**
+ * Tests the failure of the skipTo method with a buffered reader. Preferably
+ * we'd like this not to fail but at this time we don't have a good recovery.
+ *
+ * @throws IOException thrown if something unexpected happens.
+ */
+ @Test
+ public void testSkipToFailureWithBufferedReader() throws IOException {
+ final byte[] superLongBuffer = new byte[1000001];
+ // fill our buffer
+ for(int i=0;i keys = jsonObject.keys();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ Object value = jsonObject.get(key);
+ Object expectedValue = expectedJsonObject.get(key);
+ compareActualVsExpectedObjects(value, expectedValue);
+ }
+ }
+
+ /**
+ * Compare two objects for equality. Might be JSONArray, JSONObject,
+ * or something else.
+ * @param value created by the code to be tested
+ * @param expectedValue created specifically for comparing
+ */
+ private static void compareActualVsExpectedObjects(Object value,
+ Object expectedValue) {
+ if (value instanceof JSONObject && expectedValue instanceof JSONObject) {
+ // Compare JSONObjects
+ JSONObject jsonObject = (JSONObject)value;
+ JSONObject expectedJsonObject = (JSONObject)expectedValue;
+ compareActualVsExpectedJsonObjects(
+ jsonObject, expectedJsonObject);
+ } else if (value instanceof JSONArray && expectedValue instanceof JSONArray) {
+ // Compare JSONArrays
+ JSONArray jsonArray = (JSONArray)value;
+ JSONArray expectedJsonArray = (JSONArray)expectedValue;
+ compareActualVsExpectedJsonArrays(
+ jsonArray, expectedJsonArray);
+ } else {
+ /**
+ * Compare all other types using toString(). First, the types must
+ * also be equal, unless both are Number type. Certain helper
+ * classes (e.g. XML) may create Long instead of Integer for small
+ * int values.
+ */
+ if (!(value instanceof Number && expectedValue instanceof Number)) {
+ // Non-Number and non-matching types
+ assertEquals("object types should be equal ",
+ expectedValue.getClass().toString(),
+ value.getClass().toString()
+ );
+ }
+ /**
+ * Same types or both Numbers, compare by toString()
+ */
+ assertEquals("values should be equal",
+ expectedValue.toString(),
+ value.toString()
+ );
+ }
+ }
+
+ /**
+ * Asserts that all JSONObject maps are the same as the default ctor
+ * @param jsonObjects list of objects to be tested
+ */
+ public static void checkJSONObjectsMaps(List jsonObjects) {
+ if (jsonObjects == null || jsonObjects.size() == 0) {
+ return;
+ }
+ Class extends Map> mapType = new JSONObject().getMapType();
+ for (JSONObject jsonObject : jsonObjects) {
+ if (jsonObject != null) {
+ assertTrue(mapType == jsonObject.getMapType());
+ checkJSONObjectMaps(jsonObject, mapType);
+ }
+ }
+ }
+
+ /**
+ * Asserts that all JSONObject maps are the same as the default ctor
+ * @param jsonObject the object to be tested
+ */
+ public static void checkJSONObjectMaps(JSONObject jsonObject) {
+ if (jsonObject != null) {
+ checkJSONObjectMaps(jsonObject, jsonObject.getMapType());
+ }
+ }
+
+ /**
+ * Asserts that all JSONObject maps are the same as mapType
+ * @param jsonObject object to be tested
+ * @param mapType mapType to test against
+ */
+ public static void checkJSONObjectMaps(JSONObject jsonObject, Class extends Map> mapType) {
+ if (mapType == null) {
+ mapType = new JSONObject().getMapType();
+ }
+ Set keys = jsonObject.keySet();
+ for (String key : keys) {
+ Object val = jsonObject.get(key);
+ if (val instanceof JSONObject) {
+ JSONObject jsonObjectVal = (JSONObject) val;
+ assertTrue(mapType == ((JSONObject) val).getMapType());
+ checkJSONObjectMaps(jsonObjectVal, mapType);
+ } else if (val instanceof JSONArray) {
+ JSONArray jsonArrayVal = (JSONArray)val;
+ checkJSONArrayMaps(jsonArrayVal, mapType);
+ }
+ }
+ }
+
+ /**
+ * Asserts that all JSONObject maps in the JSONArray object match the default map
+ * @param jsonArrays list of JSONArray objects to be tested
+ */
+ public static void checkJSONArraysMaps(List jsonArrays) {
+ if (jsonArrays == null || jsonArrays.size() == 0) {
+ return;
+ }
+ Class extends Map> mapType = new JSONObject().getMapType();
+ for (JSONArray jsonArray : jsonArrays) {
+ if (jsonArray != null) {
+ checkJSONArrayMaps(jsonArray, mapType);
+ }
+ }
+ }
+
+ /**
+ * Asserts that all JSONObject maps in the JSONArray object match mapType
+ * @param jsonArray object to be tested
+ * @param mapType map type to be tested against
+ */
+ public static void checkJSONArrayMaps(JSONArray jsonArray, Class extends Map> mapType) {
+ if (jsonArray == null) {
+ return;
+ }
+ if (mapType == null) {
+ mapType = new JSONObject().getMapType();
+ }
+ Iterator it = jsonArray.iterator();
+ while (it.hasNext()) {
+ Object val = it.next();
+ if (val instanceof JSONObject) {
+ JSONObject jsonObjectVal = (JSONObject)val;
+ checkJSONObjectMaps(jsonObjectVal, mapType);
+ } else if (val instanceof JSONArray) {
+ JSONArray jsonArrayVal = (JSONArray)val;
+ checkJSONArrayMaps(jsonArrayVal, mapType);
+ }
+ }
+ }
+
+ /**
+ * Asserts that all JSONObject maps nested in the JSONArray match
+ * the default mapType
+ * @param jsonArray the object to be tested
+ */
+ public static void checkJSONArrayMaps(JSONArray jsonArray) {
+ if (jsonArray != null) {
+ checkJSONArrayMaps(jsonArray, null);
+ }
+ }
+}
diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java
new file mode 100755
index 000000000..ca1980c8a
--- /dev/null
+++ b/src/test/java/org/json/junit/XMLConfigurationTest.java
@@ -0,0 +1,1248 @@
+package org.json.junit;
+
+/*
+Public Domain.
+*/
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.XML;
+import org.json.XMLParserConfiguration;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests for JSON-Java XML.java with XMLParserConfiguration.java
+ */
+public class XMLConfigurationTest {
+ /**
+ * JUnit supports temporary files and folders that are cleaned up after the test.
+ * https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/
+ */
+ @Rule
+ public TemporaryFolder testFolder = new TemporaryFolder();
+
+ /**
+ * JSONObject from a null XML string.
+ * Expects a NullPointerException
+ */
+ @Test(expected=NullPointerException.class)
+ public void shouldHandleNullXML() {
+ String xmlStr = null;
+ JSONObject jsonObject =
+ XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ }
+
+ /**
+ * Empty JSONObject from an empty XML string.
+ */
+ @Test
+ public void shouldHandleEmptyXML() {
+
+ String xmlStr = "";
+ JSONObject jsonObject =
+ XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ }
+
+ /**
+ * Empty JSONObject from a non-XML string.
+ */
+ @Test
+ public void shouldHandleNonXML() {
+ String xmlStr = "{ \"this is\": \"not xml\"}";
+ JSONObject jsonObject =
+ XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
+ assertTrue("xml string should be empty", jsonObject.isEmpty());
+ }
+
+ /**
+ * Invalid XML string (tag contains a frontslash).
+ * Expects a JSONException
+ */
+ @Test
+ public void shouldHandleInvalidSlashInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " abc street \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped tag at 176 [character 14 line 4]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string ('!' char in tag)
+ * Expects a JSONException
+ */
+ @Test
+ public void shouldHandleInvalidBangInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped meta tag at 214 [character 12 line 7]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string ('!' char and no closing tag brace)
+ * Expects a JSONException
+ */
+ @Test
+ public void shouldHandleInvalidBangNoCloseInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped meta tag at 213 [character 12 line 7]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string (no end brace for tag)
+ * Expects JSONException
+ */
+ @Test
+ public void shouldHandleNoCloseStartTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr, XMLParserConfiguration.KEEP_STRINGS);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misplaced '<' at 193 [character 4 line 6]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string (partial CDATA chars in tag name)
+ * Expects JSONException
+ */
+ @Test
+ public void shouldHandleInvalidCDATABangInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " Joe Tester \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XMLParserConfiguration config =
+ new XMLParserConfiguration().withcDataTagName("altContent");
+ XML.toJSONObject(xmlStr, config);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Expected 'CDATA[' at 204 [character 11 line 5]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Null JSONObject in XML.toString()
+ */
+ @Test
+ public void shouldHandleNullJSONXML() {
+ JSONObject jsonObject= null;
+ String actualXml = XML.toString(jsonObject, null,
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("generated XML does not equal expected XML","\"null\"",actualXml);
+ }
+
+ /**
+ * Empty JSONObject in XML.toString()
+ */
+ @Test
+ public void shouldHandleEmptyJSONXML() {
+ JSONObject jsonObject= new JSONObject();
+ String xmlStr = XML.toString(jsonObject, null,
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertTrue("xml string should be empty", xmlStr.isEmpty());
+ }
+
+ /**
+ * No SML start tag. The ending tag ends up being treated as content.
+ */
+ @Test
+ public void shouldHandleNoStartTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " >\n"+
+ " \n"+
+ " ";
+ String expectedStr =
+ "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
+ "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
+ JSONObject jsonObject = XML.toJSONObject(xmlStr,
+ XMLParserConfiguration.KEEP_STRINGS);
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Valid XML to JSONObject
+ */
+ @Test
+ public void shouldHandleSimpleXML() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " Joe Tester \n"+
+ " [CDATA[Baker street 5] \n"+
+ " \n"+
+ " true \n"+
+ " false \n"+
+ " null \n"+
+ " 42 \n"+
+ " -23 \n"+
+ " -23.45 \n"+
+ " -23x.45 \n"+
+ " 1, 2, 3, 4.1, 5.2 \n"+
+ " \n"+
+ " ";
+
+ String expectedStr =
+ "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
+ "\"name\":\"Joe Tester\",\"NothingHere\":\"\",\"TrueValue\":true,\n"+
+ "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
+ "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":\"-23x.45\",\n"+
+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
+ "},\"xsi:noNamespaceSchemaLocation\":"+
+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
+ "XMLSchema-instance\"}}";
+
+ XMLParserConfiguration config =
+ new XMLParserConfiguration().withcDataTagName("altContent");
+ compareStringToJSONObject(xmlStr, expectedStr, config);
+ compareReaderToJSONObject(xmlStr, expectedStr, config);
+ compareFileToJSONObject(xmlStr, expectedStr);
+ }
+
+ /**
+ * Valid XML with comments to JSONObject
+ */
+ @Test
+ public void shouldHandleCommentsInXML() {
+
+ String xmlStr =
+ "\n"+
+ "\n"+
+ "\n"+
+ " \n"+
+ " comment ]]>\n"+
+ " Joe Tester \n"+
+ " \n"+
+ " Baker street 5 \n"+
+ " \n"+
+ " ";
+ XMLParserConfiguration config =
+ new XMLParserConfiguration().withcDataTagName("altContent");
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+
+ "street 5\",\"name\":\"Joe Tester\",\"altContent\":\" this is -- "+
+ " comment \"}}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Valid XML to XML.toString()
+ */
+ @Test
+ public void shouldHandleToString() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " [CDATA[Joe & T > e < s " t ' er]] \n"+
+ " Baker street 5 \n"+
+ " 1, 2, 3, 4.1, 5.2 \n"+
+ " \n"+
+ " ";
+
+ String expectedStr =
+ "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+
+ "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+
+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
+ "},\"xsi:noNamespaceSchemaLocation\":"+
+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
+ "XMLSchema-instance\"}}";
+
+ JSONObject jsonObject = XML.toJSONObject(xmlStr,
+ XMLParserConfiguration.KEEP_STRINGS);
+ String xmlToStr = XML.toString(jsonObject, null,
+ XMLParserConfiguration.KEEP_STRINGS);
+ JSONObject finalJsonObject = XML.toJSONObject(xmlToStr,
+ XMLParserConfiguration.KEEP_STRINGS);
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Converting a JSON doc containing '>' content to JSONObject, then
+ * XML.toString() should result in valid XML.
+ */
+ @Test
+ public void shouldHandleContentNoArraytoString() {
+ String expectedStr =
+ "{\"addresses\":{\"altContent\":\">\"}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ XMLParserConfiguration config = new XMLParserConfiguration().withcDataTagName("altContent");
+ String finalStr = XML.toString(expectedJsonObject, null, config);
+ String expectedFinalStr = "> ";
+ assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
+ finalStr+"]", expectedFinalStr.equals(finalStr));
+ }
+
+ /**
+ * Converting a JSON doc containing a 'content' array to JSONObject, then
+ * XML.toString() should result in valid XML.
+ * TODO: This is probably an error in how the 'content' keyword is used.
+ */
+ @Test
+ public void shouldHandleContentArraytoString() {
+ String expectedStr =
+ "{\"addresses\":{\"altContent\":[1, 2, 3]}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ XMLParserConfiguration config = new XMLParserConfiguration().withcDataTagName("altContent");
+ String finalStr = XML.toString(expectedJsonObject, null, config);
+ String expectedFinalStr = ""+
+ "1\n2\n3"+
+ " ";
+ assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
+ finalStr+"]", expectedFinalStr.equals(finalStr));
+ }
+
+ /**
+ * Converting a JSON doc containing a named array to JSONObject, then
+ * XML.toString() should result in valid XML.
+ */
+ @Test
+ public void shouldHandleArraytoString() {
+ String expectedStr =
+ "{\"addresses\":{"+
+ "\"something\":[1, 2, 3]}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ String finalStr = XML.toString(expectedJsonObject, null,
+ XMLParserConfiguration.KEEP_STRINGS);
+ String expectedFinalStr = ""+
+ "1 2 3 "+
+ " ";
+ assertTrue("Should handle expectedFinal: ["+expectedStr+"] final: ["+
+ finalStr+"]", expectedFinalStr.equals(finalStr));
+ }
+
+ /**
+ * Tests that the XML output for empty arrays is consistent.
+ */
+ @Test
+ public void shouldHandleEmptyArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("array",new Object[]{});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("array",new JSONArray());
+
+ final String expected = " ";
+ String output1 = XML.toString(jo1, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("Expected an empty root tag", expected, output1);
+ String output2 = XML.toString(jo2, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("Expected an empty root tag", expected, output2);
+ }
+
+ /**
+ * Tests that the XML output for arrays is consistent when an internal array is empty.
+ */
+ @Test
+ public void shouldHandleEmptyMultiArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("arr",new Object[]{"One", new String[]{}, "Four"});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{}), "Four"}));
+
+ final String expected = "One Four ";
+ String output1 = XML.toString(jo1, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("Expected a matching array", expected, output1);
+ String output2 = XML.toString(jo2, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+
+ assertEquals("Expected a matching array", expected, output2);
+ }
+
+ /**
+ * Tests that the XML output for arrays is consistent when arrays are not empty.
+ */
+ @Test
+ public void shouldHandleNonEmptyArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("arr",new String[]{"One", "Two", "Three"});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("arr",new JSONArray(new String[]{"One", "Two", "Three"}));
+
+ final String expected = "One Two Three ";
+ String output1 = XML.toString(jo1, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("Expected a non empty root tag", expected, output1);
+ String output2 = XML.toString(jo2, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("Expected a non empty root tag", expected, output2);
+ }
+
+ /**
+ * Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays.
+ */
+ @Test
+ public void shouldHandleMultiArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"}));
+
+ final String expected = "One Two Three Four ";
+ String output1 = XML.toString(jo1, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("Expected a matching array", expected, output1);
+ String output2 = XML.toString(jo2, "jo",
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals("Expected a matching array", expected, output2);
+ }
+
+ /**
+ * Converting a JSON doc containing a named array of nested arrays to
+ * JSONObject, then XML.toString() should result in valid XML.
+ */
+ @Test
+ public void shouldHandleNestedArraytoString() {
+ String xmlStr =
+ "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+
+ "\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
+ JSONObject jsonObject = new JSONObject(xmlStr);
+ String finalStr = XML.toString(jsonObject, null,
+ XMLParserConfiguration.ORIGINAL);
+ JSONObject finalJsonObject = XML.toJSONObject(finalStr);
+ String expectedStr = " "+
+ "1 2 "+
+ "3 "+
+ "test.xsdhttp://www.w3.org/2001/XMLSche"+
+ "ma-instance ";
+ JSONObject expectedJsonObject = XML.toJSONObject(expectedStr,
+ XMLParserConfiguration.ORIGINAL);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+
+
+ /**
+ * Possible bug:
+ * Illegal node-names must be converted to legal XML-node-names.
+ * The given example shows 2 nodes which are valid for JSON, but not for XML.
+ * Therefore illegal arguments should be converted to e.g. an underscore (_).
+ */
+ @Test
+ public void shouldHandleIllegalJSONNodeNames()
+ {
+ JSONObject inputJSON = new JSONObject();
+ inputJSON.append("123IllegalNode", "someValue1");
+ inputJSON.append("Illegal@node", "someValue2");
+
+ String result = XML.toString(inputJSON, null,
+ XMLParserConfiguration.KEEP_STRINGS);
+
+ /*
+ * This is invalid XML. Names should not begin with digits or contain
+ * certain values, including '@'. One possible solution is to replace
+ * illegal chars with '_', in which case the expected output would be:
+ * <___IllegalNode>someValue1someValue2
+ */
+ String expected = "<123IllegalNode>someValue1123IllegalNode>someValue2 ";
+
+ assertEquals("Length", expected.length(), result.length());
+ assertTrue("123IllegalNode", result.contains("<123IllegalNode>someValue1123IllegalNode>"));
+ assertTrue("Illegal@node", result.contains("someValue2 "));
+ }
+
+ /**
+ * JSONObject with NULL value, to XML.toString()
+ */
+ @Test
+ public void shouldHandleNullNodeValue()
+ {
+ JSONObject inputJSON = new JSONObject();
+ inputJSON.put("nullValue", JSONObject.NULL);
+ // This is a possible preferred result
+ // String expectedXML = " ";
+ /**
+ * This is the current behavior. JSONObject.NULL is emitted as
+ * the string, "null".
+ */
+ String actualXML = "null ";
+ String resultXML = XML.toString(inputJSON, null,
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertEquals(actualXML, resultXML);
+ }
+
+ @Test
+ public void shouldHandleEmptyNodeValue()
+ {
+ JSONObject inputJSON = new JSONObject();
+ inputJSON.put("Emptyness", "");
+ String expectedXmlWithoutExplicitEndTag = " ";
+ String expectedXmlWithExplicitEndTag = " ";
+ assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null,
+ new XMLParserConfiguration().withCloseEmptyTag(false)));
+ assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null,
+ new XMLParserConfiguration().withCloseEmptyTag(true)));
+ }
+
+ @Test
+ public void shouldKeepConfigurationIntactAndUpdateCloseEmptyTagChoice()
+ {
+ XMLParserConfiguration keepStrings = XMLParserConfiguration.KEEP_STRINGS;
+ XMLParserConfiguration keepStringsAndCloseEmptyTag = keepStrings.withCloseEmptyTag(true);
+ XMLParserConfiguration keepDigits = keepStringsAndCloseEmptyTag.withKeepStrings(false);
+ XMLParserConfiguration keepDigitsAndNoCloseEmptyTag = keepDigits.withCloseEmptyTag(false);
+ assertTrue(keepStrings.isKeepNumberAsString());
+ assertTrue(keepStrings.isKeepBooleanAsString());
+ assertFalse(keepStrings.isCloseEmptyTag());
+ assertTrue(keepStringsAndCloseEmptyTag.isKeepNumberAsString());
+ assertTrue(keepStringsAndCloseEmptyTag.isKeepBooleanAsString());
+ assertTrue(keepStringsAndCloseEmptyTag.isCloseEmptyTag());
+ assertFalse(keepDigits.isKeepNumberAsString());
+ assertFalse(keepDigits.isKeepBooleanAsString());
+ assertTrue(keepDigits.isCloseEmptyTag());
+ assertFalse(keepDigitsAndNoCloseEmptyTag.isKeepNumberAsString());
+ assertFalse(keepDigitsAndNoCloseEmptyTag.isKeepBooleanAsString());
+ assertFalse(keepDigitsAndNoCloseEmptyTag.isCloseEmptyTag());
+ }
+
+ /**
+ * Investigate exactly how the "content" keyword works
+ */
+ @Test
+ public void contentOperations() {
+ /*
+ * When a standalone 0) then return]]> ";
+ JSONObject jsonObject = XML.toJSONObject(xmlStr,
+ XMLParserConfiguration.KEEP_STRINGS);
+ assertTrue("1. 3 items", 3 == jsonObject.length());
+ assertTrue("1. empty tag1", "".equals(jsonObject.get("tag1")));
+ assertTrue("1. empty tag2", "".equals(jsonObject.get("tag2")));
+ assertTrue("1. content found", "if (a < b && a > 0) then return".equals(jsonObject.get("content")));
+
+ // multiple consecutive standalone cdatas are accumulated into an array
+ xmlStr = " 0) then return]]> ";
+ jsonObject = XML.toJSONObject(xmlStr,
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent"));
+ assertTrue("2. 3 items", 3 == jsonObject.length());
+ assertTrue("2. empty tag1", "".equals(jsonObject.get("tag1")));
+ assertTrue("2. empty tag2", "".equals(jsonObject.get("tag2")));
+ assertTrue("2. content array found", jsonObject.get("altContent") instanceof JSONArray);
+ JSONArray jsonArray = jsonObject.getJSONArray("altContent");
+ assertTrue("2. array size", jsonArray.length() == 2);
+ assertTrue("2. content array entry 0", "if (a < b && a > 0) then return".equals(jsonArray.get(0)));
+ assertTrue("2. content array entry 1", "here is another cdata".equals(jsonArray.get(1)));
+
+ /*
+ * text content is accumulated in a "content" inside a local JSONObject.
+ * If there is only one instance, it is saved in the context (a different JSONObject
+ * from the calling code. and the content element is discarded.
+ */
+ xmlStr = "value 1 ";
+ jsonObject = XML.toJSONObject(xmlStr,
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent"));
+ assertTrue("3. 2 items", 1 == jsonObject.length());
+ assertTrue("3. value tag1", "value 1".equals(jsonObject.get("tag1")));
+
+ /*
+ * array-style text content (multiple tags with the same name) is
+ * accumulated in a local JSONObject with key="content" and value=JSONArray,
+ * saved in the context, and then the local JSONObject is discarded.
+ */
+ xmlStr = "value 1 2 true ";
+ jsonObject = XML.toJSONObject(xmlStr,
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent"));
+ assertTrue("4. 1 item", 1 == jsonObject.length());
+ assertTrue("4. content array found", jsonObject.get("tag1") instanceof JSONArray);
+ jsonArray = jsonObject.getJSONArray("tag1");
+ assertTrue("4. array size", jsonArray.length() == 3);
+ assertTrue("4. content array entry 0", "value 1".equals(jsonArray.get(0)));
+ assertTrue("4. content array entry 1", jsonArray.getInt(1) == 2);
+ assertTrue("4. content array entry 2", jsonArray.getBoolean(2) == true);
+
+ /*
+ * Complex content is accumulated in a "content" field. For example, an element
+ * may contain a mix of child elements and text. Each text segment is
+ * accumulated to content.
+ */
+ xmlStr = "val1 val2 ";
+ jsonObject = XML.toJSONObject(xmlStr,
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent"));
+ assertTrue("5. 1 item", 1 == jsonObject.length());
+ assertTrue("5. jsonObject found", jsonObject.get("tag1")
+ instanceof JSONObject);
+ jsonObject = jsonObject.getJSONObject("tag1");
+ assertTrue("5. 2 contained items", 2 == jsonObject.length());
+ assertTrue("5. contained tag", "".equals(jsonObject.get("tag2")));
+ assertTrue("5. contained content jsonArray found",
+ jsonObject.get("altContent") instanceof JSONArray);
+ jsonArray = jsonObject.getJSONArray("altContent");
+ assertTrue("5. array size", jsonArray.length() == 2);
+ assertTrue("5. content array entry 0", "val1".equals(jsonArray.get(0)));
+ assertTrue("5. content array entry 1", "val2".equals(jsonArray.get(1)));
+
+ /*
+ * If there is only 1 complex text content, then it is accumulated in a
+ * "content" field as a string.
+ */
+ xmlStr = "val1 ";
+ jsonObject = XML.toJSONObject(xmlStr,
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent"));
+ assertTrue("6. 1 item", 1 == jsonObject.length());
+ assertTrue("6. jsonObject found", jsonObject.get("tag1") instanceof JSONObject);
+ jsonObject = jsonObject.getJSONObject("tag1");
+ assertTrue("6. contained content found",
+ "val1".equals(jsonObject.get("altContent")));
+ assertTrue("6. contained tag2", "".equals(jsonObject.get("tag2")));
+
+ /*
+ * In this corner case, the content sibling happens to have key=content
+ * We end up with an array within an array, and no content element.
+ * This is probably a bug.
+ */
+ xmlStr = "val1 ";
+ jsonObject = XML.toJSONObject(xmlStr,
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent"));
+ assertTrue("7. 1 item", 1 == jsonObject.length());
+ assertTrue("7. jsonArray found",
+ jsonObject.get("tag1") instanceof JSONArray);
+ jsonArray = jsonObject.getJSONArray("tag1");
+ assertTrue("array size 1", jsonArray.length() == 1);
+ assertTrue("7. contained array found", jsonArray.get(0)
+ instanceof JSONArray);
+ jsonArray = jsonArray.getJSONArray(0);
+ assertTrue("7. inner array size 2", jsonArray.length() == 2);
+ assertTrue("7. inner array item 0", "val1".equals(jsonArray.get(0)));
+ assertTrue("7. inner array item 1", "".equals(jsonArray.get(1)));
+
+ /*
+ * Confirm behavior of original issue
+ */
+ String jsonStr =
+ "{"+
+ "\"Profile\": {"+
+ "\"list\": {"+
+ "\"history\": {"+
+ "\"entries\": ["+
+ "{"+
+ "\"deviceId\": \"id\","+
+ "\"altContent\": {"+
+ "\"material\": ["+
+ "{"+
+ "\"stuff\": false"+
+ "}"+
+ "]"+
+ "}"+
+ "}"+
+ "]"+
+ "}"+
+ "}"+
+ "}"+
+ "}";
+ jsonObject = new JSONObject(jsonStr);
+ xmlStr = XML.toString(jsonObject, null,
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent"));
+ /*
+ * This is the created XML. Looks like content was mistaken for
+ * complex (child node + text) XML.
+ *
+ *
+ *
+ *
+ * id
+ * {"material":[{"stuff":false}]}
+ *
+ *
+ *
+ *
+ */
+ assertTrue("nothing to test here, see comment on created XML, above", true);
+ }
+
+ /**
+ * JSON string lost leading zero and converted "True" to true.
+ */
+ @Test
+ public void testToJSONArray_jsonOutput() {
+ final String originalXml = "01 1 00 0 True ";
+ final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}");
+ final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
+ new XMLParserConfiguration().withKeepStrings(false));
+ Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
+ }
+
+ /**
+ * JSON string lost leading zero and converted "True" to true.
+ */
+ @Test
+ public void testToJSONArray_jsonOutput_withKeepNumberAsString() {
+ final String originalXml = "01 1 00 0 null True ";
+ final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\",null],\"title\":true}}");
+ final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
+ new XMLParserConfiguration().withKeepNumberAsString(true));
+ Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
+ }
+
+ /**
+ * JSON string lost leading zero and converted "True" to true.
+ */
+ @Test
+ public void testToJSONArray_jsonOutput_withKeepBooleanAsString() {
+ final String originalXml = "01 1 00 0 null True ";
+ final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0,null],\"title\":\"True\"}}");
+ final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
+ new XMLParserConfiguration().withKeepBooleanAsString(true));
+ Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
+ }
+
+ /**
+ * null is "null" when keepStrings == true
+ */
+ @Test
+ public void testToJSONArray_jsonOutput_null_withKeepString() {
+ final String originalXml = "01 1 00 0 null ";
+ final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"null\"}}");
+ final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
+ new XMLParserConfiguration().withKeepStrings(true));
+ Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
+ }
+
+ /**
+ * Test keepStrings behavior when setting keepBooleanAsString, keepNumberAsString
+ */
+ @Test
+ public void test_keepStringBehavior() {
+ XMLParserConfiguration xpc = new XMLParserConfiguration().withKeepStrings(true);
+ assertEquals(xpc.isKeepStrings(), true);
+
+ xpc = xpc.withKeepBooleanAsString(true);
+ xpc = xpc.withKeepNumberAsString(false);
+ assertEquals(xpc.isKeepStrings(), false);
+
+ xpc = xpc.withKeepBooleanAsString(false);
+ xpc = xpc.withKeepNumberAsString(true);
+ assertEquals(xpc.isKeepStrings(), false);
+
+ xpc = xpc.withKeepBooleanAsString(true);
+ xpc = xpc.withKeepNumberAsString(true);
+ assertEquals(xpc.isKeepStrings(), true);
+
+ xpc = xpc.withKeepBooleanAsString(false);
+ xpc = xpc.withKeepNumberAsString(false);
+ assertEquals(xpc.isKeepStrings(), false);
+ }
+
+ /**
+ * JSON string cannot be reverted to original xml.
+ */
+ @Test
+ public void testToJSONArray_reversibility() {
+ final String originalXml = "01 1 00 0 True ";
+ XMLParserConfiguration config = new XMLParserConfiguration().withKeepStrings(false);
+ final String revertedXml =
+ XML.toString(XML.toJSONObject(originalXml, config),
+ null, config);
+ assertNotEquals(revertedXml, originalXml);
+ }
+
+ /**
+ * test passes when using the new method toJsonArray.
+ */
+ @Test
+ public void testToJsonXML() {
+ final String originalXml = "01 1 00 0 True ";
+ final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}");
+
+ final JSONObject json = XML.toJSONObject(originalXml,
+ new XMLParserConfiguration().withKeepStrings(true));
+ Util.compareActualVsExpectedJsonObjects(json, expected);
+
+ final String reverseXml = XML.toString(json);
+ // this reversal isn't exactly the same. use JSONML for an exact reversal
+ final String expectedReverseXml = "01 01 1 00 0 True ";
+
+ assertEquals("length",expectedReverseXml.length(), reverseXml.length());
+ assertTrue("array contents", reverseXml.contains("01 1 00 0 "));
+ assertTrue("item contents", reverseXml.contains("01 "));
+ assertTrue("title contents", reverseXml.contains("True "));
+ }
+
+ /**
+ * test to validate certain conditions of XML unescaping.
+ */
+ @Test
+ public void testUnescape() {
+ assertEquals("{\"xml\":\"Can cope <;\"}",
+ XML.toJSONObject("Can cope <; ",
+ XMLParserConfiguration.KEEP_STRINGS).toString());
+ assertEquals("Can cope <; ", XML.unescape("Can cope <; "));
+
+ assertEquals("{\"xml\":\"Can cope & ;\"}",
+ XML.toJSONObject("Can cope & ; ",
+ XMLParserConfiguration.KEEP_STRINGS).toString());
+ assertEquals("Can cope & ; ", XML.unescape("Can cope & ; "));
+
+ assertEquals("{\"xml\":\"Can cope &;\"}",
+ XML.toJSONObject("Can cope &; ",
+ XMLParserConfiguration.KEEP_STRINGS).toString());
+ assertEquals("Can cope &; ", XML.unescape("Can cope &; "));
+
+ // unicode entity
+ assertEquals("{\"xml\":\"Can cope 4;\"}",
+ XML.toJSONObject("Can cope 4; ",
+ XMLParserConfiguration.KEEP_STRINGS).toString());
+ assertEquals("Can cope 4; ", XML.unescape("Can cope 4; "));
+
+ // double escaped
+ assertEquals("{\"xml\":\"Can cope <\"}",
+ XML.toJSONObject("Can cope < ",
+ XMLParserConfiguration.KEEP_STRINGS).toString());
+ assertEquals("Can cope < ", XML.unescape("Can cope < "));
+
+ assertEquals("{\"xml\":\"Can cope 4\"}",
+ XML.toJSONObject("Can cope 4 ",
+ XMLParserConfiguration.KEEP_STRINGS).toString());
+ assertEquals("Can cope 4 ", XML.unescape("Can cope 4 "));
+
+ }
+
+ /**
+ * Confirm XMLParserConfiguration functionality
+ */
+ @Test
+ public void testConfig() {
+ /**
+ * 1st param is whether to keep the raw string, or call
+ * XML.stringToValue(), which may convert the token to
+ * boolean, null, or number.
+ * 2nd param is what JSON name to use for strings that are
+ * evaluated as xml content data in complex objects, e.g.
+ *
+ * value
+ * content data
+ *
+ */
+
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " content 1\n"+
+ " Sherlock Holmes \n"+
+ " content 2\n"+
+ " Baker street 5 \n"+
+ " content 3\n"+
+ " 1 \n"+
+ " \n"+
+ " ";
+
+ // keep strings, use the altContent tag
+ XMLParserConfiguration config =
+ new XMLParserConfiguration()
+ .withKeepStrings(true)
+ .withcDataTagName("altContent");
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ // num is parsed as a string
+ assertEquals(jsonObject.getJSONObject("addresses").
+ getJSONObject("address").getString("num"), "1");
+ // complex content is collected in an 'altContent' array
+ JSONArray jsonArray = jsonObject.getJSONObject("addresses").
+ getJSONObject("address").getJSONArray("altContent");
+ String expectedStr = "[\"content 1\", \"content 2\", \"content 3\"]";
+ JSONArray expectedJsonArray = new JSONArray(expectedStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+
+ // keepstrings only
+ jsonObject = XML.toJSONObject(xmlStr,
+ XMLParserConfiguration.KEEP_STRINGS);
+ // num is parsed as a string
+ assertEquals(jsonObject.getJSONObject("addresses").
+ getJSONObject("address").getString("num"), "1");
+ // complex content is collected in an 'content' array
+ jsonArray = jsonObject.getJSONObject("addresses").
+ getJSONObject("address").getJSONArray("content");
+ expectedJsonArray = new JSONArray(expectedStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+
+ // use alternate content name
+ config = new XMLParserConfiguration().withcDataTagName("altContent");
+ jsonObject = XML.toJSONObject(xmlStr, config);
+ // num is parsed as a number
+ assertEquals(jsonObject.getJSONObject("addresses").
+ getJSONObject("address").getInt("num"), 1);
+ // complex content is collected in an 'altContent' array
+ jsonArray = jsonObject.getJSONObject("addresses").
+ getJSONObject("address").getJSONArray("altContent");
+ expectedJsonArray = new JSONArray(expectedStr);
+ Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
+
+ }
+
+ /**
+ * Test forceList parameter
+ */
+ @Test
+ public void testSimpleForceList() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " Sherlock Holmes \n"+
+ " \n"+
+ " ";
+
+ String expectedStr =
+ "{\"addresses\":[{\"address\":{\"name\":\"Sherlock Holmes\"}}]}";
+
+ Set forceList = new HashSet();
+ forceList.add("addresses");
+
+ XMLParserConfiguration config =
+ new XMLParserConfiguration()
+ .withForceList(forceList);
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ JSONObject expetedJsonObject = new JSONObject(expectedStr);
+
+ Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
+ }
+ @Test
+ public void testLongForceList() {
+ String xmlStr =
+ ""+
+ ""+
+ "host1 "+
+ "Linux "+
+ ""+
+ ""+
+ "em0 "+
+ "10.0.0.1 "+
+ " "+
+ " "+
+ " "+
+ " ";
+
+ String expectedStr =
+ "{"+
+ "\"servers\": ["+
+ "{"+
+ "\"server\": {"+
+ "\"name\": \"host1\","+
+ "\"os\": \"Linux\","+
+ "\"interfaces\": ["+
+ "{"+
+ "\"interface\": {"+
+ "\"name\": \"em0\","+
+ "\"ip_address\": \"10.0.0.1\""+
+ "}}]}}]}";
+
+ Set forceList = new HashSet();
+ forceList.add("servers");
+ forceList.add("interfaces");
+
+ XMLParserConfiguration config =
+ new XMLParserConfiguration()
+ .withForceList(forceList);
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ JSONObject expetedJsonObject = new JSONObject(expectedStr);
+
+ Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
+ }
+ @Test
+ public void testMultipleTagForceList() {
+ String xmlStr =
+ "\n"+
+ " \n"+
+ " Sherlock Holmes \n"+
+ " John H. Watson \n"+
+ " \n"+
+ " ";
+
+ String expectedStr =
+ "{"+
+ "\"addresses\":["+
+ "{"+
+ "\"address\":["+
+ "{"+
+ "\"name\":["+
+ "\"Sherlock Holmes\","+
+ "\"John H. Watson\""+
+ "]"+
+ "}"+
+ "]"+
+ "}"+
+ "]"+
+ "}";
+
+ Set forceList = new HashSet();
+ forceList.add("addresses");
+ forceList.add("address");
+ forceList.add("name");
+
+ XMLParserConfiguration config =
+ new XMLParserConfiguration()
+ .withForceList(forceList);
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ JSONObject expetedJsonObject = new JSONObject(expectedStr);
+
+ Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
+ }
+ @Test
+ public void testEmptyForceList() {
+ String xmlStr =
+ " ";
+
+ String expectedStr =
+ "{\"addresses\":[]}";
+
+ Set forceList = new HashSet();
+ forceList.add("addresses");
+
+ XMLParserConfiguration config =
+ new XMLParserConfiguration()
+ .withForceList(forceList);
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ JSONObject expetedJsonObject = new JSONObject(expectedStr);
+
+ Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
+ }
+ @Test
+ public void testContentForceList() {
+ String xmlStr =
+ "Baker Street ";
+
+ String expectedStr =
+ "{\"addresses\":[\"Baker Street\"]}";
+
+ Set forceList = new HashSet();
+ forceList.add("addresses");
+
+ XMLParserConfiguration config =
+ new XMLParserConfiguration()
+ .withForceList(forceList);
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ JSONObject expetedJsonObject = new JSONObject(expectedStr);
+
+ Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
+ }
+ @Test
+ public void testEmptyTagForceList() {
+ String xmlStr =
+ " ";
+
+ String expectedStr =
+ "{\"addresses\":[]}";
+
+ Set forceList = new HashSet();
+ forceList.add("addresses");
+
+ XMLParserConfiguration config =
+ new XMLParserConfiguration()
+ .withForceList(forceList);
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ JSONObject expetedJsonObject = new JSONObject(expectedStr);
+
+ Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
+ }
+
+ @Test
+ public void testMaxNestingDepthIsSet() {
+ XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration.ORIGINAL;
+
+ assertEquals(xmlParserConfiguration.getMaxNestingDepth(), XMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);
+
+ xmlParserConfiguration = xmlParserConfiguration.withMaxNestingDepth(42);
+
+ assertEquals(xmlParserConfiguration.getMaxNestingDepth(), 42);
+
+ xmlParserConfiguration = xmlParserConfiguration.withMaxNestingDepth(0);
+
+ assertEquals(xmlParserConfiguration.getMaxNestingDepth(), 0);
+
+ xmlParserConfiguration = xmlParserConfiguration.withMaxNestingDepth(-31415926);
+
+ assertEquals(xmlParserConfiguration.getMaxNestingDepth(), XMLParserConfiguration.UNDEFINED_MAXIMUM_NESTING_DEPTH);
+
+ xmlParserConfiguration = xmlParserConfiguration.withMaxNestingDepth(Integer.MIN_VALUE);
+
+ assertEquals(xmlParserConfiguration.getMaxNestingDepth(), XMLParserConfiguration.UNDEFINED_MAXIMUM_NESTING_DEPTH);
+ }
+
+ /**
+ * Convenience method, given an input string and expected result,
+ * convert to JSONObject and compare actual to expected result.
+ * @param xmlStr the string to parse
+ * @param expectedStr the expected JSON string
+ * @param config provides more flexible XML parsing
+ * flexible XML parsing.
+ */
+ private void compareStringToJSONObject(String xmlStr, String expectedStr,
+ XMLParserConfiguration config) {
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Convenience method, given an input string and expected result,
+ * convert to JSONObject via reader and compare actual to expected result.
+ * @param xmlStr the string to parse
+ * @param expectedStr the expected JSON string
+ * @param config provides more flexible XML parsing
+ */
+ private void compareReaderToJSONObject(String xmlStr, String expectedStr,
+ XMLParserConfiguration config) {
+ /*
+ * Commenting out this method until the JSON-java code is updated
+ * to support XML.toJSONObject(reader)
+ */
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Reader reader = new StringReader(xmlStr);
+ try {
+ JSONObject jsonObject = XML.toJSONObject(reader, config);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ } catch (Exception e) {
+ assertTrue("Reader error: " +e.getMessage(), false);
+ } finally {
+ try {
+ reader.close();
+ } catch (Exception e) {}
+ }
+ }
+
+ /**
+ * Convenience method, given an input string and expected result, convert to
+ * JSONObject via file and compare actual to expected result.
+ *
+ * @param xmlStr
+ * the string to parse
+ * @param expectedStr
+ * the expected JSON string
+ * @throws IOException
+ */
+ private void compareFileToJSONObject(String xmlStr, String expectedStr) {
+ /*
+ * Commenting out this method until the JSON-java code is updated
+ * to support XML.toJSONObject(reader)
+ */
+ try {
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
+ FileWriter fileWriter = new FileWriter(tempFile);
+ try {
+ fileWriter.write(xmlStr);
+ } finally {
+ fileWriter.close();
+ }
+
+ Reader reader = new FileReader(tempFile);
+ try {
+ JSONObject jsonObject = XML.toJSONObject(reader);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ } finally {
+ reader.close();
+ }
+ } catch (IOException e) {
+ assertTrue("Error: " +e.getMessage(), false);
+ }
+ }
+}
diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java
new file mode 100644
index 000000000..2fa5daeea
--- /dev/null
+++ b/src/test/java/org/json/junit/XMLTest.java
@@ -0,0 +1,1432 @@
+package org.json.junit;
+
+/*
+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.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.json.*;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+
+/**
+ * Tests for JSON-Java XML.java
+ * Note: noSpace() will be tested by JSONMLTest
+ */
+public class XMLTest {
+ /**
+ * JUnit supports temporary files and folders that are cleaned up after the test.
+ * https://garygregory.wordpress.com/2010/01/20/junit-tip-use-rules-to-manage-temporary-files-and-folders/
+ */
+ @Rule
+ public TemporaryFolder testFolder = new TemporaryFolder();
+
+
+ /**
+ * JSONObject from a null XML string.
+ * Expects a NullPointerException
+ */
+ @Test(expected=NullPointerException.class)
+ public void shouldHandleNullXML() {
+ String xmlStr = null;
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ }
+
+ /**
+ * Empty JSONObject from an empty XML string.
+ */
+ @Test
+ public void shouldHandleEmptyXML() {
+
+ String xmlStr = "";
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("jsonObject should be empty", jsonObject.isEmpty());
+ }
+
+ /**
+ * Empty JSONObject from a non-XML string.
+ */
+ @Test
+ public void shouldHandleNonXML() {
+ String xmlStr = "{ \"this is\": \"not xml\"}";
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("xml string should be empty", jsonObject.isEmpty());
+ }
+
+ /**
+ * Invalid XML string (tag contains a frontslash).
+ * Expects a JSONException
+ */
+ @Test
+ public void shouldHandleInvalidSlashInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " abc street \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped tag at 176 [character 14 line 4]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string ('!' char in tag)
+ * Expects a JSONException
+ */
+ @Test
+ public void shouldHandleInvalidBangInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped meta tag at 214 [character 12 line 7]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string ('!' char and no closing tag brace)
+ * Expects a JSONException
+ */
+ @Test
+ public void shouldHandleInvalidBangNoCloseInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misshaped meta tag at 213 [character 12 line 7]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string (no end brace for tag)
+ * Expects JSONException
+ */
+ @Test
+ public void shouldHandleNoCloseStartTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Misplaced '<' at 193 [character 4 line 6]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Invalid XML string (partial CDATA chars in tag name)
+ * Expects JSONException
+ */
+ @Test
+ public void shouldHandleInvalidCDATABangInTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " Joe Tester \n"+
+ " \n"+
+ " \n"+
+ " ";
+ try {
+ XML.toJSONObject(xmlStr);
+ fail("Expecting a JSONException");
+ } catch (JSONException e) {
+ assertEquals("Expecting an exception message",
+ "Expected 'CDATA[' at 204 [character 11 line 5]",
+ e.getMessage());
+ }
+ }
+
+ /**
+ * Null JSONObject in XML.toString()
+ */
+ @Test
+ public void shouldHandleNullJSONXML() {
+ JSONObject jsonObject= null;
+ String actualXml=XML.toString(jsonObject);
+ assertEquals("generated XML does not equal expected XML","\"null\"",actualXml);
+ }
+
+ /**
+ * Empty JSONObject in XML.toString()
+ */
+ @Test
+ public void shouldHandleEmptyJSONXML() {
+ JSONObject jsonObject= new JSONObject();
+ String xmlStr = XML.toString(jsonObject);
+ assertTrue("xml string should be empty", xmlStr.isEmpty());
+ }
+
+ /**
+ * No SML start tag. The ending tag ends up being treated as content.
+ */
+ @Test
+ public void shouldHandleNoStartTag() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ " >\n"+
+ " \n"+
+ " ";
+ String expectedStr =
+ "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\",\""+
+ "content\":\">\"},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Valid XML to JSONObject
+ */
+ @Test
+ public void shouldHandleSimpleXML() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " Joe Tester \n"+
+ " [CDATA[Baker street 5] \n"+
+ " \n"+
+ " true \n"+
+ " false \n"+
+ " null \n"+
+ " 42 \n"+
+ " -23 \n"+
+ " -23.45 \n"+
+ " -23x.45 \n"+
+ " 1, 2, 3, 4.1, 5.2 \n"+
+ " \n"+
+ " ";
+
+ String expectedStr =
+ "{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
+ "\"name\":\"Joe Tester\",\"NothingHere\":\"\",\"TrueValue\":true,\n"+
+ "\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
+ "\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":\"-23x.45\",\n"+
+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
+ "},\"xsi:noNamespaceSchemaLocation\":"+
+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
+ "XMLSchema-instance\"}}";
+
+ compareStringToJSONObject(xmlStr, expectedStr);
+ compareReaderToJSONObject(xmlStr, expectedStr);
+ compareFileToJSONObject(xmlStr, expectedStr);
+ }
+
+ /**
+ * Tests to verify that supported escapes in XML are converted to actual values.
+ */
+ @Test
+ public void testXmlEscapeToJson(){
+ String xmlStr =
+ "\n"+
+ ""+
+ "\" "+
+ "A €33 "+
+ "A €22€ "+
+ "some text © "+
+ "" " & ' < > "+
+ "𝄢 𐅥 " +
+ " ";
+ String expectedStr =
+ "{\"root\":{" +
+ "\"rawQuote\":\"\\\"\"," +
+ "\"euro\":\"A €33\"," +
+ "\"euroX\":\"A €22€\"," +
+ "\"unknown\":\"some text ©\"," +
+ "\"known\":\"\\\" \\\" & ' < >\"," +
+ "\"high\":\"𝄢 𐅥\""+
+ "}}";
+
+ compareStringToJSONObject(xmlStr, expectedStr);
+ compareReaderToJSONObject(xmlStr, expectedStr);
+ compareFileToJSONObject(xmlStr, expectedStr);
+ }
+
+ /**
+ * Tests that control characters are escaped.
+ */
+ @Test
+ public void testJsonToXmlEscape(){
+ final String jsonSrc = "{\"amount\":\"10,00 €\","
+ + "\"description\":\"Ação Válida\u0085\","
+ + "\"xmlEntities\":\"\\\" ' & < >\""
+ + "}";
+ JSONObject json = new JSONObject(jsonSrc);
+ String xml = XML.toString(json);
+ //test control character not existing
+ assertFalse("Escaping \u0085 failed. Found in XML output.", xml.contains("\u0085"));
+ assertTrue("Escaping \u0085 failed. Entity not found in XML output.", xml.contains("
"));
+ // test normal unicode existing
+ assertTrue("Escaping € failed. Not found in XML output.", xml.contains("€"));
+ assertTrue("Escaping ç failed. Not found in XML output.", xml.contains("ç"));
+ assertTrue("Escaping ã failed. Not found in XML output.", xml.contains("ã"));
+ assertTrue("Escaping á failed. Not found in XML output.", xml.contains("á"));
+ // test XML Entities converted
+ assertTrue("Escaping \" failed. Not found in XML output.", xml.contains("""));
+ assertTrue("Escaping ' failed. Not found in XML output.", xml.contains("'"));
+ assertTrue("Escaping & failed. Not found in XML output.", xml.contains("&"));
+ assertTrue("Escaping < failed. Not found in XML output.", xml.contains("<"));
+ assertTrue("Escaping > failed. Not found in XML output.", xml.contains(">"));
+ }
+
+ /**
+ * Valid XML with comments to JSONObject
+ */
+ @Test
+ public void shouldHandleCommentsInXML() {
+
+ String xmlStr =
+ "\n"+
+ "\n"+
+ "\n"+
+ " \n"+
+ " comment ]]>\n"+
+ " Joe Tester \n"+
+ " \n"+
+ " Baker street 5 \n"+
+ " \n"+
+ " ";
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+
+ "street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+
+ " comment \"}}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Valid XML to XML.toString()
+ */
+ @Test
+ public void shouldHandleToString() {
+ String xmlStr =
+ "\n"+
+ "\n"+
+ " \n"+
+ " [CDATA[Joe & T > e < s " t ' er]] \n"+
+ " Baker street 5 \n"+
+ " 1, 2, 3, 4.1, 5.2 \n"+
+ " \n"+
+ " ";
+
+ String expectedStr =
+ "{\"addresses\":{\"address\":{\"street\":\"Baker street 5\","+
+ "\"name\":\"[CDATA[Joe & T > e < s \\\" t \\\' er]]\","+
+ "\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
+ "},\"xsi:noNamespaceSchemaLocation\":"+
+ "\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
+ "XMLSchema-instance\"}}";
+
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ String xmlToStr = XML.toString(jsonObject);
+ JSONObject finalJsonObject = XML.toJSONObject(xmlToStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Converting a JSON doc containing '>' content to JSONObject, then
+ * XML.toString() should result in valid XML.
+ */
+ @Test
+ public void shouldHandleContentNoArraytoString() {
+ String expectedStr = "{\"addresses\":{\"content\":\">\"}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ String finalStr = XML.toString(expectedJsonObject);
+ String expectedFinalStr = "> ";
+ assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+
+ finalStr+"]", expectedFinalStr, finalStr);
+ }
+
+ /**
+ * Converting a JSON doc containing a 'content' array to JSONObject, then
+ * XML.toString() should result in valid XML.
+ * TODO: This is probably an error in how the 'content' keyword is used.
+ */
+ @Test
+ public void shouldHandleContentArraytoString() {
+ String expectedStr =
+ "{\"addresses\":{" +
+ "\"content\":[1, 2, 3]}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ String finalStr = XML.toString(expectedJsonObject);
+ String expectedFinalStr = ""+
+ "1\n2\n3 ";
+ assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+
+ finalStr+"]", expectedFinalStr, finalStr);
+ }
+
+ /**
+ * Converting a JSON doc containing a named array to JSONObject, then
+ * XML.toString() should result in valid XML.
+ */
+ @Test
+ public void shouldHandleArraytoString() {
+ String expectedStr =
+ "{\"addresses\":{"+
+ "\"something\":[1, 2, 3]}}";
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ String finalStr = XML.toString(expectedJsonObject);
+ String expectedFinalStr = ""+
+ "1 2 3 "+
+ " ";
+ assertEquals("Should handle expectedFinal: ["+expectedStr+"] final: ["+
+ finalStr+"]", expectedFinalStr, finalStr);
+ }
+
+ /**
+ * Tests that the XML output for empty arrays is consistent.
+ */
+ @Test
+ public void shouldHandleEmptyArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("array",new Object[]{});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("array",new JSONArray());
+
+ final String expected = " ";
+ String output1 = XML.toString(jo1,"jo");
+ assertEquals("Expected an empty root tag", expected, output1);
+ String output2 = XML.toString(jo2,"jo");
+ assertEquals("Expected an empty root tag", expected, output2);
+ }
+
+ /**
+ * Tests that the XML output for arrays is consistent when an internal array is empty.
+ */
+ @Test
+ public void shouldHandleEmptyMultiArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("arr",new Object[]{"One", new String[]{}, "Four"});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{}), "Four"}));
+
+ final String expected = "One Four ";
+ String output1 = XML.toString(jo1,"jo");
+ assertEquals("Expected a matching array", expected, output1);
+ String output2 = XML.toString(jo2,"jo");
+ assertEquals("Expected a matching array", expected, output2);
+ }
+
+ /**
+ * Tests that the XML output for arrays is consistent when arrays are not empty.
+ */
+ @Test
+ public void shouldHandleNonEmptyArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("arr",new String[]{"One", "Two", "Three"});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("arr",new JSONArray(new String[]{"One", "Two", "Three"}));
+
+ final String expected = "One Two Three ";
+ String output1 = XML.toString(jo1,"jo");
+ assertEquals("Expected a non empty root tag", expected, output1);
+ String output2 = XML.toString(jo2,"jo");
+ assertEquals("Expected a non empty root tag", expected, output2);
+ }
+
+ /**
+ * Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays.
+ */
+ @Test
+ public void shouldHandleMultiArray(){
+ final JSONObject jo1 = new JSONObject();
+ jo1.put("arr",new Object[]{"One", new String[]{"Two", "Three"}, "Four"});
+ final JSONObject jo2 = new JSONObject();
+ jo2.put("arr",new JSONArray(new Object[]{"One", new JSONArray(new String[]{"Two", "Three"}), "Four"}));
+
+ final String expected = "One Two Three Four ";
+ String output1 = XML.toString(jo1,"jo");
+ assertEquals("Expected a matching array", expected, output1);
+ String output2 = XML.toString(jo2,"jo");
+ assertEquals("Expected a matching array", expected, output2);
+ }
+
+ /**
+ * Converting a JSON doc containing a named array of nested arrays to
+ * JSONObject, then XML.toString() should result in valid XML.
+ */
+ @Test
+ public void shouldHandleNestedArraytoString() {
+ String xmlStr =
+ "{\"addresses\":{\"address\":{\"name\":\"\",\"nocontent\":\"\","+
+ "\"outer\":[[1], [2], [3]]},\"xsi:noNamespaceSchemaLocation\":\"test.xsd\",\""+
+ "xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"}}";
+ JSONObject jsonObject = new JSONObject(xmlStr);
+ String finalStr = XML.toString(jsonObject);
+ JSONObject finalJsonObject = XML.toJSONObject(finalStr);
+ String expectedStr = " "+
+ "1 2 "+
+ "3 "+
+ "test.xsdhttp://www.w3.org/2001/XMLSche"+
+ "ma-instance ";
+ JSONObject expectedJsonObject = XML.toJSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
+ }
+
+
+ /**
+ * Possible bug:
+ * Illegal node-names must be converted to legal XML-node-names.
+ * The given example shows 2 nodes which are valid for JSON, but not for XML.
+ * Therefore illegal arguments should be converted to e.g. an underscore (_).
+ */
+ @Test
+ public void shouldHandleIllegalJSONNodeNames()
+ {
+ JSONObject inputJSON = new JSONObject();
+ inputJSON.append("123IllegalNode", "someValue1");
+ inputJSON.append("Illegal@node", "someValue2");
+
+ String result = XML.toString(inputJSON);
+
+ /*
+ * This is invalid XML. Names should not begin with digits or contain
+ * certain values, including '@'. One possible solution is to replace
+ * illegal chars with '_', in which case the expected output would be:
+ * <___IllegalNode>someValue1someValue2
+ */
+ String expected = "<123IllegalNode>someValue1123IllegalNode>someValue2 ";
+
+ assertEquals("length",expected.length(), result.length());
+ assertTrue("123IllegalNode",result.contains("<123IllegalNode>someValue1123IllegalNode>"));
+ assertTrue("Illegal@node",result.contains("someValue2 "));
+ }
+
+ /**
+ * JSONObject with NULL value, to XML.toString()
+ */
+ @Test
+ public void shouldHandleNullNodeValue()
+ {
+ JSONObject inputJSON = new JSONObject();
+ inputJSON.put("nullValue", JSONObject.NULL);
+ // This is a possible preferred result
+ // String expectedXML = " ";
+ /**
+ * This is the current behavior. JSONObject.NULL is emitted as
+ * the string, "null".
+ */
+ String actualXML = "null ";
+ String resultXML = XML.toString(inputJSON);
+ assertEquals(actualXML, resultXML);
+ }
+
+ /**
+ * Investigate exactly how the "content" keyword works
+ */
+ @Test
+ public void contentOperations() {
+ /*
+ * When a standalone 0) then return]]> ";
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("1. 3 items", 3 == jsonObject.length());
+ assertTrue("1. empty tag1", "".equals(jsonObject.get("tag1")));
+ assertTrue("1. empty tag2", "".equals(jsonObject.get("tag2")));
+ assertTrue("1. content found", "if (a < b && a > 0) then return".equals(jsonObject.get("content")));
+
+ // multiple consecutive standalone cdatas are accumulated into an array
+ xmlStr = " 0) then return]]> ";
+ jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("2. 3 items", 3 == jsonObject.length());
+ assertTrue("2. empty tag1", "".equals(jsonObject.get("tag1")));
+ assertTrue("2. empty tag2", "".equals(jsonObject.get("tag2")));
+ assertTrue("2. content array found", jsonObject.get("content") instanceof JSONArray);
+ JSONArray jsonArray = jsonObject.getJSONArray("content");
+ assertTrue("2. array size", jsonArray.length() == 2);
+ assertTrue("2. content array entry 0", "if (a < b && a > 0) then return".equals(jsonArray.get(0)));
+ assertTrue("2. content array entry 1", "here is another cdata".equals(jsonArray.get(1)));
+
+ /*
+ * text content is accumulated in a "content" inside a local JSONObject.
+ * If there is only one instance, it is saved in the context (a different JSONObject
+ * from the calling code. and the content element is discarded.
+ */
+ xmlStr = "value 1 ";
+ jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("3. 2 items", 1 == jsonObject.length());
+ assertTrue("3. value tag1", "value 1".equals(jsonObject.get("tag1")));
+
+ /*
+ * array-style text content (multiple tags with the same name) is
+ * accumulated in a local JSONObject with key="content" and value=JSONArray,
+ * saved in the context, and then the local JSONObject is discarded.
+ */
+ xmlStr = "value 1 2 true ";
+ jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("4. 1 item", 1 == jsonObject.length());
+ assertTrue("4. content array found", jsonObject.get("tag1") instanceof JSONArray);
+ jsonArray = jsonObject.getJSONArray("tag1");
+ assertTrue("4. array size", jsonArray.length() == 3);
+ assertTrue("4. content array entry 0", "value 1".equals(jsonArray.get(0)));
+ assertTrue("4. content array entry 1", jsonArray.getInt(1) == 2);
+ assertTrue("4. content array entry 2", jsonArray.getBoolean(2) == true);
+
+ /*
+ * Complex content is accumulated in a "content" field. For example, an element
+ * may contain a mix of child elements and text. Each text segment is
+ * accumulated to content.
+ */
+ xmlStr = "val1 val2 ";
+ jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("5. 1 item", 1 == jsonObject.length());
+ assertTrue("5. jsonObject found", jsonObject.get("tag1") instanceof JSONObject);
+ jsonObject = jsonObject.getJSONObject("tag1");
+ assertTrue("5. 2 contained items", 2 == jsonObject.length());
+ assertTrue("5. contained tag", "".equals(jsonObject.get("tag2")));
+ assertTrue("5. contained content jsonArray found", jsonObject.get("content") instanceof JSONArray);
+ jsonArray = jsonObject.getJSONArray("content");
+ assertTrue("5. array size", jsonArray.length() == 2);
+ assertTrue("5. content array entry 0", "val1".equals(jsonArray.get(0)));
+ assertTrue("5. content array entry 1", "val2".equals(jsonArray.get(1)));
+
+ /*
+ * If there is only 1 complex text content, then it is accumulated in a
+ * "content" field as a string.
+ */
+ xmlStr = "val1 ";
+ jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("6. 1 item", 1 == jsonObject.length());
+ assertTrue("6. jsonObject found", jsonObject.get("tag1") instanceof JSONObject);
+ jsonObject = jsonObject.getJSONObject("tag1");
+ assertTrue("6. contained content found", "val1".equals(jsonObject.get("content")));
+ assertTrue("6. contained tag2", "".equals(jsonObject.get("tag2")));
+
+ /*
+ * In this corner case, the content sibling happens to have key=content
+ * We end up with an array within an array, and no content element.
+ * This is probably a bug.
+ */
+ xmlStr = "val1 ";
+ jsonObject = XML.toJSONObject(xmlStr);
+ assertTrue("7. 1 item", 1 == jsonObject.length());
+ assertTrue("7. jsonArray found", jsonObject.get("tag1") instanceof JSONArray);
+ jsonArray = jsonObject.getJSONArray("tag1");
+ assertTrue("array size 1", jsonArray.length() == 1);
+ assertTrue("7. contained array found", jsonArray.get(0) instanceof JSONArray);
+ jsonArray = jsonArray.getJSONArray(0);
+ assertTrue("7. inner array size 2", jsonArray.length() == 2);
+ assertTrue("7. inner array item 0", "val1".equals(jsonArray.get(0)));
+ assertTrue("7. inner array item 1", "".equals(jsonArray.get(1)));
+
+ /*
+ * Confirm behavior of original issue
+ */
+ String jsonStr =
+ "{"+
+ "\"Profile\": {"+
+ "\"list\": {"+
+ "\"history\": {"+
+ "\"entries\": ["+
+ "{"+
+ "\"deviceId\": \"id\","+
+ "\"content\": {"+
+ "\"material\": ["+
+ "{"+
+ "\"stuff\": false"+
+ "}"+
+ "]"+
+ "}"+
+ "}"+
+ "]"+
+ "}"+
+ "}"+
+ "}"+
+ "}";
+ jsonObject = new JSONObject(jsonStr);
+ xmlStr = XML.toString(jsonObject);
+ /*
+ * This is the created XML. Looks like content was mistaken for
+ * complex (child node + text) XML.
+ *
+ *
+ *
+ *
+ * id
+ * {"material":[{"stuff":false}]}
+ *
+ *
+ *
+ *
+ */
+ assertTrue("nothing to test here, see comment on created XML, above", true);
+ }
+
+ /**
+ * Convenience method, given an input string and expected result,
+ * convert to JSONObject and compare actual to expected result.
+ * @param xmlStr the string to parse
+ * @param expectedStr the expected JSON string
+ */
+ private void compareStringToJSONObject(String xmlStr, String expectedStr) {
+ JSONObject jsonObject = XML.toJSONObject(xmlStr);
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Convenience method, given an input string and expected result,
+ * convert to JSONObject via reader and compare actual to expected result.
+ * @param xmlStr the string to parse
+ * @param expectedStr the expected JSON string
+ */
+ private void compareReaderToJSONObject(String xmlStr, String expectedStr) {
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ Reader reader = new StringReader(xmlStr);
+ JSONObject jsonObject = XML.toJSONObject(reader);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ }
+
+ /**
+ * Convenience method, given an input string and expected result, convert to
+ * JSONObject via file and compare actual to expected result.
+ *
+ * @param xmlStr
+ * the string to parse
+ * @param expectedStr
+ * the expected JSON string
+ * @throws IOException
+ */
+ private void compareFileToJSONObject(String xmlStr, String expectedStr) {
+ try {
+ JSONObject expectedJsonObject = new JSONObject(expectedStr);
+ File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
+ FileWriter fileWriter = new FileWriter(tempFile);
+ try {
+ fileWriter.write(xmlStr);
+ } finally {
+ fileWriter.close();
+ }
+
+ Reader reader = new FileReader(tempFile);
+ try {
+ JSONObject jsonObject = XML.toJSONObject(reader);
+ Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
+ } finally {
+ reader.close();
+ }
+ } catch (IOException e) {
+ fail("Error: " +e.getMessage());
+ }
+ }
+
+ /**
+ * JSON string lost leading zero and converted "True" to true.
+ */
+ @Test
+ public void testToJSONArray_jsonOutput() {
+ final String originalXml = "01 1 00 0 True ";
+ final JSONObject expectedJson = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}");
+ final JSONObject actualJsonOutput = XML.toJSONObject(originalXml, false);
+
+ Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expectedJson);
+ }
+
+ /**
+ * JSON string cannot be reverted to original xml.
+ */
+ @Test
+ public void testToJSONArray_reversibility() {
+ final String originalXml = "01 1 00 0 True ";
+ final String revertedXml = XML.toString(XML.toJSONObject(originalXml, false));
+
+ assertNotEquals(revertedXml, originalXml);
+ }
+
+ /**
+ * test passes when using the new method toJsonArray.
+ */
+ @Test
+ public void testToJsonXML() {
+ final String originalXml = "01 1 00 0 True ";
+ final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}");
+
+ final JSONObject actual = XML.toJSONObject(originalXml,true);
+
+ Util.compareActualVsExpectedJsonObjects(actual, expected);
+
+ final String reverseXml = XML.toString(actual);
+ // this reversal isn't exactly the same. use JSONML for an exact reversal
+ // the order of the elements may be differnet as well.
+ final String expectedReverseXml = "01 01 1 00 0 True ";
+
+ assertEquals("length",expectedReverseXml.length(), reverseXml.length());
+ assertTrue("array contents", reverseXml.contains("01 1 00 0 "));
+ assertTrue("item contents", reverseXml.contains("01 "));
+ assertTrue("title contents", reverseXml.contains("True "));
+ }
+
+ /**
+ * test to validate certain conditions of XML unescaping.
+ */
+ @Test
+ public void testUnescape() {
+ assertEquals("{\"xml\":\"Can cope <;\"}",
+ XML.toJSONObject("Can cope <; ").toString());
+ assertEquals("Can cope <; ", XML.unescape("Can cope <; "));
+
+ assertEquals("{\"xml\":\"Can cope & ;\"}",
+ XML.toJSONObject("Can cope & ; ").toString());
+ assertEquals("Can cope & ; ", XML.unescape("Can cope & ; "));
+
+ assertEquals("{\"xml\":\"Can cope &;\"}",
+ XML.toJSONObject("Can cope &; ").toString());
+ assertEquals("Can cope &; ", XML.unescape("Can cope &; "));
+
+ // unicode entity
+ assertEquals("{\"xml\":\"Can cope 4;\"}",
+ XML.toJSONObject("Can cope 4; ").toString());
+ assertEquals("Can cope 4; ", XML.unescape("Can cope 4; "));
+
+ // double escaped
+ assertEquals("{\"xml\":\"Can cope <\"}",
+ XML.toJSONObject("Can cope < ").toString());
+ assertEquals("Can cope < ", XML.unescape("Can cope < "));
+
+ assertEquals("{\"xml\":\"Can cope 4\"}",
+ XML.toJSONObject("Can cope 4 ").toString());
+ assertEquals("Can cope 4 ", XML.unescape("Can cope 4 "));
+
+ }
+
+ /**
+ * test passes when xsi:nil="true" converting to null (JSON specification-like nil conversion enabled)
+ */
+ @Test
+ public void testToJsonWithNullWhenNilConversionEnabled() {
+ final String originalXml = " ";
+ final String expectedJsonString = "{\"root\":{\"id\":null}}";
+
+ final JSONObject json = XML.toJSONObject(originalXml,
+ new XMLParserConfiguration()
+ .withKeepStrings(false)
+ .withcDataTagName("content")
+ .withConvertNilAttributeToNull(true));
+ assertEquals(expectedJsonString, json.toString());
+ }
+
+ /**
+ * test passes when xsi:nil="true" not converting to null (JSON specification-like nil conversion disabled)
+ */
+ @Test
+ public void testToJsonWithNullWhenNilConversionDisabled() {
+ final String originalXml = " ";
+ final String expectedJsonString = "{\"root\":{\"id\":{\"xsi:nil\":true}}}";
+
+ final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration());
+ assertEquals(expectedJsonString, json.toString());
+ }
+
+ /**
+ * Tests to verify that supported escapes in XML are converted to actual values.
+ */
+ @Test
+ public void testIssue537CaseSensitiveHexEscapeMinimal(){
+ String xmlStr =
+ "\n"+
+ "Neutrophils.Hypersegmented | Bld-Ser-Plas ";
+ String expectedStr =
+ "{\"root\":\"Neutrophils.Hypersegmented | Bld-Ser-Plas\"}";
+ JSONObject xmlJSONObj = XML.toJSONObject(xmlStr, true);
+ JSONObject expected = new JSONObject(expectedStr);
+ Util.compareActualVsExpectedJsonObjects(xmlJSONObj, expected);
+ }
+
+ /**
+ * Tests to verify that supported escapes in XML are converted to actual values.
+ */
+ @Test
+ public void testIssue537CaseSensitiveHexEscapeFullFile(){
+ try {
+ InputStream xmlStream = null;
+ try {
+ xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue537.xml");
+ Reader xmlReader = new InputStreamReader(xmlStream, Charset.forName("UTF-8"));
+ JSONObject actual = XML.toJSONObject(xmlReader, true);
+ InputStream jsonStream = null;
+ try {
+ jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue537.json");
+ final JSONObject expected = new JSONObject(new JSONTokener(jsonStream));
+ Util.compareActualVsExpectedJsonObjects(actual,expected);
+ } finally {
+ if (jsonStream != null) {
+ jsonStream.close();
+ }
+ }
+ } finally {
+ if (xmlStream != null) {
+ xmlStream.close();
+ }
+ }
+ } catch (IOException e) {
+ fail("file writer error: " +e.getMessage());
+ }
+ }
+
+ /**
+ * Tests to verify that supported escapes in XML are converted to actual values.
+ */
+ @Test
+ public void testIssue537CaseSensitiveHexUnEscapeDirect(){
+ String origStr =
+ "Neutrophils.Hypersegmented | Bld-Ser-Plas";
+ String expectedStr =
+ "Neutrophils.Hypersegmented | Bld-Ser-Plas";
+ String actualStr = XML.unescape(origStr);
+
+ assertEquals("Case insensitive Entity unescape", expectedStr, actualStr);
+ }
+
+ /**
+ * test passes when xsi:type="java.lang.String" not converting to string
+ */
+ @Test
+ public void testToJsonWithTypeWhenTypeConversionDisabled() {
+ String originalXml = "1234 ";
+ String expectedJsonString = "{\"root\":{\"id\":{\"xsi:type\":\"string\",\"content\":1234}}}";
+ JSONObject expectedJson = new JSONObject(expectedJsonString);
+ JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration());
+ Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
+ }
+
+ /**
+ * test passes when xsi:type="java.lang.String" converting to String
+ */
+ @Test
+ public void testToJsonWithTypeWhenTypeConversionEnabled() {
+ String originalXml = "1234 "
+ + "1234 ";
+ String expectedJsonString = "{\"root\":{\"id2\":1234,\"id1\":\"1234\"}}";
+ JSONObject expectedJson = new JSONObject(expectedJsonString);
+ Map> xsiTypeMap = new HashMap>();
+ xsiTypeMap.put("string", new XMLXsiTypeConverter() {
+ @Override public String convert(final String value) {
+ return value;
+ }
+ });
+ xsiTypeMap.put("integer", new XMLXsiTypeConverter() {
+ @Override public Integer convert(final String value) {
+ return Integer.valueOf(value);
+ }
+ });
+ JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
+ Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
+ }
+
+ @Test
+ public void testToJsonWithXSITypeWhenTypeConversionEnabled() {
+ String originalXml = "12345 54321 ";
+ String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}";
+ JSONObject expectedJson = new JSONObject(expectedJsonString);
+ Map> xsiTypeMap = new HashMap>();
+ xsiTypeMap.put("string", new XMLXsiTypeConverter() {
+ @Override public String convert(final String value) {
+ return value;
+ }
+ });
+ xsiTypeMap.put("integer", new XMLXsiTypeConverter() {
+ @Override public Integer convert(final String value) {
+ return Integer.valueOf(value);
+ }
+ });
+ JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
+ Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
+ }
+
+ @Test
+ public void testToJsonWithXSITypeWhenTypeConversionNotEnabledOnOne() {
+ String originalXml = "12345 54321 ";
+ String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}";
+ JSONObject expectedJson = new JSONObject(expectedJsonString);
+ Map> xsiTypeMap = new HashMap>();
+ xsiTypeMap.put("string", new XMLXsiTypeConverter() {
+ @Override public String convert(final String value) {
+ return value;
+ }
+ });
+ JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap));
+ Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
+ }
+
+ @Test
+ public void testXSITypeMapNotModifiable() {
+ Map> xsiTypeMap = new HashMap>();
+ XMLParserConfiguration config = new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap);
+ xsiTypeMap.put("string", new XMLXsiTypeConverter() {
+ @Override public String convert(final String value) {
+ return value;
+ }
+ });
+ assertEquals("Config Conversion Map size is expected to be 0", 0, config.getXsiTypeMap().size());
+
+ try {
+ config.getXsiTypeMap().put("boolean", new XMLXsiTypeConverter() {
+ @Override public Boolean convert(final String value) {
+ return Boolean.valueOf(value);
+ }
+ });
+ fail("Expected to be unable to modify the config");
+ } catch (Exception ignored) { }
+ }
+
+ @Test
+ public void testIndentComplicatedJsonObject(){
+ String str = "{\n" +
+ " \"success\": true,\n" +
+ " \"error\": null,\n" +
+ " \"response\": [\n" +
+ " {\n" +
+ " \"timestamp\": 1664917200,\n" +
+ " \"dateTimeISO\": \"2022-10-05T00:00:00+03:00\",\n" +
+ " \"loc\": {\n" +
+ " \"lat\": 39.91987,\n" +
+ " \"long\": 32.85427\n" +
+ " },\n" +
+ " \"place\": {\n" +
+ " \"name\": \"ankara\",\n" +
+ " \"state\": \"an\",\n" +
+ " \"country\": \"tr\"\n" +
+ " },\n" +
+ " \"profile\": {\n" +
+ " \"tz\": \"Europe/Istanbul\"\n" +
+ " },\n" +
+ " \"sun\": {\n" +
+ " \"rise\": 1664941721,\n" +
+ " \"riseISO\": \"2022-10-05T06:48:41+03:00\",\n" +
+ " \"set\": 1664983521,\n" +
+ " \"setISO\": \"2022-10-05T18:25:21+03:00\",\n" +
+ " \"transit\": 1664962621,\n" +
+ " \"transitISO\": \"2022-10-05T12:37:01+03:00\",\n" +
+ " \"midnightSun\": false,\n" +
+ " \"polarNight\": false,\n" +
+ " \"twilight\": {\n" +
+ " \"civilBegin\": 1664940106,\n" +
+ " \"civilBeginISO\": \"2022-10-05T06:21:46+03:00\",\n" +
+ " \"civilEnd\": 1664985136,\n" +
+ " \"civilEndISO\": \"2022-10-05T18:52:16+03:00\",\n" +
+ " \"nauticalBegin\": 1664938227,\n" +
+ " \"nauticalBeginISO\": \"2022-10-05T05:50:27+03:00\",\n" +
+ " \"nauticalEnd\": 1664987015,\n" +
+ " \"nauticalEndISO\": \"2022-10-05T19:23:35+03:00\",\n" +
+ " \"astronomicalBegin\": 1664936337,\n" +
+ " \"astronomicalBeginISO\": \"2022-10-05T05:18:57+03:00\",\n" +
+ " \"astronomicalEnd\": 1664988905,\n" +
+ " \"astronomicalEndISO\": \"2022-10-05T19:55:05+03:00\"\n" +
+ " }\n" +
+ " },\n" +
+ " \"moon\": {\n" +
+ " \"rise\": 1664976480,\n" +
+ " \"riseISO\": \"2022-10-05T16:28:00+03:00\",\n" +
+ " \"set\": 1664921520,\n" +
+ " \"setISO\": \"2022-10-05T01:12:00+03:00\",\n" +
+ " \"transit\": 1664994240,\n" +
+ " \"transitISO\": \"2022-10-05T21:24:00+03:00\",\n" +
+ " \"underfoot\": 1664949360,\n" +
+ " \"underfootISO\": \"2022-10-05T08:56:00+03:00\",\n" +
+ " \"phase\": {\n" +
+ " \"phase\": 0.3186,\n" +
+ " \"name\": \"waxing gibbous\",\n" +
+ " \"illum\": 71,\n" +
+ " \"age\": 9.41,\n" +
+ " \"angle\": 0.55\n" +
+ " }\n" +
+ " }\n" +
+ " }\n" +
+ " ]\n" +
+ "}" ;
+ JSONObject jsonObject = new JSONObject(str);
+ String actualIndentedXmlString = XML.toString(jsonObject, 1);
+ JSONObject actualJsonObject = XML.toJSONObject(actualIndentedXmlString);
+ String expected = "