在Java中测试两个JSON对象是否相等忽略子顺序
我正在寻找一个JSON解析库,它支持比较两个忽略子顺序的JSON对象,特别是对从web服务返回的JSON进行单元测试在Java中测试两个JSON对象是否相等忽略子顺序,java,json,junit,Java,Json,Junit,我正在寻找一个JSON解析库,它支持比较两个忽略子顺序的JSON对象,特别是对从web服务返回的JSON进行单元测试 有任何主要的JSON库支持这一点吗?org.json库只是做了一个引用比较。我将在上获取该库,并修改JSONObject和JSONArray的equals方法来进行深度相等性测试。为了确保它的工作不受子对象顺序的影响,只需将内部映射替换为TreeMap,或者使用类似Collections.sort()的内容作为一般架构点,我通常建议不要让对特定序列化格式的依赖超出存储/网络层;因
有任何主要的JSON库支持这一点吗?org.json库只是做了一个引用比较。我将在上获取该库,并修改JSONObject和JSONArray的
equals
方法来进行深度相等性测试。为了确保它的工作不受子对象顺序的影响,只需将内部映射替换为TreeMap
,或者使用类似Collections.sort()的内容作为一般架构点,我通常建议不要让对特定序列化格式的依赖超出存储/网络层;因此,我首先建议您考虑在自己的应用程序对象之间测试相等性,而不是测试它们的JSON表示形式。
话虽如此,我目前是一个超级粉丝,我快速阅读了他们的实现后,发现你想要的集合成员比较:
public boolean equals(Object o)
{
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != getClass()) {
return false;
}
ObjectNode other = (ObjectNode) o;
if (other.size() != size()) {
return false;
}
if (_children != null) {
for (Map.Entry<String, JsonNode> en : _children.entrySet()) {
String key = en.getKey();
JsonNode value = en.getValue();
JsonNode otherValue = other.get(key);
if (otherValue == null || !otherValue.equals(value)) {
return false;
}
}
}
return true;
}
公共布尔等于(对象o)
{
如果(o==this)返回true;
如果(o==null)返回false;
如果(o.getClass()!=getClass()){
返回false;
}
ObjectNode other=(ObjectNode)o;
如果(其他.size()!=size()){
返回false;
}
如果(_children!=null){
对于(Map.Entry en:_children.entrySet()){
String key=en.getKey();
JsonNode value=en.getValue();
JsonNode otherValue=other.get(key);
如果(otherValue==null | |!otherValue.equals(value)){
返回false;
}
}
}
返回true;
}
您可以尝试使用json库的类:
JSONAssert.assertEquals(
“{foo:'bar',baz:'qux'}”,
fromObject(“{foo:'bar',baz:'xyzy'}”)
);
给出:
junit.framework.ComparisonFailure: objects differed at key [baz]; expected:<[qux]> but was:<[xyzzy]>
junit.framework.ComparisonFailure:objects在键[baz]处不同;预期:但是:
我做了一件事,它非常有效,那就是将这两个对象读入HashMap,然后与常规assertEquals()进行比较。它将调用hashmaps的equals()方法,该方法将递归地比较其中的所有对象(它们将是其他hashmaps或某个单值对象,如字符串或整数)。这是使用Codehaus的Jackson JSON解析器完成的
assertEquals(mapper.readValue(expectedJson, new TypeReference<HashMap<String, Object>>(){}), mapper.readValue(actualJson, new TypeReference<HashMap<String, Object>>(){}));
assertEquals(mapper.readValue(expectedJson,newtypereference(){})、mapper.readValue(actualJson,newtypereference(){}));
如果JSON对象是数组,则可以使用类似的方法。使用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);
编辑:因为实例方法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);
你可以试试。它可以比较两个JSON对象并报告差异。它建在杰克逊的顶上
比如说
assertThatJson("{\"test\":1}").isEqualTo("{\n\"test\": 2\n}");
导致
java.lang.AssertionError: JSON documents are different:
Different value found in node "test". Expected 1, got 2.
试试天呐
其非严格模式有两个主要优点,使其不易脆化:
- 对象可扩展性(例如,如果预期值为{id:1},则仍将传递:{id:1,moredata:'x'})
- 松散数组排序(例如,['dog','cat']==['cat','dog'])
在严格模式下,它的行为更像json库的测试类
测试如下所示:
@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);
}
assertEquals()调用中的参数是expectedJSONString、actualDataString和isStrict
结果消息非常清楚,这在比较真正大的JSON对象时非常重要。对于org.JSON,我推出了自己的解决方案,一种与JSONObject实例进行比较的方法。我没有在那个项目中处理复杂的JSON对象,所以我不知道这是否在所有场景中都有效。另外,考虑到我在单元测试中使用了它,我没有将精力投入到优化中。这是:
public static boolean jsonObjsAreEqual (JSONObject js1, JSONObject js2) throws JSONException {
if (js1 == null || js2 == null) {
return (js1 == js2);
}
List<String> l1 = Arrays.asList(JSONObject.getNames(js1));
Collections.sort(l1);
List<String> l2 = Arrays.asList(JSONObject.getNames(js2));
Collections.sort(l2);
if (!l1.equals(l2)) {
return false;
}
for (String key : l1) {
Object val1 = js1.get(key);
Object val2 = js2.get(key);
if (val1 instanceof JSONObject) {
if (!(val2 instanceof JSONObject)) {
return false;
}
if (!jsonObjsAreEqual((JSONObject)val1, (JSONObject)val2)) {
return false;
}
}
if (val1 == null) {
if (val2 != null) {
return false;
}
} else if (!val1.equals(val2)) {
return false;
}
}
return true;
}
publicstaticbooleanjsonobjectsareequal(jsonobjectjs1,jsonobjectjs2)抛出JSONException{
if(js1==null | | js2==null){
返回值(js1==js2);
}
listl1=Arrays.asList(JSONObject.getNames(js1));
集合。排序(l1);
listl2=Arrays.asList(JSONObject.getNames(js2));
集合。排序(l2);
如果(!l1.等于(l2)){
返回false;
}
用于(字符串键:l1){
对象val1=js1.get(key);
对象val2=js2.get(key);
if(JSONObject的val1实例){
if(!(JSONObject的val2实例)){
返回false;
}
如果(!JSONObject)val1,(JSONObject)val2)){
返回false;
}
}
if(val1==null){
if(val2!=null){
返回false;
}
}else如果(!val1.等于(val2)){
返回false;
}
}
返回true;
}
如果您已经在使用JUnit,最新版本现在将使用Hamcrest。它是一个通用的匹配框架(特别适用于单元测试),可以扩展以构建新的匹配器
有一个名为hamcrest-json
的小型开源库,具有json感知匹配。它有很好的文档记录、测试和支持。以下是一些有用的链接:
- :
使用JSON库中对象的示例代码org.JSON.simple
:
Assert.assertThat(
jsonObject1.toJSONString(),
SameJSONAs.sameJSONAs(jsonObject2.toJSONString()));
或者,您可以(1)允许“任意顺序”数组和(2)忽略额外字段
由于Java有多种JSON库(Jackson
、GSON
、JSON-lib
,等等),因此hamcrest-JSON
支持JSON文本(如Java.lang.String
)以及Douglas Crockford的JSON库org.JSON
中的本机支持对象非常有用
最后,如果不使用JUnit,可以直接使用Hamcrest进行断言。()试试这个:
public static boolean jsonsEqual(Object obj1, Object obj2) throws JSONException
{
if (!obj1.getClass().equals(obj2.getClass()))
{
return false;
}
if (obj1 instanceof JSONObject)
{
JSONObject jsonObj1 = (JSONObject) obj1;
JSONObject jsonObj2 = (JSONObject) obj2;
String[] names = JSONObject.getNames(jsonObj1);
String[] names2 = JSONObject.getNames(jsonObj1);
if (names.length != names2.length)
{
return false;
}
for (String fieldName:names)
{
Object obj1FieldValue = jsonObj1.get(fieldName);
Object obj2FieldValue = jsonObj2.get(fieldName);
if (!jsonsEqual(obj1FieldValue, obj2FieldValue))
{
return false;
}
}
}
else if (obj1 instanceof JSONArray)
{
JSONArray obj1Array = (JSONArray) obj1;
JSONArray obj2Array = (JSONArray) obj2;
if (obj1Array.length() != obj2Array.length())
{
return false;
}
for (int i = 0; i < obj1Array.length(); i++)
{
boolean matchFound = false;
for (int j = 0; j < obj2Array.length(); j++)
{
if (jsonsEqual(obj1Array.get(i), obj2Array.get(j)))
{
matchFound = true;
break;
}
}
if (!matchFound)
{
return false;
}
}
}
else
{
if (!obj1.equals(obj2))
{
return false;
}
}
return true;
}
公共静态布尔JSONSEQUE(对象obj1,对象obj2)抛出JSONException
{
JSONObject obj1 = /*json*/;
JSONObject obj2 = /*json*/;
ObjectMapper mapper = new ObjectMapper();
JsonNode tree1 = mapper.readTree(obj1.toString());
JsonNode tree2 = mapper.readTree(obj2.toString());
return tree1.equals(tree2);
JsonAssert.assertJsonEquals(jsonNode1, jsonNode2);
java.lang.AssertionError: JSON documents have different values:
Different value found in node "heading.content[0].tag[0]". Expected 10209, got 10206.
package com.project1.helpers;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class JSONUtils {
public static boolean areEqual(Object ob1, Object ob2) throws JSONException {
Object obj1Converted = convertJsonElement(ob1);
Object obj2Converted = convertJsonElement(ob2);
return obj1Converted.equals(obj2Converted);
}
private static Object convertJsonElement(Object elem) throws JSONException {
if (elem instanceof JSONObject) {
JSONObject obj = (JSONObject) elem;
Iterator<String> keys = obj.keys();
Map<String, Object> jsonMap = new HashMap<>();
while (keys.hasNext()) {
String key = keys.next();
jsonMap.put(key, convertJsonElement(obj.get(key)));
}
return jsonMap;
} else if (elem instanceof JSONArray) {
JSONArray arr = (JSONArray) elem;
Set<Object> jsonSet = new HashSet<>();
for (int i = 0; i < arr.length(); i++) {
jsonSet.add(convertJsonElement(arr.get(i)));
}
return jsonSet;
} else {
return elem;
}
}
}
* def myJson = { foo: 'world', hey: 'ho', zee: [5], cat: { name: 'Billie' } }
* match myJson = { cat: { name: 'Billie' }, hey: 'ho', foo: 'world', zee: [5] }
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;
}
private boolean compareJson(JsonElement json1, JsonElement json2) {
boolean isEqual = true;
// Check whether both jsonElement are not null
if (json1 != null && json2 != null) {
// Check whether both jsonElement are objects
if (json1.isJsonObject() && json2.isJsonObject()) {
Set<Entry<String, JsonElement>> ens1 = ((JsonObject) json1).entrySet();
Set<Entry<String, JsonElement>> ens2 = ((JsonObject) json2).entrySet();
JsonObject json2obj = (JsonObject) json2;
if (ens1 != null && ens2 != null) {
// (ens2.size() == ens1.size())
// Iterate JSON Elements with Key values
for (Entry<String, JsonElement> en : ens1) {
isEqual = isEqual && compareJson(en.getValue(), json2obj.get(en.getKey()));
}
} else {
return false;
}
}
// Check whether both jsonElement are arrays
else if (json1.isJsonArray() && json2.isJsonArray()) {
JsonArray jarr1 = json1.getAsJsonArray();
JsonArray jarr2 = json2.getAsJsonArray();
if (jarr1.size() != jarr2.size()) {
return false;
} else {
int i = 0;
// Iterate JSON Array to JSON Elements
for (JsonElement je : jarr1) {
isEqual = isEqual && compareJson(je, jarr2.get(i));
i++;
}
}
}
// Check whether both jsonElement are null
else if (json1.isJsonNull() && json2.isJsonNull()) {
return true;
}
// Check whether both jsonElement are primitives
else if (json1.isJsonPrimitive() && json2.isJsonPrimitive()) {
if (json1.equals(json2)) {
return true;
} else {
return false;
}
} else {
return false;
}
} else if (json1 == null && json2 == null) {
return true;
} else {
return false;
}
return isEqual;
}
// Compare by regex
String expected = "{\"a\":\".*me.*\"}";
String actual = "{\"a\":\"some text\"}";
JSONCompare.assertEquals(expected, actual); // True
// Check expected array has no extra elements
String expected = "[1,\"test\",4,\"!.*\"]";
String actual = "[4,1,\"test\"]";
JSONCompare.assertEquals(expected, actual); // True
// Check expected array has no numbers
String expected = "[\"\\\\\\d+\"]";
String actual = "[\"text\",\"test\"]";
JSONCompare.assertEquals(expected, actual); // True
// Check expected array has no numbers
String expected = "[\"\\\\\\d+\"]";
String actual = "[2018]";
JSONCompare.assertNotEquals(expected, actual); // True
JSON.areEqual(json1, json2); //using BlobCity Java Commons
json1 = new JSONObject("{...}");
json2 = new JSONObject("{...}");
json1.toMap().equals(json2.toMap());
Map<Object, Object> resMap = gson.fromJson(res, new TypeToken<Map<Object, Object>>() {}.getType());
Map<Object, Object> expectedMap = gson.fromJson(expected, new TypeToken<Map<Object, Object>>() {}.getType());
Assertions.assertThat(resMap).usingRecursiveComparison().isEqualTo(expectedMap);
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());
}
}
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);
}