Gson: 比较两个json是否等价(比较java bean是否相等的通用方法)

问题场景:对老代码接口进行重构,需要验证新接口逻辑是否和旧接口一致,在重构过程中是否产生了bug

解决思路:用相同的请求参数同时调用新接口和旧接口,然后对比返回的java对象。基于线上业务实际运行代码,若返回数据一直等价,则认为新旧接口逻辑一致。在这里,将返回对象转换为json,然后通过Gson解析json,对比json是否等价来确定java对象是否等价。

解决方案:先将两个java对象转换为json,然后通过gson将json转换为JsonElement对象,分别实现两个JsonElement对象为4种具体对象时的比较方案,然后递归比较。其中,若两个对象为JsonObject(即json对象),分别比较每个key对应的value是否相等;若两个对象为JsonArray(即json数组),先将其转换为List,并且按照List对象转换为json字符串时的字典序排序,再分别比较两个List中的对应对象是否等价,从而避免了json数组无序的问题。

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * 使用Gson比较两个json字符串是否等价
 */
public class JsonSameUtil {
    private static final Gson gson = new Gson();
    private static final JsonParser parser = new JsonParser();

    /**
     * 比较两个bean是否等价
     */
    public static boolean same(Object a, Object b) {
        if (a == null) {
            return b == null;
        }
        return same(gson.toJson(a), gson.toJson(b));
    }

    /**
     * 比较两个json字符串是否等价
     */
    public static boolean same(String a, String b) {
        if (a == null) {
            return b == null;
        }
        if (a.equals(b)) {
            return true;
        }
        JsonElement aElement = parser.parse(a);
        JsonElement bElement = parser.parse(b);
        if (gson.toJson(aElement).equals(gson.toJson(bElement))) {
            return true;
        }
        return same(aElement, bElement);
    }

    private static boolean same(JsonElement a, JsonElement b) {
        if (a.isJsonObject() && b.isJsonObject()) {
            return same((JsonObject) a, (JsonObject) b);
        } else if (a.isJsonArray() && b.isJsonArray()) {
            return same((JsonArray) a, (JsonArray) b);
        } else if (a.isJsonPrimitive() && b.isJsonPrimitive()) {
            return same((JsonPrimitive) a, (JsonPrimitive) b);
        } else if (a.isJsonNull() && b.isJsonNull()) {
            return same((JsonNull) a, (JsonNull) b);
        } else {
            return Boolean.FALSE;
        }
    }

    private static boolean same(JsonObject a, JsonObject b) {
        Set aSet = a.keySet();
        Set bSet = b.keySet();
        if (!aSet.equals(bSet)) {
            return false;
        }
        for (String aKey : aSet) {
            if (!same(a.get(aKey), b.get(aKey))) {
                return false;
            }
        }
        return true;
    }

    private static boolean same(JsonArray a, JsonArray b) {
        if (a.size() != b.size()) {
            return false;
        }
        List aList = toSortedList(a);
        List bList = toSortedList(b);
        for (int i = 0; i < aList.size(); i++) {
            if (!same(aList.get(i), bList.get(i))) {
                return false;
            }
        }
        return true;
    }

    private static boolean same(JsonPrimitive a, JsonPrimitive b) {

        return a.equals(b);
    }

    private static boolean same(JsonNull a, JsonNull b) {
        return true;
    }

    private static List toSortedList(JsonArray a) {
        List aList = new ArrayList<>();
        a.forEach(aList::add);
        aList.sort(Comparator.comparing(gson::toJson));
        return aList;
    }
}

导入的gson的maven库为


        
            com.google.code.gson
            gson
            2.8.2
        
用法示例如下
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List obj1 = Arrays.asList("1", "2", "3");
        List obj2 = Arrays.asList("1", "3", "2");
        System.out.println(JsonSameUtil.same(obj1, obj2)); // true

        String str1 = "[{\"a\":1},{\"b\":2},{\"c\":3}]";
        String str2 = "[{\"a\":1},{\"c\":3},{\"b\":2}]";
        System.out.println(JsonSameUtil.same(str1, str2)); // true
    }
}




你可能感兴趣的:(Gson: 比较两个json是否等价(比较java bean是否相等的通用方法))