Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #130 assertEquals(...) may throw NullPointerException when given invalid JSON string. #152

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ private static String describe(Object value) {
} else if (value instanceof JSONObject) {
return "a JSON object";
} else {
if (value == null) {
return "null";
}
return value.toString();
}
}
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/org/skyscreamer/jsonassert/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,29 @@ private JSONParser() {}
* @throws JSONException JSON parsing error
*/
public static Object parseJSON(final String s) throws JSONException {
if (s.trim().startsWith("{")) {
if (s.trim().startsWith("{")&&s.trim().endsWith("}")) {
return new JSONObject(s);
}
else if (s.trim().startsWith("[")) {
else if (s.trim().startsWith("[")&&s.trim().endsWith("]")) {
return new JSONArray(s);
} else if (s.trim().startsWith("\"")
|| s.trim().matches(NUMBER_REGEX)) {
return new JSONString() {
@Override
public String toJSONString() {
return s;
} else if (s.trim().startsWith("\"") && s.trim().endsWith("\"")
|| s.trim().matches(NUMBER_REGEX)) {
int pos = 0;
boolean hascolon = false;
while(pos<s.length()){
if(s.charAt(pos)==':'){
hascolon = true;
}
pos++;
}
if(!hascolon){
return new JSONString() {
@Override
public String toJSONString() {
return s;
}
};
}
};
}
throw new JSONException("Unparsable JSON string: " + s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu
}
if ((expectedValue == null && actualValue != null) || (expectedValue != null && actualValue == null)) {
result.fail(prefix, expectedValue, actualValue);
return;
}
if (areNumbers(expectedValue, actualValue)) {
if (areNotSameDoubles(expectedValue, actualValue)) {
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,29 @@ public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentExc
new RegularExpressionValueMatcher<Object>("\\d"))
));
}

@Test
public void testAssertWithNullValues1() throws JSONException {
performAssertEqualsTestForMessageVerification("[{id:1},]", "[{id:1},{}]", true);
}

@Test
public void testAssertWithNullValues2() throws JSONException {
JSONAssert.assertNotEquals("[{id:1},]", "[{id:2},]", true);
}
@Test
public void testAssertWithNullValues3() throws JSONException {
JSONAssert.assertEquals("[{id:1},]" , "[{id:1},]" , true);
}
@Test
public void testAssertWithNullValues4() throws JSONException {
performAssertEqualsTestForMessageVerification("[{id:1},{id:2},]", "[{id:1},{id:2},{}]", true);
}

@Test
public void testAssertWithNullValues5() throws JSONException {
JSONAssert.assertEquals("[{id:1},{id:2},]" , "[{id:1},{id:2},]" , true);
}

private void testPass(String expected, String actual, JSONCompareMode compareMode)
throws JSONException
{
Expand Down