13 Star 73 Fork 20

codeleep / JsonDiff

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

JsonDiff 高性能json差异发现工具

GitHub license star fork GitHub stars GitHub forks GitHub issues

一、介绍

它几乎可以发现任何JSON结构的差异,并且将错误信息反馈给用户。

1.优点

  • 高效的
  • 精准定位差异
  • 轻量级
  • 依赖非常干净,只依赖fastJson,fastjson2,gson,jackson其中之一

目前支持功能

  • 支持json-path表示路径
  • 支持配置化比较行为。详情见下文
  • 支持拓展自定义比较器,用于满足特殊需求
  • 支持不同json框架选择。

二、使用文档

1.快速开始

引入依赖:

<dependency>
    <groupId>cn.xiaoandcai</groupId>
    <artifactId>json-diff</artifactId>
    <!-- 旧版本可能存在某些缺陷。版本请以maven仓库最版为准。 -->
    <version>${version}</version>
</dependency>
        
<!-- 选择json解析框架。fastjson, fastjson2,gson,jackson 之一 -->
<dependency>
    <groupId>cn.xiaoandcai</groupId>
    <artifactId>json-diff-impl-fastjson</artifactId>
    <version>${version}</version>
</dependency>

版本查看 2024-04-11 最新版本:4.1.1-RC1-RELEASE

/**
 * @author: codeleep
 * @createTime: 2022/11/22 16:57
 * @description: 使用示例
 */
public class UseExample {

    public static void main(String[] args) {
        String array1 = "[1, 2, 3, 4, 5]";
        String array2 = "[1, 3, 9, 4, 5]";

        JsonComparedOption jsonComparedOption = new JsonComparedOption().setIgnoreOrder(true);
        JsonCompareResult jsonCompareResult = new DefaultJsonDifference()
                .option(jsonComparedOption)
                .detectDiff(array1, array2);
        System.out.println(JSON.toJSONString(jsonCompareResult));
    }
}

主要分为两个对象 JsonComparedOption.class 和 JsonCompareResult.class;分别是配置对象和处理对象。

返回结果:

{
    "defectsList": [
        {
            "actual": 9,
            "expect": 2,
            "illustrate": "The expect('2') data is inconsistent with the actual('9') data",
            "indexPath": "root[]"
        }
    ],
    "match": false
}

工具会返回两个json对象的差异。

2.配置

配置 类型 备注
ignoreOrder boolean 是否比较过程中忽略数组顺序
mapping Map<String, String> 将真实字段映射到期望字段,key是真实字段name,value是期望的字段name.当真实字段和期望的字段对应的key都存在时,该配置才生效
ignorePath Set<String> 当对比的路径完全匹配时会被跳过。标准的json-path
ignoreKey Set<String> 对比object时。或忽略该key。对整个json生效

2.0.1-RC1-RELEASE 之后版本中移除了 keyFunction 配置参数。可以使用 ignorePath 来代替达到同样的效果。

工具提供了四个配置,来之对比过程中一些其他的要求。工具还在积极开发中,如果有新的需求,可以给作者提一个issuse。

在开发中。很多时候对比配置一致。可以使用 JsonDiffOption 进行开启唯一配置。这样也将获取更好的性能

3.进阶

3.1. 全局使用固定配置

由于在设计中考虑到各线程比较配置相互独立。所以默认将配置防止在 ThreadLocal 中进行存储。但在大多数情况下,我们在全局比较时,配置并不会发生变化。

工具提供了全局配置方式。采用的方式是静态类属性。这样也会获得更好的性能。

// 开启并设置全局配置
JsonDiffOption.openUniqueOption();
JsonDiffOption.setGloballyUniqueOption(new JsonComparedOption());
// 不想使用时可以调用调整回线程独有模式
 JsonDiffOption.closeUniqueOption();

3.2. 数组元素为对象关联

当我们在遇到数组元素是一个对象时。如下:

[
    {
        "date": "23日星期五",
        "sunrise": "06:16",
        "high": "高温 18.0℃"
    },
    {
        "date": "24日星期六",
        "sunrise": "06:14",
        "high": "高温 21.0℃"
    }
]

在比较时, 如果希望 date 字段一致,则认为两个对象一致。那么可以将 sunrise, high 字段都配置到 ignorePath 中。如:

HashSet<String> ignorePath = new HashSet<>();
ignorePath.add("root[].sunrise");
ignorePath.add("root[].high");

如果只是不想关注某个字段。即是 ignorePath 正常用法。配置如上。

3.3. 字段映射

在比较两个对象时。也许由于字段名变更。导致校验不通过。这时可以使用 mapping 配置。将 真实字段名称映射至期望字段名称。在比较过程中会将

actual.mappingKey 与 expect.mappingValue 认为是应该比较的对象。具体配置如下

// mapping key 是 actual 键名
// mapping value 是 expect 键名
HashMap<String, String> mapping = new HashMap<>();
mapping.put("date", "sunrise");

3.4. 字段忽略

