Browse Source

优化消息序列化方法

johnclot69 4 months ago
parent
commit
5ff74111e1

+ 3 - 1
incubator-common/build.gradle

@@ -10,9 +10,11 @@ dependencies {
     testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
 
     //系统使用log4j2作为系统日志实现 slf4J作为门面
-    api 'org.slf4j:slf4j-api:1.7.36'
+//    api 'org.slf4j:slf4j-api:1.7.36'
+    api 'org.slf4j:slf4j-api:2.0.16'
     //声明slf4j转SLF4J的桥接包
 //    api 'org.apache.logging.log4j:log4j-slf4j-impl:2.19.0'
+    testImplementation 'org.apache.logging.log4j:log4j-slf4j2-impl:2.24.1'
     //使用log4j2作为实际的日志实现
     api 'org.apache.logging.log4j:log4j-api:2.24.1'
     api 'org.apache.logging.log4j:log4j-core:2.24.1'

+ 53 - 105
incubator-core/src/main/java/com/incubator/core/net/ws/MsgBase.java

@@ -176,123 +176,71 @@ public abstract class MsgBase {
 
         // 序列化 dataMap 的每个键值对
         for (Map.Entry<String, Object> entry : this.data.entrySet()) {
-            packer.packString(entry.getKey());
-
-            // 序列化不同类型的值
-            Object value = entry.getValue();
-            if (value instanceof String) {
-                packer.packString((String) value);
-            } else if (value instanceof Integer) {
-                packer.packInt((Integer) value);
-            } else if (value instanceof Float) {
-                packer.packFloat((Float) value);
-            } else if (value instanceof Boolean) {
-                packer.packBoolean((Boolean) value);
-            } else if (value instanceof int[]) {
-                // 序列化整型数组
-                int[] intArray = (int[]) value;
-                packer.packArrayHeader(intArray.length);
-                for (int item : intArray) {
-                    packer.packInt(item);
-                }
-            } else if (value instanceof Integer[]) {
-                // 序列化整型数组
-                Integer[] intArray = (Integer[]) value;
-                packer.packArrayHeader(intArray.length);
-                for (int item : intArray) {
-                    packer.packInt(item);
-                }
-            } else if (value instanceof ArrayList) {
-                // 序列化 ArrayList
-                ArrayList<Object> list = (ArrayList<Object>) value;
-                packer.packArrayHeader(list.size());
-                for (Object item : list) {
-                    if (item instanceof String) {
-                        packer.packString((String) item);
-                    } else if (item instanceof Integer) {
-                        packer.packInt((Integer) item);
-                    } else if (item instanceof Float) {
-                        packer.packFloat((Float) item);
-                    } else if (item instanceof Boolean) {
-                        packer.packBoolean((Boolean) item);
-                    } else if (item instanceof Map) {
-                        // 递归序列化嵌套的 Map
-                        packNestedMap(packer, (Map<String, Object>) item);
-                    } else {
-                        // 其他类型跳过或做特殊处理
-                        packer.packNil();
-                    }
-                }
-            } else if (value instanceof Map) {
-                // 递归序列化 Map
-                packNestedMap(packer, (Map<String, Object>) value);
-            } else {
-                // 其他类型跳过或做特殊处理
-                packer.packNil();
-            }
+            // 序列化 key
+            packObject(packer, entry.getKey());
+            // 序列化 value
+            packObject(packer, entry.getValue());
         }
 
         packer.close();
         return packer.toByteArray();
     }
 
+    /**
+     * 辅助方法:根据对象类型序列化
+     */
+    private void packObject(MessageBufferPacker packer, Object value) throws Exception {
+        if (value instanceof String) {
+            packer.packString((String) value);
+        } else if (value instanceof Integer) {
+            packer.packInt((Integer) value);
+        } else if (value instanceof Float) {
+            packer.packFloat((Float) value);
+        } else if (value instanceof Boolean) {
+            packer.packBoolean((Boolean) value);
+        } else if (value instanceof int[]) {
+            int[] intArray = (int[]) value;
+            packer.packArrayHeader(intArray.length);
+            for (int item : intArray) {
+                packer.packInt(item);
+            }
+        } else if (value instanceof Integer[]) {
+            Integer[] intArray = (Integer[]) value;
+            packer.packArrayHeader(intArray.length);
+            for (int item : intArray) {
+                packer.packInt(item);
+            }
+        } else if (value instanceof ArrayList) {
+            ArrayList<Object> list = (ArrayList<Object>) value;
+            packer.packArrayHeader(list.size());
+            for (Object item : list) {
+                // 递归处理 ArrayList 的元素
+                packObject(packer, item);
+            }
+        } else if (value instanceof Map) {
+            // 递归处理嵌套的 Map
+            packNestedMap(packer, (Map<String, Object>) value);
+        } else {
+            packer.packNil(); // 不支持的类型序列化为 Nil
+        }
+    }
+
     /**
      * 辅助方法:递归序列化嵌套的 Map
      */
     private void packNestedMap(MessageBufferPacker packer, Map<String, Object> map) throws Exception {
-        packer.packMapHeader(map.size());
+        packer.packMapHeader(map.size()); // Map 的大小
         for (Map.Entry<String, Object> entry : map.entrySet()) {
-            packer.packString(entry.getKey());
-            Object value = entry.getValue();
-            if (value instanceof String) {
-                packer.packString((String) value);
-            } else if (value instanceof Integer) {
-                packer.packInt((Integer) value);
-            } else if (value instanceof Float) {
-                packer.packFloat((Float) value);
-            } else if (value instanceof Boolean) {
-                packer.packBoolean((Boolean) value);
-            } else if (value instanceof int[]) {
-                // 序列化整型数组
-                int[] intArray = (int[]) value;
-                packer.packArrayHeader(intArray.length);
-                for (int item : intArray) {
-                    packer.packInt(item);
-                }
-            } else if (value instanceof Integer[]) {
-                // 序列化整型数组
-                Integer[] intArray = (Integer[]) value;
-                packer.packArrayHeader(intArray.length);
-                for (int item : intArray) {
-                    packer.packInt(item);
-                }
-            }else if (value instanceof ArrayList) {
-                // 序列化 ArrayList
-                ArrayList<Object> list = (ArrayList<Object>) value;
-                packer.packArrayHeader(list.size());
-                for (Object item : list) {
-                    if (item instanceof String) {
-                        packer.packString((String) item);
-                    } else if (item instanceof Integer) {
-                        packer.packInt((Integer) item);
-                    } else if (item instanceof Float) {
-                        packer.packFloat((Float) item);
-                    } else if (item instanceof Boolean) {
-                        packer.packBoolean((Boolean) item);
-                    } else if (item instanceof Map) {
-                        // 递归序列化嵌套的 Map
-                        packNestedMap(packer, (Map<String, Object>) item);
-                    } else {
-                        // 其他类型跳过或做特殊处理
-                        packer.packNil();
-                    }
-                }
-            } else if (value instanceof Map) {
-                // 递归处理嵌套的 Map
-                packNestedMap(packer, (Map<String, Object>) value);
-            } else {
-                packer.packNil();
+            // 序列化 key
+            Object key = entry.getKey();
+            if (key == null) {
+                throw new IllegalArgumentException("Map key cannot be null");
             }
+            packObject(packer, key);
+
+            // 序列化 value
+            Object value = entry.getValue();
+            packObject(packer, value);
         }
     }
 }