我正在寻找一个JSON解析库,支持比较忽略子顺序的两个JSON对象,特别是用于从web服务返回的单元测试JSON。
有任何主要的JSON库支持这一点吗?org。Json库只是做一个引用比较。
我正在寻找一个JSON解析库,支持比较忽略子顺序的两个JSON对象,特别是用于从web服务返回的单元测试JSON。
有任何主要的JSON库支持这一点吗?org。Json库只是做一个引用比较。
当前回答
你可以尝试使用json-lib的JSONAssert类:
JSONAssert.assertEquals(
"{foo: 'bar', baz: 'qux'}",
JSONObject.fromObject("{foo: 'bar', baz: 'xyzzy'}")
);
给:
junit.framework.ComparisonFailure: objects differed at key [baz]; expected:<[qux]> but was:<[xyzzy]>
其他回答
这可能会帮助那些使用Spring Framework的人。你可以重用内部使用的在ResultActions上做断言(用于控制器测试):
进口:org.springframework.test.util.JsonExpectationsHelper
你可以编写带有详细输出的测试:
java.lang.AssertionError: someObject.someArray[1].someInternalObject2.value
Expected: 456
got: 4567
测试代码:
@Test
void test() throws Exception {
final String json1 =
"{" +
" 'someObject': {" +
" 'someArray': [" +
" {" +
" 'someInternalObject': {" +
" 'value': '123'" +
" }" +
" }," +
" {" +
" 'someInternalObject2': {" +
" 'value': '456'" +
" }" +
" }" +
" ]" +
" }" +
"}";
final String json2 =
"{" +
" 'someObject': {" +
" 'someArray': [" +
" {" +
" 'someInternalObject': {" +
" 'value': '123'" +
" }" +
" }," +
" {" +
" 'someInternalObject2': {" +
" 'value': '4567'" +
" }" +
" }" +
" ]" +
" }" +
"}";
new JsonExpectationsHelper().assertJsonEqual(json1, json2, true);
}
其他方法似乎都不太合适,所以我写下了这个:
private boolean jsonEquals(JsonNode actualJson, JsonNode expectJson) {
if(actualJson.getNodeType() != expectJson.getNodeType()) return false;
switch(expectJson.getNodeType()) {
case NUMBER:
return actualJson.asDouble() == expectJson.asDouble();
case STRING:
case BOOLEAN:
return actualJson.asText().equals(expectJson.asText());
case OBJECT:
if(actualJson.size() != expectJson.size()) return false;
Iterator<String> fieldIterator = actualJson.fieldNames();
while(fieldIterator.hasNext()) {
String fieldName = fieldIterator.next();
if(!jsonEquals(actualJson.get(fieldName), expectJson.get(fieldName))) {
return false;
}
}
break;
case ARRAY:
if(actualJson.size() != expectJson.size()) return false;
List<JsonNode> remaining = new ArrayList<>();
expectJson.forEach(remaining::add);
// O(N^2)
for(int i=0; i < actualJson.size(); ++i) {
boolean oneEquals = false;
for(int j=0; j < remaining.size(); ++j) {
if(jsonEquals(actualJson.get(i), remaining.get(j))) {
oneEquals = true;
remaining.remove(j);
break;
}
}
if(!oneEquals) return false;
}
break;
default:
throw new IllegalStateException();
}
return true;
}
使用GSON
JsonParser parser = new JsonParser();
JsonElement o1 = parser.parse("{a : {a : 2}, b : 2}");
JsonElement o2 = parser.parse("{b : 2, a : {a : 2}}");
assertEquals(o1, o2);
编辑:自GSON v2.8.6起,实例方法JsonParser。不建议使用Parse。你必须使用静态方法JsonParser.parseString:
JsonElement o1 = JsonParser.parseString("{a : {a : 2}, b : 2}");
JsonElement o2 = JsonParser.parseString("{b : 2, a : {a : 2}}");
assertEquals(o1, o2);
下面是使用Jackson ObjectMapper的代码。要了解更多,请阅读这篇文章。
import com.fasterxml.jackson.*
boolean compareJsonPojo(Object pojo1, Object pojo2) {
try {
ObjectMapper mapper = new ObjectMapper();
String str1 = mapper.writeValueAsString(pojo1);
String str2 = mapper.writeValueAsString(pojo2);
return mapper.readTree(str1).equals(mapper.readTree(str2));
} catch (JsonProcessingException e) {
throw new AssertionError("Error comparing JSON objects: " + e.getMessage());
}
}
试试天空尖叫者的圣像。
它的非严格模式有两个主要优点,使其不那么脆弱:
对象的可扩展性(例如,期望值为{id:1},仍然会通过:{id:1,moredata:'x'}。) 宽松的数组排序(例如[‘狗’,‘猫’]= =[“猫”,“狗”])
在严格模式下,它更像json-lib的test类。
测试是这样的:
@Test
public void testGetFriends() {
JSONObject data = getRESTData("/friends/367.json");
String expected = "{friends:[{id:123,name:\"Corby Page\"}"
+ ",{id:456,name:\"Solomon Duskis\"}]}";
JSONAssert.assertEquals(expected, data, false);
}
JSONAssert.assertEquals()调用中的参数是expectedJSONString, actualDataString和isStrict。
结果消息非常清晰,这在比较非常大的JSON对象时非常重要。