mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Merge pull request #999 from marilynel/master
fixed some strict mode issues
This commit is contained in:
commit
0a9364e920
@ -213,6 +213,7 @@ public class JSONObject {
|
|||||||
this();
|
this();
|
||||||
char c;
|
char c;
|
||||||
String key;
|
String key;
|
||||||
|
Object obj;
|
||||||
|
|
||||||
boolean isInitial = x.getPrevious() == 0;
|
boolean isInitial = x.getPrevious() == 0;
|
||||||
|
|
||||||
@ -230,7 +231,20 @@ public class JSONObject {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
default:
|
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 ':'.
|
// The key is followed by ':'.
|
||||||
|
@ -511,11 +511,21 @@ public class JSONTokener {
|
|||||||
throw this.syntaxError("Missing value");
|
throw this.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
Object obj = JSONObject.stringToValue(string);
|
Object obj = JSONObject.stringToValue(string);
|
||||||
// Strict mode only allows strings with explicit double quotes
|
// if obj is a boolean, look at string
|
||||||
if (jsonParserConfiguration != null &&
|
if (jsonParserConfiguration != null &&
|
||||||
jsonParserConfiguration.isStrictMode() &&
|
jsonParserConfiguration.isStrictMode()) {
|
||||||
obj instanceof String) {
|
if (obj instanceof Boolean && !"true".equals(string) && !"false".equals(string)) {
|
||||||
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
|
// Strict mode only allows lowercase true or false
|
||||||
|
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase boolean", obj));
|
||||||
|
}
|
||||||
|
else if (obj == JSONObject.NULL && !"null".equals(string)) {
|
||||||
|
// Strint mode only allows lowercase null
|
||||||
|
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not lowercase null", obj));
|
||||||
|
}
|
||||||
|
else if (obj instanceof String) {
|
||||||
|
// Strict mode only allows strings with explicit double quotes
|
||||||
|
throw this.syntaxError(String.format("Strict mode error: Value '%s' is not surrounded by quotes", obj));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
@ -3997,6 +3997,45 @@ public class JSONObjectTest {
|
|||||||
assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
|
assertThrows(JSONException.class, () -> { new JSONObject(tokener); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_strictModeWithMisCasedBooleanOrNullValue(){
|
||||||
|
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) { }
|
||||||
|
try{
|
||||||
|
JSONObject j2 = new JSONObject("{\"a\":nUlL}", 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
|
* Method to build nested map of max maxDepth
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user