Skip to content

Commit fdaeb48

Browse files
committed
fixed some strict mode issues 980
1 parent f0a78af commit fdaeb48

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
213213
this();
214214
char c;
215215
String key;
216+
Object obj;
216217

217218
boolean isInitial = x.getPrevious() == 0;
218219

@@ -230,7 +231,20 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration
230231
}
231232
return;
232233
default:
233-
key = x.nextSimpleValue(c).toString();
234+
obj = x.nextSimpleValue(c);
235+
key = obj.toString();
236+
}
237+
238+
if (jsonParserConfiguration != null && jsonParserConfiguration.isStrictMode()) {
239+
if(obj instanceof Boolean) {
240+
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be boolean", key));
241+
}
242+
if(obj == JSONObject.NULL) {
243+
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be null", key));
244+
}
245+
if(obj instanceof Number) {
246+
throw x.syntaxError(String.format("Strict mode error: key '%s' cannot be number", key));
247+
}
234248
}
235249

236250
// The key is followed by ':'.

src/main/java/org/json/JSONTokener.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,15 @@ Object nextSimpleValue(char c) {
511511
throw this.syntaxError("Missing value");
512512
}
513513
Object obj = JSONObject.stringToValue(string);
514+
// if obj is a boolean, look at string
515+
if (jsonParserConfiguration != null &&
516+
jsonParserConfiguration.isStrictMode() && obj instanceof Boolean) {
517+
if (!"true".equals(string) && !"false".equals(string)) {
518+
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase boolean", obj));
519+
}
520+
}
521+
522+
514523
// Strict mode only allows strings with explicit double quotes
515524
if (jsonParserConfiguration != null &&
516525
jsonParserConfiguration.isStrictMode() &&

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3997,6 +3997,41 @@ public void testStrictModeJSONTokener_expectException(){
39973997
assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
39983998
}
39993999

4000+
@Test
4001+
public void test_strictModeWithMisCasedBooleanValue(){
4002+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
4003+
4004+
try{
4005+
JSONObject j1 = new JSONObject("{\"a\":True}", jsonParserConfiguration);
4006+
fail("Expected an exception");
4007+
} catch (JSONException e) { }
4008+
try{
4009+
JSONObject j2 = new JSONObject("{\"a\":TRUE}", jsonParserConfiguration);
4010+
fail("Expected an exception");
4011+
} catch (JSONException e) { }
4012+
}
4013+
4014+
@Test
4015+
public void test_strictModeWithInappropriateKey(){
4016+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
4017+
4018+
// Parsing the following objects should fail
4019+
try{
4020+
JSONObject j3 = new JSONObject("{true : 3}", jsonParserConfiguration);
4021+
fail("Expected an exception");
4022+
} catch (JSONException e) { }
4023+
try{
4024+
JSONObject j4 = new JSONObject("{TRUE : 3}", jsonParserConfiguration);
4025+
fail("Expected an exception");
4026+
} catch (JSONException e) { }
4027+
try{
4028+
JSONObject j5 = new JSONObject("{1 : 3}", jsonParserConfiguration);
4029+
fail("Expected an exception");
4030+
} catch (JSONException e) { }
4031+
4032+
}
4033+
4034+
40004035
/**
40014036
* Method to build nested map of max maxDepth
40024037
*

0 commit comments

Comments
 (0)