Skip to content

Commit da757c6

Browse files
Sean LearySean Leary
authored andcommitted
pre-release-20260719 oops forgot to update new unit tests for strict mode
1 parent d24bc9e commit da757c6

1 file changed

Lines changed: 57 additions & 30 deletions

File tree

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3885,6 +3885,7 @@ public void jsonObjectParseFromJson_9() {
38853885

38863886
@Test
38873887
public void testMaxNumberLength() {
3888+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
38883889
StringBuilder sb = new StringBuilder();
38893890
for (int i = 0; i < 100; ++i) {
38903891
sb.append("9999999999");
@@ -3896,23 +3897,29 @@ public void testMaxNumberLength() {
38963897
// JSONArray with number just under the max length limit
38973898
checkJSONArrayMaxLen(sb.toString(), true, null);
38983899

3899-
// JSONObject with number just over the max length limit
3900-
checkJSONObjectMaxLen(sb + "9", false, null);
3900+
// numbers without quotes are not allowed in strict mode
3901+
if (!jsonParserConfiguration.isStrictMode()) {
3902+
// JSONObject with number just over the max length limit
3903+
checkJSONObjectMaxLen(sb + "9", false, null);
39013904

3902-
// JSONArray with number just over the max length limit
3903-
checkJSONArrayMaxLen(sb + "9", false, null);
3905+
// JSONArray with number just over the max length limit
3906+
checkJSONArrayMaxLen(sb + "9", false, null);
3907+
}
39043908

39053909
// JSONObject with number at config max length limit
39063910
checkJSONObjectMaxLen(sb.toString() + sb.toString(), true, new JSONParserConfiguration().withMaxNumberLength(2000));
39073911

39083912
// JSONArray with number at config max length limit
39093913
checkJSONArrayMaxLen((sb.toString() + sb), true, new JSONParserConfiguration().withMaxNumberLength(2000));
39103914

3911-
// JSONObject with number just over config max length limit
3912-
checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", false, new JSONParserConfiguration().withMaxNumberLength(2000));
3915+
// numbers without quotes are not allowed in strict mode
3916+
if (!jsonParserConfiguration.isStrictMode()) {
3917+
// JSONObject with number just over config max length limit
3918+
checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", false, new JSONParserConfiguration().withMaxNumberLength(2000));
39133919

3914-
// JSONArray with number just over config max length limit
3915-
checkJSONArrayMaxLen((sb.toString() + sb + "9"), false, new JSONParserConfiguration().withMaxNumberLength(2000));
3920+
// JSONArray with number just over config max length limit
3921+
checkJSONArrayMaxLen((sb.toString() + sb + "9"), false, new JSONParserConfiguration().withMaxNumberLength(2000));
3922+
}
39163923

39173924
// JSONObject with large number, no checks
39183925
checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", true,
@@ -3975,6 +3982,8 @@ private static void checkJSONArrayMaxLen(String s, boolean isValid, JSONParserCo
39753982
*/
39763983
@Test
39773984
public void testMaxNumberLengthNegativeInteger() {
3985+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
3986+
39783987
// Build a negative number string of exactly 1000 chars: "-" + 999 digits
39793988
StringBuilder sb = new StringBuilder("-");
39803989
for (int i = 0; i < 999; ++i) {
@@ -3985,17 +3994,22 @@ public void testMaxNumberLengthNegativeInteger() {
39853994
// at max length: parsed as number
39863995
checkJSONObjectMaxLen(sb.toString(), true, null);
39873996

3988-
// over max length: returned as string
3989-
sb.append("1");
3990-
assertEquals(1001, sb.length());
3991-
checkJSONObjectMaxLen(sb.toString(), false, null);
3997+
// numbers without quotes are not allowed in strict mode
3998+
if (!jsonParserConfiguration.isStrictMode()) {
3999+
// over max length: returned as string
4000+
sb.append("1");
4001+
assertEquals(1001, sb.length());
4002+
checkJSONObjectMaxLen(sb.toString(), false, null);
4003+
}
39924004
}
39934005

39944006
/**
39954007
* Test max number length for decimal notation strings via stringToValue.
39964008
*/
39974009
@Test
39984010
public void testMaxNumberLengthDecimal() {
4011+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
4012+
39994013
// Build a decimal number string of exactly 1000 chars: 499 digits + "." + 500
40004014
// digits
40014015
StringBuilder sb = new StringBuilder();
@@ -4011,17 +4025,22 @@ public void testMaxNumberLengthDecimal() {
40114025
// at max length: parsed as number (BigDecimal)
40124026
checkJSONObjectMaxLen(sb.toString(), true, null);
40134027

4014-
// over max length: returned as string
4015-
sb.append("3");
4016-
assertEquals(1001, sb.length());
4017-
checkJSONObjectMaxLen(sb.toString(), false, null);
4028+
// numbers without quotes are not allowed in strict mode
4029+
if (!jsonParserConfiguration.isStrictMode()) {
4030+
// over max length: returned as string
4031+
sb.append("3");
4032+
assertEquals(1001, sb.length());
4033+
checkJSONObjectMaxLen(sb.toString(), false, null);
4034+
}
40184035
}
40194036

40204037
/**
40214038
* Test max number length with scientific notation via stringToValue.
40224039
*/
40234040
@Test
40244041
public void testMaxNumberLengthScientificNotation() {
4042+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
4043+
40254044
// Build a scientific notation string of exactly 1000 chars
40264045
StringBuilder sb = new StringBuilder("1.");
40274046
for (int i = 0; i < 994; ++i) {
@@ -4038,9 +4057,13 @@ public void testMaxNumberLengthScientificNotation() {
40384057
for (int i = 0; i < 995; ++i) {
40394058
sb.append("0");
40404059
}
4041-
sb.append("e100");
4042-
assertEquals(1001, sb.length());
4043-
checkJSONObjectMaxLen(sb.toString(), false, null);
4060+
4061+
// numbers without quotes are not allowed in strict mode
4062+
if (!jsonParserConfiguration.isStrictMode()) {
4063+
sb.append("e100");
4064+
assertEquals(1001, sb.length());
4065+
checkJSONObjectMaxLen(sb.toString(), false, null);
4066+
}
40444067
}
40454068

40464069
/**
@@ -4297,17 +4320,21 @@ public void testStringToValueViaJSONObject() {
42974320
*/
42984321
@Test
42994322
public void testStringToNumberInvalidFormats() {
4300-
// Leading zero followed by digit → treated as string (not octal)
4301-
JSONObject jo = new JSONObject("{\"a\": 01}");
4302-
// JSONTokener with strict mode would reject, but default mode stores as string
4303-
// since stringToNumber throws NumberFormatException for "01"
4304-
Object val = jo.get("a");
4305-
assertTrue("01 should be stored as string, got " + val.getClass(), val instanceof String);
4306-
4307-
// Negative with leading zero → "-01"
4308-
jo = new JSONObject("{\"a\": -01}");
4309-
val = jo.get("a");
4310-
assertTrue("-01 should be stored as string, got " + val.getClass(), val instanceof String);
4323+
// this test should only run in non-strict mode, otherwise it will throw an exception
4324+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
4325+
if (!jsonParserConfiguration.isStrictMode()) {
4326+
// Leading zero followed by digit → treated as string (not octal)
4327+
JSONObject jo = new JSONObject("{\"a\": 01}");
4328+
// JSONTokener with strict mode would reject, but default mode stores as string
4329+
// since stringToNumber throws NumberFormatException for "01"
4330+
Object val = jo.get("a");
4331+
assertTrue("01 should be stored as string, got " + val.getClass(), val instanceof String);
4332+
4333+
// Negative with leading zero → "-01"
4334+
jo = new JSONObject("{\"a\": -01}");
4335+
val = jo.get("a");
4336+
assertTrue("-01 should be stored as string, got " + val.getClass(), val instanceof String);
4337+
}
43114338
}
43124339

43134340
}

0 commit comments

Comments
 (0)