mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 11:05:28 -04:00
restore-jsonparserconfiguration: fix unit tests to work when strictMode default is true
This commit is contained in:
parent
09536cd6c8
commit
d3c7eaf17e
@ -29,7 +29,7 @@ public class CDLTest {
|
||||
"1, 2, 3, 4\t, 5, 6, 7\n" +
|
||||
"true, false, true, true, false, false, false\n" +
|
||||
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
|
||||
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va'l6, val7\n";
|
||||
"\"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
|
||||
* might normally convert the value into a non-string.
|
||||
*/
|
||||
private static final String EXPECTED_LINES = "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, " +
|
||||
"{Col 1:\"1\", Col 2:\"2\", Col 3:\"3\", Col 4:\"4\", Col 5:\"5\", Col 6:\"6\", Col 7:\"7\"}, " +
|
||||
"{Col 1:\"true\", Col 2:\"false\", Col 3:\"true\", Col 4:\"true\", Col 5:\"false\", Col 6:\"false\", Col 7:\"false\"}, " +
|
||||
"{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, " +
|
||||
"{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va'l6, Col 7:val7}]";
|
||||
private static final String EXPECTED_LINES =
|
||||
"[ " +
|
||||
"{" +
|
||||
"\"Col 1\":\"val1\", " +
|
||||
"\"Col 2\":\"val2\", " +
|
||||
"\"Col 3\":\"val3\", " +
|
||||
"\"Col 4\":\"val4\", " +
|
||||
"\"Col 5\":\"val5\", " +
|
||||
"\"Col 6\":\"val6\", " +
|
||||
"\"Col 7\":\"val7\"" +
|
||||
"}, " +
|
||||
" {" +
|
||||
"\"Col 1\":\"1\", " +
|
||||
"\"Col 2\":\"2\", " +
|
||||
"\"Col 3\":\"3\", " +
|
||||
"\"Col 4\":\"4\", " +
|
||||
"\"Col 5\":\"5\", " +
|
||||
"\"Col 6\":\"6\", " +
|
||||
"\"Col 7\":\"7\"" +
|
||||
"}, " +
|
||||
" {" +
|
||||
"\"Col 1\":\"true\", " +
|
||||
"\"Col 2\":\"false\", " +
|
||||
"\"Col 3\":\"true\", " +
|
||||
"\"Col 4\":\"true\", " +
|
||||
"\"Col 5\":\"false\", " +
|
||||
"\"Col 6\":\"false\", " +
|
||||
"\"Col 7\":\"false\"" +
|
||||
"}, " +
|
||||
"{" +
|
||||
"\"Col 1\":\"0.23\", " +
|
||||
"\"Col 2\":\"57.42\", " +
|
||||
"\"Col 3\":\"5e27\", " +
|
||||
"\"Col 4\":\"-234.879\", " +
|
||||
"\"Col 5\":\"2.34e5\", " +
|
||||
"\"Col 6\":\"0.0\", " +
|
||||
"\"Col 7\":\"9e-3\"" +
|
||||
"}, " +
|
||||
"{" +
|
||||
"\"Col 1\":\"va\tl1\", " +
|
||||
"\"Col 2\":\"v\bal2\", " +
|
||||
"\"Col 3\":\"val3\", " +
|
||||
"\"Col 4\":\"val\f4\", " +
|
||||
"\"Col 5\":\"val5\", " +
|
||||
"\"Col 6\":\"va'l6\", " +
|
||||
"\"Col 7\":\"val7\"" +
|
||||
"}" +
|
||||
"]";
|
||||
|
||||
/**
|
||||
* Attempts to create a JSONArray from a null string.
|
||||
@ -283,11 +326,11 @@ public class CDLTest {
|
||||
*/
|
||||
@Test
|
||||
public void jsonArrayToJSONArray() {
|
||||
String nameArrayStr = "[Col1, Col2]";
|
||||
String nameArrayStr = "[\"Col1\", \"Col2\"]";
|
||||
String values = "V1, V2";
|
||||
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
|
||||
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
|
||||
JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]");
|
||||
JSONArray expectedJsonArray = new JSONArray("[{\"Col1\":\"V1\",\"Col2\":\"V2\"}]");
|
||||
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
|
||||
}
|
||||
|
||||
|
@ -476,11 +476,16 @@ public class JSONArrayTest {
|
||||
*/
|
||||
@Test
|
||||
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]";
|
||||
JSONArray jsonArray = new JSONArray(str);
|
||||
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
|
||||
assertEquals(expected, jsonArray.toList());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exercise JSONArray.join() by converting a JSONArray into a
|
||||
@ -690,8 +695,8 @@ public class JSONArrayTest {
|
||||
|
||||
String jsonArrayStr =
|
||||
"["+
|
||||
"hello,"+
|
||||
"world"+
|
||||
"\"hello\","+
|
||||
"\"world\""+
|
||||
"]";
|
||||
// 2
|
||||
jsonArray.put(new JSONArray(jsonArrayStr));
|
||||
@ -768,8 +773,8 @@ public class JSONArrayTest {
|
||||
|
||||
String jsonArrayStr =
|
||||
"["+
|
||||
"hello,"+
|
||||
"world"+
|
||||
"\"hello\","+
|
||||
"\"world\""+
|
||||
"]";
|
||||
// 2
|
||||
jsonArray.put(2, new JSONArray(jsonArrayStr));
|
||||
|
@ -625,7 +625,7 @@ public class JSONMLTest {
|
||||
"\"subValue\","+
|
||||
"{\"svAttr\":\"svValue\"},"+
|
||||
"\"abc\""+
|
||||
"],"+
|
||||
"]"+
|
||||
"],"+
|
||||
"[\"value\",3],"+
|
||||
"[\"value\",4.1],"+
|
||||
|
@ -23,18 +23,18 @@ public class JSONObjectNumberTest {
|
||||
@Parameters(name = "{index}: {0}")
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{"{value:50}", 1},
|
||||
{"{value:50.0}", 1},
|
||||
{"{value:5e1}", 1},
|
||||
{"{value:5E1}", 1},
|
||||
{"{value:5e1}", 1},
|
||||
{"{value:'50'}", 1},
|
||||
{"{value:-50}", -1},
|
||||
{"{value:-50.0}", -1},
|
||||
{"{value:-5e1}", -1},
|
||||
{"{value:-5E1}", -1},
|
||||
{"{value:-5e1}", -1},
|
||||
{"{value:'-50'}", -1}
|
||||
{"{\"value\":50}", 1},
|
||||
{"{\"value\":50.0}", 1},
|
||||
{"{\"value\":5e1}", 1},
|
||||
{"{\"value\":5E1}", 1},
|
||||
{"{\"value\":5e1}", 1},
|
||||
{"{\"value\":\"50\"}", 1},
|
||||
{"{\"value\":-50}", -1},
|
||||
{"{\"value\":-50.0}", -1},
|
||||
{"{\"value\":-5e1}", -1},
|
||||
{"{\"value\":-5E1}", -1},
|
||||
{"{\"value\":-5e1}", -1},
|
||||
{"{\"value\":\"-50\"}", -1}
|
||||
// JSON does not support octal or hex numbers;
|
||||
// see https://stackoverflow.com/a/52671839/6323312
|
||||
// "{value:062}", // octal 50
|
||||
|
@ -218,6 +218,10 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
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!}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
String textStr = jsonObject.toString();
|
||||
@ -231,6 +235,7 @@ public class JSONObjectTest {
|
||||
assertTrue("expected something!", textStr.contains("\"something!\""));
|
||||
Util.checkJSONObjectMaps(jsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongFromString(){
|
||||
@ -1069,6 +1074,10 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
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
|
||||
String str =
|
||||
"{" +
|
||||
@ -1112,6 +1121,7 @@ public class JSONObjectTest {
|
||||
jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
|
||||
Util.checkJSONObjectMaps(jsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests how JSONObject get[type] handles incorrect types
|
||||
@ -1528,7 +1538,7 @@ public class JSONObjectTest {
|
||||
"{"+
|
||||
"\"trueKey\":true,"+
|
||||
"\"falseKey\":false,"+
|
||||
"\"stringKey\":\"hello world!\","+
|
||||
"\"stringKey\":\"hello world!\""+
|
||||
"}";
|
||||
JSONObject jsonObject2 = new JSONObject(str);
|
||||
names = JSONObject.getNames(jsonObject2);
|
||||
@ -1623,7 +1633,7 @@ public class JSONObjectTest {
|
||||
"{"+
|
||||
"\"trueKey\":true,"+
|
||||
"\"falseKey\":false,"+
|
||||
"\"stringKey\":\"hello world!\","+
|
||||
"\"stringKey\":\"hello world!\""+
|
||||
"}";
|
||||
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
@ -2262,6 +2272,10 @@ public class JSONObjectTest {
|
||||
@SuppressWarnings({"boxing", "unused"})
|
||||
@Test
|
||||
public void jsonObjectParsingErrors() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||
if (jsonParserConfiguration.isStrictMode()) {
|
||||
System.out.println("Skipping JSONObjectTest jaonObjectParsingErrors() when strictMode default is true");
|
||||
} else {
|
||||
try {
|
||||
// does not start with '{'
|
||||
String str = "abc";
|
||||
@ -2579,6 +2593,7 @@ public class JSONObjectTest {
|
||||
e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
// After reverting the code, personId is stored as a string, and the behavior is as expected
|
||||
String personId = "0123";
|
||||
JSONObject j1 = new JSONObject("{personId: " + personId + "}");
|
||||
String personId = "\"0123\"";
|
||||
JSONObject j1 = new JSONObject("{\"personId\": " + personId + "}");
|
||||
assertEquals(j1.getString("personId"), "0123");
|
||||
|
||||
// Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number.
|
||||
// This example was mentioned in the same ticket
|
||||
// After reverting the code, personId is stored as a string, and the behavior is as expected
|
||||
JSONObject j2 = new JSONObject("{\"personId\":0123}");
|
||||
JSONObject j2 = new JSONObject("{\"personId\":\"0123\"}");
|
||||
assertEquals(j2.getString("personId"), "0123");
|
||||
|
||||
// Behavior uncovered while working on the code
|
||||
// All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect
|
||||
JSONObject j3 = new JSONObject("{ " +
|
||||
"\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", " +
|
||||
"\"hex4\": 00e0, \"hex5\": 00f0, \"hex6\": 0011 }");
|
||||
"\"hex4\": 00e0, \"hex5\": \"00f0\", \"hex6\": \"0011\" }");
|
||||
assertEquals(j3.getString("hex1"), "010e4");
|
||||
assertEquals(j3.getString("hex2"), "00f0");
|
||||
assertEquals(j3.getString("hex3"), "0011");
|
||||
|
@ -384,8 +384,7 @@ public class JSONPointerTest {
|
||||
String str = "{"+
|
||||
"\"string\\\\\\\\Key\":\"hello world!\","+
|
||||
|
||||
"\"\\\\\":\"slash test\"," +
|
||||
"}"+
|
||||
"\"\\\\\":\"slash test\"" +
|
||||
"}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
|
||||
|
@ -270,9 +270,9 @@ public class XMLConfigurationTest {
|
||||
|
||||
String expectedStr =
|
||||
"{\"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"+
|
||||
"\"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"+
|
||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||
|
@ -267,9 +267,9 @@ public class XMLTest {
|
||||
|
||||
String expectedStr =
|
||||
"{\"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"+
|
||||
"\"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"+
|
||||
"},\"xsi:noNamespaceSchemaLocation\":"+
|
||||
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
|
||||
@ -1180,7 +1180,7 @@ public class XMLTest {
|
||||
|
||||
@Test
|
||||
public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){
|
||||
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
|
||||
String jsonString = "{\"outer\":{\"innerOne\":\"\", \"innerTwo\":\"two\"}}";
|
||||
JSONObject jsonObject = new JSONObject(jsonString);
|
||||
String expectedXmlString = "<encloser><outer><innerOne></innerOne><innerTwo>two</innerTwo></outer></encloser>";
|
||||
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true));
|
||||
@ -1191,7 +1191,7 @@ public class XMLTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){
|
||||
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
|
||||
String jsonString = "{\"outer\":{\"innerOne\":\"\", \"innerTwo\":\"two\"}}";
|
||||
JSONObject jsonObject = new JSONObject(jsonString);
|
||||
String expectedXmlString = "<encloser><outer><innerOne/><innerTwo>two</innerTwo></outer></encloser>";
|
||||
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false));
|
||||
|
Loading…
x
Reference in New Issue
Block a user