| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -213,6 +213,7 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration | |||
| 213 | 213 | this(); | |
| 214 | 214 | char c; | |
| 215 | 215 | String key; | |
| 216 | + Object obj; | ||
| 216 | 217 | ||
| 217 | 218 | boolean isInitial = x.getPrevious() == 0; | |
| 218 | 219 | ||
@@ -230,7 +231,20 @@ public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration | |||
| 230 | 231 | } | |
| 231 | 232 | return; | |
| 232 | 233 | 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 | + } | ||
| 234 | 248 | } | |
| 235 | 249 | ||
| 236 | 250 | // The key is followed by ':'. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -511,11 +511,21 @@ Object nextSimpleValue(char c) { | |||
| 511 | 511 | throw this.syntaxError("Missing value"); | |
| 512 | 512 | } | |
| 513 | 513 | Object obj = JSONObject.stringToValue(string); | |
| 514 | - // Strict mode only allows strings with explicit double quotes | ||
| 514 | + // if obj is a boolean, look at string | ||
| 515 | 515 | 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 | + } | ||
| 519 | 529 | } | |
| 520 | 530 | return obj; | |
| 521 | 531 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3997,6 +3997,45 @@ public void testStrictModeJSONTokener_expectException(){ | |||
| 3997 | 3997 | assertThrows(JSONException.class, () -> { new JSONObject(tokener); }); | |
| 3998 | 3998 | } | |
| 3999 | 3999 | ||
| 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 | + | ||
| 4000 | 4039 | /** | |
| 4001 | 4040 | * Method to build nested map of max maxDepth | |
| 4002 | 4041 | * | |
| Back | FazBrowse Home | New Git URL |
0 commit comments