Jsonunit 比較jsondiff

2一念轮回2發表於2024-08-17
https://github.com/lukas-krecan/JsonUnit
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}]"));
assertThatJson("{\"root\":{\"test\":1, \"ignored\": 1}}")
    .whenIgnoringPaths("root.ignored"))
    .isEqualTo("{\"root\":{\"test\":1}}");

  

比較時的可選項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編寫比較麻煩,這個功能還是有潛在的使用價值的