我正在寻找一个JSON解析库,它支持比较忽略子命令的两个JSON对象,特别是用于测试从Web服务返回的JSON的单元测试.
任何主要的JSON库都支持这个吗?org.json库只是进行参考比较.
Car*_*age 145
试试Skyscreamer的JSONAssert.
它的非严格模式有两个主要优点,使其不易碎:
在严格模式下,它的行为更像json-lib的测试类.
测试看起来像这样:
@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);
}
Run Code Online (Sandbox Code Playgroud)
JSONAssert.assertEquals()调用中的参数是expectedJSONString,actualDataString和isStrict.
结果消息非常清楚,这在比较非常大的JSON对象时很重要.
Jol*_*ger 79
作为一般的架构点,我通常建议不要让特定序列化格式的依赖性超出存储/网络层; 因此,我首先建议您考虑测试您自己的应用程序对象之间的相等性,而不是它们的JSON表现形式.
话虽如此,我现在是杰克逊的忠实粉丝,我快速阅读他们的ObjectNode.equals()实现建议您进行所需的集合成员资格比较:
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;
}
Run Code Online (Sandbox Code Playgroud)
axe*_*hzf 44
使用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);
Run Code Online (Sandbox Code Playgroud)
jos*_*osh 29
我会做以下,
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);
Run Code Online (Sandbox Code Playgroud)
her*_*ung 16
您可以尝试使用json-lib的JSONAssert类:
JSONAssert.assertEquals(
"{foo: 'bar', baz: 'qux'}",
JSONObject.fromObject("{foo: 'bar', baz: 'xyzzy'}")
);
Run Code Online (Sandbox Code Playgroud)
得到:
junit.framework.ComparisonFailure: objects differed at key [baz]; expected:<[qux]> but was:<[xyzzy]>
Run Code Online (Sandbox Code Playgroud)
小智 13
使用此库:https://github.com/lukas-krecan/JsonUnit
双响炮:
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
IGNORING_ARRAY_ORDER - 忽略数组中的顺序
assertJsonEquals("{\"test\":[1,2,3]}",
"{\"test\": [3,2,1]}",
when(IGNORING_ARRAY_ORDER)
);
Run Code Online (Sandbox Code Playgroud)
kev*_*rpe 12
如果您已经在使用JUnit,最新版本现在使用Hamcrest.它是一个通用的匹配框架(特别适用于单元测试),可以扩展为构建新的匹配器.
有一个叫做hamcrest-jsonJSON感知匹配的小型开源库.它有详细记录,测试和支持.以下是一些有用的链接:
使用JSON库中的对象的示例代码org.json.simple:
Assert.assertThat(
jsonObject1.toJSONString(),
SameJSONAs.sameJSONAs(jsonObject2.toJSONString()));
Run Code Online (Sandbox Code Playgroud)
或者,您可以(1)允许"任意顺序"数组和(2)忽略额外字段.
由于有对Java(各种JSON库Jackson,GSON,json-lib等),它是有用的hamcrest-json支持JSON文本(如java.lang.String),以及原生支持从道格拉斯Crockford的JSON库对象org.json.
最后,如果您不使用JUnit,则可以直接使用Hamcrest进行断言.(我在这里写过.)
Luk*_*kas 11
你可以尝试JsonUnit.它可以比较两个JSON对象并报告差异.它建立在杰克逊之上.
例如
assertJsonEquals("{\"test\":1}", "{\n\"test\": 2\n}");
Run Code Online (Sandbox Code Playgroud)
结果是
java.lang.AssertionError: JSON documents are different:
Different value found in node "test". Expected 1, got 2.
Run Code Online (Sandbox Code Playgroud)
小智 6
我做过的一件事就是将两个对象都读入HashMap,然后与常规的assertEquals()进行比较.它将调用hashmaps的equals()方法,它将递归地比较内部的所有对象(它们将是其他哈希映射或某个单值对象,如字符串或整数).这是使用Codehaus的Jackson JSON解析器完成的.
assertEquals(mapper.readValue(expectedJson, new TypeReference<HashMap<String, Object>>(){}), mapper.readValue(actualJson, new TypeReference<HashMap<String, Object>>(){}));
Run Code Online (Sandbox Code Playgroud)
如果JSON对象是一个数组,则可以使用类似的方法.
小智 5
我正在使用它,并且对我来说很好(使用org.json。*):
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
262109 次 |
| 最近记录: |