Skip to content

Commit 0a9364e

Browse files
authored
Merge pull request #999 from marilynel/master
fixed some strict mode issues
2 parents f0a78af + c91b728 commit 0a9364e

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,21 @@ Object nextSimpleValue(char c) {
511511
throw this.syntaxError("Missing value");
512512
}
513513
Object obj = JSONObject.stringToValue(string);
514-
// Strict mode only allows strings with explicit double quotes
514+
// if obj is a boolean, look at string
515515
if (jsonParserConfiguration != null &&
516-
jsonParserConfiguration.isStrictMode() &&
517-
obj instanceof String) {
518-
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
516+
jsonParserConfiguration.isStrictMode()) {
517+
if (obj instanceof Boolean && !"true".equals(string) && !"false".equals(string)) {
518+
// Strict mode only allows lowercase true or false
519+
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase boolean", obj));
520+
}
521+
else if (obj == JSONObject.NULL && !"null".equals(string)) {
522+
// Strint mode only allows lowercase null
523+
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase null", obj));
524+
}
525+
else if (obj instanceof String) {
526+
// Strict mode only allows strings with explicit double quotes
527+
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
528+
}
519529
}
520530
return obj;
521531
}

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

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

4000+
@Test
4001+
public void test_strictModeWithMisCasedBooleanOrNullValue(){
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+
try{
4013+
JSONObject j2 = new JSONObject("{\"a\":nUlL}", jsonParserConfiguration);
4014+
fail("Expected an exception");
4015+
} catch (JSONException e) { }
4016+
}
4017+
4018+
@Test
4019+
public void test_strictModeWithInappropriateKey(){
4020+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration().withStrictMode();
4021+
4022+
// Parsing the following objects should fail
4023+
try{
4024+
JSONObject j3 = new JSONObject("{true : 3}", jsonParserConfiguration);
4025+
fail("Expected an exception");
4026+
} catch (JSONException e) { }
4027+
try{
4028+
JSONObject j4 = new JSONObject("{TRUE : 3}", jsonParserConfiguration);
4029+
fail("Expected an exception");
4030+
} catch (JSONException e) { }
4031+
try{
4032+
JSONObject j5 = new JSONObject("{1 : 3}", jsonParserConfiguration);
4033+
fail("Expected an exception");
4034+
} catch (JSONException e) { }
4035+
4036+
}
4037+
4038+
40004039
/**
40014040
* Method to build nested map of max maxDepth
40024041
*

0 commit comments

Comments
 (0)