排序实现所用依赖
主要使用的是阿里的fastjson。 commons-lang3 为工具类,里面包含String, 集合,日期等各种工具类
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.74</version> </dependency>
排序实现逻辑
排序主要从两块入手,分别是JSONObject, JSONArray.
对JSONObject进行排序
对JSONObject排序,选择使用TreeMap, 因为TreeMap的底层继承实现了NavigableMap接口,保证了TreeMap的有序性,
排序规则采用的是Comparator.naturalOrder()
具体逻辑实现:
1 public static void sortJSONbyTreeMap(JSONObject payload){ 2 TreeMap<String, Object> treeMap = new TreeMap<>(Comparator.naturalOrder()); 3 for (Map.Entry<String, Object> entry : payload.entrySet()) { 4 treeMap.put(entry.getKey(), entry.getValue()); 5 } 6 for (Map.Entry<String, Object> entry : treeMap.entrySet()) { 7 payload.remove(entry.getKey()); 8 payload.put(entry.getKey(), entry.getValue()); 9 } 10 }
对JSONArray进行排序
排序算法选择的是冒泡排序,比较实现主要从三种类型考虑,分别为Comparable, JSONObject以及JSONArray.
当类型不同的数据比较时
采用Comparable > JSONObject > JSONArray的顺序进行排序,具体实现:
1 private final static ArrayList OBJ_TYPE_LIST = new ArrayList(); 2 3 static { 4 OBJ_TYPE_LIST.add(Comparable.class.getSimpleName()); 5 OBJ_TYPE_LIST.add(JSONObject.class.getSimpleName()); 6 OBJ_TYPE_LIST.add(JSONArray.class.getSimpleName()); 7 } 8 9 public static CompareResult compareObjectType(String objTypeA, String objTypeB) { 10 Integer indexA = OBJ_TYPE_LIST.indexOf(objTypeA); 11 Integer indexB = OBJ_TYPE_LIST.indexOf(objTypeB); 12 if (indexA.compareTo(indexB) > 0) { 13 return CompareResult.GREATER; 14 } else if (indexA.compareTo(indexB) == 0) { 15 return CompareResult.EQUAL; 16 } else { 17 return CompareResult.LESS; 18 } 19 }
当类型相同时,则分别使用不同的比较逻辑实现
1. 类型为Comparable的具体实现:
1 public static CompareResult compareObject(Object objA, Object objB) { 2 String objTypeA = classifyObjectType(objA); 3 String objTypeB = classifyObjectType(objB); 4 CompareResult compareResult = compareObjectType(objTypeA, objTypeB); 5 if (compareResult == CompareResult.EQUAL) { 6 if (StringUtils.equals(objTypeA, JSONObject.class.getSimpleName())) { 7 compareResult = compareJSONObject(((JSONObject) objA), ((JSONObject) objB)); 8 } else if (StringUtils.equals(objTypeA, JSONArray.class.getSimpleName())) { 9 compareResult = compareJSONArray(((JSONArray) objA), ((JSONArray) objB)); 10 } else { 11 if (((Comparable) objA).compareTo(objB) > 0) { 12 compareResult = CompareResult.GREATER; 13 } else if (((Comparable) objA).compareTo(objB) == 0) { 14 compareResult = CompareResult.EQUAL; 15 } else { 16 compareResult = CompareResult.LESS; 17 } 18 } 19 } 20 return compareResult; 21 }
2. 类型为JSONObject的具体实现:
1 public static CompareResult compareJSONObject(JSONObject jsonObjectA, JSONObject jsonObjectB) { 2 CompareResult compareResult = CompareResult.LESS; 3 if (jsonObjectA.size() > jsonObjectB.size()) { 4 compareResult = CompareResult.GREATER; 5 } else if (jsonObjectB.size() < jsonObjectB.size()) { 6 compareResult = CompareResult.LESS; 7 } else { 8 //When jsonObjectB.size() == jsonObjectB.size() 9 Iterator<Map.Entry<String, Object>> iteratorA = jsonObjectA.entrySet().iterator(); 10 Iterator<Map.Entry<String, Object>> iteratorB = jsonObjectB.entrySet().iterator(); 11 while (iteratorA.hasNext()) { 12 Map.Entry<String, Object> entryA = iteratorA.next(); 13 Map.Entry<String, Object> entryB = iteratorB.next(); 14 15 //Compare key 16 compareResult = compareObject(entryA.getKey(), entryB.getKey()); 17 if (compareResult != CompareResult.EQUAL) { 18 break; 19 } 20 21 //Compare the type of value 22 compareResult = compareObjectType(classifyObjectType(entryA.getValue()), classifyObjectType(entryB.getValue())); 23 if (compareResult != CompareResult.EQUAL) { 24 break; 25 } 26 27 //Compare value 28 compareResult = compareObject(entryA.getValue(), entryB.getValue()); 29 if (compareResult != CompareResult.EQUAL) { 30 break; 31 } 32 } 33 } 34 return compareResult; 35 }
3. 类型为JSONArray的具体实现:
1 public static CompareResult compareJSONArray(JSONArray jsonArrayA, JSONArray jsonArrayB) { 2 CompareResult compareResult = CompareResult.LESS; 3 if (jsonArrayA.size() > jsonArrayB.size()) { 4 compareResult = CompareResult.GREATER; 5 } else if (jsonArrayA.size() < jsonArrayB.size()) { 6 compareResult = CompareResult.LESS; 7 } else { 8 //when jsonArrayA.size() == jsonArrayB.size() 9 //Compare key 10 compareResult = compareObjectType(classifyObjectType(jsonArrayA.get(0)), classifyObjectType(jsonArrayB.get(0))); 11 if (compareResult == CompareResult.EQUAL) { 12 ListIterator<Object> listIteratorA = jsonArrayA.listIterator(); 13 ListIterator<Object> listIteratorB = jsonArrayB.listIterator(); 14 while (listIteratorA.hasNext()) { 15 //Compare value 16 compareResult = compareObject(listIteratorA.next(), listIteratorB.next()); 17 if (compareResult != CompareResult.EQUAL) { 18 break; 19 } 20 } 21 } 22 } 23 return compareResult; 24 }
测试代码
1 package cn.sille.inns;
2
3 import com.alibaba.fastjson.JSON;
4 import org.junit.jupiter.api.Test;
5
6 public class JSONSortUtilTest {
7
8 @Test
9 public void test(){
10 String sample1 = "{"aa":"1231","waa":"3453","err":{"123113":{"llll":[5,1,3,2],"rrrr":"12311df","gdddf":{"vvsss":[{"qwrq":"222","aaaa":"uuuu"},{"qwrq":"444","aaaa":"342342"},{"qwrq":"234","aaaa":"tttt"}]}}},"uuuu":[{"yyyy":1231,"iiiii":"uuuu","9999":{"sssss":"ppppp"}},{"hjhj":999,"opop":"uuuu"}]}";
11 String sample2 = "{"aa":"1231","err":{"123113":{"llll":[5,1,3,2],"rrrr":"12311df","gdddf":{"vvsss":[,{"qwrq":"234","aaaa":"tttt"},{"qwrq":"222","aaaa":"uuuu"},{"qwrq":"444","aaaa":"342342"}]}}},"uuuu":[{"hjhj":999,"opop":"uuuu"},{"iiiii":"uuuu","yyyy":1231,"9999":{"sssss":"ppppp"}}],"waa":"3453"}";
12
13 System.out.println("=====================================================");
14 System.out.println(JSONSortUtil.sortJSONObject(JSON.parseObject(sample1)));
15 System.out.println("=====================================================");
16 System.out.println(JSONSortUtil.sortJSONObject(JSON.parseObject(sample2)));
17 System.out.println("=====================================================");
18
19 }
20 }
排序实现