当我遇到这个问题时,我正在使用Gson和JSONAssert库。
我有以下函数来比较两个整数:
private static void runCompareInts(int source, int target){
JSONCompareResult result = JSONCompare.compareJSON(new Gson().toJson(source),
new Gson().toJson(target),
JSONCompareMode.NON_EXTENSIBLE);
if (CollectionUtils.isEmpty(result.getFieldFailures())) {
System.out.println("Objects are same.");
} else {
System.out.println("Objects are not the same. Difference: " + result);
}
}
当我运行时runCompareInts(1, 2)
,我得到"Objects are same."
了结果,这不应该是这种情况。
我发现new Gson().toJson(1)
返回 String "1"
,这是一个有效的 JSON 字符串,因此比较应该正确进行并进入else
块。
比较整数 usingJSONAssert.assertNotEquals("1", "2", true)
不会导致任何异常。这意味着 Gson 转换值不是问题。
谁能告诉我我的runCompareInts()
功能有什么错误?谢谢你。
编辑:JSONAssert.assertNotEquals(new Gson().toJson(source), new Gson().toJson(target), true)
也可以正常工作。