|
| 1 | +package me.codeleep.jsondiff.core.handle.custom; |
| 2 | + |
| 3 | +import com.alibaba.fastjson2.JSONArray; |
| 4 | +import me.codeleep.jsondiff.common.model.JsonCompareResult; |
| 5 | +import me.codeleep.jsondiff.core.handle.array.ComplexArrayJsonNeat; |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author: codeleep |
| 11 | + * @createTime: 2023/04/25 22:22 |
| 12 | + * @description: 将补齐不整齐的数组 |
| 13 | + */ |
| 14 | +public class AlignArrayJsonDiff extends ComplexArrayJsonNeat { |
| 15 | + |
| 16 | + /** |
| 17 | + * 比较数组.调用入口。需要自己去分别调用 ignoreOrder 和 keepOrder。 |
| 18 | + * @param expect 期望的json对象 |
| 19 | + * @param actual 实际的json对象 |
| 20 | + * @return 返回比较结果 |
| 21 | + */ |
| 22 | + @Override |
| 23 | + public JsonCompareResult detectDiff(JSONArray expect, JSONArray actual) { |
| 24 | + JsonCompareResult result = new JsonCompareResult(); |
| 25 | + // 前置校验失败 |
| 26 | + if (!check(expect, actual, result, travelPath)) { |
| 27 | + return result; |
| 28 | + } |
| 29 | + // 长度不一致 |
| 30 | + int expectSize = ((JSONArray) expect).size(); |
| 31 | + int actualSize = ((JSONArray) actual).size(); |
| 32 | + if (expectSize == actualSize) { |
| 33 | + return super.detectDiff(expect, actual); |
| 34 | + } |
| 35 | + JSONArray expectIts = new JSONArray(expect); |
| 36 | + JSONArray actualIts = new JSONArray(actual); |
| 37 | + // 让期望的数组长度和实际的数组长度一致。谁短补齐谁 |
| 38 | + if (expectSize > actualSize) { |
| 39 | + actualIts.addAll(Collections.nCopies(expectSize - actualSize, null)); |
| 40 | + } else { |
| 41 | + expectIts.addAll(Collections.nCopies(actualSize - expectSize, null)); |
| 42 | + } |
| 43 | + return super.detectDiff(expectIts, actualIts); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +} |
0 commit comments