|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
package com.ruoyi.framework.config;
|
|
2
|
2
|
|
|
3
|
3
|
import java.nio.charset.Charset;
|
|
|
4
|
+import java.util.regex.Pattern;
|
|
4
|
5
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
5
|
6
|
import org.springframework.data.redis.serializer.SerializationException;
|
|
6
|
7
|
import com.alibaba.fastjson2.JSON;
|
|
|
@@ -8,6 +9,8 @@ import com.alibaba.fastjson2.JSONReader;
|
|
8
|
9
|
import com.alibaba.fastjson2.JSONWriter;
|
|
9
|
10
|
import com.alibaba.fastjson2.JSONObject;
|
|
10
|
11
|
import com.alibaba.fastjson2.filter.Filter;
|
|
|
12
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
13
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
11
|
14
|
import com.ruoyi.common.constant.Constants;
|
|
12
|
15
|
|
|
13
|
16
|
/**
|
|
|
@@ -21,6 +24,11 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
21
|
24
|
|
|
22
|
25
|
static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(Constants.JSON_WHITELIST_STR);
|
|
23
|
26
|
|
|
|
27
|
+ private static final Pattern LONG_LITERAL = Pattern.compile("(\\d+)L(?=[,\\}\\]\\s])");
|
|
|
28
|
+
|
|
|
29
|
+ private static final ObjectMapper JACKSON = new ObjectMapper()
|
|
|
30
|
+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
31
|
+
|
|
24
|
32
|
private Class<T> clazz;
|
|
25
|
33
|
|
|
26
|
34
|
public FastJson2JsonRedisSerializer(Class<T> clazz)
|
|
|
@@ -46,7 +54,7 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
46
|
54
|
{
|
|
47
|
55
|
return null;
|
|
48
|
56
|
}
|
|
49
|
|
- String str = normalizeLegacyCollectionSyntax(new String(bytes, DEFAULT_CHARSET));
|
|
|
57
|
+ String str = unwrapNestedJsonString(normalizeLegacyCollectionSyntax(new String(bytes, DEFAULT_CHARSET)));
|
|
50
|
58
|
try
|
|
51
|
59
|
{
|
|
52
|
60
|
Object parsed = JSON.parseObject(str, clazz, AUTO_TYPE_FILTER, JSONReader.Feature.SupportAutoType);
|
|
|
@@ -66,9 +74,52 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
66
|
74
|
}
|
|
67
|
75
|
catch (Exception fallbackEx)
|
|
68
|
76
|
{
|
|
69
|
|
- throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
|
|
|
77
|
+ try
|
|
|
78
|
+ {
|
|
|
79
|
+ return readWithJackson(str);
|
|
|
80
|
+ }
|
|
|
81
|
+ catch (Exception jacksonEx)
|
|
|
82
|
+ {
|
|
|
83
|
+ throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
|
|
|
84
|
+ }
|
|
|
85
|
+ }
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ @SuppressWarnings("unchecked")
|
|
|
90
|
+ private T readWithJackson(String str) throws Exception
|
|
|
91
|
+ {
|
|
|
92
|
+ if (clazz == Object.class || clazz == null)
|
|
|
93
|
+ {
|
|
|
94
|
+ return (T) JACKSON.readValue(str, Object.class);
|
|
|
95
|
+ }
|
|
|
96
|
+ return JACKSON.readValue(str, clazz);
|
|
|
97
|
+ }
|
|
|
98
|
+
|
|
|
99
|
+ /** 外层被 FastJSON 再包一层引号时解包 */
|
|
|
100
|
+ private static String unwrapNestedJsonString(String str)
|
|
|
101
|
+ {
|
|
|
102
|
+ if (str == null)
|
|
|
103
|
+ {
|
|
|
104
|
+ return null;
|
|
|
105
|
+ }
|
|
|
106
|
+ String trimmed = str.trim();
|
|
|
107
|
+ if (trimmed.length() >= 2 && trimmed.startsWith("\"") && trimmed.endsWith("\""))
|
|
|
108
|
+ {
|
|
|
109
|
+ try
|
|
|
110
|
+ {
|
|
|
111
|
+ String inner = JSON.parseObject(trimmed, String.class);
|
|
|
112
|
+ if (inner != null && (inner.startsWith("{") || inner.startsWith("[")))
|
|
|
113
|
+ {
|
|
|
114
|
+ return inner;
|
|
|
115
|
+ }
|
|
|
116
|
+ }
|
|
|
117
|
+ catch (Exception ignored)
|
|
|
118
|
+ {
|
|
|
119
|
+ // keep original
|
|
70
|
120
|
}
|
|
71
|
121
|
}
|
|
|
122
|
+ return str;
|
|
72
|
123
|
}
|
|
73
|
124
|
|
|
74
|
125
|
@SuppressWarnings("unchecked")
|
|
|
@@ -112,6 +163,12 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
112
|
163
|
{
|
|
113
|
164
|
return str;
|
|
114
|
165
|
}
|
|
115
|
|
- return str.replace("LinkedHashSet[", "[").replace("HashSet[", "[").replace("Set[", "[");
|
|
|
166
|
+ return LONG_LITERAL.matcher(str
|
|
|
167
|
+ .replace("LinkedHashSet[", "[")
|
|
|
168
|
+ .replace("HashSet[", "[")
|
|
|
169
|
+ .replace("TreeSet[", "[")
|
|
|
170
|
+ .replace("Set[", "[")
|
|
|
171
|
+ .replace("ArrayList[", "[")
|
|
|
172
|
+ .replace("LinkedList[", "[")).replaceAll("$1");
|
|
116
|
173
|
}
|
|
117
|
174
|
}
|