对于基本类型整数,JSONCompareResult不适用于GSON。为什么?

问题描述 投票:0回答:1

当我遇到这个问题时,我正在玩GsonJSONAssert库。

我有以下函数来比较两个整数:

    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块。

使用JSONAssert.assertNotEquals("1", "2", true)比较整数不会导致任何异常。这意味着Gson转换值不是问题。

谁能告诉我我的runCompareInts()功能的错误是什么?谢谢。

编辑:JSONAssert.assertNotEquals(new Gson().toJson(source), new Gson().toJson(target), true)也工作正常。

java json gson assert jsonassert
1个回答
0
投票

如果阻止,请使用内部的result.isFailureOnField()

我在jsonassert库中看到了消息的问题,compareJson(final JSONString expected, final JSONString actual)类的方法JSONCompare在失败时返回空错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.