1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Compare commits

..

No commits in common. "master" and "20180813" have entirely different histories.

72 changed files with 1242 additions and 13653 deletions

View file

@ -1,74 +0,0 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java CI with Maven
on:
push:
# branches: [ master ]
pull_request:
branches: [ master ]
jobs:
# old-school build and jar method. No tests run or compiled.
build-1_6:
runs-on: ubuntu-16.04
strategy:
matrix:
# build for java 1.6, however don't run any tests
java: [ 1.6 ]
name: Java ${{ matrix.java }}
steps:
- uses: actions/checkout@v2
- name: Setup java
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Compile Java ${{ matrix.java }}
run: |
mkdir -p target/classes
javac -d target/classes/ src/main/java/org/json/*.java
- name: Create java ${{ matrix.java }} JAR
run: |
jar cvf target/org.json.jar -C target/classes .
- name: Upload Java ${{ matrix.java }} JAR
uses: actions/upload-artifact@v1
with:
name: Java ${{ matrix.java }} JAR
path: target/org.json.jar
build:
runs-on: ubuntu-16.04
strategy:
matrix:
# build against supported Java LTS versions:
java: [ 1.7, 8, 11 ]
name: Java ${{ matrix.java }}
steps:
- uses: actions/checkout@v2
- name: Setup java
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Compile Java ${{ matrix.java }}
run: mvn clean compile -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.javadoc.skip=true
- name: Run Tests ${{ matrix.java }}
run: |
mvn test -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }}
- name: Build Test Report ${{ matrix.java }}
if: ${{ always() }}
run: |
mvn surefire-report:report-only -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }}
mvn site -DgenerateReports=false -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.target=${{ matrix.java }}
- name: Upload Test Results ${{ matrix.java }}
if: ${{ always() }}
uses: actions/upload-artifact@v1
with:
name: Test Results ${{ matrix.java }}
path: target/surefire-reports/
- name: Upload Test Report ${{ matrix.java }}
if: ${{ always() }}
uses: actions/upload-artifact@v1
with:
name: Test Report ${{ matrix.java }}
path: target/site/

10
.gitignore vendored
View file

@ -4,13 +4,3 @@
# ignore Intellij Idea project files # ignore Intellij Idea project files
.idea .idea
*.iml *.iml
/target/
/bin/
build
.settings/
/.gradle/
/gradle/
/gradlew
/gradlew.bat
.gitmodules

View file

@ -55,7 +55,7 @@ public class CDL {
private static String getValue(JSONTokener x) throws JSONException { private static String getValue(JSONTokener x) throws JSONException {
char c; char c;
char q; char q;
StringBuilder sb; StringBuffer sb;
do { do {
c = x.next(); c = x.next();
} while (c == ' ' || c == '\t'); } while (c == ' ' || c == '\t');
@ -65,7 +65,7 @@ public class CDL {
case '"': case '"':
case '\'': case '\'':
q = c; q = c;
sb = new StringBuilder(); sb = new StringBuffer();
for (;;) { for (;;) {
c = x.next(); c = x.next();
if (c == q) { if (c == q) {
@ -98,7 +98,7 @@ public class CDL {
* Produce a JSONArray of strings from a row of comma delimited values. * Produce a JSONArray of strings from a row of comma delimited values.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @return A JSONArray of strings. * @return A JSONArray of strings.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
JSONArray ja = new JSONArray(); JSONArray ja = new JSONArray();
@ -134,7 +134,7 @@ public class CDL {
* method. * method.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @return A JSONObject combining the names and values. * @return A JSONObject combining the names and values.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
throws JSONException { throws JSONException {
@ -184,7 +184,7 @@ public class CDL {
* using the first row as a source of names. * using the first row as a source of names.
* @param string The comma delimited text. * @param string The comma delimited text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONArray toJSONArray(String string) throws JSONException { public static JSONArray toJSONArray(String string) throws JSONException {
return toJSONArray(new JSONTokener(string)); return toJSONArray(new JSONTokener(string));
@ -195,7 +195,7 @@ public class CDL {
* using the first row as a source of names. * using the first row as a source of names.
* @param x The JSONTokener containing the comma delimited text. * @param x The JSONTokener containing the comma delimited text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONArray toJSONArray(JSONTokener x) throws JSONException { public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
return toJSONArray(rowToJSONArray(x), x); return toJSONArray(rowToJSONArray(x), x);
@ -207,7 +207,7 @@ public class CDL {
* @param names A JSONArray of strings. * @param names A JSONArray of strings.
* @param string The comma delimited text. * @param string The comma delimited text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONArray toJSONArray(JSONArray names, String string) public static JSONArray toJSONArray(JSONArray names, String string)
throws JSONException { throws JSONException {
@ -220,7 +220,7 @@ public class CDL {
* @param names A JSONArray of strings. * @param names A JSONArray of strings.
* @param x A JSONTokener of the source text. * @param x A JSONTokener of the source text.
* @return A JSONArray of JSONObjects. * @return A JSONArray of JSONObjects.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONArray toJSONArray(JSONArray names, JSONTokener x) public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
throws JSONException { throws JSONException {
@ -248,7 +248,7 @@ public class CDL {
* JSONObject. * JSONObject.
* @param ja A JSONArray of JSONObjects. * @param ja A JSONArray of JSONObjects.
* @return A comma delimited text. * @return A comma delimited text.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static String toString(JSONArray ja) throws JSONException { public static String toString(JSONArray ja) throws JSONException {
JSONObject jo = ja.optJSONObject(0); JSONObject jo = ja.optJSONObject(0);
@ -268,14 +268,14 @@ public class CDL {
* @param names A JSONArray of strings. * @param names A JSONArray of strings.
* @param ja A JSONArray of JSONObjects. * @param ja A JSONArray of JSONObjects.
* @return A comma delimited text. * @return A comma delimited text.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static String toString(JSONArray names, JSONArray ja) public static String toString(JSONArray names, JSONArray ja)
throws JSONException { throws JSONException {
if (names == null || names.length() == 0) { if (names == null || names.length() == 0) {
return null; return null;
} }
StringBuilder sb = new StringBuilder(); StringBuffer sb = new StringBuffer();
for (int i = 0; i < ja.length(); i += 1) { for (int i = 0; i < ja.length(); i += 1) {
JSONObject jo = ja.optJSONObject(i); JSONObject jo = ja.optJSONObject(i);
if (jo != null) { if (jo != null) {

View file

@ -1,7 +1,5 @@
package org.json; package org.json;
import java.util.Locale;
/* /*
Copyright (c) 2002 JSON.org Copyright (c) 2002 JSON.org
@ -29,7 +27,6 @@ SOFTWARE.
/** /**
* Convert a web browser cookie specification to a JSONObject and back. * Convert a web browser cookie specification to a JSONObject and back.
* JSON and Cookies are both notations for name/value pairs. * JSON and Cookies are both notations for name/value pairs.
* See also: <a href="https://tools.ietf.org/html/rfc6265">https://tools.ietf.org/html/rfc6265</a>
* @author JSON.org * @author JSON.org
* @version 2015-12-09 * @version 2015-12-09
*/ */
@ -68,65 +65,41 @@ public class Cookie {
/** /**
* Convert a cookie specification string into a JSONObject. The string * Convert a cookie specification string into a JSONObject. The string
* must contain a name value pair separated by '='. The name and the value * will contain a name value pair separated by '='. The name and the value
* will be unescaped, possibly converting '+' and '%' sequences. The * will be unescaped, possibly converting '+' and '%' sequences. The
* cookie properties may follow, separated by ';', also represented as * cookie properties may follow, separated by ';', also represented as
* name=value (except the Attribute properties like "Secure" or "HttpOnly", * name=value (except the secure property, which does not have a value).
* 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 * 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 * stored under the key "value". This method does not do checking or
* validation of the parameters. It only converts the cookie string into * validation of the parameters. It only converts the cookie string into
* a JSONObject. All attribute names are converted to lower case keys in the * a JSONObject.
* JSONObject (HttpOnly =&gt; 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. * @param string The cookie specification string.
* @return A JSONObject containing "name", "value", and possibly other * @return A JSONObject containing "name", "value", and possibly other
* members. * members.
* @throws JSONException If there is an error parsing the Cookie String. * @throws JSONException
* 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) { public static JSONObject toJSONObject(String string) throws JSONException {
final JSONObject jo = new JSONObject();
String name; String name;
JSONObject jo = new JSONObject();
Object value; Object value;
JSONTokener x = new JSONTokener(string); JSONTokener x = new JSONTokener(string);
jo.put("name", x.nextTo('='));
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('='); x.next('=');
jo.put("value", unescape(x.nextTo(';')).trim()); jo.put("value", x.nextTo(';'));
// discard the ';'
x.next(); x.next();
// parse the remaining cookie attributes
while (x.more()) { while (x.more()) {
name = unescape(x.nextTo("=;")).trim().toLowerCase(Locale.ROOT); name = unescape(x.nextTo("=;"));
// don't allow a cookies attributes to overwrite it's 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() != '=') { if (x.next() != '=') {
value = Boolean.TRUE; if (name.equals("secure")) {
value = Boolean.TRUE;
} else {
throw x.syntaxError("Missing '=' in cookie parameter.");
}
} else { } else {
value = unescape(x.nextTo(';')).trim(); value = unescape(x.nextTo(';'));
x.next(); x.next();
} }
// only store non-blank attributes jo.put(name, value);
if(!"".equals(name) && !"".equals(value)) {
jo.put(name, value);
}
} }
return jo; return jo;
} }
@ -134,63 +107,35 @@ public class Cookie {
/** /**
* Convert a JSONObject into a cookie specification string. The JSONObject * Convert a JSONObject into a cookie specification string. The JSONObject
* must contain "name" and "value" members (case insensitive). * must contain "name" and "value" members.
* If the JSONObject contains other members, they will be appended to the cookie * If the JSONObject contains "expires", "domain", "path", or "secure"
* specification string. User-Agents are instructed to ignore unknown attributes, * members, they will be appended to the cookie specification string.
* so ensure your JSONObject is using only known attributes. * All other members are ignored.
* See also: <a href="https://tools.ietf.org/html/rfc6265">https://tools.ietf.org/html/rfc6265</a>
* @param jo A JSONObject * @param jo A JSONObject
* @return A cookie specification string * @return A cookie specification string
* @throws JSONException thrown if the cookie has no name. * @throws JSONException
*/ */
public static String toString(JSONObject jo) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String name = null; sb.append(escape(jo.getString("name")));
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("=");
sb.append(escape((String)value)); sb.append(escape(jo.getString("value")));
if (jo.has("expires")) {
for(String key : jo.keySet()){ sb.append(";expires=");
if("name".equalsIgnoreCase(key) sb.append(jo.getString("expires"));
|| "value".equalsIgnoreCase(key)) { }
// already processed above if (jo.has("domain")) {
continue; sb.append(";domain=");
} sb.append(escape(jo.getString("domain")));
value = jo.opt(key); }
if(value instanceof Boolean) { if (jo.has("path")) {
if(Boolean.TRUE.equals(value)) { sb.append(";path=");
sb.append(';').append(escape(key)); sb.append(escape(jo.getString("path")));
} }
// don't emit false values if (jo.optBoolean("secure")) {
} else { sb.append(";secure");
sb.append(';')
.append(escape(key))
.append('=')
.append(escape(value.toString()));
}
} }
return sb.toString(); return sb.toString();
} }

View file

@ -42,7 +42,7 @@ public class CookieList {
* cookieJSONObject.getString("value")); * cookieJSONObject.getString("value"));
* @param string A cookie list string * @param string A cookie list string
* @return A JSONObject * @return A JSONObject
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONObject toJSONObject(String string) throws JSONException { public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject();
@ -63,7 +63,7 @@ public class CookieList {
* in the names and values are replaced by "%hh". * in the names and values are replaced by "%hh".
* @param jo A JSONObject * @param jo A JSONObject
* @return A cookie list string * @return A cookie list string
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static String toString(JSONObject jo) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
boolean b = false; boolean b = false;

View file

@ -1,162 +1,162 @@
package org.json; package org.json;
/* /*
Copyright (c) 2002 JSON.org Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
The Software shall be used for Good, not Evil. The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
import java.util.Locale; import java.util.Locale;
/** /**
* Convert an HTTP header to a JSONObject and back. * Convert an HTTP header to a JSONObject and back.
* @author JSON.org * @author JSON.org
* @version 2015-12-09 * @version 2015-12-09
*/ */
public class HTTP { public class HTTP {
/** Carriage return/line feed. */ /** Carriage return/line feed. */
public static final String CRLF = "\r\n"; public static final String CRLF = "\r\n";
/** /**
* Convert an HTTP header string into a JSONObject. It can be a request * Convert an HTTP header string into a JSONObject. It can be a request
* header or a response header. A request header will contain * header or a response header. A request header will contain
* <pre>{ * <pre>{
* Method: "POST" (for example), * Method: "POST" (for example),
* "Request-URI": "/" (for example), * "Request-URI": "/" (for example),
* "HTTP-Version": "HTTP/1.1" (for example) * "HTTP-Version": "HTTP/1.1" (for example)
* }</pre> * }</pre>
* A response header will contain * A response header will contain
* <pre>{ * <pre>{
* "HTTP-Version": "HTTP/1.1" (for example), * "HTTP-Version": "HTTP/1.1" (for example),
* "Status-Code": "200" (for example), * "Status-Code": "200" (for example),
* "Reason-Phrase": "OK" (for example) * "Reason-Phrase": "OK" (for example)
* }</pre> * }</pre>
* In addition, the other parameters in the header will be captured, using * In addition, the other parameters in the header will be captured, using
* the HTTP field names as JSON names, so that <pre>{@code * the HTTP field names as JSON names, so that <pre>
* Date: Sun, 26 May 2002 18:06:04 GMT * Date: Sun, 26 May 2002 18:06:04 GMT
* Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s
* Cache-Control: no-cache}</pre> * Cache-Control: no-cache</pre>
* become * become
* <pre>{@code * <pre>{...
* Date: "Sun, 26 May 2002 18:06:04 GMT", * Date: "Sun, 26 May 2002 18:06:04 GMT",
* Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s", * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s",
* "Cache-Control": "no-cache", * "Cache-Control": "no-cache",
* ...}</pre> * ...}</pre>
* It does no further checking or conversion. It does not parse dates. * It does no further checking or conversion. It does not parse dates.
* It does not do '%' transforms on URLs. * It does not do '%' transforms on URLs.
* @param string An HTTP header string. * @param string An HTTP header string.
* @return A JSONObject containing the elements and attributes * @return A JSONObject containing the elements and attributes
* of the XML string. * of the XML string.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public static JSONObject toJSONObject(String string) throws JSONException { public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject();
HTTPTokener x = new HTTPTokener(string); HTTPTokener x = new HTTPTokener(string);
String token; String token;
token = x.nextToken(); token = x.nextToken();
if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) { if (token.toUpperCase(Locale.ROOT).startsWith("HTTP")) {
// Response // Response
jo.put("HTTP-Version", token); jo.put("HTTP-Version", token);
jo.put("Status-Code", x.nextToken()); jo.put("Status-Code", x.nextToken());
jo.put("Reason-Phrase", x.nextTo('\0')); jo.put("Reason-Phrase", x.nextTo('\0'));
x.next(); x.next();
} else { } else {
// Request // Request
jo.put("Method", token); jo.put("Method", token);
jo.put("Request-URI", x.nextToken()); jo.put("Request-URI", x.nextToken());
jo.put("HTTP-Version", x.nextToken()); jo.put("HTTP-Version", x.nextToken());
} }
// Fields // Fields
while (x.more()) { while (x.more()) {
String name = x.nextTo(':'); String name = x.nextTo(':');
x.next(':'); x.next(':');
jo.put(name, x.nextTo('\0')); jo.put(name, x.nextTo('\0'));
x.next(); x.next();
} }
return jo; return jo;
} }
/** /**
* Convert a JSONObject into an HTTP header. A request header must contain * Convert a JSONObject into an HTTP header. A request header must contain
* <pre>{ * <pre>{
* Method: "POST" (for example), * Method: "POST" (for example),
* "Request-URI": "/" (for example), * "Request-URI": "/" (for example),
* "HTTP-Version": "HTTP/1.1" (for example) * "HTTP-Version": "HTTP/1.1" (for example)
* }</pre> * }</pre>
* A response header must contain * A response header must contain
* <pre>{ * <pre>{
* "HTTP-Version": "HTTP/1.1" (for example), * "HTTP-Version": "HTTP/1.1" (for example),
* "Status-Code": "200" (for example), * "Status-Code": "200" (for example),
* "Reason-Phrase": "OK" (for example) * "Reason-Phrase": "OK" (for example)
* }</pre> * }</pre>
* Any other members of the JSONObject will be output as HTTP fields. * Any other members of the JSONObject will be output as HTTP fields.
* The result will end with two CRLF pairs. * The result will end with two CRLF pairs.
* @param jo A JSONObject * @param jo A JSONObject
* @return An HTTP header string. * @return An HTTP header string.
* @throws JSONException if the object does not contain enough * @throws JSONException if the object does not contain enough
* information. * information.
*/ */
public static String toString(JSONObject jo) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
sb.append(jo.getString("HTTP-Version")); sb.append(jo.getString("HTTP-Version"));
sb.append(' '); sb.append(' ');
sb.append(jo.getString("Status-Code")); sb.append(jo.getString("Status-Code"));
sb.append(' '); sb.append(' ');
sb.append(jo.getString("Reason-Phrase")); sb.append(jo.getString("Reason-Phrase"));
} else if (jo.has("Method") && jo.has("Request-URI")) { } else if (jo.has("Method") && jo.has("Request-URI")) {
sb.append(jo.getString("Method")); sb.append(jo.getString("Method"));
sb.append(' '); sb.append(' ');
sb.append('"'); sb.append('"');
sb.append(jo.getString("Request-URI")); sb.append(jo.getString("Request-URI"));
sb.append('"'); sb.append('"');
sb.append(' '); sb.append(' ');
sb.append(jo.getString("HTTP-Version")); sb.append(jo.getString("HTTP-Version"));
} else { } else {
throw new JSONException("Not enough material for an HTTP header."); throw new JSONException("Not enough material for an HTTP header.");
} }
sb.append(CRLF); sb.append(CRLF);
// Don't use the new entrySet API to maintain Android support // Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) { for (final String key : jo.keySet()) {
String value = jo.optString(key); String value = jo.optString(key);
if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) && if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) &&
!"Reason-Phrase".equals(key) && !"Method".equals(key) && !"Reason-Phrase".equals(key) && !"Method".equals(key) &&
!"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) { !"Request-URI".equals(key) && !JSONObject.NULL.equals(value)) {
sb.append(key); sb.append(key);
sb.append(": "); sb.append(": ");
sb.append(jo.optString(key)); sb.append(jo.optString(key));
sb.append(CRLF); sb.append(CRLF);
} }
} }
sb.append(CRLF); sb.append(CRLF);
return sb.toString(); return sb.toString();
} }
} }

View file