如果有一些字段是想在整个json都进行忽略的,可以使用 ignoreKey 进行全局忽略。当然如果不想全局忽略,但是配置了该项,还是会被忽略掉。

HashSet<String> ignoreKey = new HashSet<>();
ignoreKey.add("sunrise");
ignoreKey.add("high");

3.5 自定义比较器

在某些特俗场景下,我们需要自己实现比较器。这时可以通过 me.codeleep.jsondiff.core.handle.HandleFactory 来进行实现。可以参考 me.codeleep.jsondiff.core.handle.AbstractHandleFactory 实现

将我们的实现设置到JsonComparedOption中即可生效。

对于各个类型的比较器可以尝试继承系统比较器来进行使用:

  • me.codeleep.jsondiff.core.handle.array.ComplexArrayJsonNeat
  • me.codeleep.jsondiff.core.handle.object.ComplexObjectJsonNeat
  • me.codeleep.jsondiff.core.handle.other.ComplexOtherJsonNeat
  • me.codeleep.jsondiff.core.handle.primitive.ComplexPrimitiveJsonNeat

在工具中,目前提供了一个 me.codeleep.jsondiff.core.handle.custom.AlignArrayJsonDiff 来实现当数组长度不一致时,进行补齐,从而避免直接返回错误信息。

4.其他说明

前面提到工具几乎可以支持所有json结果的对比校验,并且发现差异。那它到底可以支持哪些呢,不知道是否符合你的需求呢?

  • 对象 ✅

    这是最简单的数据结构了,其中元素都以key-value构成,key是字符串,value可以是任何数据结构。

  • 数组 ✅

    支持严格顺序对比和忽略顺序对比,可以细化数组元素的类型

    • 基本类型 ✅

    • 对象类型 ✅

      该类型在对比时,可以通过ignorePath参数进行元素是否进行比较,将不关心的元素忽略掉。当然ignoreKey也可以,但其是全局生效

    • 数组类型 ✅

      元素也是数组,这样就形成了多维数组,工具理论上来说支持n维数组的对比

    • 元素类型不统一 ✅

      数组中,类型可能包含前面三种类型,这时工具会按照类型分类进行匹配,最后找不到的元素再反馈给用户。

由于json结构在单个看来,就只有对象和数组两种类型,该工具完美支持了所有类型。

三、交流群:710435809

  • 如果长时间未通过可发邮件至 codeleep@163.com 与我联系
  • 如果觉得对你有帮助,欢迎star。你的start是对我最大的鼓励。

四、测试用例编写规范

1.测试用例文件字段

文件中数据结构

{
  "right": [
    {
      "actual": [{"a": 1}],
      "expect": [{"b": 2}],
      "ret": {},
      "option": {
        "ignoreOrder": false,
        "mapping": {"a": "b"},
        "ignorePath": ["root[].a"],
        "ignoreKey": ["b"]
      }
    }
  ],
  "err"[.....],
  "optionRight":[......],
  "optionErr":[........],
}

每一个测试文件结构都如上,right,err,optionRight,optionErr分别为测试种类名称,测试名称下对应的是每一个测试用例[]中每一个object都是对应的一个用例每一测试用例为如下类似的object

名称 类型 备注
right 数组 正常用例
err 数组 异常用例
optionRight 数组 配置正常用例
optionErr 数组 配置异常用例
注: right,err,不会加载配置
    {
      "actual": [{"a": 1}],
      "expect": [{"b": 2}],
      "ret": {},
      "option": {
        "ignoreOrder": false,
        "mapping": {"a": 1},
        "ignorePath": ["root[].a"],
        "ignoreKey": ["b"]
      }
}

"actual": 实际需要对比的值 类型:测试array文件为array 测试object文件为object
"expect": 期望该值为什么 类型:测试array文件为array 测试object文件为object
"ret": 返回内容,如果用例为right中的自动判断返回内容中match是否为true进行判断 类型:object
"option": 配置信息 对应本项目中的配置文件 类型:Object
   "ignoreOrder": 是否忽略数组书序 类型:Boolean
   "mapping": key 是 actual value 是 expect 映射 类型:Object
   "ignorePath": 忽略的path. 以 . 来区分json层级; 会精准匹配路径 类型:array
   "ignoreKey": 忽略的key。actual中有的字段,但expect没有的,会被忽略掉 类型:array

2.执行测试

直接进入测试代码执行

进入src/test/java/me/codeleep/jsondiff/test/ 中有两个后缀为Test的java文件,可直接运行该文件,也能进入其中运行指定方法 注:每一个方法运行都会把对应集合的方法全部运行一遍

通过TestNG xml文件执行

进入src/test/resources/testNG 其中有三个文件,分别是执行全部的case,执行全部异常或者匹配错误错误的用例,执行全部应当匹配正确 

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

一款高性能且功能强大的Json差异发现工具 它几乎可以发现任何结果结构的json数据的不同之处 展开 收起
Java
Apache-2.0
取消

发行版 (11)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/codeleep/json-diff.git
git@gitee.com:codeleep/json-diff.git
codeleep
json-diff
JsonDiff
dev_4.x

搜索帮助