|
|
@@ -0,0 +1,66 @@
|
|
|
+package com.xiesx.fastboot.core.fastjson.serializer;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.serializer.JSONSerializer;
|
|
|
+import com.alibaba.fastjson.serializer.ObjectSerializer;
|
|
|
+import com.alibaba.fastjson.serializer.SerializeWriter;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Type;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * List<Long>类型序列化器
|
|
|
+ * 将List<Long>序列化为带双引号的数字数组
|
|
|
+ */
|
|
|
+public class ListLongSerializer implements ObjectSerializer {
|
|
|
+
|
|
|
+ public static final ListLongSerializer instance = new ListLongSerializer();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
|
|
|
+ System.out.println("ListLongSerializer.write() 被调用 - fieldName: " + fieldName + ", fieldType: " + fieldType);
|
|
|
+
|
|
|
+ SerializeWriter out = serializer.out;
|
|
|
+
|
|
|
+ if (object == null) {
|
|
|
+ out.writeNull();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<?> list = (List<?>) object;
|
|
|
+
|
|
|
+ // 检查是否所有元素都是Long类型
|
|
|
+ boolean allLongs = true;
|
|
|
+ for (Object item : list) {
|
|
|
+ if (item != null && !(item instanceof Long)) {
|
|
|
+ allLongs = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果不是所有元素都是Long类型,使用默认序列化
|
|
|
+ if (!allLongs) {
|
|
|
+ serializer.write(list);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 自定义序列化Long类型的List
|
|
|
+ out.write('[');
|
|
|
+
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ if (i > 0) {
|
|
|
+ out.write(',');
|
|
|
+ }
|
|
|
+
|
|
|
+ Object item = list.get(i);
|
|
|
+ if (item == null) {
|
|
|
+ out.writeNull();
|
|
|
+ } else {
|
|
|
+ // 将Long类型转换为带双引号的字符串
|
|
|
+ out.writeString(item.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ out.write(']');
|
|
|
+ }
|
|
|
+}
|