restore-jsonparserconfiguration: fix unit tests to work when strictMode default is true

This commit is contained in:
Sean Leary 2024-12-15 13:18:39 -06:00
parent 09536cd6c8
commit d3c7eaf17e
8 changed files with 461 additions and 399 deletions

View File

@ -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);
} }

View File

@ -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));

View File

@ -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],"+

View File

@ -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

View File

@ -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,41 +1074,45 @@ 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 =
"{"+ "{" +
"\"hexNumber\":-0x123,"+ "\"hexNumber\":-0x123," +
"\"tooManyZeros\":00,"+ "\"tooManyZeros\":00," +
"\"negativeInfinite\":-Infinity,"+ "\"negativeInfinite\":-Infinity," +
"\"negativeNaN\":-NaN,"+ "\"negativeNaN\":-NaN," +
"\"negativeFraction\":-.01,"+ "\"negativeFraction\":-.01," +
"\"tooManyZerosFraction\":00.001,"+ "\"tooManyZerosFraction\":00.001," +
"\"negativeHexFloat\":-0x1.fffp1,"+ "\"negativeHexFloat\":-0x1.fffp1," +
"\"hexFloat\":0x1.0P-1074,"+ "\"hexFloat\":0x1.0P-1074," +
"\"floatIdentifier\":0.1f,"+ "\"floatIdentifier\":0.1f," +
"\"doubleIdentifier\":0.1d"+ "\"doubleIdentifier\":0.1d" +
"}"; "}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
Object obj; Object obj;
obj = jsonObject.get( "hexNumber" ); obj = jsonObject.get("hexNumber");
assertFalse( "hexNumber must not be a number (should throw exception!?)", assertFalse("hexNumber must not be a number (should throw exception!?)",
obj instanceof Number ); obj instanceof Number);
assertTrue("hexNumber currently evaluates to string", assertTrue("hexNumber currently evaluates to string",
obj.equals("-0x123")); obj.equals("-0x123"));
assertTrue( "tooManyZeros currently evaluates to string", assertTrue("tooManyZeros currently evaluates to string",
jsonObject.get( "tooManyZeros" ).equals("00")); jsonObject.get("tooManyZeros").equals("00"));
obj = jsonObject.get("negativeInfinite"); obj = jsonObject.get("negativeInfinite");
assertTrue( "negativeInfinite currently evaluates to string", assertTrue("negativeInfinite currently evaluates to string",
obj.equals("-Infinity")); obj.equals("-Infinity"));
obj = jsonObject.get("negativeNaN"); obj = jsonObject.get("negativeNaN");
assertTrue( "negativeNaN currently evaluates to string", assertTrue("negativeNaN currently evaluates to string",
obj.equals("-NaN")); obj.equals("-NaN"));
assertTrue( "negativeFraction currently evaluates to double -0.01", assertTrue("negativeFraction currently evaluates to double -0.01",
jsonObject.get( "negativeFraction" ).equals(BigDecimal.valueOf(-0.01))); jsonObject.get("negativeFraction").equals(BigDecimal.valueOf(-0.01)));
assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", assertTrue("tooManyZerosFraction currently evaluates to double 0.001",
jsonObject.optLong( "tooManyZerosFraction" )==0); jsonObject.optLong("tooManyZerosFraction") == 0);
assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", assertTrue("negativeHexFloat currently evaluates to double -3.99951171875",
jsonObject.get( "negativeHexFloat" ).equals(Double.valueOf(-3.99951171875))); jsonObject.get("negativeHexFloat").equals(Double.valueOf(-3.99951171875)));
assertTrue("hexFloat currently evaluates to double 4.9E-324", assertTrue("hexFloat currently evaluates to double 4.9E-324",
jsonObject.get("hexFloat").equals(Double.valueOf(4.9E-324))); jsonObject.get("hexFloat").equals(Double.valueOf(4.9E-324)));
assertTrue("floatIdentifier currently evaluates to double 0.1", assertTrue("floatIdentifier currently evaluates to double 0.1",
@ -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,10 +2272,14 @@ 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";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"A JSONObject text must begin with '{' at 1 [character 2 line 1]", "A JSONObject text must begin with '{' at 1 [character 2 line 1]",
@ -2274,7 +2288,7 @@ public class JSONObjectTest {
try { try {
// does not end with '}' // does not end with '}'
String str = "{"; String str = "{";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"A JSONObject text must end with '}' at 1 [character 2 line 1]", "A JSONObject text must end with '}' at 1 [character 2 line 1]",
@ -2283,7 +2297,7 @@ public class JSONObjectTest {
try { try {
// key with no ':' // key with no ':'
String str = "{\"myKey\" = true}"; String str = "{\"myKey\" = true}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Expected a ':' after a key at 10 [character 11 line 1]", "Expected a ':' after a key at 10 [character 11 line 1]",
@ -2292,7 +2306,7 @@ public class JSONObjectTest {
try { try {
// entries with no ',' separator // entries with no ',' separator
String str = "{\"myKey\":true \"myOtherKey\":false}"; String str = "{\"myKey\":true \"myOtherKey\":false}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Expected a ',' or '}' at 15 [character 16 line 1]", "Expected a ',' or '}' at 15 [character 16 line 1]",
@ -2301,7 +2315,7 @@ public class JSONObjectTest {
try { try {
// key is a nested map // key is a nested map
String str = "{{\"foo\": \"bar\"}: \"baz\"}"; String str = "{{\"foo\": \"bar\"}: \"baz\"}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing value at 1 [character 2 line 1]", "Missing value at 1 [character 2 line 1]",
@ -2310,7 +2324,7 @@ public class JSONObjectTest {
try { try {
// key is a nested array containing a map // key is a nested array containing a map
String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}"; String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing value at 9 [character 10 line 1]", "Missing value at 9 [character 10 line 1]",
@ -2319,7 +2333,7 @@ public class JSONObjectTest {
try { try {
// key contains } // key contains }
String str = "{foo}: 2}"; String str = "{foo}: 2}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Expected a ':' after a key at 5 [character 6 line 1]", "Expected a ':' after a key at 5 [character 6 line 1]",
@ -2328,7 +2342,7 @@ public class JSONObjectTest {
try { try {
// key contains ] // key contains ]
String str = "{foo]: 2}"; String str = "{foo]: 2}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Expected a ':' after a key at 5 [character 6 line 1]", "Expected a ':' after a key at 5 [character 6 line 1]",
@ -2337,7 +2351,7 @@ public class JSONObjectTest {
try { try {
// \0 after , // \0 after ,
String str = "{\"myKey\":true, \0\"myOtherKey\":false}"; String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
assertNull("Expected an exception",new JSONObject(str)); assertNull("Expected an exception", new JSONObject(str));
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"A JSONObject text must end with '}' at 15 [character 16 line 1]", "A JSONObject text must end with '}' at 15 [character 16 line 1]",
@ -2378,7 +2392,7 @@ public class JSONObjectTest {
} }
try { try {
// invalid numberToString() // invalid numberToString()
JSONObject.numberToString((Number)null); JSONObject.numberToString((Number) null);
fail("Expected an exception"); fail("Expected an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
@ -2412,10 +2426,10 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key (level 0) // test exception message when including a duplicate key (level 0)
String str = "{\n" String str = "{\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
+" \"attr03\":\"value-03\",\n" + " \"attr03\":\"value-03\",\n"
+" \"attr03\":\"value-04\"\n" + " \"attr03\":\"value-04\"\n"
+ "}"; + "}";
new JSONObject(str); new JSONObject(str);
fail("Expected an exception"); fail("Expected an exception");
@ -2427,13 +2441,13 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key (level 0) holding an object // test exception message when including a duplicate key (level 0) holding an object
String str = "{\n" String str = "{\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
+" \"attr03\":\"value-03\",\n" + " \"attr03\":\"value-03\",\n"
+" \"attr03\": {" + " \"attr03\": {"
+" \"attr04-01\":\"value-04-01\",n" + " \"attr04-01\":\"value-04-01\",n"
+" \"attr04-02\":\"value-04-02\",n" + " \"attr04-02\":\"value-04-02\",n"
+" \"attr04-03\":\"value-04-03\"n" + " \"attr04-03\":\"value-04-03\"n"
+ " }\n" + " }\n"
+ "}"; + "}";
new JSONObject(str); new JSONObject(str);
@ -2446,15 +2460,15 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key (level 0) holding an array // test exception message when including a duplicate key (level 0) holding an array
String str = "{\n" String str = "{\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
+" \"attr03\":\"value-03\",\n" + " \"attr03\":\"value-03\",\n"
+" \"attr03\": [\n" + " \"attr03\": [\n"
+" {" + " {"
+" \"attr04-01\":\"value-04-01\",n" + " \"attr04-01\":\"value-04-01\",n"
+" \"attr04-02\":\"value-04-02\",n" + " \"attr04-02\":\"value-04-02\",n"
+" \"attr04-03\":\"value-04-03\"n" + " \"attr04-03\":\"value-04-03\"n"
+" }\n" + " }\n"
+ " ]\n" + " ]\n"
+ "}"; + "}";
new JSONObject(str); new JSONObject(str);
@ -2467,14 +2481,14 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key (level 1) // test exception message when including a duplicate key (level 1)
String str = "{\n" String str = "{\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
+" \"attr03\":\"value-03\",\n" + " \"attr03\":\"value-03\",\n"
+" \"attr04\": {\n" + " \"attr04\": {\n"
+" \"attr04-01\":\"value04-01\",\n" + " \"attr04-01\":\"value04-01\",\n"
+" \"attr04-02\":\"value04-02\",\n" + " \"attr04-02\":\"value04-02\",\n"
+" \"attr04-03\":\"value04-03\",\n" + " \"attr04-03\":\"value04-03\",\n"
+" \"attr04-03\":\"value04-04\"\n" + " \"attr04-03\":\"value04-04\"\n"
+ " }\n" + " }\n"
+ "}"; + "}";
new JSONObject(str); new JSONObject(str);
@ -2487,19 +2501,19 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key (level 1) holding an object // test exception message when including a duplicate key (level 1) holding an object
String str = "{\n" String str = "{\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
+" \"attr03\":\"value-03\",\n" + " \"attr03\":\"value-03\",\n"
+" \"attr04\": {\n" + " \"attr04\": {\n"
+" \"attr04-01\":\"value04-01\",\n" + " \"attr04-01\":\"value04-01\",\n"
+" \"attr04-02\":\"value04-02\",\n" + " \"attr04-02\":\"value04-02\",\n"
+" \"attr04-03\":\"value04-03\",\n" + " \"attr04-03\":\"value04-03\",\n"
+" \"attr04-03\": {\n" + " \"attr04-03\": {\n"
+" \"attr04-04-01\":\"value04-04-01\",\n" + " \"attr04-04-01\":\"value04-04-01\",\n"
+" \"attr04-04-02\":\"value04-04-02\",\n" + " \"attr04-04-02\":\"value04-04-02\",\n"
+" \"attr04-04-03\":\"value04-04-03\",\n" + " \"attr04-04-03\":\"value04-04-03\",\n"
+" }\n" + " }\n"
+" }\n" + " }\n"
+ "}"; + "}";
new JSONObject(str); new JSONObject(str);
fail("Expected an exception"); fail("Expected an exception");
@ -2511,21 +2525,21 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key (level 1) holding an array // test exception message when including a duplicate key (level 1) holding an array
String str = "{\n" String str = "{\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\":\"value-02\",\n" + " \"attr02\":\"value-02\",\n"
+" \"attr03\":\"value-03\",\n" + " \"attr03\":\"value-03\",\n"
+" \"attr04\": {\n" + " \"attr04\": {\n"
+" \"attr04-01\":\"value04-01\",\n" + " \"attr04-01\":\"value04-01\",\n"
+" \"attr04-02\":\"value04-02\",\n" + " \"attr04-02\":\"value04-02\",\n"
+" \"attr04-03\":\"value04-03\",\n" + " \"attr04-03\":\"value04-03\",\n"
+" \"attr04-03\": [\n" + " \"attr04-03\": [\n"
+" {\n" + " {\n"
+" \"attr04-04-01\":\"value04-04-01\",\n" + " \"attr04-04-01\":\"value04-04-01\",\n"
+" \"attr04-04-02\":\"value04-04-02\",\n" + " \"attr04-04-02\":\"value04-04-02\",\n"
+" \"attr04-04-03\":\"value04-04-03\",\n" + " \"attr04-04-03\":\"value04-04-03\",\n"
+" }\n" + " }\n"
+" ]\n" + " ]\n"
+" }\n" + " }\n"
+ "}"; + "}";
new JSONObject(str); new JSONObject(str);
fail("Expected an exception"); fail("Expected an exception");
@ -2537,14 +2551,14 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key in object (level 0) within an array // test exception message when including a duplicate key in object (level 0) within an array
String str = "[\n" String str = "[\n"
+" {\n" + " {\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\":\"value-02\"\n" + " \"attr02\":\"value-02\"\n"
+" },\n" + " },\n"
+" {\n" + " {\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr01\":\"value-02\"\n" + " \"attr01\":\"value-02\"\n"
+" }\n" + " }\n"
+ "]"; + "]";
new JSONArray(str); new JSONArray(str);
fail("Expected an exception"); fail("Expected an exception");
@ -2556,20 +2570,20 @@ public class JSONObjectTest {
try { try {
// test exception message when including a duplicate key in object (level 1) within an array // test exception message when including a duplicate key in object (level 1) within an array
String str = "[\n" String str = "[\n"
+" {\n" + " {\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\": {\n" + " \"attr02\": {\n"
+" \"attr02-01\":\"value-02-01\",\n" + " \"attr02-01\":\"value-02-01\",\n"
+" \"attr02-02\":\"value-02-02\"\n" + " \"attr02-02\":\"value-02-02\"\n"
+" }\n" + " }\n"
+" },\n" + " },\n"
+" {\n" + " {\n"
+" \"attr01\":\"value-01\",\n" + " \"attr01\":\"value-01\",\n"
+" \"attr02\": {\n" + " \"attr02\": {\n"
+" \"attr02-01\":\"value-02-01\",\n" + " \"attr02-01\":\"value-02-01\",\n"
+" \"attr02-01\":\"value-02-02\"\n" + " \"attr02-01\":\"value-02-02\"\n"
+" }\n" + " }\n"
+" }\n" + " }\n"
+ "]"; + "]";
new JSONArray(str); new JSONArray(str);
fail("Expected an exception"); fail("Expected an exception");
@ -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");

View File

@ -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

View File

@ -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/"+

View File

@ -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));