redis修正

This commit is contained in:
2025-12-18 14:00:31 +08:00
parent 0c6e76dbbd
commit 3d423972bc

View File

@@ -37,9 +37,15 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{ {
return new byte[0]; return new byte[0];
} }
// 对String类型直接返回原始字节不做JSON序列化
if (t instanceof String)
{
return ((String) t).getBytes(DEFAULT_CHARSET);
}
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET); return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
} }
@SuppressWarnings("unchecked")
@Override @Override
public T deserialize(byte[] bytes) throws SerializationException public T deserialize(byte[] bytes) throws SerializationException
{ {
@@ -48,7 +54,15 @@ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
return null; return null;
} }
String str = new String(bytes, DEFAULT_CHARSET); String str = new String(bytes, DEFAULT_CHARSET);
// 对String类型直接返回字符串
if (clazz == String.class || clazz == Object.class && !str.startsWith("{") && !str.startsWith("["))
{
// 如果是纯字符串非JSON格式直接返回
if (!str.startsWith("\"") && !str.startsWith("{") && !str.startsWith("["))
{
return (T) str;
}
}
return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER); return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER);
} }
} }