fixed some strict mode issues 980

This commit is contained in:
marilynel 2025-07-13 12:41:17 -08:00
parent f0a78aff61
commit fdaeb486ed
3 changed files with 59 additions and 1 deletions

View File

@ -213,6 +213,7 @@ public class JSONObject {
this();
char c;
String key;
Object obj;
boolean isInitial = x.getPrevious() == 0;
@ -230,7 +231,20 @@ public class JSONObject {
}
return;
default:
key = x.nextSimpleValue(c).toString();
obj = x.nextSimpleValue(c);
key = obj.toString();
}
if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) {
if(obj instanceof Boolean) {
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be boolean", key));
}
if(obj == JSONObject.NULL) {
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be null", key));
}
if(obj instanceof Number) {
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be number", key));
}
}
// The key is followed by ':'.

View File

@ -511,6 +511,15 @@ public class JSONTokener {
throw this.syntaxError("Missing value");
}
Object obj = JSONObject.stringToValue(string);
// if obj is a boolean, look at string
if (jsonParserConfiguration != null &&
jsonParserConfiguration.isStrictMode() && obj instanceof Boolean) {
if (!"true".equals(string) && !"false".equals(string)) {
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase boolean", obj));
}
}
// Strict mode only allows strings with explicit double quotes
if (jsonParserConfiguration != null &&
jsonParserConfiguration.isStrictMode() &&

View File

@ -3997,6 +3997,41 @@ public class JSONObjectTest {
assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
}
@Test
public void test_strictModeWithMisCasedBooleanValue(){
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
try{
JSONObject j1 = new JSONObject("{\"a\":True}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
try{
JSONObject j2 = new JSONObject("{\"a\":TRUE}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
}
@Test
public void test_strictModeWithInappropriateKey(){
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
// Parsing the following objects should fail
try{
JSONObject j3 = new JSONObject("{true : 3}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
try{
JSONObject j4 = new JSONObject("{TRUE : 3}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
try{
JSONObject j5 = new JSONObject("{1 : 3}", jsonParserConfiguration);
fail("Expected an exception");
} catch (JSONException e) { }
}
/**
* Method to build nested map of max maxDepth
*