隐藏页面特效

Jsonunit 比较jsondiff

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json; ... // compares two JSON documents (note lenient parsing of expected value) assertThatJson("{\"a\":1, \"b\":2}").isEqualTo("{b:2, a:1}"); // objects are automatically serialized before comparison assertThatJson(jsonObject).isEqualTo("{\n\"test\": 1\n}"); // AssertJ map assertions (numbers are converted to BigDecimals) assertThatJson("{\"a\":1}").isObject().containsEntry("a", BigDecimal.valueOf(1)); // Type placeholders assertThatJson("{\"a\":1, \"b\": {\"c\" :3}}").isObject().containsValue(json("{\"c\" :\"${json-unit.any-number}\"}")); // AssertJ array assertion assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}").node("a").isArray().contains(json("{\"c\": 1}")); // Can ignore array order assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}").when(Option.IGNORING_ARRAY_ORDER).node("a").isArray() .isEqualTo(json("[{\"c\": 1}, {\"b\": 1} ,{\"d\": 1}]")); // custom matcher assertThatJson("{\"test\":-1}") .withConfiguration(c -> c.withMatcher("positive", greaterThan(valueOf(0)))) .isEqualTo("{\"test\": \"${json-unit.matches:positive}\"}"); // and assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}").and( a -> a.node("test.a").isEqualTo(1), a -> a.node("test.b").isEqualTo(2) );
<dependency> <groupId>net.javacrumbs.json-unit</groupId> <artifactId>json-unit-assertj</artifactId> <version>2.7.0</version> <scope>test</scope> </dependency>
assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}") .when(TREATING_NULL_AS_ABSENT) // 具有空值的字段等效于不存在的字段 .when(IGNORING_ARRAY_ORDER) // 忽略数组中的顺序 .when(IGNORING_EXTRA_ARRAY_ITEMS) // 忽略意外的数组项 .when(IGNORING_EXTRA_FIELDS) // 忽略比较值中的额外字段 .when(IGNORE_VALUES) // 忽略值并仅比较类型 .node("a").isArray() .isEqualTo(json("[{\"c\": 1}, {\"b\": 1} ,{\"d\": 1}]"));
1
2
3
4
5
<code-pre class="code-pre" id="pre-bMaWkx"><code-line class="line-numbers-rows"></code-line>assertThatJson("{\"root\":{\"test\":1, \"ignored\": 1}}")
<code-line class="line-numbers-rows"></code-line>    .whenIgnoringPaths("root.ignored"))
<code-line class="line-numbers-rows"></code-line>    .isEqualTo("{\"root\":{\"test\":1}}");
<code-line class="line-numbers-rows"></code-line>
</code-pre>

  

比较时的可选项Options

Options

There are multiple options how you can configure the comparison

TREATING_NULL_AS_ABSENT - fields with null values are equivalent to absent fields. For example, this test passes

代码语言:javascript
复制
assertJsonEquals("{\"test\":{\"a\":1}}", "{\"test\":{\"a\":1, \"b\": null, \"c\": null}}", when(TREATING_NULL_AS_ABSENT));

IGNORING_ARRAY_ORDER - ignores order in arrays

代码语言:javascript
复制
assertJsonEquals("{\"test\":[1,2,3]}", "{\"test\":[3,2,1]}", when(IGNORING_ARRAY_ORDER));

IGNORING_EXTRA_ARRAY_ITEMS - ignores unexpected array items

代码语言:javascript
复制
assertJsonEquals("{\"test\":[1,2,3]}", "{\"test\":[1,2,3,4]}", when(IGNORING_EXTRA_ARRAY_ITEMS)); assertJsonEquals("{\"test\":[1,2,3]}", "{\"test\":[5,5,4,4,3,3,2,2,1,1]}", when(IGNORING_EXTRA_ARRAY_ITEMS, IGNORING_ARRAY_ORDER));

IGNORING_EXTRA_FIELDS - ignores extra fields in the compared value

代码语言:javascript
复制
assertThatJson("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}") .when(IGNORING_EXTRA_FIELDS) .isEqualTo("{\"test\":{\"b\":2}}");

IGNORE_VALUES - ignores values and compares only types

代码语言:javascript
复制
assertJsonEquals("{\"test\":{\"a\":1,\"b\":2,\"c\":3}}", "{\"test\":{\"a\":3,\"b\":2,\"c\":1}}", when(IGNORING_VALUES));

It is possible to combine options.

代码语言:javascript
复制
assertJsonEquals("{\"test\":[{\"key\":1},{\"key\":2},{\"key\":3}]}", "{\"test\":[{\"key\":3},{\"key\":2, \"extraField\":2},{\"key\":1}]}", when(IGNORING_ARRAY_ORDER, IGNORING_EXTRA_FIELDS));

In Hamcrest assertion you can set the option like this

代码语言:javascript
复制
assertThat("{\"test\":{\"a\":1, \"b\":2, \"c\":3}}", jsonEquals("{\"test\":{\"b\":2}}").when(IGNORING_EXTRA_FIELDS));

json2xml

这个团队还开发了一个json转为xml的工具,还支持了array/attribute等较为复杂的内容 https://github.com/lukas-krecan/json2xml 由于好多金融系统间的协议使用到了XML,但是xml编写比较麻烦,这个功能还是有潜在的使用价值的


__EOF__

本文作者一念轮回
本文链接https://www.cnblogs.com/lbzwd/p/18364026.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   2一念轮回2  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示