@ -43,8 +43,8 @@ public class HTTPTokener extends JSONTokener {
/** /**
* Get the next token or string. This is used in parsing HTTP headers. * Get the next token or string. This is used in parsing HTTP headers.
* @throws JSONException
* @return A String. * @return A String.
* @throws JSONException if a syntax error occurs
*/ */
public String nextToken() throws JSONException { public String nextToken() throws JSONException {
char c; char c;

View file

@ -173,41 +173,30 @@ public class JSONArray implements Iterable<Object> {
this.myArrayList = new ArrayList<Object>(); this.myArrayList = new ArrayList<Object>();
} else { } else {
this.myArrayList = new ArrayList<Object>(collection.size()); this.myArrayList = new ArrayList<Object>(collection.size());
this.addAll(collection); for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o));
}
} }
} }
/** /**
* Construct a JSONArray from an array. * 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 * @throws JSONException
* If not an array or if an array value is non-finite number. * 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 { public JSONArray(Object array) throws JSONException {
this(); this();
this.addAll(array); if (array.getClass().isArray()) {
} int length = Array.getLength(array);
this.myArrayList.ensureCapacity(length);
/** for (int i = 0; i < length; i += 1) {
* Construct a JSONArray with the specified initial capacity. this.put(JSONObject.wrap(Array.get(array, i)));
* }
* @param initialCapacity } else {
* 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( throw new JSONException(
"JSONArray initial capacity cannot be negative."); "JSONArray initial value should be a string or collection or array.");
} }
this.myArrayList = new ArrayList<Object>(initialCapacity);
} }
@Override @Override
@ -254,7 +243,7 @@ public class JSONArray implements Iterable<Object> {
.equalsIgnoreCase("true"))) { .equalsIgnoreCase("true"))) {
return true; return true;
} }
throw wrongValueFormatException(index, "boolean", null); throw new JSONException("JSONArray[" + index + "] is not a boolean.");
} }
/** /**
@ -268,14 +257,12 @@ public class JSONArray implements Iterable<Object> {
* to a number. * to a number.
*/ */
public double getDouble(int index) throws JSONException { public double getDouble(int index) throws JSONException {
final Object object = this.get(index); Object object = this.get(index);
if(object instanceof Number) {
return ((Number)object).doubleValue();
}
try { try {
return Double.parseDouble(object.toString()); return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble((String) object);
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(index, "double", e); throw new JSONException("JSONArray[" + index + "] is not a number.", e);
} }
} }
@ -290,14 +277,13 @@ public class JSONArray implements Iterable<Object> {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public float getFloat(int index) throws JSONException { public float getFloat(int index) throws JSONException {
final Object object = this.get(index); Object object = this.get(index);
if(object instanceof Number) {
return ((Float)object).floatValue();
}
try { try {
return Float.parseFloat(object.toString()); return object instanceof Number ? ((Number) object).floatValue()
: Float.parseFloat(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(index, "float", e); throw new JSONException("JSONArray[" + index
+ "] is not a number.", e);
} }
} }
@ -319,41 +305,36 @@ public class JSONArray implements Iterable<Object> {
} }
return JSONObject.stringToNumber(object.toString()); return JSONObject.stringToNumber(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(index, "number", e); throw new JSONException("JSONArray[" + index + "] is not a number.", e);
} }
} }
/** /**
* Get the enum value associated with an index. * Get the enum value associated with an index.
* *
* @param <E> * @param clazz
* Enum Type * The type of enum to retrieve.
* @param clazz * @param index
* The type of enum to retrieve. * The index must be between 0 and length() - 1.
* @param index * @return The enum value at the index location
* The index must be between 0 and length() - 1. * @throws JSONException
* @return The enum value at the index location * if the key is not found or if the value cannot be converted
* @throws JSONException * to an enum.
* if the key is not found or if the value cannot be converted */
* to an enum.
*/
public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException { public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException {
E val = optEnum(clazz, index); E val = optEnum(clazz, index);
if(val==null) { if(val==null) {
// JSONException should really take a throwable argument. // JSONException should really take a throwable argument.
// If it did, I would re-implement this with the Enum.valueOf // If it did, I would re-implement this with the Enum.valueOf
// method and place any thrown exception in the JSONException // method and place any thrown exception in the JSONException
throw wrongValueFormatException(index, "enum of type " throw new JSONException("JSONArray[" + index + "] is not an enum of type "
+ JSONObject.quote(clazz.getSimpleName()), null); + JSONObject.quote(clazz.getSimpleName()) + ".");
} }
return val; return val;
} }
/** /**
* Get the BigDecimal value associated with an index. If the value is float * Get the BigDecimal value associated with an index.
* or double, the the {@link BigDecimal#BigDecimal(double)} constructor
* will be used. See notes on the constructor for conversion issues that
* may arise.
* *
* @param index * @param index
* The index must be between 0 and length() - 1. * The index must be between 0 and length() - 1.
@ -364,11 +345,12 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigDecimal getBigDecimal (int index) throws JSONException { public BigDecimal getBigDecimal (int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
BigDecimal val = JSONObject.objectToBigDecimal(object, null); try {
if(val == null) { return new BigDecimal(object.toString());
throw wrongValueFormatException(index, "BigDecimal", object, null); } catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] could not convert to BigDecimal.", e);
} }
return val;
} }
/** /**
@ -383,11 +365,12 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigInteger getBigInteger (int index) throws JSONException { public BigInteger getBigInteger (int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
BigInteger val = JSONObject.objectToBigInteger(object, null); try {
if(val == null) { return new BigInteger(object.toString());
throw wrongValueFormatException(index, "BigInteger", object, null); } catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] could not convert to BigInteger.", e);
} }
return val;
} }
/** /**
@ -400,14 +383,12 @@ public class JSONArray implements Iterable<Object> {
* If the key is not found or if the value is not a number. * If the key is not found or if the value is not a number.
*/ */
public int getInt(int index) throws JSONException { public int getInt(int index) throws JSONException {
final Object object = this.get(index); Object object = this.get(index);
if(object instanceof Number) {
return ((Number)object).intValue();
}
try { try {
return Integer.parseInt(object.toString()); return object instanceof Number ? ((Number) object).intValue()
: Integer.parseInt((String) object);
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(index, "int", e); throw new JSONException("JSONArray[" + index + "] is not a number.", e);
} }
} }
@ -426,7 +407,7 @@ public class JSONArray implements Iterable<Object> {
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray) object; return (JSONArray) object;
} }
throw wrongValueFormatException(index, "JSONArray", null); throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
} }
/** /**
@ -444,7 +425,7 @@ public class JSONArray implements Iterable<Object> {
if (object instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject) object; return (JSONObject) object;
} }
throw wrongValueFormatException(index, "JSONObject", null); throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
} }
/** /**
@ -458,14 +439,12 @@ public class JSONArray implements Iterable<Object> {
* to a number. * to a number.
*/ */
public long getLong(int index) throws JSONException { public long getLong(int index) throws JSONException {
final Object object = this.get(index); Object object = this.get(index);
if(object instanceof Number) {
return ((Number)object).longValue();
}
try { try {
return Long.parseLong(object.toString()); return object instanceof Number ? ((Number) object).longValue()
: Long.parseLong((String) object);
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(index, "long", e); throw new JSONException("JSONArray[" + index + "] is not a number.", e);
} }
} }
@ -483,7 +462,7 @@ public class JSONArray implements Iterable<Object> {
if (object instanceof String) { if (object instanceof String) {
return (String) object; return (String) object;
} }
throw wrongValueFormatException(index, "String", null); throw new JSONException("JSONArray[" + index + "] not a string.");
} }
/** /**
@ -510,16 +489,13 @@ public class JSONArray implements Iterable<Object> {
*/ */
public String join(String separator) throws JSONException { public String join(String separator) throws JSONException {
int len = this.length(); int len = this.length();
if (len == 0) { StringBuilder sb = new StringBuilder();
return "";
}
StringBuilder sb = new StringBuilder(
JSONObject.valueToString(this.myArrayList.get(0)));
for (int i = 1; i < len; i++) { for (int i = 0; i < len; i += 1) {
sb.append(separator) if (i > 0) {
.append(JSONObject.valueToString(this.myArrayList.get(i))); sb.append(separator);
}
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
} }
return sb.toString(); return sb.toString();
} }
@ -602,15 +578,21 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public double optDouble(int index, double defaultValue) { public double optDouble(int index, double defaultValue) {
final Number val = this.optNumber(index, null); Object val = this.opt(index);
if (val == null) { if (JSONObject.NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
final double doubleValue = val.doubleValue(); if (val instanceof Number){
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { return ((Number) val).doubleValue();
// return defaultValue; }
// } if (val instanceof String) {
return doubleValue; try {
return Double.parseDouble((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -638,15 +620,21 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public float optFloat(int index, float defaultValue) { public float optFloat(int index, float defaultValue) {
final Number val = this.optNumber(index, null); Object val = this.opt(index);
if (val == null) { if (JSONObject.NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
final float floatValue = val.floatValue(); if (val instanceof Number){
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { return ((Number) val).floatValue();
// return floatValue; }
// } if (val instanceof String) {
return floatValue; try {
return Float.parseFloat((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -674,18 +662,27 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public int optInt(int index, int defaultValue) { public int optInt(int index, int defaultValue) {
final Number val = this.optNumber(index, null); Object val = this.opt(index);
if (val == null) { if (JSONObject.NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
return val.intValue(); 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 defaultValue;
} }
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param index * @param index
@ -699,8 +696,6 @@ public class JSONArray implements Iterable<Object> {
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param index * @param index
@ -730,6 +725,7 @@ public class JSONArray implements Iterable<Object> {
} }
} }
/** /**
* Get the optional BigInteger value associated with an index. The * Get the optional BigInteger value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the * defaultValue is returned if there is no value for the index, or if the
@ -743,16 +739,37 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigInteger optBigInteger(int index, BigInteger defaultValue) { public BigInteger optBigInteger(int index, BigInteger defaultValue) {
Object val = this.opt(index); Object val = this.opt(index);
return JSONObject.objectToBigInteger(val, defaultValue); 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;
}
} }
/** /**
* Get the optional BigDecimal value associated with an index. The * Get the optional BigDecimal value associated with an index. The
* defaultValue is returned if there is no value for the index, or if 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. If the value * value is not a number and cannot be converted to a number.
* is float or double, the the {@link BigDecimal#BigDecimal(double)}
* constructor will be used. See notes on the constructor for conversion
* issues that may arise.
* *
* @param index * @param index
* The index must be between 0 and length() - 1. * The index must be between 0 and length() - 1.
@ -762,7 +779,27 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) { public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
Object val = this.opt(index); Object val = this.opt(index);
return JSONObject.objectToBigDecimal(val, defaultValue); 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;
}
} }
/** /**
@ -817,11 +854,22 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public long optLong(int index, long defaultValue) { public long optLong(int index, long defaultValue) {
final Number val = this.optNumber(index, null); Object val = this.opt(index);
if (val == null) { if (JSONObject.NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
return val.longValue(); 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 defaultValue;
} }
/** /**
@ -1163,36 +1211,6 @@ public class JSONArray implements Iterable<Object> {
} }
return this.put(value); return this.put(value);
} }
/**
* Put or replace a collection's elements in the JSONArray.
*
* @param collection
* A Collection.
* @return this.
*/
public JSONArray putAll(Collection<?> collection) {
this.addAll(collection);
return this;
}
/**
* Put or replace an array's elements in the JSONArray.
*
* @param array
* Array. If the parameter passed is null, or not an array, an
* exception will be thrown.
* @return this.
*
* @throws JSONException
* If not an array or if an array value is non-finite number.
* @throws NullPointerException
* Thrown if the array parameter is null.
*/
public JSONArray putAll(Object array) throws JSONException {
this.addAll(array);
return this;
}
/** /**
* Creates a JSONPointer using an initialization string and tries to * Creates a JSONPointer using an initialization string and tries to
@ -1218,8 +1236,8 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Uses a user initialized JSONPointer and tries to * Uses a uaer initialized JSONPointer and tries to
* match it to an item within this JSONArray. For example, given a * match it to an item whithin this JSONArray. For example, given a
* JSONArray initialized with this document: * JSONArray initialized with this document:
* <pre> * <pre>
* [ * [
@ -1368,7 +1386,7 @@ public class JSONArray implements Iterable<Object> {
/** /**
* Make a pretty-printed JSON text of this JSONArray. * Make a pretty-printed JSON text of this JSONArray.
* *
* <p>If <pre> {@code indentFactor > 0}</pre> and the {@link JSONArray} has only * <p>If <code>indentFactor > 0</code> and the {@link JSONArray} has only
* one element, then the array will be output on a single line: * one element, then the array will be output on a single line:
* <pre>{@code [1]}</pre> * <pre>{@code [1]}</pre>
* *
@ -1390,7 +1408,7 @@ public class JSONArray implements Iterable<Object> {
* object, beginning with <code>[</code>&nbsp;<small>(left * object, beginning with <code>[</code>&nbsp;<small>(left
* bracket)</small> and ending with <code>]</code> * bracket)</small> and ending with <code>]</code>
* &nbsp;<small>(right bracket)</small>. * &nbsp;<small>(right bracket)</small>.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public String toString(int indentFactor) throws JSONException { public String toString(int indentFactor) throws JSONException {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
@ -1405,9 +1423,9 @@ public class JSONArray implements Iterable<Object> {
* <p><b> * <p><b>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
*</b> *</b>
* @param writer the writer object *
* @return The writer. * @return The writer.
* @throws JSONException if a called function fails * @throws JSONException
*/ */
public Writer write(Writer writer) throws JSONException { public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0); return this.write(writer, 0, 0);
@ -1416,7 +1434,7 @@ public class JSONArray implements Iterable<Object> {
/** /**
* Write the contents of the JSONArray as JSON text to a writer. * Write the contents of the JSONArray as JSON text to a writer.
* *
* <p>If <pre>{@code indentFactor > 0}</pre> and the {@link JSONArray} has only * <p>If <code>indentFactor > 0</code> and the {@link JSONArray} has only
* one element, then the array will be output on a single line: * one element, then the array will be output on a single line:
* <pre>{@code [1]}</pre> * <pre>{@code [1]}</pre>
* *
@ -1439,12 +1457,12 @@ public class JSONArray implements Iterable<Object> {
* @param indent * @param indent
* The indentation of the top level. * The indentation of the top level.
* @return The writer. * @return The writer.
* @throws JSONException if a called function fails or unable to write * @throws JSONException
*/ */
public Writer write(Writer writer, int indentFactor, int indent) public Writer write(Writer writer, int indentFactor, int indent)
throws JSONException { throws JSONException {
try { try {
boolean needsComma = false; boolean commanate = false;
int length = this.length(); int length = this.length();
writer.write('['); writer.write('[');
@ -1456,23 +1474,23 @@ public class JSONArray implements Iterable<Object> {
throw new JSONException("Unable to write JSONArray value at index: 0", e); throw new JSONException("Unable to write JSONArray value at index: 0", e);
} }
} else if (length != 0) { } else if (length != 0) {
final int newIndent = indent + indentFactor; final int newindent = indent + indentFactor;
for (int i = 0; i < length; i += 1) { for (int i = 0; i < length; i += 1) {
if (needsComma) { if (commanate) {
writer.write(','); writer.write(',');
} }
if (indentFactor > 0) { if (indentFactor > 0) {
writer.write('\n'); writer.write('\n');
} }
JSONObject.indent(writer, newIndent); JSONObject.indent(writer, newindent);
try { try {
JSONObject.writeValue(writer, this.myArrayList.get(i), JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, newIndent); indentFactor, newindent);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: " + i, e); throw new JSONException("Unable to write JSONArray value at index: " + i, e);
} }
needsComma = true; commanate = true;
} }
if (indentFactor > 0) { if (indentFactor > 0) {
writer.write('\n'); writer.write('\n');
@ -1489,7 +1507,7 @@ public class JSONArray implements Iterable<Object> {
/** /**
* Returns a java.util.List containing all of the elements in this array. * 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 * If an element in the array is a JSONArray or JSONObject it will also
* be converted to a List and a Map respectively. * be converted.
* <p> * <p>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* *
@ -1517,79 +1535,7 @@ public class JSONArray implements Iterable<Object> {
* @return true if JSONArray is empty, otherwise false. * @return true if JSONArray is empty, otherwise false.
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return this.myArrayList.isEmpty(); return myArrayList.isEmpty();
}
/**
* Add a collection's elements to the JSONArray.
*
* @param collection
* A Collection.
*/
private void addAll(Collection<?> collection) {
this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size());
for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o));
}
}
/**
* Add an array's elements to the JSONArray.
*
* @param array
* Array. If the parameter passed is null, or not an array, an
* exception will be thrown.
*
* @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) throws JSONException {
if (array.getClass().isArray()) {
int length = Array.getLength(array);
this.myArrayList.ensureCapacity(this.myArrayList.size() + length);
for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i)));
}
} 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,
Throwable cause) {
return new JSONException(
"JSONArray[" + idx + "] is not a " + valueType + "."
, cause);
}
/**
* 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) {
return new JSONException(
"JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
, cause);
} }
} }

View file

@ -1,29 +1,5 @@
package org.json; 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.
*/
/** /**
* The JSONException is thrown by the JSON.org classes when things are amiss. * The JSONException is thrown by the JSON.org classes when things are amiss.
* *

View file

@ -41,7 +41,7 @@ public class JSONML {
* if we are at the outermost level. * if we are at the outermost level.
* @param keepStrings Don't type-convert text nodes and attribute values * @param keepStrings Don't type-convert text nodes and attribute values
* @return A JSONArray if the value is the outermost tag, otherwise null. * @return A JSONArray if the value is the outermost tag, otherwise null.
* @throws JSONException if a parsing error occurs * @throws JSONException
*/ */
private static Object parse( private static Object parse(
XMLTokener x, XMLTokener x,
@ -238,7 +238,7 @@ public class JSONML {
* attributes, then the second element will be JSONObject containing the * attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and * name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child tags. * JSONArrays will represent the child tags.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param string The source string. * @param string The source string.
* @return A JSONArray containing the structured data from the XML string. * @return A JSONArray containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONArray * @throws JSONException Thrown on error converting to a JSONArray
@ -258,7 +258,7 @@ public class JSONML {
* As opposed to toJSONArray this method does not attempt to convert * As opposed to toJSONArray this method does not attempt to convert
* any text node or attribute value to any type * any text node or attribute value to any type
* but just leaves it as a string. * but just leaves it as a string.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param string The source string. * @param string The source string.
* @param keepStrings If true, then values will not be coerced into boolean * @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings * or numeric values and will instead be left as strings
@ -280,7 +280,7 @@ public class JSONML {
* As opposed to toJSONArray this method does not attempt to convert * As opposed to toJSONArray this method does not attempt to convert
* any text node or attribute value to any type * any text node or attribute value to any type
* but just leaves it as a string. * but just leaves it as a string.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param x An XMLTokener. * @param x An XMLTokener.
* @param keepStrings If true, then values will not be coerced into boolean * @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings * or numeric values and will instead be left as strings
@ -299,7 +299,7 @@ public class JSONML {
* attributes, then the second element will be JSONObject containing the * attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and * name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child content and tags. * JSONArrays will represent the child content and tags.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param x An XMLTokener. * @param x An XMLTokener.
* @return A JSONArray containing the structured data from the XML string. * @return A JSONArray containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONArray * @throws JSONException Thrown on error converting to a JSONArray
@ -317,7 +317,7 @@ public class JSONML {
* contains children, the object will have a "childNodes" property which * contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects. * will be an array of strings and JsonML JSONObjects.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param string The XML source text. * @param string The XML source text.
* @return A JSONObject containing the structured data from the XML string. * @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONObject * @throws JSONException Thrown on error converting to a JSONObject
@ -335,7 +335,7 @@ public class JSONML {
* contains children, the object will have a "childNodes" property which * contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects. * will be an array of strings and JsonML JSONObjects.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param string The XML source text. * @param string The XML source text.
* @param keepStrings If true, then values will not be coerced into boolean * @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings * or numeric values and will instead be left as strings
@ -355,7 +355,7 @@ public class JSONML {
* contains children, the object will have a "childNodes" property which * contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects. * will be an array of strings and JsonML JSONObjects.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param x An XMLTokener of the XML source text. * @param x An XMLTokener of the XML source text.
* @return A JSONObject containing the structured data from the XML string. * @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown on error converting to a JSONObject * @throws JSONException Thrown on error converting to a JSONObject
@ -373,7 +373,7 @@ public class JSONML {
* contains children, the object will have a "childNodes" property which * contains children, the object will have a "childNodes" property which
* will be an array of strings and JsonML JSONObjects. * will be an array of strings and JsonML JSONObjects.
* Comments, prologs, DTDs, and <pre>{@code &lt;[ [ ]]>}</pre> are ignored. * Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param x An XMLTokener of the XML source text. * @param x An XMLTokener of the XML source text.
* @param keepStrings If true, then values will not be coerced into boolean * @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings * or numeric values and will instead be left as strings

View file

@ -45,7 +45,6 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
/** /**
* A JSONObject is an unordered collection of name/value pairs. Its external * A JSONObject is an unordered collection of name/value pairs. Its external
@ -151,12 +150,6 @@ public class JSONObject {
return "null"; return "null";
} }
} }
/**
* 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. * The map where the JSONObject's properties are kept.
@ -194,7 +187,7 @@ public class JSONObject {
* @param names * @param names
* An array of strings. * An array of strings.
*/ */
public JSONObject(JSONObject jo, String ... names) { public JSONObject(JSONObject jo, String[] names) {
this(names.length); this(names.length);
for (int i = 0; i < names.length; i += 1) { for (int i = 0; i < names.length; i += 1) {
try { try {
@ -378,7 +371,7 @@ public class JSONObject {
* An array of strings, the names of the fields to be obtained * An array of strings, the names of the fields to be obtained
* from the object. * from the object.
*/ */
public JSONObject(Object object, String ... names) { public JSONObject(Object object, String names[]) {
this(names.length); this(names.length);
Class<?> c = object.getClass(); Class<?> c = object.getClass();
for (int i = 0; i < names.length; i += 1) { for (int i = 0; i < names.length; i += 1) {
@ -521,7 +514,8 @@ public class JSONObject {
} else if (object instanceof JSONArray) { } else if (object instanceof JSONArray) {
this.put(key, ((JSONArray) object).put(value)); this.put(key, ((JSONArray) object).put(value));
} else { } else {
throw wrongValueFormatException(key, "JSONArray", null, null); throw new JSONException("JSONObject[" + key
+ "] is not a JSONArray.");
} }
return this; return this;
} }
@ -575,26 +569,26 @@ public class JSONObject {
} }
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E> * @param clazz
* Enum Type * The type of enum to retrieve.
* @param clazz * @param key
* The type of enum to retrieve. * A key string.
* @param key * @return The enum value associated with the key
* A key string. * @throws JSONException
* @return The enum value associated with the key * if the key is not found or if the value cannot be converted
* @throws JSONException * to an enum.
* if the key is not found or if the value cannot be converted */
* to an enum.
*/
public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONException { public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONException {
E val = optEnum(clazz, key); E val = optEnum(clazz, key);
if(val==null) { if(val==null) {
// JSONException should really take a throwable argument. // JSONException should really take a throwable argument.
// If it did, I would re-implement this with the Enum.valueOf // If it did, I would re-implement this with the Enum.valueOf
// method and place any thrown exception in the JSONException // method and place any thrown exception in the JSONException
throw wrongValueFormatException(key, "enum of type " + quote(clazz.getSimpleName()), null); throw new JSONException("JSONObject[" + quote(key)
+ "] is not an enum of type " + quote(clazz.getSimpleName())
+ ".");
} }
return val; return val;
} }
@ -620,7 +614,8 @@ public class JSONObject {
.equalsIgnoreCase("true"))) { .equalsIgnoreCase("true"))) {
return true; return true;
} }
throw wrongValueFormatException(key, "Boolean", null); throw new JSONException("JSONObject[" + quote(key)
+ "] is not a Boolean.");
} }
/** /**
@ -635,18 +630,16 @@ public class JSONObject {
*/ */
public BigInteger getBigInteger(String key) throws JSONException { public BigInteger getBigInteger(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
BigInteger ret = objectToBigInteger(object, null); try {
if (ret != null) { return new BigInteger(object.toString());
return ret; } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigInteger.", e);
} }
throw wrongValueFormatException(key, "BigInteger", object, null);
} }
/** /**
* Get the BigDecimal value associated with a key. If the value is float or * Get the BigDecimal value associated with a key.
* double, the the {@link BigDecimal#BigDecimal(double)} constructor will
* be used. See notes on the constructor for conversion issues that may
* arise.
* *
* @param key * @param key
* A key string. * A key string.
@ -657,11 +650,15 @@ public class JSONObject {
*/ */
public BigDecimal getBigDecimal(String key) throws JSONException { public BigDecimal getBigDecimal(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
BigDecimal ret = objectToBigDecimal(object, null); if (object instanceof BigDecimal) {
if (ret != null) { return (BigDecimal)object;
return ret; }
try {
return new BigDecimal(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigDecimal.", e);
} }
throw wrongValueFormatException(key, "BigDecimal", object, null);
} }
/** /**
@ -675,14 +672,13 @@ public class JSONObject {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public double getDouble(String key) throws JSONException { public double getDouble(String key) throws JSONException {
final Object object = this.get(key); Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).doubleValue();
}
try { try {
return Double.parseDouble(object.toString()); return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(key, "double", e); throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
} }
} }
@ -697,14 +693,13 @@ public class JSONObject {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public float getFloat(String key) throws JSONException { public float getFloat(String key) throws JSONException {
final Object object = this.get(key); Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).floatValue();
}
try { try {
return Float.parseFloat(object.toString()); return object instanceof Number ? ((Number) object).floatValue()
: Float.parseFloat(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(key, "float", e); throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
} }
} }
@ -726,7 +721,8 @@ public class JSONObject {
} }
return stringToNumber(object.toString()); return stringToNumber(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(key, "number", e); throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
} }
} }
@ -741,14 +737,13 @@ public class JSONObject {
* to an integer. * to an integer.
*/ */
public int getInt(String key) throws JSONException { public int getInt(String key) throws JSONException {
final Object object = this.get(key); Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).intValue();
}
try { try {
return Integer.parseInt(object.toString()); return object instanceof Number ? ((Number) object).intValue()
: Integer.parseInt((String) object);
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(key, "int", e); throw new JSONException("JSONObject[" + quote(key)
+ "] is not an int.", e);
} }
} }
@ -766,7 +761,8 @@ public class JSONObject {
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray) object; return (JSONArray) object;
} }
throw wrongValueFormatException(key, "JSONArray", null); throw new JSONException("JSONObject[" + quote(key)
+ "] is not a JSONArray.");
} }
/** /**
@ -783,7 +779,8 @@ public class JSONObject {
if (object instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject) object; return (JSONObject) object;
} }
throw wrongValueFormatException(key, "JSONObject", null); throw new JSONException("JSONObject[" + quote(key)
+ "] is not a JSONObject.");
} }
/** /**
@ -797,22 +794,19 @@ public class JSONObject {
* to a long. * to a long.
*/ */
public long getLong(String key) throws JSONException { public long getLong(String key) throws JSONException {
final Object object = this.get(key); Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).longValue();
}
try { try {
return Long.parseLong(object.toString()); return object instanceof Number ? ((Number) object).longValue()
: Long.parseLong((String) object);
} catch (Exception e) { } catch (Exception e) {
throw wrongValueFormatException(key, "long", e); throw new JSONException("JSONObject[" + quote(key)
+ "] is not a long.", e);
} }
} }
/** /**
* Get an array of field names from a JSONObject. * 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. * @return An array of field names, or null if there are no names.
*/ */
public static String[] getNames(JSONObject jo) { public static String[] getNames(JSONObject jo) {
@ -823,10 +817,8 @@ public class JSONObject {
} }
/** /**
* Get an array of public field names from an Object. * Get an array of field names from an Object.
* *
* @param object
* object to read
* @return An array of field names, or null if there are no names. * @return An array of field names, or null if there are no names.
*/ */
public static String[] getNames(Object object) { public static String[] getNames(Object object) {
@ -860,7 +852,7 @@ public class JSONObject {
if (object instanceof String) { if (object instanceof String) {
return (String) object; return (String) object;
} }
throw wrongValueFormatException(key, "string", null); throw new JSONException("JSONObject[" + quote(key) + "] not a string.");
} }
/** /**
@ -876,11 +868,8 @@ public class JSONObject {
/** /**
* Increment a property of a JSONObject. If there is no such property, * Increment a property of a JSONObject. If there is no such property,
* create one with a value of 1 (Integer). If there is such a property, and if it is * create one with a value of 1. If there is such a property, and if it is
* an Integer, Long, Double, Float, BigInteger, or BigDecimal then add one to it. * an Integer, Long, Double, or Float, 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 * @param key
* A key string. * A key string.
@ -893,18 +882,18 @@ public class JSONObject {
Object value = this.opt(key); Object value = this.opt(key);
if (value == null) { if (value == null) {
this.put(key, 1); 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) { } else if (value instanceof Integer) {
this.put(key, ((Integer) value).intValue() + 1); this.put(key, ((Integer) value).intValue() + 1);
} else if (value instanceof Long) { } else if (value instanceof Long) {
this.put(key, ((Long) value).longValue() + 1L); this.put(key, ((Long) value).longValue() + 1L);
} 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) { } else if (value instanceof Double) {
this.put(key, ((Double) value).doubleValue() + 1.0d); this.put(key, ((Double) value).doubleValue() + 1.0d);
} else if (value instanceof BigDecimal) { } else if (value instanceof Float) {
this.put(key, ((BigDecimal)value).add(BigDecimal.ONE)); this.put(key, ((Float) value).floatValue() + 1.0f);
} else { } else {
throw new JSONException("Unable to increment [" + quote(key) + "]."); throw new JSONException("Unable to increment [" + quote(key) + "].");
} }
@ -979,7 +968,7 @@ public class JSONObject {
* @return true if JSONObject is empty, otherwise false. * @return true if JSONObject is empty, otherwise false.
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return this.map.isEmpty(); return map.isEmpty();
} }
/** /**
@ -1040,8 +1029,6 @@ public class JSONObject {
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param key * @param key
@ -1055,8 +1042,6 @@ public class JSONObject {
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param key * @param key
@ -1128,10 +1113,7 @@ public class JSONObject {
/** /**
* Get an optional BigDecimal associated with a key, or the defaultValue if * 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 * 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. If the value * string, an attempt will be made to evaluate it as a number.
* 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 * @param key
* A key string. * A key string.
@ -1141,16 +1123,6 @@ public class JSONObject {
*/ */
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) { public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
Object val = this.opt(key); 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) {
if (NULL.equals(val)) { if (NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
@ -1161,10 +1133,6 @@ public class JSONObject {
return new BigDecimal((BigInteger) val); return new BigDecimal((BigInteger) val);
} }
if (val instanceof Double || val instanceof Float){ if (val instanceof Double || val instanceof Float){
final double d = ((Number) val).doubleValue();
if(Double.isNaN(d)) {
return defaultValue;
}
return new BigDecimal(((Number) val).doubleValue()); return new BigDecimal(((Number) val).doubleValue());
} }
if (val instanceof Long || val instanceof Integer if (val instanceof Long || val instanceof Integer
@ -1192,16 +1160,6 @@ public class JSONObject {
*/ */
public BigInteger optBigInteger(String key, BigInteger defaultValue) { public BigInteger optBigInteger(String key, BigInteger defaultValue) {
Object val = this.opt(key); 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)) { if (NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
@ -1212,11 +1170,7 @@ public class JSONObject {
return ((BigDecimal) val).toBigInteger(); return ((BigDecimal) val).toBigInteger();
} }
if (val instanceof Double || val instanceof Float){ if (val instanceof Double || val instanceof Float){
final double d = ((Number) val).doubleValue(); return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
if(Double.isNaN(d)) {
return defaultValue;
}
return new BigDecimal(d).toBigInteger();
} }
if (val instanceof Long || val instanceof Integer if (val instanceof Long || val instanceof Integer
|| val instanceof Short || val instanceof Byte){ || val instanceof Short || val instanceof Byte){
@ -1264,15 +1218,21 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public double optDouble(String key, double defaultValue) { public double optDouble(String key, double defaultValue) {
Number val = this.optNumber(key); Object val = this.opt(key);
if (val == null) { if (NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
final double doubleValue = val.doubleValue(); if (val instanceof Number){
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { return ((Number) val).doubleValue();
// return defaultValue; }
// } if (val instanceof String) {
return doubleValue; try {
return Double.parseDouble((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1300,15 +1260,21 @@ public class JSONObject {
* @return The value. * @return The value.
*/ */
public float optFloat(String key, float defaultValue) { public float optFloat(String key, float defaultValue) {
Number val = this.optNumber(key); Object val = this.opt(key);
if (val == null) { if (JSONObject.NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
final float floatValue = val.floatValue(); if (val instanceof Number){
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { return ((Number) val).floatValue();
// return defaultValue; }
// } if (val instanceof String) {
return floatValue; try {
return Float.parseFloat((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1336,11 +1302,22 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public int optInt(String key, int defaultValue) { public int optInt(String key, int defaultValue) {
final Number val = this.optNumber(key, null); Object val = this.opt(key);
if (val == null) { if (NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
return val.intValue(); 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 defaultValue;
} }
/** /**
@ -1394,12 +1371,22 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public long optLong(String key, long defaultValue) { public long optLong(String key, long defaultValue) {
final Number val = this.optNumber(key, null); Object val = this.opt(key);
if (val == null) { if (NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){
return ((Number) val).longValue();
}
return val.longValue(); if (val instanceof String) {
try {
return new BigDecimal((String) val).longValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1437,11 +1424,14 @@ public class JSONObject {
return (Number) val; return (Number) val;
} }
try { if (val instanceof String) {
return stringToNumber(val.toString()); try {
} catch (Exception e) { return stringToNumber((String) val);
return defaultValue; } catch (Exception e) {
return defaultValue;
}
} }
return defaultValue;
} }
/** /**
@ -1522,11 +1512,11 @@ public class JSONObject {
} }
} }
private static boolean isValidMethodName(String name) { private boolean isValidMethodName(String name) {
return !"getClass".equals(name) && !"getDeclaringClass".equals(name); return !"getClass".equals(name) && !"getDeclaringClass".equals(name);
} }
private static String getKeyNameFromMethod(Method method) { private String getKeyNameFromMethod(Method method) {
final int ignoreDepth = getAnnotationDepth(method, JSONPropertyIgnore.class); final int ignoreDepth = getAnnotationDepth(method, JSONPropertyIgnore.class);
if (ignoreDepth > 0) { if (ignoreDepth > 0) {
final int forcedNameDepth = getAnnotationDepth(method, JSONPropertyName.class); final int forcedNameDepth = getAnnotationDepth(method, JSONPropertyName.class);
@ -1833,10 +1823,8 @@ public class JSONObject {
* are both non-null, and only if there is not already a member with that * are both non-null, and only if there is not already a member with that
* name. * name.
* *
* @param key * @param key string
* key to insert into * @param value object
* @param value
* value to insert
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* if the key is a duplicate * if the key is a duplicate
@ -1947,10 +1935,9 @@ public class JSONObject {
/** /**
* Produce a string in double quotes with backslash sequences in all the * Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within &lt;/, producing * right places. A backslash will be inserted within </, producing <\/,
* &lt;\/, allowing JSON text to be delivered in HTML. In JSON text, a * allowing JSON text to be delivered in HTML. In JSON text, a string cannot
* string cannot contain a control character or an unescaped quote or * contain a control character or an unescaped quote or backslash.
* backslash.
* *
* @param string * @param string
* A String * A String
@ -2109,54 +2096,48 @@ public class JSONObject {
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
// decimal representation // decimal representation
if (isDecimalNotation(val)) { if (isDecimalNotation(val)) {
// Use a BigDecimal all the time so we keep the original // quick dirty way to see if we need a BigDecimal instead of a Double
// representation. BigDecimal doesn't support -0.0, ensure we // this only handles some cases of overflow or underflow
// keep that by forcing a decimal. if (val.length()>14) {
try { return new BigDecimal(val);
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.");
}
} }
} final Double d = Double.valueOf(val);
// block items like 00 01 etc. Java number parsers treat these as Octal. if (d.isInfinite() || d.isNaN()) {
if(initial == '0' && val.length() > 1) { // if we can't parse it as a double, go up to BigDecimal
char at1 = val.charAt(1); // this is probably due to underflow like 4.32e-678
if(at1 >= '0' && at1 <= '9') { // or overflow like 4.65e5324. The size of the string is small
throw new NumberFormatException("val ["+val+"] is not a valid number."); // but can't be held in a Double.
} return new BigDecimal(val);
} 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.");
} }
return d;
} }
// integer representation. // integer representation.
// This will narrow any values to the smallest reasonable Object representation // This will narrow any values to the smallest reasonable Object representation
// (Integer, Long, or BigInteger) // (Integer, Long, or BigInteger)
// BigInteger down conversion: We use a similar bitLenth compare as // 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 // BigInteger#intValueExact uses. Increases GC, but objects hold
// only what they need. i.e. Less runtime overhead if the value is // only what they need. i.e. Less runtime overhead if the value is
// long lived. // long lived. Which is the better tradeoff? This is closer to what's
// in stringToValue.
BigInteger bi = new BigInteger(val); BigInteger bi = new BigInteger(val);
if(bi.bitLength() <= 31){ if(bi.bitLength()<=31){
return Integer.valueOf(bi.intValue()); return Integer.valueOf(bi.intValue());
} }
if(bi.bitLength() <= 63){ if(bi.bitLength()<=63){
return Long.valueOf(bi.longValue()); return Long.valueOf(bi.longValue());
} }
return bi; return bi;
@ -2169,26 +2150,22 @@ public class JSONObject {
* can't be converted, return the string. * can't be converted, return the string.
* *
* @param string * @param string
* A String. can not be null. * A String.
* @return A simple JSON value. * @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 // Changes to this method must be copied to the corresponding method in
// the XML class to keep full support for Android // the XML class to keep full support for Android
public static Object stringToValue(String string) { public static Object stringToValue(String string) {
if ("".equals(string)) { if (string.equals("")) {
return string; return string;
} }
if (string.equalsIgnoreCase("true")) {
// check JSON key words true/false/null
if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if ("false".equalsIgnoreCase(string)) { if (string.equalsIgnoreCase("false")) {
return Boolean.FALSE; return Boolean.FALSE;
} }
if ("null".equalsIgnoreCase(string)) { if (string.equalsIgnoreCase("null")) {
return JSONObject.NULL; return JSONObject.NULL;
} }
@ -2200,7 +2177,22 @@ public class JSONObject {
char initial = string.charAt(0); char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
try { try {
return stringToNumber(string); // 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;
}
} else {
Long myLong = Long.valueOf(string);
if (string.equals(myLong.toString())) {
if (myLong.longValue() == myLong.intValue()) {
return Integer.valueOf(myLong.intValue());
}
return myLong;
}
}
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
@ -2278,16 +2270,16 @@ public class JSONObject {
/** /**
* Make a pretty-printed JSON text of this JSONObject. * Make a pretty-printed JSON text of this JSONObject.
* *
* <p>If <pre>{@code indentFactor > 0}</pre> and the {@link JSONObject} * <p>If <code>indentFactor > 0</code> and the {@link JSONObject}
* has only one key, then the object will be output on a single line: * has only one key, then the object will be output on a single line:
* <pre>{@code {"key": 1}}</pre> * <pre>{@code {"key": 1}}</pre>
* *
* <p>If an object has 2 or more keys, then it will be output across * <p>If an object has 2 or more keys, then it will be output across
* multiple lines: <pre>{@code { * multiple lines: <code><pre>{
* "key1": 1, * "key1": 1,
* "key2": "value 2", * "key2": "value 2",
* "key3": 3 * "key3": 3
* }}</pre> * }</pre></code>
* <p><b> * <p><b>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* </b> * </b>
@ -2399,9 +2391,9 @@ public class JSONObject {
* <p><b> * <p><b>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* </b> * </b>
* @param writer the writer object *
* @return The writer. * @return The writer.
* @throws JSONException if a called function has an error * @throws JSONException
*/ */
public Writer write(Writer writer) throws JSONException { public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0); return this.write(writer, 0, 0);
@ -2422,9 +2414,13 @@ public class JSONObject {
} else if (value instanceof Number) { } else if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
final String numberAsString = numberToString((Number) value); final String numberAsString = numberToString((Number) value);
if(NUMBER_PATTERN.matcher(numberAsString).matches()) { try {
// Use the BigDecimal constructor for its parser to validate the format.
@SuppressWarnings("unused")
BigDecimal testNum = new BigDecimal(numberAsString);
// Close enough to a JSON number that we will use it unquoted
writer.write(numberAsString); writer.write(numberAsString);
} else { } catch (NumberFormatException ex){
// The Number value is not a valid JSON number. // The Number value is not a valid JSON number.
// Instead we will quote it as a string // Instead we will quote it as a string
quote(numberAsString, writer); quote(numberAsString, writer);
@ -2460,16 +2456,16 @@ public class JSONObject {
/** /**
* Write the contents of the JSONObject as JSON text to a writer. * Write the contents of the JSONObject as JSON text to a writer.
* *
* <p>If <pre>{@code indentFactor > 0}</pre> and the {@link JSONObject} * <p>If <code>indentFactor > 0</code> and the {@link JSONObject}
* has only one key, then the object will be output on a single line: * has only one key, then the object will be output on a single line:
* <pre>{@code {"key": 1}}</pre> * <pre>{@code {"key": 1}}</pre>
* *
* <p>If an object has 2 or more keys, then it will be output across * <p>If an object has 2 or more keys, then it will be output across
* multiple lines: <pre>{@code { * multiple lines: <code><pre>{
* "key1": 1, * "key1": 1,
* "key2": "value 2", * "key2": "value 2",
* "key3": 3 * "key3": 3
* }}</pre> * }</pre></code>
* <p><b> * <p><b>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* </b> * </b>
@ -2481,13 +2477,12 @@ public class JSONObject {
* @param indent * @param indent
* The indentation of the top level. * The indentation of the top level.
* @return The writer. * @return The writer.
* @throws JSONException if a called function has an error or a write error * @throws JSONException
* occurs
*/ */
public Writer write(Writer writer, int indentFactor, int indent) public Writer write(Writer writer, int indentFactor, int indent)
throws JSONException { throws JSONException {
try { try {
boolean needsComma = false; boolean commanate = false;
final int length = this.length(); final int length = this.length();
writer.write('{'); writer.write('{');
@ -2505,15 +2500,15 @@ public class JSONObject {
throw new JSONException("Unable to write JSONObject value for key: " + key, e); throw new JSONException("Unable to write JSONObject value for key: " + key, e);
} }
} else if (length != 0) { } else if (length != 0) {
final int newIndent = indent + indentFactor; final int newindent = indent + indentFactor;
for (final Entry<String,?> entry : this.entrySet()) { for (final Entry<String,?> entry : this.entrySet()) {
if (needsComma) { if (commanate) {
writer.write(','); writer.write(',');
} }
if (indentFactor > 0) { if (indentFactor > 0) {
writer.write('\n'); writer.write('\n');
} }
indent(writer, newIndent); indent(writer, newindent);
final String key = entry.getKey(); final String key = entry.getKey();
writer.write(quote(key)); writer.write(quote(key));
writer.write(':'); writer.write(':');
@ -2521,11 +2516,11 @@ public class JSONObject {
writer.write(' '); writer.write(' ');
} }
try { try {
writeValue(writer, entry.getValue(), indentFactor, newIndent); writeValue(writer, entry.getValue(), indentFactor, newindent);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("Unable to write JSONObject value for key: " + key, e); throw new JSONException("Unable to write JSONObject value for key: " + key, e);
} }
needsComma = true; commanate = true;
} }
if (indentFactor > 0) { if (indentFactor > 0) {
writer.write('\n'); writer.write('\n');
@ -2565,37 +2560,4 @@ public class JSONObject {
} }
return results; 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,
Throwable cause) {
return new JSONException(
"JSONObject[" + quote(key) + "] is not a " + valueType + "."
, cause);
}
/**
* 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) {
return new JSONException(
"JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value + ")."
, cause);
}
} }

View file

@ -68,7 +68,6 @@ public class JSONPointer {
/** /**
* Creates a {@code JSONPointer} instance using the tokens previously set using the * Creates a {@code JSONPointer} instance using the tokens previously set using the
* {@link #append(String)} method calls. * {@link #append(String)} method calls.
* @return a JSONPointer object
*/ */
public JSONPointer build() { public JSONPointer build() {
return new JSONPointer(this.refTokens); return new JSONPointer(this.refTokens);
@ -187,7 +186,7 @@ public class JSONPointer {
this.refTokens = new ArrayList<String>(refTokens); this.refTokens = new ArrayList<String>(refTokens);
} }
private static String unescape(String token) { private String unescape(String token) {
return token.replace("~1", "/").replace("~0", "~") return token.replace("~1", "/").replace("~0", "~")
.replace("\\\"", "\"") .replace("\\\"", "\"")
.replace("\\\\", "\\"); .replace("\\\\", "\\");
@ -229,13 +228,13 @@ public class JSONPointer {
* @return the matched object. If no matching item is found a * @return the matched object. If no matching item is found a
* @throws JSONPointerException is thrown if the index is out of bounds * @throws JSONPointerException is thrown if the index is out of bounds
*/ */
private static Object readByIndexToken(Object current, String indexToken) throws JSONPointerException { private Object readByIndexToken(Object current, String indexToken) throws JSONPointerException {
try { try {
int index = Integer.parseInt(indexToken); int index = Integer.parseInt(indexToken);
JSONArray currentArr = (JSONArray) current; JSONArray currentArr = (JSONArray) current;
if (index >= currentArr.length()) { if (index >= currentArr.length()) {
throw new JSONPointerException(format("index %s is out of bounds - the array has %d elements", indexToken, throw new JSONPointerException(format("index %d is out of bounds - the array has %d elements", index,
Integer.valueOf(currentArr.length()))); currentArr.length()));
} }
try { try {
return currentArr.get(index); return currentArr.get(index);
@ -268,7 +267,7 @@ public class JSONPointer {
* @param token the JSONPointer segment value to be escaped * @param token the JSONPointer segment value to be escaped
* @return the escaped value for the token * @return the escaped value for the token
*/ */
private static String escape(String token) { private String escape(String token) {
return token.replace("~", "~0") return token.replace("~", "~0")
.replace("/", "~1") .replace("/", "~1")
.replace("\\", "\\\\") .replace("\\", "\\\\")
@ -278,7 +277,6 @@ public class JSONPointer {
/** /**
* Returns a string representing the JSONPointer path value using URI * Returns a string representing the JSONPointer path value using URI
* fragment identifier representation * fragment identifier representation
* @return a uri fragment string
*/ */
public String toURIFragment() { public String toURIFragment() {
try { try {

18
JSONString.java Normal file
View file

@ -0,0 +1,18 @@
package org.json;
/**
* The <code>JSONString</code> interface allows a <code>toJSONString()</code>
* method so that a class can change the behavior of
* <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>,
* and <code>JSONWriter.value(</code>Object<code>)</code>. The
* <code>toJSONString</code> method will be used instead of the default behavior
* of using the Object's <code>toString()</code> method and quoting the result.
*/
public interface JSONString {
/**
* The <code>toJSONString</code> method allows a class to produce its own JSON
* serialization.
*
* @return A strictly syntactically correct JSON text.
*/
public String toJSONString();
}

View file

@ -1,79 +1,79 @@
package org.json; package org.json;
/* /*
Copyright (c) 2006 JSON.org Copyright (c) 2006 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
The Software shall be used for Good, not Evil. The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
import java.io.StringWriter; import java.io.StringWriter;
/** /**
* JSONStringer provides a quick and convenient way of producing JSON text. * JSONStringer provides a quick and convenient way of producing JSON text.
* The texts produced strictly conform to JSON syntax rules. No whitespace is * 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 * added, so the results are ready for transmission or storage. Each instance of
* JSONStringer can produce one JSON text. * JSONStringer can produce one JSON text.
* <p> * <p>
* A JSONStringer instance provides a <code>value</code> method for appending * A JSONStringer instance provides a <code>value</code> method for appending
* values to the * values to the
* text, and a <code>key</code> * text, and a <code>key</code>
* method for adding keys before values in objects. There are <code>array</code> * method for adding keys before values in objects. There are <code>array</code>
* and <code>endArray</code> methods that make and bound array values, and * and <code>endArray</code> methods that make and bound array values, and
* <code>object</code> and <code>endObject</code> methods which make and bound * <code>object</code> and <code>endObject</code> methods which make and bound
* object values. All of these methods return the JSONWriter instance, * object values. All of these methods return the JSONWriter instance,
* permitting cascade style. For example, <pre> * permitting cascade style. For example, <pre>
* myString = new JSONStringer() * myString = new JSONStringer()
* .object() * .object()
* .key("JSON") * .key("JSON")
* .value("Hello, World!") * .value("Hello, World!")
* .endObject() * .endObject()
* .toString();</pre> which produces the string <pre> * .toString();</pre> which produces the string <pre>
* {"JSON":"Hello, World!"}</pre> * {"JSON":"Hello, World!"}</pre>
* <p> * <p>
* The first method called must be <code>array</code> or <code>object</code>. * The first method called must be <code>array</code> or <code>object</code>.
* There are no methods for adding commas or colons. JSONStringer adds them for * 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. * you. Objects and arrays can be nested up to 20 levels deep.
* <p> * <p>
* This can sometimes be easier than using a JSONObject to build a string. * This can sometimes be easier than using a JSONObject to build a string.
* @author JSON.org * @author JSON.org
* @version 2015-12-09 * @version 2015-12-09
*/ */
public class JSONStringer extends JSONWriter { public class JSONStringer extends JSONWriter {
/** /**
* Make a fresh JSONStringer. It can be used to build one JSON text. * Make a fresh JSONStringer. It can be used to build one JSON text.
*/ */
public JSONStringer() { public JSONStringer() {
super(new StringWriter()); super(new StringWriter());
} }
/** /**
* Return the JSON text. This method is used to obtain the product of the * Return the JSON text. This method is used to obtain the product of the
* JSONStringer instance. It will return <code>null</code> if there was a * JSONStringer instance. It will return <code>null</code> if there was a
* problem in the construction of the JSON text (such as the calls to * problem in the construction of the JSON text (such as the calls to
* <code>array</code> were not properly balanced with calls to * <code>array</code> were not properly balanced with calls to
* <code>endArray</code>). * <code>endArray</code>).
* @return The JSON text. * @return The JSON text.
*/ */
@Override @Override
public String toString() { public String toString() {
return this.mode == 'd' ? this.writer.toString() : null; return this.mode == 'd' ? this.writer.toString() : null;
} }
} }

View file

@ -448,9 +448,7 @@ public class JSONTokener {
sb.append(c); sb.append(c);
c = this.next(); c = this.next();
} }
if (!this.eof) { this.back();
this.back();
}
string = sb.toString().trim(); string = sb.toString().trim();
if ("".equals(string)) { if ("".equals(string)) {

View file

@ -1,414 +1,418 @@
package org.json; package org.json;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.math.BigDecimal;
import java.util.Map; import java.util.Collection;
import java.util.Map;
/*
Copyright (c) 2006 JSON.org /*
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 Permission is hereby granted, free of charge, to any person obtaining a copy
in the Software without restriction, including without limitation the rights of this software and associated documentation files (the "Software"), to deal
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell in the Software without restriction, including without limitation the rights
copies of the Software, and to permit persons to whom the Software is to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
furnished to do so, subject to the following conditions: 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 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 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, THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SOFTWARE. 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 * JSONWriter provides a quick and convenient way of producing JSON text.
* added, so the results are ready for transmission or storage. Each instance of * The texts produced strictly conform to JSON syntax rules. No whitespace is
* JSONWriter can produce one JSON text. * added, so the results are ready for transmission or storage. Each instance of
* <p> * JSONWriter can produce one JSON text.
* A JSONWriter instance provides a <code>value</code> method for appending * <p>
* values to the * A JSONWriter instance provides a <code>value</code> method for appending
* text, and a <code>key</code> * values to the
* method for adding keys before values in objects. There are <code>array</code> * text, and a <code>key</code>
* and <code>endArray</code> methods that make and bound array values, and * method for adding keys before values in objects. There are <code>array</code>
* <code>object</code> and <code>endObject</code> methods which make and bound * and <code>endArray</code> methods that make and bound array values, and
* object values. All of these methods return the JSONWriter instance, * <code>object</code> and <code>endObject</code> methods which make and bound
* permitting a cascade style. For example, <pre> * object values. All of these methods return the JSONWriter instance,
* new JSONWriter(myWriter) * permitting a cascade style. For example, <pre>
* .object() * new JSONWriter(myWriter)
* .key("JSON") * .object()
* .value("Hello, World!") * .key("JSON")
* .endObject();</pre> which writes <pre> * .value("Hello, World!")
* {"JSON":"Hello, World!"}</pre> * .endObject();</pre> which writes <pre>
* <p> * {"JSON":"Hello, World!"}</pre>
* The first method called must be <code>array</code> or <code>object</code>. * <p>
* There are no methods for adding commas or colons. JSONWriter adds them for * The first method called must be <code>array</code> or <code>object</code>.
* you. Objects and arrays can be nested up to 200 levels deep. * There are no methods for adding commas or colons. JSONWriter adds them for
* <p> * 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. * <p>
* @author JSON.org * This can sometimes be easier than using a JSONObject to build a string.
* @version 2016-08-08 * @author JSON.org
*/ * @version 2016-08-08
public class JSONWriter { */
private static final int maxdepth = 200; public class JSONWriter {
private static final int maxdepth = 200;
/**
* The comma flag determines if a comma should be output before the next /**
* value. * The comma flag determines if a comma should be output before the next
*/ * value.
private boolean comma; */
private boolean comma;
/**
* The current mode. Values: /**
* 'a' (array), * The current mode. Values:
* 'd' (done), * 'a' (array),
* 'i' (initial), * 'd' (done),
* 'k' (key), * 'i' (initial),
* 'o' (object). * 'k' (key),
*/ * 'o' (object).
protected char mode; */
protected char mode;
/**
* The object/array stack. /**
*/ * The object/array stack.
private final JSONObject stack[]; */
private final JSONObject stack[];
/**
* The stack top index. A value of 0 indicates that the stack is empty. /**
*/ * The stack top index. A value of 0 indicates that the stack is empty.
private int top; */
private int top;
/**
* The writer that will receive the output. /**
*/ * The writer that will receive the output.
protected Appendable writer; */
protected Appendable writer;
/**
* Make a fresh JSONWriter. It can be used to build one JSON text. /**
* @param w an appendable object * Make a fresh JSONWriter. It can be used to build one JSON text.
*/ */
public JSONWriter(Appendable w) { public JSONWriter(Appendable w) {
this.comma = false; this.comma = false;
this.mode = 'i'; this.mode = 'i';
this.stack = new JSONObject[maxdepth]; this.stack = new JSONObject[maxdepth];
this.top = 0; this.top = 0;
this.writer = w; this.writer = w;
} }
/** /**
* Append a value. * Append a value.
* @param string A string value. * @param string A string value.
* @return this * @return this
* @throws JSONException If the value is out of sequence. * @throws JSONException If the value is out of sequence.
*/ */
private JSONWriter append(String string) throws JSONException { private JSONWriter append(String string) throws JSONException {
if (string == null) { if (string == null) {
throw new JSONException("Null pointer"); throw new JSONException("Null pointer");
} }
if (this.mode == 'o' || this.mode == 'a') { if (this.mode == 'o' || this.mode == 'a') {
try { try {
if (this.comma && this.mode == 'a') { if (this.comma && this.mode == 'a') {
this.writer.append(','); this.writer.append(',');
} }
this.writer.append(string); this.writer.append(string);
} catch (IOException e) { } catch (IOException e) {
// Android as of API 25 does not support this exception constructor // Android as of API 25 does not support this exception constructor
// however we won't worry about it. If an exception is happening here // however we won't worry about it. If an exception is happening here
// it will just throw a "Method not found" exception instead. // it will just throw a "Method not found" exception instead.
throw new JSONException(e); throw new JSONException(e);
} }
if (this.mode == 'o') { if (this.mode == 'o') {
this.mode = 'k'; this.mode = 'k';
} }
this.comma = true; this.comma = true;
return this; return this;
} }
throw new JSONException("Value out of sequence."); throw new JSONException("Value out of sequence.");
} }
/** /**
* Begin appending a new array. All values until the balancing * Begin appending a new array. All values until the balancing
* <code>endArray</code> will be appended to this array. The * <code>endArray</code> will be appended to this array. The
* <code>endArray</code> method must be called to mark the array's end. * <code>endArray</code> method must be called to mark the array's end.
* @return this * @return this
* @throws JSONException If the nesting is too deep, or if the object is * @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 * started in the wrong place (for example as a key or after the end of the
* outermost array or object). * outermost array or object).
*/ */
public JSONWriter array() throws JSONException { public JSONWriter array() throws JSONException {
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') { if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
this.push(null); this.push(null);
this.append("["); this.append("[");
this.comma = false; this.comma = false;
return this; return this;
} }
throw new JSONException("Misplaced array."); throw new JSONException("Misplaced array.");
} }
/** /**
* End something. * End something.
* @param m Mode * @param m Mode
* @param c Closing character * @param c Closing character
* @return this * @return this
* @throws JSONException If unbalanced. * @throws JSONException If unbalanced.
*/ */
private JSONWriter end(char m, char c) throws JSONException { private JSONWriter end(char m, char c) throws JSONException {
if (this.mode != m) { if (this.mode != m) {
throw new JSONException(m == 'a' throw new JSONException(m == 'a'
? "Misplaced endArray." ? "Misplaced endArray."
: "Misplaced endObject."); : "Misplaced endObject.");
} }
this.pop(m); this.pop(m);
try { try {
this.writer.append(c); this.writer.append(c);
} catch (IOException e) { } catch (IOException e) {
// Android as of API 25 does not support this exception constructor // Android as of API 25 does not support this exception constructor
// however we won't worry about it. If an exception is happening here // however we won't worry about it. If an exception is happening here
// it will just throw a "Method not found" exception instead. // it will just throw a "Method not found" exception instead.
throw new JSONException(e); throw new JSONException(e);
} }
this.comma = true; this.comma = true;
return this; return this;
} }
/** /**
* End an array. This method most be called to balance calls to * End an array. This method most be called to balance calls to
* <code>array</code>. * <code>array</code>.
* @return this * @return this
* @throws JSONException If incorrectly nested. * @throws JSONException If incorrectly nested.
*/ */
public JSONWriter endArray() throws JSONException { public JSONWriter endArray() throws JSONException {
return this.end('a', ']'); return this.end('a', ']');
} }
/** /**
* End an object. This method most be called to balance calls to * End an object. This method most be called to balance calls to
* <code>object</code>. * <code>object</code>.
* @return this * @return this
* @throws JSONException If incorrectly nested. * @throws JSONException If incorrectly nested.
*/ */
public JSONWriter endObject() throws JSONException { public JSONWriter endObject() throws JSONException {
return this.end('k', '}'); return this.end('k', '}');
} }
/** /**
* Append a key. The key will be associated with the next value. In an * Append a key. The key will be associated with the next value. In an
* object, every value must be preceded by a key. * object, every value must be preceded by a key.
* @param string A key string. * @param string A key string.
* @return this * @return this
* @throws JSONException If the key is out of place. For example, keys * @throws JSONException If the key is out of place. For example, keys
* do not belong in arrays or if the key is null. * do not belong in arrays or if the key is null.
*/ */
public JSONWriter key(String string) throws JSONException { public JSONWriter key(String string) throws JSONException {
if (string == null) { if (string == null) {
throw new JSONException("Null key."); throw new JSONException("Null key.");
} }
if (this.mode == 'k') { if (this.mode == 'k') {
try { try {
JSONObject topObject = this.stack[this.top - 1]; JSONObject topObject = this.stack[this.top - 1];
// don't use the built in putOnce method to maintain Android support // don't use the built in putOnce method to maintain Android support
if(topObject.has(string)) { if(topObject.has(string)) {
throw new JSONException("Duplicate key \"" + string + "\""); throw new JSONException("Duplicate key \"" + string + "\"");
} }
topObject.put(string, true); topObject.put(string, true);
if (this.comma) { if (this.comma) {
this.writer.append(','); this.writer.append(',');
} }
this.writer.append(JSONObject.quote(string)); this.writer.append(JSONObject.quote(string));
this.writer.append(':'); this.writer.append(':');
this.comma = false; this.comma = false;
this.mode = 'o'; this.mode = 'o';
return this; return this;
} catch (IOException e) { } catch (IOException e) {
// Android as of API 25 does not support this exception constructor // Android as of API 25 does not support this exception constructor
// however we won't worry about it. If an exception is happening here // however we won't worry about it. If an exception is happening here
// it will just throw a "Method not found" exception instead. // it will just throw a "Method not found" exception instead.
throw new JSONException(e); throw new JSONException(e);
} }
} }
throw new JSONException("Misplaced key."); throw new JSONException("Misplaced key.");
} }
/** /**
* Begin appending a new object. All keys and values until the balancing * Begin appending a new object. All keys and values until the balancing
* <code>endObject</code> will be appended to this object. The * <code>endObject</code> will be appended to this object. The
* <code>endObject</code> method must be called to mark the object's end. * <code>endObject</code> method must be called to mark the object's end.
* @return this * @return this
* @throws JSONException If the nesting is too deep, or if the object is * @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 * started in the wrong place (for example as a key or after the end of the
* outermost array or object). * outermost array or object).
*/ */
public JSONWriter object() throws JSONException { public JSONWriter object() throws JSONException {
if (this.mode == 'i') { if (this.mode == 'i') {
this.mode = 'o'; this.mode = 'o';
} }
if (this.mode == 'o' || this.mode == 'a') { if (this.mode == 'o' || this.mode == 'a') {
this.append("{"); this.append("{");
this.push(new JSONObject()); this.push(new JSONObject());
this.comma = false; this.comma = false;
return this; return this;
} }
throw new JSONException("Misplaced object."); throw new JSONException("Misplaced object.");
} }
/** /**
* Pop an array or object scope. * Pop an array or object scope.
* @param c The scope to close. * @param c The scope to close.
* @throws JSONException If nesting is wrong. * @throws JSONException If nesting is wrong.
*/ */
private void pop(char c) throws JSONException { private void pop(char c) throws JSONException {
if (this.top <= 0) { if (this.top <= 0) {
throw new JSONException("Nesting error."); throw new JSONException("Nesting error.");
} }
char m = this.stack[this.top - 1] == null ? 'a' : 'k'; char m = this.stack[this.top - 1] == null ? 'a' : 'k';
if (m != c) { if (m != c) {
throw new JSONException("Nesting error."); throw new JSONException("Nesting error.");
} }
this.top -= 1; this.top -= 1;
this.mode = this.top == 0 this.mode = this.top == 0
? 'd' ? 'd'
: this.stack[this.top - 1] == null : this.stack[this.top - 1] == null
? 'a' ? 'a'
: 'k'; : 'k';
} }
/** /**
* Push an array or object scope. * Push an array or object scope.
* @param jo The scope to open. * @param jo The scope to open.
* @throws JSONException If nesting is too deep. * @throws JSONException If nesting is too deep.
*/ */
private void push(JSONObject jo) throws JSONException { private void push(JSONObject jo) throws JSONException {
if (this.top >= maxdepth) { if (this.top >= maxdepth) {
throw new JSONException("Nesting too deep."); throw new JSONException("Nesting too deep.");
} }
this.stack[this.top] = jo; this.stack[this.top] = jo;
this.mode = jo == null ? 'a' : 'k'; this.mode = jo == null ? 'a' : 'k';
this.top += 1; this.top += 1;
} }
/** /**
* Make a JSON text of an Object value. If the object has an * 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 * value.toJSONString() method, then that method will be used to produce the
* JSON text. The method is required to produce a strictly conforming text. * 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 * 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 * 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 * 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 * 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 * JSONObject will be made from it and its toJSONString method will be
* called. Otherwise, the value's toString method will be called, and the * called. Otherwise, the value's toString method will be called, and the
* result will be quoted. * result will be quoted.
* *
* <p> * <p>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* *
* @param value * @param value
* The value to be serialized. * The value to be serialized.
* @return a printable, displayable, transmittable representation of the * @return a printable, displayable, transmittable representation of the
* object, beginning with <code>{</code>&nbsp;<small>(left * object, beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right * brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>. * brace)</small>.
* @throws JSONException * @throws JSONException
* If the value is or contains an invalid number. * If the value is or contains an invalid number.
*/ */
public static String valueToString(Object value) throws JSONException { public static String valueToString(Object value) throws JSONException {
if (value == null || value.equals(null)) { if (value == null || value.equals(null)) {
return "null"; return "null";
} }
if (value instanceof JSONString) { if (value instanceof JSONString) {
String object; Object object;
try { try {
object = ((JSONString) value).toJSONString(); object = ((JSONString) value).toJSONString();
} catch (Exception e) { } catch (Exception e) {
throw new JSONException(e); throw new JSONException(e);
} }
if (object != null) { if (object instanceof String) {
return object; return (String) object;
} }
throw new JSONException("Bad value from toJSONString: " + object); throw new JSONException("Bad value from toJSONString: " + object);
} }
if (value instanceof Number) { if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
final String numberAsString = JSONObject.numberToString((Number) value); final String numberAsString = JSONObject.numberToString((Number) value);
if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) { try {
// Close enough to a JSON number that we will return it unquoted // Use the BigDecimal constructor for it's parser to validate the format.
return numberAsString; @SuppressWarnings("unused")
} BigDecimal unused = new BigDecimal(numberAsString);
// The Number value is not a valid JSON number. // Close enough to a JSON number that we will return it unquoted
// Instead we will quote it as a string return numberAsString;
return JSONObject.quote(numberAsString); } catch (NumberFormatException ex){
} // The Number value is not a valid JSON number.
if (value instanceof Boolean || value instanceof JSONObject // Instead we will quote it as a string
|| value instanceof JSONArray) { return JSONObject.quote(numberAsString);
return value.toString(); }
} }
if (value instanceof Map) { if (value instanceof Boolean || value instanceof JSONObject
Map<?, ?> map = (Map<?, ?>) value; || value instanceof JSONArray) {
return new JSONObject(map).toString(); return value.toString();
} }
if (value instanceof Collection) { if (value instanceof Map) {
Collection<?> coll = (Collection<?>) value; Map<?, ?> map = (Map<?, ?>) value;
return new JSONArray(coll).toString(); return new JSONObject(map).toString();
} }
if (value.getClass().isArray()) { if (value instanceof Collection) {
return new JSONArray(value).toString(); Collection<?> coll = (Collection<?>) value;
} return new JSONArray(coll).toString();
if(value instanceof Enum<?>){ }
return JSONObject.quote(((Enum<?>)value).name()); if (value.getClass().isArray()) {
} return new JSONArray(value).toString();
return JSONObject.quote(value.toString()); }
} if(value instanceof Enum<?>){
return JSONObject.quote(((Enum<?>)value).name());
/** }
* Append either the value <code>true</code> or the value return JSONObject.quote(value.toString());
* <code>false</code>. }
* @param b A boolean.
* @return this /**
* @throws JSONException if a called function has an error * Append either the value <code>true</code> or the value
*/ * <code>false</code>.
public JSONWriter value(boolean b) throws JSONException { * @param b A boolean.
return this.append(b ? "true" : "false"); * @return this
} * @throws JSONException
*/
/** public JSONWriter value(boolean b) throws JSONException {
* Append a double value. return this.append(b ? "true" : "false");
* @param d A double. }
* @return this
* @throws JSONException If the number is not finite. /**
*/ * Append a double value.
public JSONWriter value(double d) throws JSONException { * @param d A double.
return this.value(Double.valueOf(d)); * @return this
} * @throws JSONException If the number is not finite.
*/
/** public JSONWriter value(double d) throws JSONException {
* Append a long value. return this.value(Double.valueOf(d));
* @param l A long. }
* @return this
* @throws JSONException if a called function has an error /**
*/ * Append a long value.
public JSONWriter value(long l) throws JSONException { * @param l A long.
return this.append(Long.toString(l)); * @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. * Append an object value.
*/ * @param object The object to append. It can be null, or a Boolean, Number,
public JSONWriter value(Object object) throws JSONException { * String, JSONObject, or JSONArray, or an object that implements JSONString.
return this.append(valueToString(object)); * @return this
} * @throws JSONException If the value is out of sequence.
} */
public JSONWriter value(Object object) throws JSONException {
return this.append(valueToString(object));
}
}

View file

@ -1,3 +1,4 @@
============================================================================
Copyright (c) 2002 JSON.org Copyright (c) 2002 JSON.org
@ -20,4 +21,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.

View file

@ -37,7 +37,7 @@ public class Property {
* Converts a property file object into a JSONObject. The property file object is a table of name value pairs. * Converts a property file object into a JSONObject. The property file object is a table of name value pairs.
* @param properties java.util.Properties * @param properties java.util.Properties
* @return JSONObject * @return JSONObject
* @throws JSONException if a called function has an error * @throws JSONException
*/ */
public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException { public static JSONObject toJSONObject(java.util.Properties properties) throws JSONException {
// can't use the new constructor for Android support // can't use the new constructor for Android support
@ -57,7 +57,7 @@ public class Property {
* Converts the JSONObject into a property file object. * Converts the JSONObject into a property file object.
* @param jo JSONObject * @param jo JSONObject
* @return java.util.Properties * @return java.util.Properties
* @throws JSONException if a called function has an error * @throws JSONException
*/ */
public static Properties toProperties(JSONObject jo) throws JSONException { public static Properties toProperties(JSONObject jo) throws JSONException {
Properties properties = new Properties(); Properties properties = new Properties();

View file

@ -3,8 +3,6 @@ JSON in Java [package org.json]
[![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json) [![Maven Central](https://img.shields.io/maven-central/v/org.json/json.svg)](https://mvnrepository.com/artifact/org.json/json)
**[Click here if you just want the latest release jar file.](https://repo1.maven.org/maven2/org/json/json/20200518/json-20200518.jar)**
JSON is a light-weight, language independent, data interchange format. JSON is a light-weight, language independent, data interchange format.
See http://www.JSON.org/ See http://www.JSON.org/
@ -22,9 +20,6 @@ package.
The package compiles on Java 1.6-1.8. The package compiles on Java 1.6-1.8.
# With 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. If you have difficulty using the new structure, please open an issue so we can work through it.
**JSONObject.java**: The `JSONObject` can parse text from a `String` or a `JSONTokener` **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 to produce a map-like object. The object provides methods for manipulating its
@ -35,9 +30,7 @@ to produce a vector-like object. The object provides methods for manipulating
its contents, and for producing a JSON compliant array serialization. its contents, and for producing a JSON compliant array serialization.
**JSONTokener.java**: The `JSONTokener` breaks a text into a sequence of individual **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 tokens. It can be constructed from a `String`, `Reader`, or `InputStream`.
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 **JSONException.java**: The `JSONException` is the standard exception type thrown
by this package. by this package.
@ -87,11 +80,13 @@ cookie lists.
**XMLTokener.java**: `XMLTokener` extends `JSONTokener` for parsing XML text. **XMLTokener.java**: `XMLTokener` extends `JSONTokener` for parsing XML text.
Unit tests are now included in the project, but require Java 1.8 at the present time. This will be fixed in a forthcoming commit. Unit tests are maintained in a separate project. Contributing developers can test
JSON-java pull requests with the code in this project:
https://github.com/stleary/JSON-Java-unit-test
Numeric types in this package comply with 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 [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). [RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159#section-6).
This package fully supports `Integer`, `Long`, and `Double` Java types. Partial support This package fully supports `Integer`, `Long`, and `Double` Java types. Partial support
for `BigInteger` and `BigDecimal` values in `JSONObject` and `JSONArray` objects is provided for `BigInteger` and `BigDecimal` values in `JSONObject` and `JSONArray` objects is provided
in the form of `get()`, `opt()`, and `put()` API methods. in the form of `get()`, `opt()`, and `put()` API methods.
@ -99,27 +94,17 @@ in the form of `get()`, `opt()`, and `put()` API methods.
Although 1.6 compatibility is currently supported, it is not a project goal and may be Although 1.6 compatibility is currently supported, it is not a project goal and may be
removed in some future release. removed in some future release.
In compliance with RFC8259 page 10 section 9, the parser is more lax with what is valid In compliance with RFC7159 page 10 section 9, the parser is more lax with what is valid
JSON than the Generator. For Example, the tab character (U+0009) is allowed when reading JSON than the Generator. For Example, the tab character (U+0009) is allowed when reading
JSON Text strings, but when output by the Generator, tab is properly converted to \t in JSON Text strings, but when output by the Generator, tab is properly converted to \t in
the string. Other instances may occur where reading invalid JSON text does not cause an 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 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 invalid number formats (1.2e6.3) will cause errors as such documents can not be read
reliably. reliably.
Some notible 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
Release history: Release history:
~~~ ~~~
20200518 Recent commits and snapshot before project structure change
20190722 Recent commits
20180813 POM change to include Automatic-Module-Name (#431) 20180813 POM change to include Automatic-Module-Name (#431)
20180130 Recent commits 20180130 Recent commits
@ -148,60 +133,4 @@ as of 29 July, 2015.
JSON-java releases can be found by searching the Maven repository for groupId "org.json" JSON-java releases can be found by searching the Maven repository for groupId "org.json"
and artifactId "json". For example: and artifactId "json". For example:
https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22
# Unit tests
The test suite can be executed with Maven by running:
```
mvn test
```
The test suite can be executed with Gradle (6.4 or greater) by running:
```
gradle clean build test
```
## Conventions
Test filenames should consist of the name of the module being tested, with the suffix "Test".
For example, <b>Cookie.java</b> is tested by <b>CookieTest.java</b>.
<b>The fundamental issues with JSON-Java testing are:</b><br>
* <b>JSONObjects</b> are unordered, making simple string comparison ineffective.
* Comparisons via **equals()** is not currently supported. Neither <b>JSONArray</b> nor <b>JSONObject</b> override <b>hashCode()</b> or <b>equals()</b>, so comparison defaults to the <b>Object</b> equals(), which is not useful.
* Access to the <b>JSONArray</b> and <b>JSONObject</b> internal containers for comparison is not currently available.
<b>General issues with unit testing are:</b><br>
* Just writing tests to make coverage goals tends to result in poor tests.
* Unit tests are a form of documentation - how a given method actually 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 prevents regressions and keeps 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 the it works as intended.
**Caveats:**
JSON-Java is Java 1.6-compatible, but JSON-Java-unit-tests requires Java 1.8. If you see this error when building JSON-Java-unit-test, make sure you have 1.8 installed, on your path, and set in JAVA_HOME:
```
Execution failed for task ':compileJava'.
> invalid flag: -parameters
```
| Resource files used in test |
| ------------- |
| EnumTest.java |
| MyBean.java |
| MyBigNumberBean.java |
| MyEnum.java |
| MyEnumClass.java |
| MyEnumField.java |
| MyJsonString.java |
| MyPublicClass.java |
| PropertyTest.java |
| JunitTestSuite.java |
| StringsResourceBundle.java |
| TestRunner.java |
| Util.java |

View file

@ -26,20 +26,17 @@ SOFTWARE.
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator; import java.util.Iterator;
/** /**
* This provides static methods to convert an XML text into a JSONObject, and to * This provides static methods to convert an XML text into a JSONObject, and to
* covert a JSONObject into an XML text. * covert a JSONObject into an XML text.
* *
* @author JSON.org * @author JSON.org
* @version 2016-08-10 * @version 2016-08-10
*/ */
@SuppressWarnings("boxing") @SuppressWarnings("boxing")
public class XML { public class XML {
/** The Character '&amp;'. */ /** The Character '&amp;'. */
public static final Character AMP = '&'; public static final Character AMP = '&';
@ -52,7 +49,7 @@ public class XML {
/** The Character '='. */ /** The Character '='. */
public static final Character EQ = '='; public static final Character EQ = '=';
/** The Character <pre>{@code '>'. }</pre>*/ /** The Character '>'. */
public static final Character GT = '>'; public static final Character GT = '>';
/** The Character '&lt;'. */ /** The Character '&lt;'. */
@ -66,12 +63,7 @@ public class XML {
/** The Character '/'. */ /** The Character '/'. */
public static final Character SLASH = '/'; public static final Character SLASH = '/';
/**
* Null attribute name
*/
public static final String NULL_ATTR = "xsi:nil";
/** /**
* Creates an iterator for navigating Code Points in a string instead of * Creates an iterator for navigating Code Points in a string instead of
* characters. Once Java7 support is dropped, this can be replaced with * characters. Once Java7 support is dropped, this can be replaced with
@ -79,7 +71,7 @@ public class XML {
* string.codePoints() * string.codePoints()
* </code> * </code>
* which is available in Java8 and above. * which is available in Java8 and above.
* *
* @see <a href= * @see <a href=
* "http://stackoverflow.com/a/21791059/6030888">http://stackoverflow.com/a/21791059/6030888</a> * "http://stackoverflow.com/a/21791059/6030888">http://stackoverflow.com/a/21791059/6030888</a>
*/ */
@ -114,15 +106,15 @@ public class XML {
/** /**
* Replace special characters with XML escapes: * Replace special characters with XML escapes:
* *
* <pre>{@code * <pre>
* &amp; (ampersand) is replaced by &amp;amp; * &amp; <small>(ampersand)</small> is replaced by &amp;amp;
* &lt; (less than) is replaced by &amp;lt; * &lt; <small>(less than)</small> is replaced by &amp;lt;
* &gt; (greater than) is replaced by &amp;gt; * &gt; <small>(greater than)</small> is replaced by &amp;gt;
* &quot; (double quote) is replaced by &amp;quot; * &quot; <small>(double quote)</small> is replaced by &amp;quot;
* &apos; (single quote / apostrophe) is replaced by &amp;apos; * &apos; <small>(single quote / apostrophe)</small> is replaced by &amp;apos;
* }</pre> * </pre>
* *
* @param string * @param string
* The string to be escaped. * The string to be escaped.
* @return The escaped string. * @return The escaped string.
@ -158,17 +150,17 @@ public class XML {
} }
return sb.toString(); return sb.toString();
} }
/** /**
* @param cp code point to test * @param cp code point to test
* @return true if the code point is not valid for an XML * @return true if the code point is not valid for an XML
*/ */
private static boolean mustEscape(int cp) { private static boolean mustEscape(int cp) {
/* Valid range from https://www.w3.org/TR/REC-xml/#charsets /* Valid range from https://www.w3.org/TR/REC-xml/#charsets
* *
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] * #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
* *
* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
*/ */
// isISOControl is true when (cp >= 0 && cp <= 0x1F) || (cp >= 0x7F && cp <= 0x9F) // 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 // all ISO control characters are out of range except tabs and new lines
@ -187,7 +179,7 @@ public class XML {
/** /**
* Removes XML escapes from the string. * Removes XML escapes from the string.
* *
* @param string * @param string
* string to remove escapes from * string to remove escapes from
* @return string with converted entities * @return string with converted entities
@ -219,7 +211,7 @@ public class XML {
/** /**
* Throw an exception if the string contains whitespace. Whitespace is not * Throw an exception if the string contains whitespace. Whitespace is not
* allowed in tagNames and attributes. * allowed in tagNames and attributes.
* *
* @param string * @param string
* A string. * A string.
* @throws JSONException Thrown if the string contains whitespace or is empty. * @throws JSONException Thrown if the string contains whitespace or is empty.
@ -239,7 +231,7 @@ public class XML {
/** /**
* Scan the content following the named tag, attaching it to the context. * Scan the content following the named tag, attaching it to the context.
* *
* @param x * @param x
* The XMLTokener containing the source string. * The XMLTokener containing the source string.
* @param context * @param context
@ -249,11 +241,11 @@ public class XML {
* @return true if the close tag is processed. * @return true if the close tag is processed.
* @throws JSONException * @throws JSONException
*/ */
private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config) private static boolean parse(XMLTokener x, JSONObject context, String name, boolean keepStrings)
throws JSONException { throws JSONException {
char c; char c;
int i; int i;
JSONObject jsonObject = null; JSONObject jsonobject = null;
String string; String string;
String tagName; String tagName;
Object token; Object token;
@ -286,7 +278,7 @@ public class XML {
if (x.next() == '[') { if (x.next() == '[') {
string = x.nextCDATA(); string = x.nextCDATA();
if (string.length() > 0) { if (string.length() > 0) {
context.accumulate(config.cDataTagName, string); context.accumulate("content", string);
} }
return false; return false;
} }
@ -334,8 +326,7 @@ public class XML {
} else { } else {
tagName = (String) token; tagName = (String) token;
token = null; token = null;
jsonObject = new JSONObject(); jsonobject = new JSONObject();
boolean nilAttributeFound = false;
for (;;) { for (;;) {
if (token == null) { if (token == null) {
token = x.nextToken(); token = x.nextToken();
@ -349,20 +340,11 @@ public class XML {
if (!(token instanceof String)) { if (!(token instanceof String)) {
throw x.syntaxError("Missing value"); throw x.syntaxError("Missing value");
} }
jsonobject.accumulate(string,
if (config.convertNilAttributeToNull keepStrings ? ((String)token) : stringToValue((String) token));
&& NULL_ATTR.equals(string)
&& Boolean.parseBoolean((String) token)) {
nilAttributeFound = true;
} else if (!nilAttributeFound) {
jsonObject.accumulate(string,
config.keepStrings
? ((String) token)
: stringToValue((String) token));
}
token = null; token = null;
} else { } else {
jsonObject.accumulate(string, ""); jsonobject.accumulate(string, "");
} }
@ -371,10 +353,8 @@ public class XML {
if (x.nextToken() != GT) { if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped tag"); throw x.syntaxError("Misshaped tag");
} }
if (nilAttributeFound) { if (jsonobject.length() > 0) {
context.accumulate(tagName, JSONObject.NULL); context.accumulate(tagName, jsonobject);
} else if (jsonObject.length() > 0) {
context.accumulate(tagName, jsonObject);
} else { } else {
context.accumulate(tagName, ""); context.accumulate(tagName, "");
} }
@ -392,20 +372,21 @@ public class XML {
} else if (token instanceof String) { } else if (token instanceof String) {
string = (String) token; string = (String) token;
if (string.length() > 0) { if (string.length() > 0) {
jsonObject.accumulate(config.cDataTagName, jsonobject.accumulate("content",
config.keepStrings ? string : stringToValue(string)); keepStrings ? string : stringToValue(string));
} }
} else if (token == LT) { } else if (token == LT) {
// Nested element // Nested element
if (parse(x, jsonObject, tagName, config)) { if (parse(x, jsonobject, tagName,keepStrings)) {
if (jsonObject.length() == 0) { if (jsonobject.length() == 0) {
context.accumulate(tagName, ""); context.accumulate(tagName, "");
} else if (jsonObject.length() == 1 } else if (jsonobject.length() == 1
&& jsonObject.opt(config.cDataTagName) != null) { && jsonobject.opt("content") != null) {
context.accumulate(tagName, jsonObject.opt(config.cDataTagName)); context.accumulate(tagName,
jsonobject.opt("content"));
} else { } else {
context.accumulate(tagName, jsonObject); context.accumulate(tagName, jsonobject);
} }
return false; return false;
} }
@ -417,29 +398,26 @@ public class XML {
} }
} }
} }
/** /**
* This method is the same as {@link JSONObject#stringToValue(String)}. * This method is the same as {@link JSONObject#stringToValue(String)}.
* *
* @param string String to convert * @param string String to convert
* @return JSON value of this string or the string * @return JSON value of this string or the string
*/ */
// To maintain compatibility with the Android API, this method is a direct copy of // 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. // 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) { public static Object stringToValue(String string) {
if ("".equals(string)) { if (string.equals("")) {
return string; return string;
} }
if (string.equalsIgnoreCase("true")) {
// check JSON key words true/false/null
if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if ("false".equalsIgnoreCase(string)) { if (string.equalsIgnoreCase("false")) {
return Boolean.FALSE; return Boolean.FALSE;
} }
if ("null".equalsIgnoreCase(string)) { if (string.equalsIgnoreCase("null")) {
return JSONObject.NULL; return JSONObject.NULL;
} }
@ -451,84 +429,28 @@ public class XML {
char initial = string.charAt(0); char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
try { try {
return stringToNumber(string); // if we want full Big Number support this block can be replaced with:
// return stringToNumber(string);
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|| string.indexOf('E') > -1 || "-0".equals(string)) {
Double d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} else {
Long myLong = Long.valueOf(string);
if (string.equals(myLong.toString())) {
if (myLong.longValue() == myLong.intValue()) {
return Integer.valueOf(myLong.intValue());
}
return myLong;
}
}
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
return string; return string;
} }
/**
* direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support.
*/
private 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);
}
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 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.
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);
}
/** /**
* Convert a well-formed (but not necessarily valid) XML string into a * Convert a well-formed (but not necessarily valid) XML string into a
@ -538,17 +460,16 @@ public class XML {
* name/value pairs and arrays of values. JSON does not does not like to * name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar * distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a * elements are represented as JSONArrays. Content text may be placed in a
* "content" member. Comments, prologs, DTDs, and <pre>{@code * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
* &lt;[ [ ]]>}</pre>
* are ignored. * are ignored.
* *
* @param string * @param string
* The source string. * The source string.
* @return A JSONObject containing the structured data from the XML string. * @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown if there is an errors while parsing the string * @throws JSONException Thrown if there is an errors while parsing the string
*/ */
public static JSONObject toJSONObject(String string) throws JSONException { public static JSONObject toJSONObject(String string) throws JSONException {
return toJSONObject(string, XMLParserConfiguration.ORIGINAL); return toJSONObject(string, false);
} }
/** /**
@ -559,8 +480,7 @@ public class XML {
* name/value pairs and arrays of values. JSON does not does not like to * name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar * distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a * elements are represented as JSONArrays. Content text may be placed in a
* "content" member. Comments, prologs, DTDs, and <pre>{@code * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
* &lt;[ [ ]]>}</pre>
* are ignored. * are ignored.
* *
* @param reader The XML source reader. * @param reader The XML source reader.
@ -568,7 +488,7 @@ public class XML {
* @throws JSONException Thrown if there is an errors while parsing the string * @throws JSONException Thrown if there is an errors while parsing the string
*/ */
public static JSONObject toJSONObject(Reader reader) throws JSONException { public static JSONObject toJSONObject(Reader reader) throws JSONException {
return toJSONObject(reader, XMLParserConfiguration.ORIGINAL); return toJSONObject(reader, false);
} }
/** /**
@ -579,8 +499,7 @@ public class XML {
* name/value pairs and arrays of values. JSON does not does not like to * name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar * distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a * elements are represented as JSONArrays. Content text may be placed in a
* "content" member. Comments, prologs, DTDs, and <pre>{@code * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
* &lt;[ [ ]]>}</pre>
* are ignored. * are ignored.
* *
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
@ -593,39 +512,12 @@ public class XML {
* @throws JSONException Thrown if there is an errors while parsing the string * @throws JSONException Thrown if there is an errors while parsing the string
*/ */
public static JSONObject toJSONObject(Reader reader, boolean keepStrings) throws JSONException { 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 <pre>{@code
* &lt;[ [ ]]>}</pre>
* 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(); JSONObject jo = new JSONObject();
XMLTokener x = new XMLTokener(reader); XMLTokener x = new XMLTokener(reader);
while (x.more()) { while (x.more()) {
x.skipPast("<"); x.skipPast("<");
if(x.more()) { if(x.more()) {
parse(x, jo, null, config); parse(x, jo, null, keepStrings);
} }
} }
return jo; return jo;
@ -639,13 +531,12 @@ public class XML {
* name/value pairs and arrays of values. JSON does not does not like to * name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar * distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a * elements are represented as JSONArrays. Content text may be placed in a
* "content" member. Comments, prologs, DTDs, and <pre>{@code * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
* &lt;[ [ ]]>}</pre>
* are ignored. * are ignored.
* *
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to * 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. * numbers but will instead be the exact value as seen in the XML document.
* *
* @param string * @param string
* The source string. * The source string.
* @param keepStrings If true, then values will not be coerced into boolean * @param keepStrings If true, then values will not be coerced into boolean
@ -657,46 +548,21 @@ public class XML {
return toJSONObject(new StringReader(string), keepStrings); 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 <pre>{@code
* &lt;[ [ ]]>}</pre>
* 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. * Convert a JSONObject into a well-formed, element-normal XML string.
* *
* @param object * @param object
* A JSONObject. * A JSONObject.
* @return A string. * @return A string.
* @throws JSONException Thrown if there is an error parsing the string * @throws JSONException Thrown if there is an error parsing the string
*/ */
public static String toString(Object object) throws JSONException { public static String toString(Object object) throws JSONException {
return toString(object, null, XMLParserConfiguration.ORIGINAL); return toString(object, null);
} }
/** /**
* Convert a JSONObject into a well-formed, element-normal XML string. * Convert a JSONObject into a well-formed, element-normal XML string.
* *
* @param object * @param object
* A JSONObject. * A JSONObject.
* @param tagName * @param tagName
@ -704,23 +570,7 @@ public class XML {
* @return A string. * @return A string.
* @throws JSONException Thrown if there is an error parsing the string * @throws JSONException Thrown if there is an error parsing the string
*/ */
public static String toString(final Object object, final String tagName) { 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 { throws JSONException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
JSONArray ja; JSONArray ja;
@ -748,7 +598,7 @@ public class XML {
} }
// Emit content in body // Emit content in body
if (key.equals(config.cDataTagName)) { if ("content".equals(key)) {
if (value instanceof JSONArray) { if (value instanceof JSONArray) {
ja = (JSONArray) value; ja = (JSONArray) value;
int jaLength = ja.length(); int jaLength = ja.length();
@ -776,12 +626,12 @@ public class XML {
sb.append('<'); sb.append('<');
sb.append(key); sb.append(key);
sb.append('>'); sb.append('>');
sb.append(toString(val, null, config)); sb.append(toString(val));
sb.append("</"); sb.append("</");
sb.append(key); sb.append(key);
sb.append('>'); sb.append('>');
} else { } else {
sb.append(toString(val, key, config)); sb.append(toString(val, key));
} }
} }
} else if ("".equals(value)) { } else if ("".equals(value)) {
@ -792,12 +642,12 @@ public class XML {
// Emit a new tag <k> // Emit a new tag <k>
} else { } else {
sb.append(toString(value, key, config)); sb.append(toString(value, key));
} }
} }
if (tagName != null) { if (tagName != null) {
// Emit the </tagName> close tag // Emit the </tagname> close tag
sb.append("</"); sb.append("</");
sb.append(tagName); sb.append(tagName);
sb.append('>'); sb.append('>');
@ -819,7 +669,7 @@ public class XML {
// XML does not have good support for arrays. If an array // XML does not have good support for arrays. If an array
// appears in a place where XML is lacking, synthesize an // appears in a place where XML is lacking, synthesize an
// <array> element. // <array> element.
sb.append(toString(val, tagName == null ? "array" : tagName, config)); sb.append(toString(val, tagName == null ? "array" : tagName));
} }
return sb.toString(); return sb.toString();
} }

View file

@ -90,13 +90,12 @@ public class XMLTokener extends JSONTokener {
/** /**
* Get the next XML outer token, trimming whitespace. There are two kinds * Get the next XML outer token, trimming whitespace. There are two kinds
* of tokens: the <pre>{@code '<' }</pre> character which begins a markup * of tokens: the '<' character which begins a markup tag, and the content
* tag, and the content
* text between markup tags. * text between markup tags.
* *
* @return A string, or a <pre>{@code '<' }</pre> Character, or null if * @return A string, or a '<' Character, or null if there is no more
* there is no more source text. * source text.
* @throws JSONException if a called function has an error * @throws JSONException
*/ */
public Object nextContent() throws JSONException { public Object nextContent() throws JSONException {
char c; char c;
@ -130,15 +129,13 @@ public class XMLTokener extends JSONTokener {
/** /**
* <pre>{@code
* Return the next entity. These entities are translated to Characters: * Return the next entity. These entities are translated to Characters:
* &amp; &apos; &gt; &lt; &quot;. * <code>&amp; &apos; &gt; &lt; &quot;</code>.
* }</pre>
* @param ampersand An ampersand character. * @param ampersand An ampersand character.
* @return A Character or an entity String if the entity is not recognized. * @return A Character or an entity String if the entity is not recognized.
* @throws JSONException If missing ';' in XML entity. * @throws JSONException If missing ';' in XML entity.
*/ */
public Object nextEntity(@SuppressWarnings("unused") char ampersand) throws JSONException { public Object nextEntity(char ampersand) throws JSONException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (;;) { for (;;) {
char c = next(); char c = next();
@ -155,7 +152,7 @@ public class XMLTokener extends JSONTokener {
} }
/** /**
* Unescape an XML entity encoding; * Unescapes an XML entity encoding;
* @param e entity (only the actual entity value, not the preceding & or ending ; * @param e entity (only the actual entity value, not the preceding & or ending ;
* @return * @return
*/ */
@ -186,14 +183,11 @@ public class XMLTokener extends JSONTokener {
/** /**
* <pre>{@code
* Returns the next XML meta token. This is used for skipping over <!...> * Returns the next XML meta token. This is used for skipping over <!...>
* and <?...?> structures. * and <?...?> structures.
* }</pre> * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
* @return <pre>{@code Syntax characters (< > / = ! ?) are returned as
* Character, and strings and names are returned as Boolean. We don't care * Character, and strings and names are returned as Boolean. We don't care
* what the values actually are. * what the values actually are.
* }</pre>
* @throws JSONException If a string is not properly closed or if the XML * @throws JSONException If a string is not properly closed or if the XML
* is badly structured. * is badly structured.
*/ */
@ -238,7 +232,6 @@ public class XMLTokener extends JSONTokener {
} }
switch (c) { switch (c) {
case 0: case 0:
throw syntaxError("Unterminated string");
case '<': case '<':
case '>': case '>':
case '/': case '/':
@ -256,12 +249,10 @@ public class XMLTokener extends JSONTokener {
/** /**
* <pre>{@code
* Get the next XML Token. These tokens are found inside of angle * 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: <code>/ > = ! ?</code> or it
* may be a string wrapped in single quotes or double quotes, or it may be a * may be a string wrapped in single quotes or double quotes, or it may be a
* name. * name.
* }</pre>
* @return a String or a Character. * @return a String or a Character.
* @throws JSONException If the XML is not well formed. * @throws JSONException If the XML is not well formed.
*/ */

View file

@ -1,58 +0,0 @@
/*
* This file was generated by the Gradle 'init' task.
*/
apply plugin: 'java'
apply plugin: 'eclipse'
// apply plugin: 'jacoco'
apply plugin: 'maven-publish'
//plugins {
// id 'java'
//id 'maven-publish'
// }
repositories {
mavenLocal()
maven {
url = uri('https://oss.sonatype.org/content/repositories/snapshots')
}
maven {
url = uri('http://repo.maven.apache.org/maven2')
}
}
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'com.jayway.jsonpath:json-path:2.1.0'
testImplementation 'org.mockito:mockito-core:1.9.5'
}
subprojects {
tasks.withType(Javadoc).all { enabled = false }
}
group = 'org.json'
version = 'v20200429-SNAPSHOT'
description = 'JSON in Java'
sourceCompatibility = '1.7'
configurations.all {
}
java {
withSourcesJar()
withJavadocJar()
}
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

Binary file not shown.

View file

@ -1,5 +0,0 @@
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

183
gradlew vendored
View file

@ -1,183 +0,0 @@
#!/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" "$@"

103
gradlew.bat vendored
View file

@ -1,103 +0,0 @@
@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

193
pom.xml
View file

@ -1,193 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>v20200429-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>JSON in Java</name>
<description>
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 is a large number of JSON packages
in Java. Perhaps someday the Java community will standardize on one. Until
then, choose carefully.
The license includes this restriction: "The software shall be used for good,
not evil." If your conscience cannot live with that, then choose a different
package.
</description>
<url>https://github.com/douglascrockford/JSON-java</url>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</parent>
<scm>
<url>https://github.com/douglascrockford/JSON-java.git</url>
<connection>scm:git:git://github.com/douglascrockford/JSON-java.git</connection>
<developerConnection>scm:git:git@github.com:douglascrockford/JSON-java.git</developerConnection>
</scm>
<licenses>
<license>
<name>The JSON License</name>
<url>http://json.org/license.html</url>
<distribution>repo</distribution>
<comments>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.
</comments>
</license>
</licenses>
<developers>
<developer>
<name>Douglas Crockford</name>
<email>douglas@crockford.com</email>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
org.json
</Export-Package>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>org.json</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -1,43 +0,0 @@
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.
*/
/**
* The <code>JSONString</code> interface allows a <code>toJSONString()</code>
* method so that a class can change the behavior of
* <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>,
* and <code>JSONWriter.value(</code>Object<code>)</code>. The
* <code>toJSONString</code> method will be used instead of the default behavior
* of using the Object's <code>toString()</code> method and quoting the result.
*/
public interface JSONString {
/**
* The <code>toJSONString</code> method allows a class to produce its own JSON
* serialization.
*
* @return A strictly syntactically correct JSON text.
*/
public String toJSONString();
}

View file

@ -1,107 +0,0 @@
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.
*/
/**
* Configuration object for the XML parser.
* @author AylwardJ
*
*/
public class XMLParserConfiguration {
/** 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(true);
/**
* 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)
*/
public final boolean keepStrings;
/**
* 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 <code>null</code> to indicate no CDATA
* processing.
*/
public final 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)
*/
public final boolean convertNilAttributeToNull;
/**
* Default parser configuration. Does not keep strings, and the CDATA Tag Name is "content".
*/
public XMLParserConfiguration () {
this(false, "content", false);
}
/**
* Configure the parser string processing and use the default CDATA Tag Name as "content".
* @param keepStrings <code>true</code> to parse all values as string.
* <code>false</code> to try and convert XML string values into a JSON value.
*/
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 <code>null</code> to
* disable CDATA processing
* @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
* to use that value as the JSONObject key name to process as CDATA.
*/
public XMLParserConfiguration (final String cDataTagName) {
this(false, cDataTagName, false);
}
/**
* Configure the parser to use custom settings.
* @param keepStrings <code>true</code> to parse all values as string.
* <code>false</code> to try and convert XML string values into a JSON value.
* @param cDataTagName<code>null</code> to disable CDATA processing. Any other value
* to use that value as the JSONObject key name to process as CDATA.
*/
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
this.keepStrings = keepStrings;
this.cDataTagName = cDataTagName;
this.convertNilAttributeToNull = false;
}
/**
* Configure the parser to use custom settings.
* @param keepStrings <code>true</code> to parse all values as string.
* <code>false</code> to try and convert XML string values into a JSON value.
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value
* to use that value as the JSONObject key name to process as CDATA.
* @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null.
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
*/
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
this.keepStrings = keepStrings;
this.cDataTagName = cDataTagName;
this.convertNilAttributeToNull = convertNilAttributeToNull;
}
}

View file

@ -1,324 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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.
*/
String lines = new String(
"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.
*/
String expectedLines = new String(
"[{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());
}
}
/**
* 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);
assertTrue("CDL should return null when the input string is empty",
jsonArray == null);
}
/**
* 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");
assertTrue("expected length should be 1",jsonArray.length() == 1);
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(this.lines);
JSONArray expectedJsonArray = new JSONArray(this.expectedLines);
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(this.lines);
String jsonStr = CDL.toString(jsonArray);
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
JSONArray expectedJsonArray = new JSONArray(this.expectedLines);
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
}
}

View file

@ -1,210 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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
* <p>
* 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.
* <p>
* CookieList should not be considered as just a list of Cookie objects:<br>
* - CookieList stores a cookie name/value pair as a single entry; Cookie stores
* it as 2 entries (key="name" and key="value").<br>
* - CookieList requires multiple name/value pairs as input; Cookie allows the
* 'secure' name with no associated value<br>
* - CookieList has no special handling for attribute name/value pairs.<br>
*/
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")));
}
}

View file

@ -1,262 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import static org.junit.Assert.*;
import org.json.*;
import org.junit.Test;
/**
* HTTP cookie specification: RFC6265
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.<br>
* Note: Cookie throws an exception, but CookieList does not.<br>
* 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.<br>
* Note: Cookie throws an exception, but CookieList does not.<br>
* 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));
}
}

View file

@ -1,453 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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 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:<br>
* JSONObject.getEnum(class, key)<br>
* JSONObject.optEnum(class, key)<br>
* JSONObject.optEnum(class, key, default)<br>
* JSONArray.getEnum(class, index)<br>
* JSONArray.optEnum(class, index)<br>
* JSONArray.optEnum(class, index, default)<br>
* <p>
* 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);
}
}

View file

@ -1,220 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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);
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,856 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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.",
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/x>\n"+
" <street>abc street</street>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <!>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <!\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <abc\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <abc/>\n"+
" </>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation=\"test.xsd\">\n"+
" <address>\n"+
" <name/>\n"+
" <abc/>\n"+
" </address\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>Joe Tester</name>\n"+
" <![[]>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
"<address attr1=\"attrValue1\" attr2=\"attrValue2\" attr3=\"attrValue3\">\n"+
"<name nameType=\"mine\">myName</name>\n"+
"<nocontent/>>\n"+
"</address>\n"+
"</addresses>";
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.
* <p>
* 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.
* <p>
* 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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
"<address addrType=\"my address\">\n"+
"<name nameType=\"my name\">Joe Tester</name>\n"+
"<street><![CDATA[Baker street 5]]></street>\n"+
"<NothingHere except=\"an attribute\"/>\n"+
"<TrueValue>true</TrueValue>\n"+
"<FalseValue>false</FalseValue>\n"+
"<NullValue>null</NullValue>\n"+
"<PositiveValue>42</PositiveValue>\n"+
"<NegativeValue>-23</NegativeValue>\n"+
"<DoubleValue>-23.45</DoubleValue>\n"+
"<Nan>-23x.45</Nan>\n"+
"<ArrayOfNum>\n"+
"<value>1</value>\n"+
"<value>2</value>\n"+
"<value><subValue svAttr=\"svValue\">abc</subValue></value>\n"+
"<value>3</value>\n"+
"<value>4.1</value>\n"+
"<value>5.2</value>\n"+
"</ArrayOfNum>\n"+
"</address>\n"+
"</addresses>";
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.
* <p>
* This test shows how XML comments are handled.
*/
@Test
public void commentsInXML() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<!-- this is a comment -->\n"+
"<addresses>\n"+
"<address>\n"+
"<!-- <!--[CDATA[ this is -- <another> comment ]] -->\n"+
"<name>Joe Tester</name>\n"+
"<!-- this is a - multi line \n"+
"comment -->\n"+
"<street>Baker street 5</street>\n"+
"</address>\n"+
"</addresses>";
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 = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
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 = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
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 = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
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 = "<readResult><errors someAttr=\"arrtValue\"><code>400</code></errors><errors><code>402</code></errors></readResult>";
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 = "<readResult><errors someAttr=\"arrtValue\"><code>400</code></errors><errors><code>402</code></errors></readResult>";
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));
assertEquals("original JSON does not equal the new JSON",originalJson, 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 &nbsp;
// 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 <tag attr1="v1" attr2="v2" /> 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 = "<table class=\"MyTable\" style=\"background-color:yellow\"><tr><td class=\"MyTD\" style=\"border:1px solid black\">#5D28D1</td><td class=\"MyTD\" style=\"background-color:red\">Example text here</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AF44EF</td><td class=\"MyTD\" style=\"background-color:green\">127310656</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AAD034</td><td class=\"MyTD\" style=\"background-color:blue\">&nbsp;<span style=\"background-color:maroon\">&copy;</span>&nbsp;</td></tr></table>";
// 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 = "<table class=\"MyTable\" style=\"background-color:yellow\"><tr><td class=\"MyTD\" style=\"border:1px solid black\">#5D28D1</td><td class=\"MyTD\" style=\"background-color:red\">Example text here</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AF44EF</td><td class=\"MyTD\" style=\"background-color:green\">127310656</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AAD034</td><td class=\"MyTD\" style=\"background-color:blue\">&amp;<span style=\"background-color:maroon\">&gt;</span>&lt;</td></tr></table>";
// 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?<!C*^P^Y?^X9?~?t?)??,z^XA???S}?Q??.q?j????]");
fail("Exception expected for invalid JSON.");
} catch (JSONException ex) {
assertEquals("Exception string did not match: ",
"Unterminated string at 271 [character 272 line 1]",
ex.getMessage());
}
}
@Test (timeout = 6000)
public void testIssue484InfinteLoop2() {
try {
String input = "??*\n" +
"??|?CglR??`??>?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?<!C*?9?~?t?)??,zA???S}?Q??.q?j????]";
JSONML.toJSONObject(input);
fail("Exception expected for invalid JSON.");
} catch (JSONException ex) {
assertEquals("Exception string did not match: ",
"Unterminated string at 242 [character 238 line 2]",
ex.getMessage());
}
}
}

View file

@ -1,80 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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"));
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,384 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
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;
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() {
assertSame(document, query(""));
}
@SuppressWarnings("unused")
@Test(expected = NullPointerException.class)
public void nullPointer() {
new JSONPointer((String) null);
}
@Test
public void objectPropertyQuery() {
assertSame(document.get("foo"), query("/foo"));
}
@Test
public void arrayIndexQuery() {
assertSame(document.getJSONArray("foo").get(0), query("/foo/0"));
}
@Test(expected = JSONPointerException.class)
public void stringPropOfArrayFailure() {
query("/foo/bar");
}
@Test
public void queryByEmptyKey() {
assertSame(document.get(""), query("/"));
}
@Test
public void queryByEmptyKeySubObject() {
assertSame(document.getJSONObject("obj").getJSONObject(""), query("/obj/"));
}
@Test
public void queryByEmptyKeySubObjectSubOject() {
assertSame(
document.getJSONObject("obj").getJSONObject("").get(""),
query("/obj//")
);
}
@Test
public void queryByEmptyKeySubObjectValue() {
assertSame(
document.getJSONObject("obj").getJSONObject("").get("subKey"),
query("/obj//subKey")
);
}
@Test
public void slashEscaping() {
assertSame(document.get("a/b"), query("/a~1b"));
}
@Test
public void tildeEscaping() {
assertSame(document.get("m~n"), query("/m~0n"));
}
@Test
public void backslashEscaping() {
assertSame(document.get("i\\j"), query("/i\\\\j"));
}
@Test
public void quotationEscaping() {
assertSame(document.get("k\"l"), query("/k\\\\\\\"l"));
}
@Test
public void whitespaceKey() {
assertSame(document.get(" "), query("/ "));
}
@Test
public void uriFragmentNotation() {
assertSame(document.get("foo"), query("#/foo"));
}
@Test
public void uriFragmentNotationRoot() {
assertSame(document, query("#"));
}
@Test
public void uriFragmentPercentHandling() {
assertSame(document.get("c%d"), query("#/c%25d"));
assertSame(document.get("e^f"), query("#/e%5Ef"));
assertSame(document.get("g|h"), query("#/g%7Ch"));
assertSame(document.get("m~n"), 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);
}
}

View file

@ -1,360 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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);
try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[null]".equals(output));
jsonArray = new JSONArray();
jsonArray.put(JSONObject.NULL);
}
try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[null]".equals(output));
jsonArray = new JSONArray();
jsonArray.put(new JSONObject());
}
try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[{}]".equals(output));
jsonArray = new JSONArray();
jsonArray.put(new JSONArray());
}
try (StringWriter writer = new StringWriter();) {
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);
}
try (StringWriter writer = new StringWriter();) {
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);
}
try (StringWriter writer = new StringWriter();) {
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);
}
try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
jsonArray = new JSONArray();
jsonArray.put(24);
}
try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[24]".equals(output));
jsonArray = new JSONArray();
jsonArray.put("string value");
}
try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[\"string value\"]".equals(output));
jsonArray = new JSONArray();
jsonArray.put(true);
}
try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[true]".equals(output));
}
}
/**
* 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);
try (StringWriter writer = new StringWriter();) {
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));
}
}
/**
* 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);
try (StringWriter writer = new StringWriter();) {
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()));
}
}
}
/**
* 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);
try (StringWriter writer = new StringWriter();) {
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");
}
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);
try (StringWriter writer = new StringWriter();) {
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));
}
}
/**
* 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);
try (StringWriter writer = new StringWriter();) {
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));
}
}
/**
* 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;
}
}
}

View file

@ -1,377 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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")));
}
}

View file

@ -1,319 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
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 {
try(Reader reader = new StringReader("some test string")) {
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());
}
}
}
/**
* verify that back() fails as expected.
* @throws IOException thrown if something unexpected happens.
*/
@Test
public void verifyBackFailureDoubleBack() throws IOException {
try(Reader reader = new StringReader("some test string")) {
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());
}
}
}
@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);
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 {
try(StringReader sr = new StringReader(testStr);){
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;
}
}
/**
* 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<superLongBuffer.length;i++) {
superLongBuffer[i] = 'A';
}
try(Reader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(superLongBuffer)))) {
final JSONTokener tokener = new JSONTokener(reader);
try {
// this should fail since the internal markAhead buffer is only 1,000,000
// but 'B' doesn't exist in our buffer that is 1,000,001 in size
tokener.skipTo('B');
fail("Expected exception");
} catch (JSONException e) {
assertEquals("Mark invalid", e.getMessage());
} catch (Exception e) {
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
}
}
}
/**
* Tests the success of the skipTo method with a String reader.
*
* @throws IOException thrown if something unexpected happens.
*/
@Test
public void testSkipToSuccessWithStringReader() throws IOException {
final StringBuilder superLongBuffer = new StringBuilder(1000001);
// fill our buffer
for(int i=0;i<superLongBuffer.length();i++) {
superLongBuffer.append('A');
}
try(Reader reader = new StringReader(superLongBuffer.toString())) {
final JSONTokener tokener = new JSONTokener(reader);
try {
// this should not fail since the internal markAhead is ignored for StringReaders
tokener.skipTo('B');
} catch (Exception e) {
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
}
}
}
/**
* Verify that next and back are working properly and tracking the correct positions
* with different new line combinations.
*/
@Test
public void testNextBackComboWithNewLines() {
final String testString = "this is\nA test\r\nWith some different\rNew Lines";
// ^ ^ ^ ^
// index positions 0 8 16 36
final JSONTokener tokener = new JSONTokener(testString);
assertEquals(" at 0 [character 1 line 1]", tokener.toString());
assertEquals('t',tokener.next());
assertEquals(" at 1 [character 2 line 1]", tokener.toString());
tokener.skipTo('\n');
assertEquals("skipTo() improperly modifying indexes"," at 7 [character 8 line 1]", tokener.toString());
assertEquals('\n',tokener.next());
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
assertEquals('A',tokener.next());
assertEquals(" at 9 [character 1 line 2]", tokener.toString());
tokener.back();
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
tokener.skipTo('\r');
assertEquals("skipTo() improperly modifying indexes"," at 14 [character 6 line 2]", tokener.toString());
// verify \r\n combo doesn't increment the line twice
assertEquals('\r', tokener.next());
assertEquals(" at 15 [character 0 line 3]", tokener.toString());
assertEquals('\n', tokener.next());
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
// verify stepping back after reading the \n of an \r\n combo doesn't increment the line incorrectly
tokener.back();
assertEquals(" at 15 [character 6 line 2]", tokener.toString());
assertEquals('\n', tokener.next());
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
assertEquals('W', tokener.next());
assertEquals(" at 17 [character 1 line 3]", tokener.toString());
assertEquals('i', tokener.next());
assertEquals(" at 18 [character 2 line 3]", tokener.toString());
tokener.skipTo('\r');
assertEquals("skipTo() improperly modifying indexes"," at 35 [character 19 line 3]", tokener.toString());
assertEquals('\r', tokener.next());
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
tokener.back();
assertEquals(" at 35 [character 19 line 3]", tokener.toString());
assertEquals('\r', tokener.next());
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
assertEquals('N', tokener.next());
assertEquals(" at 37 [character 1 line 4]", tokener.toString());
// verify we get the same data just walking though, no calls to back
final JSONTokener t2 = new JSONTokener(testString);
for(int i=0; i<7; i++) {
assertTrue(t2.toString().startsWith(" at " + i + " "));
assertEquals(testString.charAt(i), t2.next());
}
assertEquals(" at 7 [character 8 line 1]", t2.toString());
assertEquals(testString.charAt(7), t2.next());
assertEquals(" at 8 [character 0 line 2]", t2.toString());
for(int i=8; i<14; i++) {
assertTrue(t2.toString().startsWith(" at " + i + " "));
assertEquals(testString.charAt(i), t2.next());
}
assertEquals(" at 14 [character 6 line 2]", t2.toString());
assertEquals('\r', t2.next());
assertEquals(" at 15 [character 0 line 3]", t2.toString());
assertEquals('\n', t2.next());
assertEquals(" at 16 [character 0 line 3]", t2.toString());
assertEquals('W', t2.next());
assertEquals(" at 17 [character 1 line 3]", t2.toString());
for(int i=17; i<37; i++) {
assertTrue(t2.toString().startsWith(" at " + i + " "));
assertEquals(testString.charAt(i), t2.next());
}
assertEquals(" at 37 [character 1 line 4]", t2.toString());
for(int i=37; i<testString.length(); i++) {
assertTrue(t2.toString().startsWith(" at " + i + " "));
assertEquals(testString.charAt(i), t2.next());
}
assertEquals(" at "+ testString.length() +" [character 9 line 4]", t2.toString());
// end of the input
assertEquals(0, t2.next());
assertFalse(t2.more());
}
}

View file

@ -1,122 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import java.util.*;
import static org.junit.Assert.*;
import org.json.*;
import org.junit.Test;
/**
* Tests for JSON-Java Property
*/
public class PropertyTest {
/**
* JSONObject from null properties object should
* result in an empty JSONObject.
*/
@Test
public void shouldHandleNullProperties() {
Properties properties = null;
JSONObject jsonObject = Property.toJSONObject(properties);
assertTrue("jsonObject should be empty", jsonObject.isEmpty());
}
/**
* JSONObject from empty properties object should
* result in an empty JSONObject.
*/
@Test
public void shouldHandleEmptyProperties() {
Properties properties = new Properties();
JSONObject jsonObject = Property.toJSONObject(properties);
assertTrue("jsonObject should be empty", jsonObject.isEmpty());
}
/**
* JSONObject from simple properties object.
*/
@Test
public void shouldHandleProperties() {
Properties properties = new Properties();
properties.put("Illinois", "Springfield");
properties.put("Missouri", "Jefferson City");
properties.put("Washington", "Olympia");
properties.put("California", "Sacramento");
properties.put("Indiana", "Indianapolis");
JSONObject jsonObject = Property.toJSONObject(properties);
assertTrue("jsonObject should contain 5 items", jsonObject.length() == 5);
assertTrue("jsonObject should contain Illinois property",
"Springfield".equals(jsonObject.get("Illinois")));
assertTrue("jsonObject should contain Missouri property",
"Jefferson City".equals(jsonObject.get("Missouri")));
assertTrue("jsonObject should contain Washington property",
"Olympia".equals(jsonObject.get("Washington")));
assertTrue("jsonObject should contain California property",
"Sacramento".equals(jsonObject.get("California")));
assertTrue("jsonObject should contain Indiana property",
"Indianapolis".equals(jsonObject.get("Indiana")));
}
/**
* Null JSONObject toProperties() should result in an empty
* Properties object.
*/
@Test
public void shouldHandleNullJSONProperty() {
JSONObject jsonObject= null;
Properties properties = Property.toProperties(jsonObject);
assertTrue("properties should be empty",
properties.size() == 0);
}
/**
* Properties should convert to JSONObject, and back to
* Properties without changing.
*/
@Test
public void shouldHandleJSONProperty() {
Properties properties = new Properties();
properties.put("Illinois", "Springfield");
properties.put("Missouri", "Jefferson City");
properties.put("Washington", "Olympia");
properties.put("California", "Sacramento");
properties.put("Indiana", "Indianapolis");
JSONObject jsonObject = Property.toJSONObject(properties);
Properties jsonProperties = Property.toProperties(jsonObject);
assertTrue("property objects should match",
properties.equals(jsonProperties));
}
}

View file

@ -1,122 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import static org.junit.Assert.*;
import java.util.*;
import org.json.*;
/**
* These are helpful utility methods that perform basic comparisons
* between various objects. In most cases, the comparisons are not
* order-dependent, or else the order is known.
*/
public class Util {
/**
* Compares two JSONArrays for equality.
* The arrays need not be in the same order.
* @param jsonArray created by the code to be tested
* @param expectedJsonArray created specifically for comparing
*/
public static void compareActualVsExpectedJsonArrays(JSONArray jsonArray,
JSONArray expectedJsonArray) {
assertTrue("jsonArray lengths should be equal",
jsonArray.length() == expectedJsonArray.length());
for (int i = 0; i < jsonArray.length(); ++i) {
Object value = jsonArray.get(i);
Object expectedValue = expectedJsonArray.get(i);
compareActualVsExpectedObjects(value, expectedValue);
}
}
/**
* Compares two JSONObjects for equality. The objects need not be
* in the same order
* @param jsonObject created by the code to be tested
* @param expectedJsonObject created specifically for comparing
*/
public static void compareActualVsExpectedJsonObjects(
JSONObject jsonObject, JSONObject expectedJsonObject) {
assertTrue("jsonObjects should have the same length",
jsonObject.length() == expectedJsonObject.length());
Iterator<String> 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
* @param key key to the jsonObject entry to be compared
*/
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
assertTrue("object types should be equal for actual: "+
value.toString()+" ("+
value.getClass().toString()+") expected: "+
expectedValue.toString()+" ("+
expectedValue.getClass().toString()+")",
value.getClass().toString().equals(
expectedValue.getClass().toString()));
}
/**
* Same types or both Numbers, compare by toString()
*/
assertTrue("string values should be equal for actual: "+
value.toString()+" expected: "+expectedValue.toString(),
value.toString().equals(expectedValue.toString()));
}
}
}

View file

@ -1,958 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import static org.junit.Assert.assertEquals;
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.Reader;
import java.io.StringReader;
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;
/**
* 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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/x>\n"+
" <street>abc street</street>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <!>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <!\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <abc\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>Joe Tester</name>\n"+
" <![[]>\n"+
" </address>\n"+
"</addresses>";
try {
XMLParserConfiguration config =
new XMLParserConfiguration("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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <nocontent/>>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>Joe Tester</name>\n"+
" <street>[CDATA[Baker street 5]</street>\n"+
" <NothingHere/>\n"+
" <TrueValue>true</TrueValue>\n"+
" <FalseValue>false</FalseValue>\n"+
" <NullValue>null</NullValue>\n"+
" <PositiveValue>42</PositiveValue>\n"+
" <NegativeValue>-23</NegativeValue>\n"+
" <DoubleValue>-23.45</DoubleValue>\n"+
" <Nan>-23x.45</Nan>\n"+
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
" </address>\n"+
"</addresses>";
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("altContent");
compareStringToJSONObject(xmlStr, expectedStr, config);
compareReaderToJSONObject(xmlStr, expectedStr, config);
compareFileToJSONObject(xmlStr, expectedStr);
}
/**
* Valid XML with comments to JSONObject
*/
@Test
public void shouldHandleCommentsInXML() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<!-- this is a comment -->\n"+
"<addresses>\n"+
" <address>\n"+
" <![CDATA[ this is -- <another> comment ]]>\n"+
" <name>Joe Tester</name>\n"+
" <!-- this is a - multi line \n"+
" comment -->\n"+
" <street>Baker street 5</street>\n"+
" </address>\n"+
"</addresses>";
XMLParserConfiguration config =
new XMLParserConfiguration("altContent");
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+
"street 5\",\"name\":\"Joe Tester\",\"altContent\":\" this is -- "+
"<another> comment \"}}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
/**
* Valid XML to XML.toString()
*/
@Test
public void shouldHandleToString() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>[CDATA[Joe &amp; T &gt; e &lt; s &quot; t &apos; er]]</name>\n"+
" <street>Baker street 5</street>\n"+
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
" </address>\n"+
"</addresses>";
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("altContent");
String finalStr = XML.toString(expectedJsonObject, null, config);
String expectedFinalStr = "<addresses>&gt;</addresses>";
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("altContent");
String finalStr = XML.toString(expectedJsonObject, null, config);
String expectedFinalStr = "<addresses>"+
"1\n2\n3"+
"</addresses>";
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 = "<addresses>"+
"<something>1</something><something>2</something><something>3</something>"+
"</addresses>";
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 = "<jo></jo>";
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 = "<jo><arr>One</arr><arr></arr><arr>Four</arr></jo>";
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 = "<jo><arr>One</arr><arr>Two</arr><arr>Three</arr></jo>";
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 = "<jo><arr>One</arr><arr><array>Two</array><array>Three</array></arr><arr>Four</arr></jo>";
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 = "<addresses><address><name/><nocontent/>"+
"<outer><array>1</array></outer><outer><array>2</array>"+
"</outer><outer><array>3</array></outer>"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
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>someValue1</___IllegalNode><Illegal_node>someValue2</Illegal_node>
*/
String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>";
assertEquals("Length", expected.length(), result.length());
assertTrue("123IllegalNode", result.contains("<123IllegalNode>someValue1</123IllegalNode>"));
assertTrue("Illegal@node", result.contains("<Illegal@node>someValue2</Illegal@node>"));
}
/**
* 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 = "<nullValue/>";
/**
* This is the current behavior. JSONObject.NULL is emitted as
* the string, "null".
*/
String actualXML = "<nullValue>null</nullValue>";
String resultXML = XML.toString(inputJSON, null,
XMLParserConfiguration.KEEP_STRINGS);
assertEquals(actualXML, resultXML);
}
/**
* Investigate exactly how the "content" keyword works
*/
@Test
public void contentOperations() {
/*
* When a standalone <!CDATA[...]] structure is found while parsing XML into a
* JSONObject, the contents are placed in a string value with key="content".
*/
String xmlStr = "<tag1></tag1><![CDATA[if (a < b && a > 0) then return]]><tag2></tag2>";
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 = "<tag1></tag1><![CDATA[if (a < b && a > 0) then return]]><tag2></tag2><![CDATA[here is another cdata]]>";
jsonObject = XML.toJSONObject(xmlStr,
new XMLParserConfiguration(true, "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 = "<tag1>value 1</tag1>";
jsonObject = XML.toJSONObject(xmlStr,
new XMLParserConfiguration(true, "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 = "<tag1>value 1</tag1><tag1>2</tag1><tag1>true</tag1>";
jsonObject = XML.toJSONObject(xmlStr,
new XMLParserConfiguration(true, "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 = "<tag1>val1<tag2/>val2</tag1>";
jsonObject = XML.toJSONObject(xmlStr,
new XMLParserConfiguration(true, "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 = "<tag1>val1<tag2/></tag1>";
jsonObject = XML.toJSONObject(xmlStr,
new XMLParserConfiguration(true, "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 = "<tag1>val1<altContent/></tag1>";
jsonObject = XML.toJSONObject(xmlStr,
new XMLParserConfiguration(true, "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(true, "altContent"));
/*
* This is the created XML. Looks like content was mistaken for
* complex (child node + text) XML.
* <Profile>
* <list>
* <history>
* <entries>
* <deviceId>id</deviceId>
* {&quot;material&quot;:[{&quot;stuff&quot;:false}]}
* </entries>
* </history>
* </list>
* </Profile>
*/
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 = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":true}}");
final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
new XMLParserConfiguration(false));
Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
}
/**
* JSON string cannot be reverted to original xml.
*/
@Test
public void testToJSONArray_reversibility() {
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
XMLParserConfiguration config = new XMLParserConfiguration(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 = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"True\"}}");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration(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 = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>";
assertEquals("length",expectedReverseXml.length(), reverseXml.length());
assertTrue("array contents", reverseXml.contains("<id>01</id><id>1</id><id>00</id><id>0</id>"));
assertTrue("item contents", reverseXml.contains("<item><id>01</id></item>"));
assertTrue("title contents", reverseXml.contains("<title>True</title>"));
}
/**
* test to validate certain conditions of XML unescaping.
*/
@Test
public void testUnescape() {
assertEquals("{\"xml\":\"Can cope <;\"}",
XML.toJSONObject("<xml>Can cope &lt;; </xml>",
XMLParserConfiguration.KEEP_STRINGS).toString());
assertEquals("Can cope <; ", XML.unescape("Can cope &lt;; "));
assertEquals("{\"xml\":\"Can cope & ;\"}",
XML.toJSONObject("<xml>Can cope &amp; ; </xml>",
XMLParserConfiguration.KEEP_STRINGS).toString());
assertEquals("Can cope & ; ", XML.unescape("Can cope &amp; ; "));
assertEquals("{\"xml\":\"Can cope &;\"}",
XML.toJSONObject("<xml>Can cope &amp;; </xml>",
XMLParserConfiguration.KEEP_STRINGS).toString());
assertEquals("Can cope &; ", XML.unescape("Can cope &amp;; "));
// unicode entity
assertEquals("{\"xml\":\"Can cope 4;\"}",
XML.toJSONObject("<xml>Can cope &#x34;; </xml>",
XMLParserConfiguration.KEEP_STRINGS).toString());
assertEquals("Can cope 4; ", XML.unescape("Can cope &#x34;; "));
// double escaped
assertEquals("{\"xml\":\"Can cope &lt;\"}",
XML.toJSONObject("<xml>Can cope &amp;lt; </xml>",
XMLParserConfiguration.KEEP_STRINGS).toString());
assertEquals("Can cope &lt; ", XML.unescape("Can cope &amp;lt; "));
assertEquals("{\"xml\":\"Can cope &#x34;\"}",
XML.toJSONObject("<xml>Can cope &amp;#x34; </xml>",
XMLParserConfiguration.KEEP_STRINGS).toString());
assertEquals("Can cope &#x34; ", XML.unescape("Can cope &amp;#x34; "));
}
/**
* 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.
* <parent>
* <child>value</child>
* content data
* </tag>
*/
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" content 1\n"+
" <name>Sherlock Holmes</name>\n"+
" content 2\n"+
" <street>Baker street 5</street>\n"+
" content 3\n"+
" <num>1</num>\n"+
" </address>\n"+
"</addresses>";
// keep strings, use the altContent tag
XMLParserConfiguration config =
new XMLParserConfiguration(true, "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("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);
}
/**
* 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);
try(Reader reader = new StringReader(xmlStr);) {
JSONObject jsonObject = XML.toJSONObject(reader, config);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
} catch (IOException e) {
assertTrue("IO Reader error: " +e.getMessage(), false);
}
}
/**
* 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");
try(FileWriter fileWriter = new FileWriter(tempFile);){
fileWriter.write(xmlStr);
}
try(Reader reader = new FileReader(tempFile);){
JSONObject jsonObject = XML.toJSONObject(reader);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
} catch (IOException e) {
assertTrue("file writer error: " +e.getMessage(), false);
}
}
}

View file

@ -1,901 +0,0 @@
package org.json.junit;
/*
Copyright (c) 2020 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
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.Reader;
import java.io.StringReader;
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;
/**
* 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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/x>\n"+
" <street>abc street</street>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <!>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <!\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <abc\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>Joe Tester</name>\n"+
" <![[]>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <nocontent/>>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>Joe Tester</name>\n"+
" <street>[CDATA[Baker street 5]</street>\n"+
" <NothingHere/>\n"+
" <TrueValue>true</TrueValue>\n"+
" <FalseValue>false</FalseValue>\n"+
" <NullValue>null</NullValue>\n"+
" <PositiveValue>42</PositiveValue>\n"+
" <NegativeValue>-23</NegativeValue>\n"+
" <DoubleValue>-23.45</DoubleValue>\n"+
" <Nan>-23x.45</Nan>\n"+
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
" </address>\n"+
"</addresses>";
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 =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<root>"+
"<rawQuote>\"</rawQuote>"+
"<euro>A &#8364;33</euro>"+
"<euroX>A &#x20ac;22&#x20AC;</euroX>"+
"<unknown>some text &copy;</unknown>"+
"<known>&#x0022; &quot; &amp; &apos; &lt; &gt;</known>"+
"<high>&#x1D122; &#x10165;</high>" +
"</root>";
String expectedStr =
"{\"root\":{" +
"\"rawQuote\":\"\\\"\"," +
"\"euro\":\"A €33\"," +
"\"euroX\":\"A €22€\"," +
"\"unknown\":\"some text &copy;\"," +
"\"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("&#x85;"));
// 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("&quot;"));
assertTrue("Escaping ' failed. Not found in XML output.", xml.contains("&apos;"));
assertTrue("Escaping & failed. Not found in XML output.", xml.contains("&amp;"));
assertTrue("Escaping < failed. Not found in XML output.", xml.contains("&lt;"));
assertTrue("Escaping > failed. Not found in XML output.", xml.contains("&gt;"));
}
/**
* Valid XML with comments to JSONObject
*/
@Test
public void shouldHandleCommentsInXML() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<!-- this is a comment -->\n"+
"<addresses>\n"+
" <address>\n"+
" <![CDATA[ this is -- <another> comment ]]>\n"+
" <name>Joe Tester</name>\n"+
" <!-- this is a - multi line \n"+
" comment -->\n"+
" <street>Baker street 5</street>\n"+
" </address>\n"+
"</addresses>";
JSONObject jsonObject = XML.toJSONObject(xmlStr);
String expectedStr = "{\"addresses\":{\"address\":{\"street\":\"Baker "+
"street 5\",\"name\":\"Joe Tester\",\"content\":\" this is -- "+
"<another> comment \"}}}";
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
/**
* Valid XML to XML.toString()
*/
@Test
public void shouldHandleToString() {
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name>[CDATA[Joe &amp; T &gt; e &lt; s &quot; t &apos; er]]</name>\n"+
" <street>Baker street 5</street>\n"+
" <ArrayOfNum>1, 2, 3, 4.1, 5.2</ArrayOfNum>\n"+
" </address>\n"+
"</addresses>";
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 = "<addresses>&gt;</addresses>";
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 = "<addresses>"+
"1\n2\n3</addresses>";
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 = "<addresses>"+
"<something>1</something><something>2</something><something>3</something>"+
"</addresses>";
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 = "<jo></jo>";
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 = "<jo><arr>One</arr><arr></arr><arr>Four</arr></jo>";
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 = "<jo><arr>One</arr><arr>Two</arr><arr>Three</arr></jo>";
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 = "<jo><arr>One</arr><arr><array>Two</array><array>Three</array></arr><arr>Four</arr></jo>";
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 = "<addresses><address><name/><nocontent/>"+
"<outer><array>1</array></outer><outer><array>2</array>"+
"</outer><outer><array>3</array></outer>"+
"</address><xsi:noNamespaceSchemaLocation>test.xsd</xsi:noName"+
"spaceSchemaLocation><xmlns:xsi>http://www.w3.org/2001/XMLSche"+
"ma-instance</xmlns:xsi></addresses>";
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>someValue1</___IllegalNode><Illegal_node>someValue2</Illegal_node>
*/
String expected = "<123IllegalNode>someValue1</123IllegalNode><Illegal@node>someValue2</Illegal@node>";
assertEquals("length",expected.length(), result.length());
assertTrue("123IllegalNode",result.contains("<123IllegalNode>someValue1</123IllegalNode>"));
assertTrue("Illegal@node",result.contains("<Illegal@node>someValue2</Illegal@node>"));
}
/**
* 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 = "<nullValue/>";
/**
* This is the current behavior. JSONObject.NULL is emitted as
* the string, "null".
*/
String actualXML = "<nullValue>null</nullValue>";
String resultXML = XML.toString(inputJSON);
assertEquals(actualXML, resultXML);
}
/**
* Investigate exactly how the "content" keyword works
*/
@Test
public void contentOperations() {
/*
* When a standalone <!CDATA[...]] structure is found while parsing XML into a
* JSONObject, the contents are placed in a string value with key="content".
*/
String xmlStr = "<tag1></tag1><![CDATA[if (a < b && a > 0) then return]]><tag2></tag2>";
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 = "<tag1></tag1><![CDATA[if (a < b && a > 0) then return]]><tag2></tag2><![CDATA[here is another cdata]]>";
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 = "<tag1>value 1</tag1>";
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 = "<tag1>value 1</tag1><tag1>2</tag1><tag1>true</tag1>";
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 = "<tag1>val1<tag2/>val2</tag1>";
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 = "<tag1>val1<tag2/></tag1>";
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 = "<tag1>val1<content/></tag1>";
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.
* <Profile>
* <list>
* <history>
* <entries>
* <deviceId>id</deviceId>
* {&quot;material&quot;:[{&quot;stuff&quot;:false}]}
* </entries>
* </history>
* </list>
* </Profile>
*/
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");
try(FileWriter fileWriter = new FileWriter(tempFile);){
fileWriter.write(xmlStr);
}
try(Reader reader = new FileReader(tempFile);){
JSONObject jsonObject = XML.toJSONObject(reader);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
} catch (IOException e) {
fail("file writer error: " +e.getMessage());
}
}
/**
* JSON string lost leading zero and converted "True" to true.
*/
@Test
public void testToJSONArray_jsonOutput() {
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
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 = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
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 = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
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 = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>";
assertEquals("length",expectedReverseXml.length(), reverseXml.length());
assertTrue("array contents", reverseXml.contains("<id>01</id><id>1</id><id>00</id><id>0</id>"));
assertTrue("item contents", reverseXml.contains("<item><id>01</id></item>"));
assertTrue("title contents", reverseXml.contains("<title>True</title>"));
}
/**
* test to validate certain conditions of XML unescaping.
*/
@Test
public void testUnescape() {
assertEquals("{\"xml\":\"Can cope <;\"}",
XML.toJSONObject("<xml>Can cope &lt;; </xml>").toString());
assertEquals("Can cope <; ", XML.unescape("Can cope &lt;; "));
assertEquals("{\"xml\":\"Can cope & ;\"}",
XML.toJSONObject("<xml>Can cope &amp; ; </xml>").toString());
assertEquals("Can cope & ; ", XML.unescape("Can cope &amp; ; "));
assertEquals("{\"xml\":\"Can cope &;\"}",
XML.toJSONObject("<xml>Can cope &amp;; </xml>").toString());
assertEquals("Can cope &; ", XML.unescape("Can cope &amp;; "));
// unicode entity
assertEquals("{\"xml\":\"Can cope 4;\"}",
XML.toJSONObject("<xml>Can cope &#x34;; </xml>").toString());
assertEquals("Can cope 4; ", XML.unescape("Can cope &#x34;; "));
// double escaped
assertEquals("{\"xml\":\"Can cope &lt;\"}",
XML.toJSONObject("<xml>Can cope &amp;lt; </xml>").toString());
assertEquals("Can cope &lt; ", XML.unescape("Can cope &amp;lt; "));
assertEquals("{\"xml\":\"Can cope &#x34;\"}",
XML.toJSONObject("<xml>Can cope &amp;#x34; </xml>").toString());
assertEquals("Can cope &#x34; ", XML.unescape("Can cope &amp;#x34; "));
}
/**
* test passes when xsi:nil="true" converting to null (JSON specification-like nil conversion enabled)
*/
@Test
public void testToJsonWithNullWhenNilConversionEnabled() {
final String originalXml = "<root><id xsi:nil=\"true\"/></root>";
final String expectedJsonString = "{\"root\":{\"id\":null}}";
final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, "content", 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 = "<root><id xsi:nil=\"true\"/></root>";
final String expectedJsonString = "{\"root\":{\"id\":{\"xsi:nil\":true}}}";
final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration());
assertEquals(expectedJsonString, json.toString());
}
}

View file

@ -1,13 +0,0 @@
package org.json.junit.data;
/**
* test class for verifying write errors.
* @author John Aylward
*
*/
public class BrokenToString {
@Override
public String toString() {
throw new IllegalStateException("Something went horribly wrong!");
}
}

View file

@ -1,66 +0,0 @@
/**
*
*/
package org.json.junit.data;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
/**
* Object for testing the exception handling in {@link JSONObject#populateMap}.
*
* @author John Aylward
*/
public class ExceptionalBean {
/**
* @return a closeable.
*/
public Closeable getCloseable() {
// anonymous inner class did not work...
return new MyCloseable();
}
/**
* @return Nothing really. Just can't be void.
* @throws IllegalAccessException
* always thrown
*/
public int getIllegalAccessException() throws IllegalAccessException {
throw new IllegalAccessException("Yup, it's illegal");
}
/**
* @return Nothing really. Just can't be void.
* @throws IllegalArgumentException
* always thrown
*/
public int getIllegalArgumentException() throws IllegalArgumentException {
throw new IllegalArgumentException("Yup, it's illegal");
}
/**
* @return Nothing really. Just can't be void.
* @throws InvocationTargetException
* always thrown
*/
public int getInvocationTargetException() throws InvocationTargetException {
throw new InvocationTargetException(new Exception("Yup, it's illegal"));
}
/** My closeable class. */
public static final class MyCloseable implements Closeable {
/**
* @return a string
*/
public String getString() {
return "Yup, it's closeable";
}
@Override
public void close() throws IOException {
throw new IOException("Closing is too hard!");
}
}
}

View file

@ -1,180 +0,0 @@
package org.json.junit.data;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
/**
* basic fraction class, no frills.
* @author John Aylward
*
*/
public class Fraction extends Number implements Comparable<Fraction> {
/**
* serial id.
*/
private static final long serialVersionUID = 1L;
/**
* value as a big decimal.
*/
private final BigDecimal bigDecimal;
/**
* value of the denominator.
*/
private final BigInteger denominator;
/**
* value of the numerator.
*/
private final BigInteger numerator;
/**
* @param numerator
* numerator
* @param denominator
* denominator
*/
public Fraction(final BigInteger numerator, final BigInteger denominator) {
super();
if (numerator == null || denominator == null) {
throw new IllegalArgumentException("All values must be non-null");
}
if (denominator.compareTo(BigInteger.ZERO)==0) {
throw new IllegalArgumentException("Divide by zero");
}
final BigInteger n;
final BigInteger d;
// normalize fraction
if (denominator.signum()<0) {
n = numerator.negate();
d = denominator.negate();
} else {
n = numerator;
d = denominator;
}
this.numerator = n;
this.denominator = d;
if (n.compareTo(BigInteger.ZERO)==0) {
this.bigDecimal = BigDecimal.ZERO;
} else if (n.compareTo(d)==0) {// i.e. 4/4, 10/10
this.bigDecimal = BigDecimal.ONE;
} else {
this.bigDecimal = new BigDecimal(this.numerator).divide(new BigDecimal(this.denominator),
RoundingMode.HALF_EVEN);
}
}
/**
* @param numerator
* numerator
* @param denominator
* denominator
*/
public Fraction(final long numerator, final long denominator) {
this(BigInteger.valueOf(numerator),BigInteger.valueOf(denominator));
}
/**
* @return the decimal
*/
public BigDecimal bigDecimalValue() {
return this.bigDecimal;
}
@Override
public int compareTo(final Fraction o) {
// .equals call this, so no .equals compare allowed
// if they are the same reference, just return equals
if (this == o) {
return 0;
}
// if my denominators are already equal, just compare the numerators
if (this.denominator.compareTo(o.denominator)==0) {
return this.numerator.compareTo(o.numerator);
}
// get numerators of common denominators
// a x ay xb
// --- --- = ---- ----
// b y by yb
final BigInteger thisN = this.numerator.multiply(o.denominator);
final BigInteger otherN = o.numerator.multiply(this.denominator);
return thisN.compareTo(otherN);
}
@Override
public double doubleValue() {
return this.bigDecimal.doubleValue();
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final Fraction other = (Fraction) obj;
return this.compareTo(other) == 0;
}
@Override
public float floatValue() {
return this.bigDecimal.floatValue();
}
/**
* @return the denominator
*/
public BigInteger getDenominator() {
return this.denominator;
}
/**
* @return the numerator
*/
public BigInteger getNumerator() {
return this.numerator;
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.bigDecimal == null ? 0 : this.bigDecimal.hashCode());
return result;
}
@Override
public int intValue() {
return this.bigDecimal.intValue();
}
@Override
public long longValue() {
return this.bigDecimal.longValue();
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.numerator + "/" + this.denominator;
}
}

View file

@ -1,79 +0,0 @@
package org.json.junit.data;
import java.io.StringReader;
/**
*
* @author John Aylward
*
* @param <T>
* generic number value
*/
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
/**
* @param genericValue
* value to initiate with
*/
public GenericBean(T genericValue) {
super();
this.genericValue = genericValue;
}
/** */
protected T genericValue;
/** to be used by the calling test to see how often the getter is called */
public int genericGetCounter;
/** to be used by the calling test to see how often the setter is called */
public int genericSetCounter;
/** @return the genericValue */
public T getGenericValue() {
this.genericGetCounter++;
return this.genericValue;
}
/**
* @param genericValue
* generic value to set
*/
public void setGenericValue(T genericValue) {
this.genericSetCounter++;
this.genericValue = genericValue;
}
@Override
public Integer getIntKey() {
return Integer.valueOf(42);
}
@Override
public Double getDoubleKey() {
return Double.valueOf(4.2);
}
@Override
public String getStringKey() {
return "MyString Key";
}
@Override
public String getEscapeStringKey() {
return "\"My String with \"s";
}
@Override
public Boolean isTrueKey() {
return Boolean.TRUE;
}
@Override
public Boolean isFalseKey() {
return Boolean.FALSE;
}
@Override
public StringReader getStringReaderKey() {
return new StringReader("Some String Value in a reader");
}
}

View file

@ -1,69 +0,0 @@
/**
*
*/
package org.json.junit.data;
/**
* @author john
*
*/
public class GenericBeanInt extends GenericBean<Integer> {
/** */
final char a = 'A';
/** @return the a */
public char getA() {
return this.a;
}
/**
* Should not be beanable
*
* @return false
*/
public boolean getable() {
return false;
}
/**
* Should not be beanable
*
* @return false
*/
public boolean get() {
return false;
}
/**
* Should not be beanable
*
* @return false
*/
public boolean is() {
return false;
}
/**
* Should be beanable
*
* @return false
*/
public boolean isB() {
return this.genericValue.equals((Integer.valueOf(this.a+1)));
}
/**
* @param genericValue
* the value to initiate with.
*/
public GenericBeanInt(Integer genericValue) {
super(genericValue);
}
/** override to generate a bridge method */
@Override
public Integer getGenericValue() {
return super.getGenericValue();
}
}

View file

@ -1,16 +0,0 @@
package org.json.junit.data;
import java.io.*;
/**
* Used in testing when Bean behavior is needed
*/
public interface MyBean {
public Integer getIntKey();
public Double getDoubleKey();
public String getStringKey();
public String getEscapeStringKey();
public Boolean isTrueKey();
public Boolean isFalseKey();
public StringReader getStringReaderKey();
}

View file

@ -1,20 +0,0 @@
package org.json.junit.data;
import org.json.JSONPropertyName;
/**
* Test bean for the {@link JSONPropertyName} annotation.
*/
public class MyBeanCustomName implements MyBeanCustomNameInterface {
public int getSomeInt() { return 42; }
@JSONPropertyName("")
public long getSomeLong() { return 42L; }
@JSONPropertyName("myStringField")
public String getSomeString() { return "someStringValue"; }
@JSONPropertyName("Some Weird NAme that Normally Wouldn't be possible!")
public double getMyDouble() { return 0.0d; }
@Override
public float getSomeFloat() { return 2.0f; }
@Override
public int getIgnoredInt() { return 40; }
}

View file

@ -1,11 +0,0 @@
package org.json.junit.data;
import org.json.JSONPropertyIgnore;
import org.json.JSONPropertyName;
public interface MyBeanCustomNameInterface {
@JSONPropertyName("InterfaceField")
float getSomeFloat();
@JSONPropertyIgnore
int getIgnoredInt();
}

View file

@ -1,32 +0,0 @@
/**
*
*/
package org.json.junit.data;
import org.json.JSONPropertyIgnore;
import org.json.JSONPropertyName;
/**
* Test bean to verify that the {@link org.json.JSONPropertyName} annotation
* is inherited.
*/
public class MyBeanCustomNameSubClass extends MyBeanCustomName {
@Override
@JSONPropertyName("forcedInt")
public int getIgnoredInt() { return 42*42; }
@Override
@JSONPropertyName("newIntFieldName")
public int getSomeInt() { return 43; }
@Override
public String getSomeString() { return "subClassString"; }
@Override
@JSONPropertyName("AMoreNormalName")
public double getMyDouble() { return 1.0d; }
@Override
public float getSomeFloat() { return 3.0f; }
@JSONPropertyIgnore
@JSONPropertyName("ShouldBeIgnored")
public boolean getShouldNotBeJSON() { return true; }
@JSONPropertyName("Getable")
public boolean getable() { return true; }
}

View file

@ -1,11 +0,0 @@
package org.json.junit.data;
import java.math.*;
/**
* Used in testing when a Bean containing big numbers is needed
*/
public interface MyBigNumberBean {
public BigInteger getBigInteger();
public BigDecimal getBigDecimal();
}

View file

@ -1,10 +0,0 @@
package org.json.junit.data;
/**
* An enum with no methods or data
*/
public enum MyEnum {
VAL1,
VAL2,
VAL3;
}

View file

@ -1,22 +0,0 @@
package org.json.junit.data;
/**
* this is simply a class that contains some enum instances
*/
public class MyEnumClass {
private MyEnum myEnum;
private MyEnumField myEnumField;
public MyEnum getMyEnum() {
return this.myEnum;
}
public void setMyEnum(MyEnum myEnum) {
this.myEnum = myEnum;
}
public MyEnumField getMyEnumField() {
return this.myEnumField;
}
public void setMyEnumField(MyEnumField myEnumField) {
this.myEnumField = myEnumField;
}
}

View file

@ -1,28 +0,0 @@
package org.json.junit.data;
/**
* An enum that contains getters and some internal fields
*/
@SuppressWarnings("boxing")
public enum MyEnumField {
VAL1(1, "val 1"),
VAL2(2, "val 2"),
VAL3(3, "val 3");
private String value;
private Integer intVal;
private MyEnumField(Integer intVal, String value) {
this.value = value;
this.intVal = intVal;
}
public String getValue() {
return this.value;
}
public Integer getIntVal() {
return this.intVal;
}
@Override
public String toString(){
return this.value;
}
}

View file

@ -1,14 +0,0 @@
package org.json.junit.data;
import org.json.*;
/**
* Used in testing when a JSONString is needed
*/
public class MyJsonString implements JSONString {
@Override
public String toJSONString() {
return "my string";
}
}

View file

@ -1,12 +0,0 @@
package org.json.junit.data;
public class MyLocaleBean {
private final String id = "beanId";
private final String i = "beanI";
public String getId() {
return this.id;
}
public String getI() {
return this.i;
}
}

View file

@ -1,97 +0,0 @@
package org.json.junit.data;
import java.math.BigDecimal;
/**
* Number override for testing. Number overrides should always override
* toString, hashCode, and Equals.
*
* @see <a
* href="https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html">The
* Numbers Classes</a>
* @see <a
* href="https://docs.oracle.com/javase/tutorial/java/data/numberformat.html">Formatting
* Numeric Print Output</a>
*
* @author John Aylward
*/
public class MyNumber extends Number {
private Number number = BigDecimal.valueOf(42);
/**
*/
private static final long serialVersionUID = 1L;
/**
* @return number!
*/
public Number getNumber() {
return this.number;
}
@Override
public int intValue() {
return getNumber().intValue();
}
@Override
public long longValue() {
return getNumber().longValue();
}
@Override
public float floatValue() {
return getNumber().floatValue();
}
@Override
public double doubleValue() {
return getNumber().doubleValue();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*
* Number overrides should in general always override the toString method.
*/
@Override
public String toString() {
return getNumber().toString();
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.number == null) ? 0 : this.number.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof MyNumber)) {
return false;
}
MyNumber other = (MyNumber) obj;
if (this.number == null) {
if (other.number != null) {
return false;
}
} else if (!this.number.equals(other.number)) {
return false;
}
return true;
}
}

View file

@ -1,13 +0,0 @@
package org.json.junit.data;
/**
* Class that holds our MyNumber override as a property.
* @author John Aylward
*/
public class MyNumberContainer {
private MyNumber myNumber = new MyNumber();
/**
* @return a MyNumber.
*/
public Number getMyNumber() {return this.myNumber;}
}

View file

@ -1,10 +0,0 @@
package org.json.junit.data;
/**
* Need a class with some public data members for testing
*/
@SuppressWarnings("boxing")
public class MyPublicClass {
public Integer publicInt = 42;
public String publicString = "abc";
}

View file

@ -1,91 +0,0 @@
package org.json.junit.data;
/**
* Sample singleton for use with bean testing.
*
* @author John Aylward
*
*/
public final class Singleton {
/** */
private int someInt;
/** */
private String someString;
/** single instance. */
private static final Singleton INSTANCE = new Singleton();
/** @return the singleton instance. */
public static final Singleton getInstance() {
return INSTANCE;
}
/** */
private Singleton() {
if (INSTANCE != null) {
throw new IllegalStateException("Already instantiated");
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
return INSTANCE;
}
/** @return someInt */
public int getSomeInt() {
return this.someInt;
}
/**
* sets someInt.
*
* @param someInt
* the someInt to set
*/
public void setSomeInt(int someInt) {
this.someInt = someInt;
}
/** @return someString */
public String getSomeString() {
return this.someString;
}
/**
* sets someString.
*
* @param someString
* the someString to set
*/
public void setSomeString(String someString) {
this.someString = someString;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.someInt;
result = prime * result + ((this.someString == null) ? 0 : this.someString.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Singleton other = (Singleton) obj;
if (this.someInt != other.someInt)
return false;
if (this.someString == null) {
if (other.someString != null)
return false;
} else if (!this.someString.equals(other.someString))
return false;
return true;
}
}

View file

@ -1,62 +0,0 @@
package org.json.junit.data;
/**
* Sample singleton done as an Enum for use with bean testing.
*
* @author John Aylward
*
*/
public enum SingletonEnum {
/**
* the singleton instance.
*/
INSTANCE;
/** */
private int someInt;
/** */
private String someString;
/** single instance. */
/**
* @return the singleton instance. In a real application, I'd hope no one did
* this to an enum singleton.
*/
public static final SingletonEnum getInstance() {
return INSTANCE;
}
/** */
private SingletonEnum() {
}
/** @return someInt */
public int getSomeInt() {
return this.someInt;
}
/**
* sets someInt.
*
* @param someInt
* the someInt to set
*/
public void setSomeInt(int someInt) {
this.someInt = someInt;
}
/** @return someString */
public String getSomeString() {
return this.someString;
}
/**
* sets someString.
*
* @param someString
* the someString to set
*/
public void setSomeString(String someString) {
this.someString = someString;
}
}

View file

@ -1,19 +0,0 @@
package org.json.junit.data;
import java.util.*;
/**
* A resource bundle class
*/
public class StringsResourceBundle extends ListResourceBundle {
@Override
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"greetings.hello", "Hello, "},
{"greetings.world", "World!"},
{"farewells.later", "Later, "},
{"farewells.gator", "Alligator!"}
};
}

View file

@ -1,68 +0,0 @@
/**
*
*/
package org.json.junit.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author John Aylward
*/
public class WeirdList {
/** */
private final List<Integer> list = new ArrayList<>();
/**
* @param vals
*/
public WeirdList(Integer... vals) {
this.list.addAll(Arrays.asList(vals));
}
/**
* @return a copy of the list
*/
public List<Integer> get() {
return new ArrayList<>(this.list);
}
/**
* @return a copy of the list
*/
public List<Integer> getALL() {
return new ArrayList<>(this.list);
}
/**
* get a value at an index.
*
* @param i
* index to get
* @return the value at the index
*/
public Integer get(int i) {
return this.list.get(i);
}
/**
* get a value at an index.
*
* @param i
* index to get
* @return the value at the index
*/
@SuppressWarnings("boxing")
public int getInt(int i) {
return this.list.get(i);
}
/**
* @param value
* new value to add to the end of the list
*/
public void add(Integer value) {
this.list.add(value);
}
}

View file

@ -1,28 +0,0 @@
{
"foo":
[
"bar",
"baz"
],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8,
"obj" : {
"key" : "value",
"other~key" : {
"another/key" : [
"val"
]
},
"" : {
"" : "empty key of an object with an empty key",
"subKey" : "Some other value"
}
}
}