mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 11:25:30 -04:00
Merge pull request #921 from stleary/restore-jsonparserconfiguration
Restore strict mode text parsing
This commit is contained in:
commit
ac40a6faa3
@ -67,6 +67,12 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
*/
|
*/
|
||||||
private final ArrayList<Object> myArrayList;
|
private final ArrayList<Object> myArrayList;
|
||||||
|
|
||||||
|
// strict mode checks after constructor require access to this object
|
||||||
|
private JSONTokener jsonTokener;
|
||||||
|
|
||||||
|
// strict mode checks after constructor require access to this object
|
||||||
|
private JSONParserConfiguration jsonParserConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct an empty JSONArray.
|
* Construct an empty JSONArray.
|
||||||
*/
|
*/
|
||||||
@ -83,7 +89,27 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* If there is a syntax error.
|
* If there is a syntax error.
|
||||||
*/
|
*/
|
||||||
public JSONArray(JSONTokener x) throws JSONException {
|
public JSONArray(JSONTokener x) throws JSONException {
|
||||||
|
this(x, new JSONParserConfiguration());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
|
||||||
|
*
|
||||||
|
* @param x A JSONTokener instance from which the JSONArray is constructed.
|
||||||
|
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
|
||||||
|
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
|
||||||
|
*/
|
||||||
|
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||||
this();
|
this();
|
||||||
|
|
||||||
|
if (this.jsonParserConfiguration == null) {
|
||||||
|
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||||
|
}
|
||||||
|
if (this.jsonTokener == null) {
|
||||||
|
this.jsonTokener = x;
|
||||||
|
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
if (x.nextClean() != '[') {
|
if (x.nextClean() != '[') {
|
||||||
throw x.syntaxError("A JSONArray text must start with '['");
|
throw x.syntaxError("A JSONArray text must start with '['");
|
||||||
}
|
}
|
||||||
@ -114,6 +140,17 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
throw x.syntaxError("Expected a ',' or ']'");
|
throw x.syntaxError("Expected a ',' or ']'");
|
||||||
}
|
}
|
||||||
if (nextChar == ']') {
|
if (nextChar == ']') {
|
||||||
|
// trailing commas are not allowed in strict mode
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
throw x.syntaxError("Strict mode error: Expected another array element");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (nextChar == ',') {
|
||||||
|
// consecutive commas are not allowed in strict mode
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
throw x.syntaxError("Strict mode error: Expected a valid array element");
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
x.back();
|
x.back();
|
||||||
@ -138,7 +175,32 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
* If there is a syntax error.
|
* If there is a syntax error.
|
||||||
*/
|
*/
|
||||||
public JSONArray(String source) throws JSONException {
|
public JSONArray(String source) throws JSONException {
|
||||||
this(new JSONTokener(source));
|
this(source, new JSONParserConfiguration());
|
||||||
|
// Strict mode does not allow trailing chars
|
||||||
|
if (this.jsonParserConfiguration.isStrictMode() &&
|
||||||
|
this.jsonTokener.nextClean() != 0) {
|
||||||
|
throw jsonTokener.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a JSONArray from a source JSON text.
|
||||||
|
*
|
||||||
|
* @param source
|
||||||
|
* A string that begins with <code>[</code> <small>(left
|
||||||
|
* bracket)</small> and ends with <code>]</code>
|
||||||
|
* <small>(right bracket)</small>.
|
||||||
|
* @param jsonParserConfiguration the parser config object
|
||||||
|
* @throws JSONException
|
||||||
|
* If there is a syntax error.
|
||||||
|
*/
|
||||||
|
public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||||
|
this(new JSONTokener(source), jsonParserConfiguration);
|
||||||
|
// Strict mode does not allow trailing chars
|
||||||
|
if (this.jsonParserConfiguration.isStrictMode() &&
|
||||||
|
this.jsonTokener.nextClean() != 0) {
|
||||||
|
throw jsonTokener.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -152,6 +152,12 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
public static final Object NULL = new Null();
|
public static final Object NULL = new Null();
|
||||||
|
|
||||||
|
// strict mode checks after constructor require access to this object
|
||||||
|
private JSONTokener jsonTokener;
|
||||||
|
|
||||||
|
// strict mode checks after constructor require access to this object
|
||||||
|
private JSONParserConfiguration jsonParserConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct an empty JSONObject.
|
* Construct an empty JSONObject.
|
||||||
*/
|
*/
|
||||||
@ -211,6 +217,15 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||||
this();
|
this();
|
||||||
|
|
||||||
|
if (this.jsonParserConfiguration == null) {
|
||||||
|
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||||
|
}
|
||||||
|
if (this.jsonTokener == null) {
|
||||||
|
this.jsonTokener = x;
|
||||||
|
this.jsonTokener.setJsonParserConfiguration(this.jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
String key;
|
String key;
|
||||||
|
|
||||||
@ -255,8 +270,16 @@ public class JSONObject {
|
|||||||
|
|
||||||
switch (x.nextClean()) {
|
switch (x.nextClean()) {
|
||||||
case ';':
|
case ';':
|
||||||
|
// In strict mode semicolon is not a valid separator
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
throw x.syntaxError("Strict mode error: Invalid character ';' found");
|
||||||
|
}
|
||||||
case ',':
|
case ',':
|
||||||
if (x.nextClean() == '}') {
|
if (x.nextClean() == '}') {
|
||||||
|
// trailing commas are not allowed in strict mode
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
throw x.syntaxError("Strict mode error: Expected another object element");
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (x.end()) {
|
if (x.end()) {
|
||||||
@ -433,6 +456,11 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
public JSONObject(String source) throws JSONException {
|
public JSONObject(String source) throws JSONException {
|
||||||
this(source, new JSONParserConfiguration());
|
this(source, new JSONParserConfiguration());
|
||||||
|
// Strict mode does not allow trailing chars
|
||||||
|
if (this.jsonParserConfiguration.isStrictMode() &&
|
||||||
|
this.jsonTokener.nextClean() != 0) {
|
||||||
|
throw new JSONException("Strict mode error: Unparsed characters found at end of input text");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -451,6 +479,11 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||||
this(new JSONTokener(source), jsonParserConfiguration);
|
this(new JSONTokener(source), jsonParserConfiguration);
|
||||||
|
// Strict mode does not allow trailing chars
|
||||||
|
if (this.jsonParserConfiguration.isStrictMode() &&
|
||||||
|
this.jsonTokener.nextClean() != 0) {
|
||||||
|
throw new JSONException("Strict mode error: Unparsed characters found at end of input text");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,6 +17,12 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
this.overwriteDuplicateKey = false;
|
this.overwriteDuplicateKey = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text.
|
||||||
|
* Garbage chars at the end of the doc, unquoted string, and single-quoted strings are all disallowed.
|
||||||
|
*/
|
||||||
|
private boolean strictMode;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected JSONParserConfiguration clone() {
|
protected JSONParserConfiguration clone() {
|
||||||
JSONParserConfiguration clone = new JSONParserConfiguration();
|
JSONParserConfiguration clone = new JSONParserConfiguration();
|
||||||
@ -58,6 +64,35 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the strict mode configuration for the JSON parser with default true value
|
||||||
|
* <p>
|
||||||
|
* When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
|
||||||
|
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
|
||||||
|
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
|
||||||
|
* @return a new JSONParserConfiguration instance with the updated strict mode setting
|
||||||
|
*/
|
||||||
|
public JSONParserConfiguration withStrictMode() {
|
||||||
|
return withStrictMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the strict mode configuration for the JSON parser.
|
||||||
|
* <p>
|
||||||
|
* When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
|
||||||
|
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
|
||||||
|
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
|
||||||
|
*
|
||||||
|
* @param mode a boolean value indicating whether strict mode should be enabled or not
|
||||||
|
* @return a new JSONParserConfiguration instance with the updated strict mode setting
|
||||||
|
*/
|
||||||
|
public JSONParserConfiguration withStrictMode(final boolean mode) {
|
||||||
|
JSONParserConfiguration clone = this.clone();
|
||||||
|
clone.strictMode = mode;
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parser's behavior when meeting duplicate keys, controls whether the parser should
|
* The parser's behavior when meeting duplicate keys, controls whether the parser should
|
||||||
* overwrite duplicate keys or not.
|
* overwrite duplicate keys or not.
|
||||||
@ -67,4 +102,11 @@ public class JSONParserConfiguration extends ParserConfiguration {
|
|||||||
public boolean isOverwriteDuplicateKey() {
|
public boolean isOverwriteDuplicateKey() {
|
||||||
return this.overwriteDuplicateKey;
|
return this.overwriteDuplicateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the current strict mode setting.
|
||||||
|
*/
|
||||||
|
public boolean isStrictMode() {
|
||||||
|
return this.strictMode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,8 @@ public class JSONTokener {
|
|||||||
/** the number of characters read in the previous line. */
|
/** the number of characters read in the previous line. */
|
||||||
private long characterPreviousLine;
|
private long characterPreviousLine;
|
||||||
|
|
||||||
|
// access to this object is required for strict mode checking
|
||||||
|
private JSONParserConfiguration jsonParserConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a JSONTokener from a Reader. The caller must close the Reader.
|
* Construct a JSONTokener from a Reader. The caller must close the Reader.
|
||||||
@ -70,6 +72,21 @@ public class JSONTokener {
|
|||||||
this(new StringReader(s));
|
this(new StringReader(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter
|
||||||
|
* @return jsonParserConfiguration
|
||||||
|
*/
|
||||||
|
public JSONParserConfiguration getJsonParserConfiguration() {
|
||||||
|
return jsonParserConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setter
|
||||||
|
* @param jsonParserConfiguration new value for jsonParserConfiguration
|
||||||
|
*/
|
||||||
|
public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfiguration) {
|
||||||
|
this.jsonParserConfiguration = jsonParserConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Back up one character. This provides a sort of lookahead capability,
|
* Back up one character. This provides a sort of lookahead capability,
|
||||||
@ -409,14 +426,14 @@ public class JSONTokener {
|
|||||||
case '{':
|
case '{':
|
||||||
this.back();
|
this.back();
|
||||||
try {
|
try {
|
||||||
return new JSONObject(this);
|
return new JSONObject(this, jsonParserConfiguration);
|
||||||
} catch (StackOverflowError e) {
|
} catch (StackOverflowError e) {
|
||||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||||
}
|
}
|
||||||
case '[':
|
case '[':
|
||||||
this.back();
|
this.back();
|
||||||
try {
|
try {
|
||||||
return new JSONArray(this);
|
return new JSONArray(this, jsonParserConfiguration);
|
||||||
} catch (StackOverflowError e) {
|
} catch (StackOverflowError e) {
|
||||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||||
}
|
}
|
||||||
@ -427,6 +444,12 @@ public class JSONTokener {
|
|||||||
Object nextSimpleValue(char c) {
|
Object nextSimpleValue(char c) {
|
||||||
String string;
|
String string;
|
||||||
|
|
||||||
|
// Strict mode only allows strings with explicit double quotes
|
||||||
|
if (jsonParserConfiguration != null &&
|
||||||
|
jsonParserConfiguration.isStrictMode() &&
|
||||||
|
c == '\'') {
|
||||||
|
throw this.syntaxError("Strict mode error: Single quoted strings are not allowed");
|
||||||
|
}
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
@ -455,7 +478,14 @@ public class JSONTokener {
|
|||||||
if ("".equals(string)) {
|
if ("".equals(string)) {
|
||||||
throw this.syntaxError("Missing value");
|
throw this.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
return JSONObject.stringToValue(string);
|
Object obj = JSONObject.stringToValue(string);
|
||||||
|
// Strict mode only allows strings with explicit double quotes
|
||||||
|
if (jsonParserConfiguration != null &&
|
||||||
|
jsonParserConfiguration.isStrictMode() &&
|
||||||
|
obj instanceof String) {
|
||||||
|
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ public class CDLTest {
|
|||||||
"1, 2, 3, 4\t, 5, 6, 7\n" +
|
"1, 2, 3, 4\t, 5, 6, 7\n" +
|
||||||
"true, false, true, true, false, false, false\n" +
|
"true, false, true, true, false, false, false\n" +
|
||||||
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\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";
|
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"va'l6\", val7\n";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,11 +38,54 @@ public class CDLTest {
|
|||||||
* values all must be quoted in the cases where the JSONObject parsing
|
* values all must be quoted in the cases where the JSONObject parsing
|
||||||
* might normally convert the value into a non-string.
|
* might normally convert the value into a non-string.
|
||||||
*/
|
*/
|
||||||
private static final String EXPECTED_LINES = "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, " +
|
private static final String EXPECTED_LINES =
|
||||||
"{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\":\"val1\", " +
|
||||||
"{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}]";
|
"\"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.
|
* Attempts to create a JSONArray from a null string.
|
||||||
@ -283,11 +326,11 @@ public class CDLTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonArrayToJSONArray() {
|
public void jsonArrayToJSONArray() {
|
||||||
String nameArrayStr = "[Col1, Col2]";
|
String nameArrayStr = "[\"Col1\", \"Col2\"]";
|
||||||
String values = "V1, V2";
|
String values = "V1, V2";
|
||||||
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
|
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
|
||||||
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
|
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
|
||||||
JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]");
|
JSONArray expectedJsonArray = new JSONArray("[{\"Col1\":\"V1\",\"Col2\":\"V2\"}]");
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,11 +476,16 @@ public class JSONArrayTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void unquotedText() {
|
public void unquotedText() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
System.out.println("Skipping JSONArrayTest unquotedText() when strictMode default is true");
|
||||||
|
} else {
|
||||||
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
|
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
|
||||||
JSONArray jsonArray = new JSONArray(str);
|
JSONArray jsonArray = new JSONArray(str);
|
||||||
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
|
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
|
||||||
assertEquals(expected, jsonArray.toList());
|
assertEquals(expected, jsonArray.toList());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exercise JSONArray.join() by converting a JSONArray into a
|
* Exercise JSONArray.join() by converting a JSONArray into a
|
||||||
@ -690,8 +695,8 @@ public class JSONArrayTest {
|
|||||||
|
|
||||||
String jsonArrayStr =
|
String jsonArrayStr =
|
||||||
"["+
|
"["+
|
||||||
"hello,"+
|
"\"hello\","+
|
||||||
"world"+
|
"\"world\""+
|
||||||
"]";
|
"]";
|
||||||
// 2
|
// 2
|
||||||
jsonArray.put(new JSONArray(jsonArrayStr));
|
jsonArray.put(new JSONArray(jsonArrayStr));
|
||||||
@ -768,8 +773,8 @@ public class JSONArrayTest {
|
|||||||
|
|
||||||
String jsonArrayStr =
|
String jsonArrayStr =
|
||||||
"["+
|
"["+
|
||||||
"hello,"+
|
"\"hello\","+
|
||||||
"world"+
|
"\"world\""+
|
||||||
"]";
|
"]";
|
||||||
// 2
|
// 2
|
||||||
jsonArray.put(2, new JSONArray(jsonArrayStr));
|
jsonArray.put(2, new JSONArray(jsonArrayStr));
|
||||||
|
@ -625,7 +625,7 @@ public class JSONMLTest {
|
|||||||
"\"subValue\","+
|
"\"subValue\","+
|
||||||
"{\"svAttr\":\"svValue\"},"+
|
"{\"svAttr\":\"svValue\"},"+
|
||||||
"\"abc\""+
|
"\"abc\""+
|
||||||
"],"+
|
"]"+
|
||||||
"],"+
|
"],"+
|
||||||
"[\"value\",3],"+
|
"[\"value\",3],"+
|
||||||
"[\"value\",4.1],"+
|
"[\"value\",4.1],"+
|
||||||
|
@ -23,18 +23,18 @@ public class JSONObjectNumberTest {
|
|||||||
@Parameters(name = "{index}: {0}")
|
@Parameters(name = "{index}: {0}")
|
||||||
public static Collection<Object[]> data() {
|
public static Collection<Object[]> data() {
|
||||||
return Arrays.asList(new Object[][]{
|
return Arrays.asList(new Object[][]{
|
||||||
{"{value:50}", 1},
|
{"{\"value\":50}", 1},
|
||||||
{"{value:50.0}", 1},
|
{"{\"value\":50.0}", 1},
|
||||||
{"{value:5e1}", 1},
|
{"{\"value\":5e1}", 1},
|
||||||
{"{value:5E1}", 1},
|
{"{\"value\":5E1}", 1},
|
||||||
{"{value:5e1}", 1},
|
{"{\"value\":5e1}", 1},
|
||||||
{"{value:'50'}", 1},
|
{"{\"value\":\"50\"}", 1},
|
||||||
{"{value:-50}", -1},
|
{"{\"value\":-50}", -1},
|
||||||
{"{value:-50.0}", -1},
|
{"{\"value\":-50.0}", -1},
|
||||||
{"{value:-5e1}", -1},
|
{"{\"value\":-5e1}", -1},
|
||||||
{"{value:-5E1}", -1},
|
{"{\"value\":-5E1}", -1},
|
||||||
{"{value:-5e1}", -1},
|
{"{\"value\":-5e1}", -1},
|
||||||
{"{value:'-50'}", -1}
|
{"{\"value\":\"-50\"}", -1}
|
||||||
// JSON does not support octal or hex numbers;
|
// JSON does not support octal or hex numbers;
|
||||||
// see https://stackoverflow.com/a/52671839/6323312
|
// see https://stackoverflow.com/a/52671839/6323312
|
||||||
// "{value:062}", // octal 50
|
// "{value:062}", // octal 50
|
||||||
|
@ -218,6 +218,10 @@ public class JSONObjectTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void unquotedText() {
|
public void unquotedText() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
System.out.println("Skipping JSONObjectTest unquotedText() when strictMode default is true");
|
||||||
|
} else {
|
||||||
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
|
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
String textStr = jsonObject.toString();
|
String textStr = jsonObject.toString();
|
||||||
@ -231,6 +235,7 @@ public class JSONObjectTest {
|
|||||||
assertTrue("expected something!", textStr.contains("\"something!\""));
|
assertTrue("expected something!", textStr.contains("\"something!\""));
|
||||||
Util.checkJSONObjectMaps(jsonObject);
|
Util.checkJSONObjectMaps(jsonObject);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLongFromString(){
|
public void testLongFromString(){
|
||||||
@ -1069,6 +1074,10 @@ public class JSONObjectTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonInvalidNumberValues() {
|
public void jsonInvalidNumberValues() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
System.out.println("Skipping JSONObjectTest jsonInvalidNumberValues() when strictMode default is true");
|
||||||
|
} else {
|
||||||
// Number-notations supported by Java and invalid as JSON
|
// Number-notations supported by Java and invalid as JSON
|
||||||
String str =
|
String str =
|
||||||
"{" +
|
"{" +
|
||||||
@ -1112,6 +1121,7 @@ public class JSONObjectTest {
|
|||||||
jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
|
jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
|
||||||
Util.checkJSONObjectMaps(jsonObject);
|
Util.checkJSONObjectMaps(jsonObject);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests how JSONObject get[type] handles incorrect types
|
* Tests how JSONObject get[type] handles incorrect types
|
||||||
@ -1528,7 +1538,7 @@ public class JSONObjectTest {
|
|||||||
"{"+
|
"{"+
|
||||||
"\"trueKey\":true,"+
|
"\"trueKey\":true,"+
|
||||||
"\"falseKey\":false,"+
|
"\"falseKey\":false,"+
|
||||||
"\"stringKey\":\"hello world!\","+
|
"\"stringKey\":\"hello world!\""+
|
||||||
"}";
|
"}";
|
||||||
JSONObject jsonObject2 = new JSONObject(str);
|
JSONObject jsonObject2 = new JSONObject(str);
|
||||||
names = JSONObject.getNames(jsonObject2);
|
names = JSONObject.getNames(jsonObject2);
|
||||||
@ -1623,7 +1633,7 @@ public class JSONObjectTest {
|
|||||||
"{"+
|
"{"+
|
||||||
"\"trueKey\":true,"+
|
"\"trueKey\":true,"+
|
||||||
"\"falseKey\":false,"+
|
"\"falseKey\":false,"+
|
||||||
"\"stringKey\":\"hello world!\","+
|
"\"stringKey\":\"hello world!\""+
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
@ -2262,6 +2272,10 @@ public class JSONObjectTest {
|
|||||||
@SuppressWarnings({"boxing", "unused"})
|
@SuppressWarnings({"boxing", "unused"})
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectParsingErrors() {
|
public void jsonObjectParsingErrors() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
System.out.println("Skipping JSONObjectTest jaonObjectParsingErrors() when strictMode default is true");
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
// does not start with '{'
|
// does not start with '{'
|
||||||
String str = "abc";
|
String str = "abc";
|
||||||
@ -2579,6 +2593,7 @@ public class JSONObjectTest {
|
|||||||
e.getMessage());
|
e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Confirm behavior when putOnce() is called with null parameters
|
* Confirm behavior when putOnce() is called with null parameters
|
||||||
@ -3815,21 +3830,21 @@ public class JSONObjectTest {
|
|||||||
|
|
||||||
// Behavior documented in #826 JSONObject parsing 0-led numeric strings as ints
|
// Behavior documented in #826 JSONObject parsing 0-led numeric strings as ints
|
||||||
// After reverting the code, personId is stored as a string, and the behavior is as expected
|
// After reverting the code, personId is stored as a string, and the behavior is as expected
|
||||||
String personId = "0123";
|
String personId = "\"0123\"";
|
||||||
JSONObject j1 = new JSONObject("{personId: " + personId + "}");
|
JSONObject j1 = new JSONObject("{\"personId\": " + personId + "}");
|
||||||
assertEquals(j1.getString("personId"), "0123");
|
assertEquals(j1.getString("personId"), "0123");
|
||||||
|
|
||||||
// Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number.
|
// Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number.
|
||||||
// This example was mentioned in the same ticket
|
// This example was mentioned in the same ticket
|
||||||
// After reverting the code, personId is stored as a string, and the behavior is as expected
|
// After reverting the code, personId is stored as a string, and the behavior is as expected
|
||||||
JSONObject j2 = new JSONObject("{\"personId\":0123}");
|
JSONObject j2 = new JSONObject("{\"personId\":\"0123\"}");
|
||||||
assertEquals(j2.getString("personId"), "0123");
|
assertEquals(j2.getString("personId"), "0123");
|
||||||
|
|
||||||
// Behavior uncovered while working on the code
|
// Behavior uncovered while working on the code
|
||||||
// All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect
|
// All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect
|
||||||
JSONObject j3 = new JSONObject("{ " +
|
JSONObject j3 = new JSONObject("{ " +
|
||||||
"\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", " +
|
"\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", " +
|
||||||
"\"hex4\": 00e0, \"hex5\": 00f0, \"hex6\": 0011 }");
|
"\"hex4\": 00e0, \"hex5\": \"00f0\", \"hex6\": \"0011\" }");
|
||||||
assertEquals(j3.getString("hex1"), "010e4");
|
assertEquals(j3.getString("hex1"), "010e4");
|
||||||
assertEquals(j3.getString("hex2"), "00f0");
|
assertEquals(j3.getString("hex2"), "00f0");
|
||||||
assertEquals(j3.getString("hex3"), "0011");
|
assertEquals(j3.getString("hex3"), "0011");
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
package org.json.junit;
|
package org.json.junit;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONParserConfiguration;
|
import org.json.JSONParserConfiguration;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import java.io.IOException;
|
||||||
import static org.junit.Assert.assertTrue;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class JSONParserConfigurationTest {
|
public class JSONParserConfigurationTest {
|
||||||
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
|
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
|
||||||
@ -43,4 +51,510 @@ public class JSONParserConfigurationTest {
|
|||||||
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
assertTrue(jsonParserConfiguration.isOverwriteDuplicateKey());
|
||||||
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
assertEquals(42, jsonParserConfiguration.getMaxNestingDepth());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInput_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
List<String> strictModeInputTestCases = getNonCompliantJSONArrayList();
|
||||||
|
// this is a lot easier to debug when things stop working
|
||||||
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
|
String testCase = strictModeInputTestCases.get(i);
|
||||||
|
try {
|
||||||
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
String s = jsonArray.toString();
|
||||||
|
String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
|
||||||
|
fail(msg);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// its all good
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObjects_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
List<String> strictModeInputTestCases = getNonCompliantJSONObjectList();
|
||||||
|
// this is a lot easier to debug when things stop working
|
||||||
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
|
String testCase = strictModeInputTestCases.get(i);
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
String s = jsonObject.toString();
|
||||||
|
String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
|
||||||
|
fail(msg);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// its all good
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "[]";
|
||||||
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonArray.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmptyObject_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{}";
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidNestedArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCase = "[[\"c\"], [10.2], [true, false, true]]";
|
||||||
|
|
||||||
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
JSONArray arrayShouldContainStringAt0 = jsonArray.getJSONArray(0);
|
||||||
|
JSONArray arrayShouldContainNumberAt0 = jsonArray.getJSONArray(1);
|
||||||
|
JSONArray arrayShouldContainBooleanAt0 = jsonArray.getJSONArray(2);
|
||||||
|
|
||||||
|
assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
|
||||||
|
assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
|
||||||
|
assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidNestedObject_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCase = "{\"a0\":[\"c\"], \"a1\":[10.2], \"a2\":[true, false, true]}";
|
||||||
|
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
JSONArray arrayShouldContainStringAt0 = jsonObject.getJSONArray("a0");
|
||||||
|
JSONArray arrayShouldContainNumberAt0 = jsonObject.getJSONArray("a1");
|
||||||
|
JSONArray arrayShouldContainBooleanAt0 = jsonObject.getJSONArray("a2");
|
||||||
|
|
||||||
|
assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
|
||||||
|
assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
|
||||||
|
assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJsonException(){
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "[[]]";
|
||||||
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonArray.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidEmptyArrayInsideObject_testStrictModeTrue_shouldNotThrowJsonException(){
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":[]}";
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidEmptyArrayInsideArray_testStrictModeFalse_shouldNotThrowJsonException(){
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
String testCase = "[[]]";
|
||||||
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonArray.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidEmptyArrayInsideObject_testStrictModeFalse_shouldNotThrowJsonException(){
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
String testCase = "{\"a0\":[]}";
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidStringArray_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "[badString]";
|
||||||
|
JSONException je = assertThrows(JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Value 'badString' is not surrounded by quotes at 10 [character 11 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidStringObject_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":badString}";
|
||||||
|
JSONException je = assertThrows(JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Value 'badString' is not surrounded by quotes at 15 [character 16 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void allowNullArrayInStrictMode() {
|
||||||
|
String expected = "[null]";
|
||||||
|
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
|
assertEquals(expected, jsonArray.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void allowNullObjectInStrictMode() {
|
||||||
|
String expected = "{\"a0\":null}";
|
||||||
|
JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
|
assertEquals(expected, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleNumericArray() {
|
||||||
|
String expected = "[10]";
|
||||||
|
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
|
assertEquals(expected, jsonArray.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleNumericObject() {
|
||||||
|
String expected = "{\"a0\":10}";
|
||||||
|
JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
|
assertEquals(expected, jsonObject.toString());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
||||||
|
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
|
||||||
|
String compliantJsonArrayAsString = lines.collect(Collectors.joining());
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCompliantJSONObjectFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
||||||
|
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonObject.json"))) {
|
||||||
|
String compliantJsonObjectAsString = lines.collect(Collectors.joining());
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
new JSONObject(compliantJsonObjectAsString, jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputArrays_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
|
||||||
|
List<String> strictModeInputTestCases = getNonCompliantJSONArrayList();
|
||||||
|
|
||||||
|
// this is a lot easier to debug when things stop working
|
||||||
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
|
String testCase = strictModeInputTestCases.get(i);
|
||||||
|
try {
|
||||||
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
|
||||||
|
fail(String.format("Noncompliant array index: %d", i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObjects_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
|
||||||
|
List<String> strictModeInputTestCases = getNonCompliantJSONObjectList();
|
||||||
|
|
||||||
|
// this is a lot easier to debug when things stop working
|
||||||
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
|
String testCase = strictModeInputTestCases.get(i);
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
|
||||||
|
fail(String.format("Noncompliant array index: %d", i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "[1,2];[3,4]";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Unparsed characters found at end of input text at 6 [character 7 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":[1,2];\"a1\":[3,4]}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Invalid character ';' found at 12 [character 13 line 1]", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "[\"1\",\"2\"];[3,4]";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Unparsed characters found at end of input text at 10 [character 11 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObjectWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":[\"1\",\"2\"];\"a1\":[3,4]}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Invalid character ';' found at 16 [character 17 line 1]", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "[{\"test\": implied}]";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Value 'implied' is not surrounded by quotes at 17 [character 18 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":{\"test\": implied}]}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Strict mode error: Value 'implied' is not surrounded by quotes at 22 [character 23 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
String testCase = "[{\"test\": implied}]";
|
||||||
|
new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObject_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
String testCase = "{\"a0\":{\"test\": implied}}";
|
||||||
|
new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonCompliantQuotesArray_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCaseOne = "[\"abc', \"test\"]";
|
||||||
|
String testCaseTwo = "['abc\", \"test\"]";
|
||||||
|
String testCaseThree = "['abc']";
|
||||||
|
String testCaseFour = "[{'testField': \"testValue\"}]";
|
||||||
|
|
||||||
|
JSONException jeOne = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
|
||||||
|
JSONException jeTwo = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
|
||||||
|
JSONException jeThree = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONArray(testCaseThree, jsonParserConfiguration));
|
||||||
|
JSONException jeFour = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONArray(testCaseFour, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"Expected a ',' or ']' at 10 [character 11 line 1]",
|
||||||
|
jeOne.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||||
|
jeTwo.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||||
|
jeThree.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Strict mode error: Single quoted strings are not allowed at 3 [character 4 line 1]",
|
||||||
|
jeFour.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonCompliantQuotesObject_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCaseOne = "{\"abc': \"test\"}";
|
||||||
|
String testCaseTwo = "{'abc\": \"test\"}";
|
||||||
|
String testCaseThree = "{\"a\":'abc'}";
|
||||||
|
String testCaseFour = "{'testField': \"testValue\"}";
|
||||||
|
|
||||||
|
JSONException jeOne = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseOne, jsonParserConfiguration));
|
||||||
|
JSONException jeTwo = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseTwo, jsonParserConfiguration));
|
||||||
|
JSONException jeThree = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseThree, jsonParserConfiguration));
|
||||||
|
JSONException jeFour = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseFour, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"Expected a ':' after a key at 10 [character 11 line 1]",
|
||||||
|
jeOne.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||||
|
jeTwo.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Strict mode error: Single quoted strings are not allowed at 6 [character 7 line 1]",
|
||||||
|
jeThree.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Strict mode error: Single quoted strings are not allowed at 2 [character 3 line 1]",
|
||||||
|
jeFour.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUnbalancedQuotesArray_testStrictModeFalse_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
|
||||||
|
String testCaseOne = "[\"abc', \"test\"]";
|
||||||
|
String testCaseTwo = "['abc\", \"test\"]";
|
||||||
|
|
||||||
|
JSONException jeOne = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONArray(testCaseOne, jsonParserConfiguration));
|
||||||
|
JSONException jeTwo = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONArray(testCaseTwo, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals("Expected a ',' or ']' at 10 [character 11 line 1]", jeOne.getMessage());
|
||||||
|
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUnbalancedQuotesObject_testStrictModeFalse_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
|
||||||
|
String testCaseOne = "{\"abc': \"test\"}";
|
||||||
|
String testCaseTwo = "{'abc\": \"test\"}";
|
||||||
|
|
||||||
|
JSONException jeOne = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseOne, jsonParserConfiguration));
|
||||||
|
JSONException jeTwo = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseTwo, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals("Expected a ':' after a key at 10 [character 11 line 1]", jeOne.getMessage());
|
||||||
|
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCase = "[{test: implied}]";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals("Strict mode error: Value 'test' is not surrounded by quotes at 6 [character 7 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCase = "{test: implied}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant json but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals("Strict mode error: Value 'test' is not surrounded by quotes at 5 [character 6 line 1]",
|
||||||
|
je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||||
|
* this class.
|
||||||
|
*
|
||||||
|
* @return List with JSON strings.
|
||||||
|
*/
|
||||||
|
private List<String> getNonCompliantJSONArrayList() {
|
||||||
|
return Arrays.asList(
|
||||||
|
"[1],",
|
||||||
|
"[1,]",
|
||||||
|
"[,]",
|
||||||
|
"[,,]",
|
||||||
|
"[[1],\"sa\",[2]]a",
|
||||||
|
"[1],\"dsa\": \"test\"",
|
||||||
|
"[[a]]",
|
||||||
|
"[]asdf",
|
||||||
|
"[]]",
|
||||||
|
"[]}",
|
||||||
|
"[][",
|
||||||
|
"[]{",
|
||||||
|
"[],",
|
||||||
|
"[]:",
|
||||||
|
"[],[",
|
||||||
|
"[],{",
|
||||||
|
"[1,2];[3,4]",
|
||||||
|
"[test]",
|
||||||
|
"[{'testSingleQuote': 'testSingleQuote'}]",
|
||||||
|
"[1, 2,3]:[4,5]",
|
||||||
|
"[{test: implied}]",
|
||||||
|
"[{\"test\": implied}]",
|
||||||
|
"[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]",
|
||||||
|
"[{test: \"implied\"}]");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||||
|
* this class.
|
||||||
|
*
|
||||||
|
* @return List with JSON strings.
|
||||||
|
*/
|
||||||
|
private List<String> getNonCompliantJSONObjectList() {
|
||||||
|
return Arrays.asList(
|
||||||
|
"{\"a\":1},",
|
||||||
|
"{\"a\":1,}",
|
||||||
|
"{\"a0\":[1],\"a1\":\"sa\",\"a2\":[2]}a",
|
||||||
|
"{\"a\":1},\"dsa\": \"test\"",
|
||||||
|
"{\"a\":[a]}",
|
||||||
|
"{}asdf",
|
||||||
|
"{}}",
|
||||||
|
"{}]",
|
||||||
|
"{}{",
|
||||||
|
"{}[",
|
||||||
|
"{},",
|
||||||
|
"{}:",
|
||||||
|
"{},{",
|
||||||
|
"{},[",
|
||||||
|
"{\"a0\":[1,2];\"a1\":[3,4]}",
|
||||||
|
"{\"a\":test}",
|
||||||
|
"{a:{'testSingleQuote': 'testSingleQuote'}}",
|
||||||
|
"{\"a0\":1, \"a1\":2,\"a2\":3}:{\"a3\":4,\"a4\":5}",
|
||||||
|
"{\"a\":{test: implied}}",
|
||||||
|
"{a:{\"test\": implied}}",
|
||||||
|
"{a:[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]}",
|
||||||
|
"{a:{test: \"implied\"}}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -384,8 +384,7 @@ public class JSONPointerTest {
|
|||||||
String str = "{"+
|
String str = "{"+
|
||||||
"\"string\\\\\\\\Key\":\"hello world!\","+
|
"\"string\\\\\\\\Key\":\"hello world!\","+
|
||||||
|
|
||||||
"\"\\\\\":\"slash test\"," +
|
"\"\\\\\":\"slash test\"" +
|
||||||
"}"+
|
|
||||||
"}";
|
"}";
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
|
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
|
||||||
|
@ -270,9 +270,9 @@ public class XMLConfigurationTest {
|
|||||||
|
|
||||||
String expectedStr =
|
String expectedStr =
|
||||||
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
|
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
|
||||||
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+
|
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",\"TrueValue\":true,\n"+
|
||||||
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
|
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
|
||||||
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+
|
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":\"-23x.45\",\n"+
|
||||||
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
||||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||||
|
@ -267,9 +267,9 @@ public class XMLTest {
|
|||||||
|
|
||||||
String expectedStr =
|
String expectedStr =
|
||||||
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
|
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
|
||||||
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+
|
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",\"TrueValue\":true,\n"+
|
||||||
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
|
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
|
||||||
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+
|
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":\"-23x.45\",\n"+
|
||||||
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
|
||||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||||
@ -1180,7 +1180,7 @@ public class XMLTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){
|
public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){
|
||||||
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
|
String jsonString = "{\"outer\":{\"innerOne\":\"\", \"innerTwo\":\"two\"}}";
|
||||||
JSONObject jsonObject = new JSONObject(jsonString);
|
JSONObject jsonObject = new JSONObject(jsonString);
|
||||||
String expectedXmlString = "<encloser><outer><innerOne></innerOne><innerTwo>two</innerTwo></outer></encloser>";
|
String expectedXmlString = "<encloser><outer><innerOne></innerOne><innerTwo>two</innerTwo></outer></encloser>";
|
||||||
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true));
|
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true));
|
||||||
@ -1191,7 +1191,7 @@ public class XMLTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){
|
public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){
|
||||||
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
|
String jsonString = "{\"outer\":{\"innerOne\":\"\", \"innerTwo\":\"two\"}}";
|
||||||
JSONObject jsonObject = new JSONObject(jsonString);
|
JSONObject jsonObject = new JSONObject(jsonString);
|
||||||
String expectedXmlString = "<encloser><outer><innerOne/><innerTwo>two</innerTwo></outer></encloser>";
|
String expectedXmlString = "<encloser><outer><innerOne/><innerTwo>two</innerTwo></outer></encloser>";
|
||||||
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false));
|
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false));
|
||||||
|
317
src/test/resources/compliantJsonArray.json
Normal file
317
src/test/resources/compliantJsonArray.json
Normal file
@ -0,0 +1,317 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"_id": "6606c27d2ab4a0102d49420a",
|
||||||
|
"index": 0,
|
||||||
|
"guid": "441331fb-84d1-4873-a649-3814621a0370",
|
||||||
|
"isActive": true,
|
||||||
|
"balance": "$2,691.63",
|
||||||
|
"picture": "http://example.abc/32x32",
|
||||||
|
"age": 26,
|
||||||
|
"eyeColor": "blue",
|
||||||
|
"name": "abc",
|
||||||
|
"gender": "female",
|
||||||
|
"company": "example",
|
||||||
|
"email": "abc@def.com",
|
||||||
|
"phone": "+1 (123) 456-7890",
|
||||||
|
"address": "123 Main St",
|
||||||
|
"about": "Laborum magna tempor officia irure cillum nulla incididunt Lorem dolor veniam elit cupidatat amet. Veniam veniam exercitation nulla consectetur officia esse ex sunt nulla nisi ea cillum nisi reprehenderit. Qui aliquip reprehenderit aliqua aliquip aliquip anim sit magna nostrud dolore veniam velit elit aliquip.\r\n",
|
||||||
|
"registered": "2016-07-22T03:18:11 -01:00",
|
||||||
|
"latitude": -21.544934,
|
||||||
|
"longitude": 72.765495,
|
||||||
|
"tags": [
|
||||||
|
"consectetur",
|
||||||
|
"minim",
|
||||||
|
"sunt",
|
||||||
|
"in",
|
||||||
|
"ut",
|
||||||
|
"velit",
|
||||||
|
"anim"
|
||||||
|
],
|
||||||
|
"friends": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "abc def"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "ghi jkl"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "mno pqr"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"greeting": "Hello, abc! You have 10 unread messages.",
|
||||||
|
"favoriteFruit": "banana"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "6606c27d0a45df5121fb765f",
|
||||||
|
"index": 1,
|
||||||
|
"guid": "fd774715-de85-44b9-b498-c214d8f68d9f",
|
||||||
|
"isActive": true,
|
||||||
|
"balance": "$2,713.96",
|
||||||
|
"picture": "http://placehold.it/32x32",
|
||||||
|
"age": 27,
|
||||||
|
"eyeColor": "green",
|
||||||
|
"name": "def",
|
||||||
|
"gender": "female",
|
||||||
|
"company": "sample",
|
||||||
|
"email": "def@abc.com",
|
||||||
|
"phone": "+1 (123) 456-78910",
|
||||||
|
"address": "1234 Main St",
|
||||||
|
"about": "Ea id cupidatat eiusmod culpa. Nulla consequat esse elit enim et pariatur eiusmod ipsum. Consequat eu non reprehenderit in.\r\n",
|
||||||
|
"registered": "2015-04-06T07:54:22 -01:00",
|
||||||
|
"latitude": 83.512347,
|
||||||
|
"longitude": -9.368739,
|
||||||
|
"tags": [
|
||||||
|
"excepteur",
|
||||||
|
"non",
|
||||||
|
"nostrud",
|
||||||
|
"laboris",
|
||||||
|
"laboris",
|
||||||
|
"qui",
|
||||||
|
"aute"
|
||||||
|
],
|
||||||
|
"friends": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "sample example"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "test name"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "aaa aaaa"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"greeting": "Hello, test! You have 7 unread messages.",
|
||||||
|
"favoriteFruit": "apple"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "6606c27dfb3a0e4e7e7183d3",
|
||||||
|
"index": 2,
|
||||||
|
"guid": "688b0c36-98e0-4ee7-86b8-863638d79b5f",
|
||||||
|
"isActive": false,
|
||||||
|
"balance": "$3,514.35",
|
||||||
|
"picture": "http://placehold.it/32x32",
|
||||||
|
"age": 32,
|
||||||
|
"eyeColor": "green",
|
||||||
|
"name": "test",
|
||||||
|
"gender": "female",
|
||||||
|
"company": "test",
|
||||||
|
"email": "test@test.com",
|
||||||
|
"phone": "+1 (123) 456-7890",
|
||||||
|
"address": "123 Main St",
|
||||||
|
"about": "Mollit officia adipisicing ex nisi non Lorem sunt quis est. Irure exercitation duis ipsum qui ullamco eu ea commodo occaecat minim proident. Incididunt nostrud ex cupidatat eiusmod mollit anim irure culpa. Labore voluptate voluptate labore nisi sit eu. Dolor sit proident velit dolor deserunt labore sit ipsum incididunt eiusmod reprehenderit voluptate. Duis anim velit officia laboris consequat officia dolor sint dolor nisi ex.\r\n",
|
||||||
|
"registered": "2021-11-02T12:50:05 -00:00",
|
||||||
|
"latitude": -82.969939,
|
||||||
|
"longitude": 86.415645,
|
||||||
|
"tags": [
|
||||||
|
"aliquip",
|
||||||
|
"et",
|
||||||
|
"est",
|
||||||
|
"nulla",
|
||||||
|
"nulla",
|
||||||
|
"tempor",
|
||||||
|
"adipisicing"
|
||||||
|
],
|
||||||
|
"friends": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "sample"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "example"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"greeting": "Hello, test! You have 1 unread messages.",
|
||||||
|
"favoriteFruit": "strawberry"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "6606c27d204bc2327fc9ba23",
|
||||||
|
"index": 3,
|
||||||
|
"guid": "be970cba-306e-4cbd-be08-c265a43a61fa",
|
||||||
|
"isActive": true,
|
||||||
|
"balance": "$3,691.63",
|
||||||
|
"picture": "http://placehold.it/32x32",
|
||||||
|
"age": 35,
|
||||||
|
"eyeColor": "brown",
|
||||||
|
"name": "another test",
|
||||||
|
"gender": "male",
|
||||||
|
"company": "TEST",
|
||||||
|
"email": "anothertest@anothertest.com",
|
||||||
|
"phone": "+1 (321) 987-6543",
|
||||||
|
"address": "123 Example Main St",
|
||||||
|
"about": "Do proident consectetur minim quis. In adipisicing culpa Lorem fugiat cillum exercitation velit velit. Non voluptate laboris deserunt veniam et sint consectetur irure aliqua quis eiusmod consectetur elit id. Ex sint do anim Lorem excepteur eu nulla.\r\n",
|
||||||
|
"registered": "2020-06-25T04:55:25 -01:00",
|
||||||
|
"latitude": 63.614955,
|
||||||
|
"longitude": -109.299405,
|
||||||
|
"tags": [
|
||||||
|
"irure",
|
||||||
|
"esse",
|
||||||
|
"non",
|
||||||
|
"mollit",
|
||||||
|
"laborum",
|
||||||
|
"adipisicing",
|
||||||
|
"ad"
|
||||||
|
],
|
||||||
|
"friends": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "sample"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "example"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"greeting": "Hello, another test! You have 5 unread messages.",
|
||||||
|
"favoriteFruit": "apple"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "6606c27df63eb5f390cb9989",
|
||||||
|
"index": 4,
|
||||||
|
"guid": "2c3e5115-758d-468e-99c5-c9afa26e1f9f",
|
||||||
|
"isActive": true,
|
||||||
|
"balance": "$1,047.20",
|
||||||
|
"picture": "http://test.it/32x32",
|
||||||
|
"age": 30,
|
||||||
|
"eyeColor": "green",
|
||||||
|
"name": "Test Name",
|
||||||
|
"gender": "female",
|
||||||
|
"company": "test",
|
||||||
|
"email": "testname@testname.com",
|
||||||
|
"phone": "+1 (999) 999-9999",
|
||||||
|
"address": "999 Test Main St",
|
||||||
|
"about": "Voluptate exercitation tempor consectetur velit magna ea occaecat cupidatat consectetur anim aute. Aliquip est aute ipsum laboris non irure qui consectetur tempor quis do ea Lorem. Cupidatat exercitation ad culpa aliqua amet commodo mollit reprehenderit exercitation adipisicing amet et laborum pariatur.\r\n",
|
||||||
|
"registered": "2023-01-19T02:43:18 -00:00",
|
||||||
|
"latitude": 14.15208,
|
||||||
|
"longitude": 170.411535,
|
||||||
|
"tags": [
|
||||||
|
"dolor",
|
||||||
|
"qui",
|
||||||
|
"cupidatat",
|
||||||
|
"aliqua",
|
||||||
|
"laboris",
|
||||||
|
"reprehenderit",
|
||||||
|
"sint"
|
||||||
|
],
|
||||||
|
"friends": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "sample"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "example"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"greeting": "Hello, test! You have 6 unread messages.",
|
||||||
|
"favoriteFruit": "apple"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "6606c27d01d19fa29853d59c",
|
||||||
|
"index": 5,
|
||||||
|
"guid": "816cda74-5d4b-498f-9724-20f340d5f5bf",
|
||||||
|
"isActive": false,
|
||||||
|
"balance": "$2,628.74",
|
||||||
|
"picture": "http://testing.it/32x32",
|
||||||
|
"age": 28,
|
||||||
|
"eyeColor": "green",
|
||||||
|
"name": "Testing",
|
||||||
|
"gender": "female",
|
||||||
|
"company": "test",
|
||||||
|
"email": "testing@testing.com",
|
||||||
|
"phone": "+1 (888) 888-8888",
|
||||||
|
"address": "123 Main St",
|
||||||
|
"about": "Cupidatat non ut nulla qui excepteur in minim non et nulla fugiat. Dolor quis laborum occaecat veniam dolor ullamco deserunt amet veniam dolor quis proident tempor laboris. In cillum duis ut quis. Aliqua cupidatat magna proident velit tempor veniam et consequat laborum ex dolore qui. Incididunt deserunt magna minim Lorem consectetur.\r\n",
|
||||||
|
"registered": "2017-10-14T11:14:08 -01:00",
|
||||||
|
"latitude": -5.345728,
|
||||||
|
"longitude": -9.706491,
|
||||||
|
"tags": [
|
||||||
|
"officia",
|
||||||
|
"velit",
|
||||||
|
"laboris",
|
||||||
|
"qui",
|
||||||
|
"cupidatat",
|
||||||
|
"cupidatat",
|
||||||
|
"ad"
|
||||||
|
],
|
||||||
|
"friends": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "sample"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "example"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"greeting": "Hello, testing! You have 2 unread messages.",
|
||||||
|
"favoriteFruit": "strawberry"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"_id": "6606c27d803003cede1d6deb",
|
||||||
|
"index": 6,
|
||||||
|
"guid": "4ee550bc-0920-4104-b3ce-ebf9db6a803f",
|
||||||
|
"isActive": true,
|
||||||
|
"balance": "$1,709.31",
|
||||||
|
"picture": "http://sample.it/32x32",
|
||||||
|
"age": 31,
|
||||||
|
"eyeColor": "blue",
|
||||||
|
"name": "Sample Name",
|
||||||
|
"gender": "female",
|
||||||
|
"company": "Sample",
|
||||||
|
"email": "sample@sample.com",
|
||||||
|
"phone": "+1 (777) 777-7777",
|
||||||
|
"address": "123 Main St",
|
||||||
|
"about": "Lorem ex proident ipsum ullamco velit sit nisi eiusmod cillum. Id tempor irure culpa nisi sit non qui veniam non ut. Aliquip reprehenderit excepteur mollit quis excepteur ex sit. Quis do eu veniam do ullamco occaecat eu cupidatat nisi laborum tempor minim fugiat pariatur. Ex in nulla ex velit.\r\n",
|
||||||
|
"registered": "2019-04-08T03:54:36 -01:00",
|
||||||
|
"latitude": -70.660321,
|
||||||
|
"longitude": 71.547525,
|
||||||
|
"tags": [
|
||||||
|
"consequat",
|
||||||
|
"veniam",
|
||||||
|
"pariatur",
|
||||||
|
"aliqua",
|
||||||
|
"cillum",
|
||||||
|
"eu",
|
||||||
|
"officia"
|
||||||
|
],
|
||||||
|
"friends": [
|
||||||
|
{
|
||||||
|
"id": 0,
|
||||||
|
"name": "Test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "Sample"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"name": "Example"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"greeting": "Hello, Sample! You have 6 unread messages.",
|
||||||
|
"favoriteFruit": "apple"
|
||||||
|
}
|
||||||
|
]
|
3703
src/test/resources/compliantJsonObject.json
Normal file
3703
src/test/resources/compliantJsonObject.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user