mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
restore-jsonparserconfiguration: add jsonobject strict tests. Detect semicolon instead of colon separator in object
This commit is contained in:
parent
1f0729cadb
commit
09536cd6c8
@ -268,8 +268,14 @@ public class JSONObject {
|
|||||||
|
|
||||||
switch (x.nextClean()) {
|
switch (x.nextClean()) {
|
||||||
case ';':
|
case ';':
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
throw x.syntaxError("Invalid character ';' found in object in strict mode");
|
||||||
|
}
|
||||||
case ',':
|
case ',':
|
||||||
if (x.nextClean() == '}') {
|
if (x.nextClean() == '}') {
|
||||||
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
|
throw x.syntaxError("Expected another object element");
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (x.end()) {
|
if (x.end()) {
|
||||||
@ -468,6 +474,13 @@ public class JSONObject {
|
|||||||
*/
|
*/
|
||||||
public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
public JSONObject(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
|
||||||
this(new JSONTokener(source), jsonParserConfiguration);
|
this(new JSONTokener(source), jsonParserConfiguration);
|
||||||
|
if (this.jsonParserConfiguration.isStrictMode()) {
|
||||||
|
char c = jsonTokener.nextClean();
|
||||||
|
if (c != 0) {
|
||||||
|
throw jsonTokener.syntaxError(String.format("invalid character '%s' found after end of array", c));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,7 +4,6 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONParserConfiguration;
|
import org.json.JSONParserConfiguration;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -54,19 +53,37 @@ public class JSONParserConfigurationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInvalidInputArrays_testStrictModeTrue_shouldThrowJsonException() {
|
public void givenInvalidInput_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
List<String> strictModeInputTestCases = getNonCompliantJSONArrayList();
|
||||||
List<String> strictModeInputTestCases = getNonCompliantJSONList();
|
|
||||||
|
|
||||||
// this is a lot easier to debug when things stop working
|
// this is a lot easier to debug when things stop working
|
||||||
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
String testCase = strictModeInputTestCases.get(i);
|
String testCase = strictModeInputTestCases.get(i);
|
||||||
try {
|
try {
|
||||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
String s = jsonArray.toString();
|
String s = jsonArray.toString();
|
||||||
System.out.println("Expected an exception, but got: " + s + " Noncompliant Array index: " + i);
|
String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
|
||||||
|
fail(msg);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// its all good
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObjects_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
List<String> strictModeInputTestCases = getNonCompliantJSONObjectList();
|
||||||
|
// this is a lot easier to debug when things stop working
|
||||||
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
|
String testCase = strictModeInputTestCases.get(i);
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
String s = jsonObject.toString();
|
||||||
|
String msg = "Expected an exception, but got: " + s + " Noncompliant Array index: " + i;
|
||||||
|
fail(msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// its all good
|
// its all good
|
||||||
}
|
}
|
||||||
@ -77,16 +94,22 @@ public class JSONParserConfigurationTest {
|
|||||||
public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
String testCase = "[]";
|
String testCase = "[]";
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
|
||||||
assertEquals(testCase, jsonArray.toString());
|
assertEquals(testCase, jsonArray.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidDoubleArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
public void givenEmptyObject_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{}";
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidNestedArray_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
@ -103,48 +126,90 @@ public class JSONParserConfigurationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJsonException(){
|
public void givenValidNestedObject_testStrictModeTrue_shouldNotThrowJsonException() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCase = "{\"a0\":[\"c\"], \"a1\":[10.2], \"a2\":[true, false, true]}";
|
||||||
|
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
JSONArray arrayShouldContainStringAt0 = jsonObject.getJSONArray("a0");
|
||||||
|
JSONArray arrayShouldContainNumberAt0 = jsonObject.getJSONArray("a1");
|
||||||
|
JSONArray arrayShouldContainBooleanAt0 = jsonObject.getJSONArray("a2");
|
||||||
|
|
||||||
|
assertTrue(arrayShouldContainStringAt0.get(0) instanceof String);
|
||||||
|
assertTrue(arrayShouldContainNumberAt0.get(0) instanceof Number);
|
||||||
|
assertTrue(arrayShouldContainBooleanAt0.get(0) instanceof Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidEmptyArrayInsideArray_testStrictModeTrue_shouldNotThrowJsonException(){
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
String testCase = "[[]]";
|
String testCase = "[[]]";
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
|
||||||
assertEquals(testCase, jsonArray.toString());
|
assertEquals(testCase, jsonArray.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidEmptyArrayInsideObject_testStrictModeTrue_shouldNotThrowJsonException(){
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":[]}";
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidEmptyArrayInsideArray_testStrictModeFalse_shouldNotThrowJsonException(){
|
public void givenValidEmptyArrayInsideArray_testStrictModeFalse_shouldNotThrowJsonException(){
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(false);
|
.withStrictMode(false);
|
||||||
|
|
||||||
String testCase = "[[]]";
|
String testCase = "[[]]";
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
|
|
||||||
assertEquals(testCase, jsonArray.toString());
|
assertEquals(testCase, jsonArray.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInvalidString_testStrictModeTrue_shouldThrowJsonException() {
|
public void givenValidEmptyArrayInsideObject_testStrictModeFalse_shouldNotThrowJsonException(){
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
String testCase = "{\"a0\":[]}";
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
assertEquals(testCase, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidStringArray_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
String testCase = "[badString]";
|
String testCase = "[badString]";
|
||||||
|
|
||||||
JSONException je = assertThrows(JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
JSONException je = assertThrows(JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
assertEquals("Value 'badString' is not surrounded by quotes at 10 [character 11 line 1]", je.getMessage());
|
assertEquals("Value 'badString' is not surrounded by quotes at 10 [character 11 line 1]", je.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void allowNullInStrictMode() {
|
public void givenInvalidStringObject_testStrictModeTrue_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":badString}";
|
||||||
|
JSONException je = assertThrows(JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Value 'badString' is not surrounded by quotes at 15 [character 16 line 1]", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void allowNullArrayInStrictMode() {
|
||||||
String expected = "[null]";
|
String expected = "[null]";
|
||||||
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
|
JSONArray jsonArray = new JSONArray(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
assertEquals(expected, jsonArray.toString());
|
assertEquals(expected, jsonArray.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void allowNullObjectInStrictMode() {
|
||||||
|
String expected = "{\"a0\":null}";
|
||||||
|
JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
|
assertEquals(expected, jsonObject.toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleNumericArray() {
|
public void shouldHandleNumericArray() {
|
||||||
String expected = "[10]";
|
String expected = "[10]";
|
||||||
@ -152,16 +217,31 @@ public class JSONParserConfigurationTest {
|
|||||||
assertEquals(expected, jsonArray.toString());
|
assertEquals(expected, jsonArray.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleNumericObject() {
|
||||||
|
String expected = "{\"a0\":10}";
|
||||||
|
JSONObject jsonObject = new JSONObject(expected, new JSONParserConfiguration().withStrictMode(true));
|
||||||
|
assertEquals(expected, jsonObject.toString());
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
public void givenCompliantJSONArrayFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
||||||
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
|
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonArray.json"))) {
|
||||||
String compliantJsonArrayAsString = lines.collect(Collectors.joining());
|
String compliantJsonArrayAsString = lines.collect(Collectors.joining());
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration);
|
new JSONArray(compliantJsonArrayAsString, jsonParserConfiguration);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCompliantJSONObjectFile_testStrictModeTrue_shouldNotThrowAnyException() throws IOException {
|
||||||
|
try (Stream<String> lines = Files.lines(Paths.get("src/test/resources/compliantJsonObject.json"))) {
|
||||||
|
String compliantJsonObjectAsString = lines.collect(Collectors.joining());
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
new JSONObject(compliantJsonObjectAsString, jsonParserConfiguration);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -169,7 +249,7 @@ public class JSONParserConfigurationTest {
|
|||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(false);
|
.withStrictMode(false);
|
||||||
|
|
||||||
List<String> strictModeInputTestCases = getNonCompliantJSONList();
|
List<String> strictModeInputTestCases = getNonCompliantJSONArrayList();
|
||||||
|
|
||||||
// this is a lot easier to debug when things stop working
|
// this is a lot easier to debug when things stop working
|
||||||
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
@ -178,62 +258,108 @@ public class JSONParserConfigurationTest {
|
|||||||
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
|
System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
|
||||||
|
fail(String.format("Noncompliant array index: %d", i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObjects_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
|
||||||
|
List<String> strictModeInputTestCases = getNonCompliantJSONObjectList();
|
||||||
|
|
||||||
|
// this is a lot easier to debug when things stop working
|
||||||
|
for (int i = 0; i < strictModeInputTestCases.size(); ++i) {
|
||||||
|
String testCase = strictModeInputTestCases.get(i);
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Unexpected exception: " + e.getMessage() + " Noncompliant Array index: " + i);
|
||||||
|
fail(String.format("Noncompliant array index: %d", i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strictModeInputTestCases.forEach(testCase -> new JSONArray(testCase, jsonParserConfiguration));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
String testCase = "[1,2];[3,4]";
|
String testCase = "[1,2];[3,4]";
|
||||||
|
|
||||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
assertEquals("invalid character ';' found after end of array at 6 [character 7 line 1]", je.getMessage());
|
assertEquals("invalid character ';' found after end of array at 6 [character 7 line 1]", je.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":[1,2];\"a1\":[3,4]}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Invalid character ';' found in object in strict mode at 12 [character 13 line 1]", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
public void givenInvalidInputArrayWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
String testCase = "[\"1\",\"2\"];[3,4]";
|
String testCase = "[\"1\",\"2\"];[3,4]";
|
||||||
|
|
||||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
assertEquals("invalid character ';' found after end of array at 10 [character 11 line 1]", je.getMessage());
|
assertEquals("invalid character ';' found after end of array at 10 [character 11 line 1]", je.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObjectWithNumericStrings_testStrictModeTrue_shouldThrowInvalidCharacterErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":[\"1\",\"2\"];\"a1\":[3,4]}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Invalid character ';' found in object in strict mode at 16 [character 17 line 1]", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
String testCase = "[{\"test\": implied}]";
|
String testCase = "[{\"test\": implied}]";
|
||||||
|
|
||||||
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
JSONException.class, () -> new JSONArray(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
assertEquals("Value 'implied' is not surrounded by quotes at 17 [character 18 line 1]", je.getMessage());
|
assertEquals("Value 'implied' is not surrounded by quotes at 17 [character 18 line 1]", je.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowValueNotSurroundedByQuotesErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
String testCase = "{\"a0\":{\"test\": implied}]}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant array but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
assertEquals("Value 'implied' is not surrounded by quotes at 22 [character 23 line 1]", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() {
|
public void givenInvalidInputArray_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(false);
|
.withStrictMode(false);
|
||||||
|
|
||||||
String testCase = "[{\"test\": implied}]";
|
String testCase = "[{\"test\": implied}]";
|
||||||
|
|
||||||
new JSONArray(testCase, jsonParserConfiguration);
|
new JSONArray(testCase, jsonParserConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNonCompliantQuotes_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
|
public void givenInvalidInputObject_testStrictModeFalse_shouldNotThrowAnyException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
String testCase = "{\"a0\":{\"test\": implied}}";
|
||||||
|
new JSONObject(testCase, jsonParserConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonCompliantQuotesArray_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(true);
|
.withStrictMode(true);
|
||||||
|
|
||||||
@ -266,7 +392,40 @@ public class JSONParserConfigurationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUnbalancedQuotes_testStrictModeFalse_shouldThrowJsonException() {
|
public void givenNonCompliantQuotesObject_testStrictModeTrue_shouldThrowJsonExceptionWithConcreteErrorDescription() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCaseOne = "{\"abc': \"test\"}";
|
||||||
|
String testCaseTwo = "{'abc\": \"test\"}";
|
||||||
|
String testCaseThree = "{\"a\":'abc'}";
|
||||||
|
String testCaseFour = "{'testField': \"testValue\"}";
|
||||||
|
|
||||||
|
JSONException jeOne = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseOne, jsonParserConfiguration));
|
||||||
|
JSONException jeTwo = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseTwo, jsonParserConfiguration));
|
||||||
|
JSONException jeThree = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseThree, jsonParserConfiguration));
|
||||||
|
JSONException jeFour = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseFour, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
"Expected a ':' after a key at 10 [character 11 line 1]",
|
||||||
|
jeOne.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Single quote wrap not allowed in strict mode at 2 [character 3 line 1]",
|
||||||
|
jeTwo.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Single quote wrap not allowed in strict mode at 6 [character 7 line 1]",
|
||||||
|
jeThree.getMessage());
|
||||||
|
assertEquals(
|
||||||
|
"Single quote wrap not allowed in strict mode at 2 [character 3 line 1]",
|
||||||
|
jeFour.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUnbalancedQuotesArray_testStrictModeFalse_shouldThrowJsonException() {
|
||||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
.withStrictMode(false);
|
.withStrictMode(false);
|
||||||
|
|
||||||
@ -282,6 +441,22 @@ public class JSONParserConfigurationTest {
|
|||||||
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
|
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUnbalancedQuotesObject_testStrictModeFalse_shouldThrowJsonException() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(false);
|
||||||
|
|
||||||
|
String testCaseOne = "{\"abc': \"test\"}";
|
||||||
|
String testCaseTwo = "{'abc\": \"test\"}";
|
||||||
|
|
||||||
|
JSONException jeOne = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseOne, jsonParserConfiguration));
|
||||||
|
JSONException jeTwo = assertThrows(JSONException.class,
|
||||||
|
() -> new JSONObject(testCaseTwo, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals("Expected a ':' after a key at 10 [character 11 line 1]", jeOne.getMessage());
|
||||||
|
assertEquals("Unterminated string. Character with int code 0 is not allowed within a quoted string. at 15 [character 16 line 1]", jeTwo.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
public void givenInvalidInputArray_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
||||||
@ -295,13 +470,25 @@ public class JSONParserConfigurationTest {
|
|||||||
assertEquals("Value 'test' is not surrounded by quotes at 6 [character 7 line 1]", je.getMessage());
|
assertEquals("Value 'test' is not surrounded by quotes at 6 [character 7 line 1]", je.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInvalidInputObject_testStrictModeTrue_shouldThrowKeyNotSurroundedByQuotesErrorMessage() {
|
||||||
|
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
|
||||||
|
.withStrictMode(true);
|
||||||
|
|
||||||
|
String testCase = "{test: implied}";
|
||||||
|
JSONException je = assertThrows("expected non-compliant json but got instead: " + testCase,
|
||||||
|
JSONException.class, () -> new JSONObject(testCase, jsonParserConfiguration));
|
||||||
|
|
||||||
|
assertEquals("Value 'test' is not surrounded by quotes at 5 [character 6 line 1]", je.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||||
* this class.
|
* this class.
|
||||||
*
|
*
|
||||||
* @return List with JSON strings.
|
* @return List with JSON strings.
|
||||||
*/
|
*/
|
||||||
private List<String> getNonCompliantJSONList() {
|
private List<String> getNonCompliantJSONArrayList() {
|
||||||
return Arrays.asList(
|
return Arrays.asList(
|
||||||
"[1],",
|
"[1],",
|
||||||
"[1,]",
|
"[1,]",
|
||||||
@ -326,4 +513,38 @@ public class JSONParserConfigurationTest {
|
|||||||
"[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]",
|
"[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]",
|
||||||
"[{test: \"implied\"}]");
|
"[{test: \"implied\"}]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method contains short but focused use-case samples and is exclusively used to test strictMode unit tests in
|
||||||
|
* this class.
|
||||||
|
*
|
||||||
|
* @return List with JSON strings.
|
||||||
|
*/
|
||||||
|
private List<String> getNonCompliantJSONObjectList() {
|
||||||
|
return Arrays.asList(
|
||||||
|
"{\"a\":1},",
|
||||||
|
"{\"a\":1,}",
|
||||||
|
"{\"a0\":[1],\"a1\":\"sa\",\"a2\":[2]}a",
|
||||||
|
"{\"a\":1},\"dsa\": \"test\"",
|
||||||
|
"{\"a\":[a]}",
|
||||||
|
"{}asdf",
|
||||||
|
"{}}",
|
||||||
|
"{}]",
|
||||||
|
"{}{",
|
||||||
|
"{}[",
|
||||||
|
"{},",
|
||||||
|
"{}:",
|
||||||
|
"{},{",
|
||||||
|
"{},[",
|
||||||
|
"{\"a0\":[1,2];\"a1\":[3,4]}",
|
||||||
|
"{\"a\":test}",
|
||||||
|
"{a:{'testSingleQuote': 'testSingleQuote'}}",
|
||||||
|
"{\"a0\":1, \"a1\":2,\"a2\":3}:{\"a3\":4,\"a4\":5}",
|
||||||
|
"{\"a\":{test: implied}}",
|
||||||
|
"{a:{\"test\": implied}}",
|
||||||
|
"{a:[{\"number\":\"7990154836330\",\"color\":'c'},{\"number\":8784148854580,\"color\":RosyBrown},{\"number\":\"5875770107113\",\"color\":\"DarkSeaGreen\"}]}",
|
||||||
|
"{a:{test: \"implied\"}}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
3703
src/test/resources/compliantJsonObject.json
Normal file
3703
src/test/resources/compliantJsonObject.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user