Skip to content

Commit

Permalink
fixed issue skyscreamer#130
Browse files Browse the repository at this point in the history
  • Loading branch information
zihanxu3 committed Dec 8, 2021
1 parent 523009b commit 81a3602
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
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
19 changes: 18 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,24 @@ 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);
}

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

0 comments on commit 81a3602

Please sign in to comment.