mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-21 11:07:23 +08:00
refactor: 重构 sa-token-fastjson 插件
This commit is contained in:
@@ -15,11 +15,8 @@
|
||||
*/
|
||||
package cn.dev33.satoken.json;
|
||||
|
||||
import cn.dev33.satoken.session.SaSessionForFastjsonCustomized;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JSON 转换器, Fastjson 版实现
|
||||
*
|
||||
@@ -40,17 +37,8 @@ public class SaJsonTemplateForFastjson implements SaJsonTemplate {
|
||||
* 反序列化:json 字符串 → 对象
|
||||
*/
|
||||
@Override
|
||||
public Object jsonToObject(String jsonStr) {
|
||||
// TODO: 此处待更改,需要让其自动识别类型
|
||||
return JSON.parseObject(jsonStr, SaSessionForFastjsonCustomized.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化:json 字符串 → Map
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> jsonToMap(String jsonStr) {
|
||||
return JSON.parseObject(jsonStr, Map.class);
|
||||
public<T> T jsonToObject(String jsonStr, Class<T> type) {
|
||||
return JSON.parseObject(jsonStr, type);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -37,6 +37,9 @@ public class SaTokenPluginForFastjson implements SaTokenPlugin {
|
||||
// 重写 SaSession 生成策略
|
||||
SaStrategy.instance.createSession = SaSessionForFastjsonCustomized::new;
|
||||
|
||||
// 指定 SaSession 类型
|
||||
SaStrategy.instance.sessionClassType = SaSessionForFastjsonCustomized.class;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -17,6 +17,7 @@ package cn.dev33.satoken.session;
|
||||
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* Fastjson 定制版 SaSession,重写类型转换API
|
||||
@@ -52,31 +53,22 @@ public class SaSessionForFastjsonCustomized extends SaSession {
|
||||
*/
|
||||
@Override
|
||||
public <T> T getModel(String key, Class<T> cs) {
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
}
|
||||
return JSON.parseObject(getString(key), cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取值 (指定转换类型, 并指定值为Null时返回的默认值)
|
||||
* @param <T> 泛型
|
||||
* @param key key
|
||||
* @param cs 指定转换类型
|
||||
* @param defaultValue 值为Null时返回的默认值
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
|
||||
// 如果是想取出为基础类型
|
||||
Object value = get(key);
|
||||
if(valueIsNull(value)) {
|
||||
return (T)defaultValue;
|
||||
}
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
return SaFoxUtil.getValueByType(value, cs);
|
||||
}
|
||||
// 为空提前返回
|
||||
if(valueIsNull(value)) {
|
||||
return null;
|
||||
}
|
||||
// 如果是 JSONObject 类型直接转,否则先转为 String 再转
|
||||
if(value instanceof JSONObject) {
|
||||
JSONObject jo = (JSONObject) value;
|
||||
return jo.toJavaObject(cs);
|
||||
} else {
|
||||
return JSON.parseObject(value.toString(), cs);
|
||||
}
|
||||
return JSON.parseObject(getString(key), cs);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -36,7 +36,6 @@ import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JSON 转换器, Jackson 版实现
|
||||
@@ -110,7 +109,6 @@ public class SaJsonTemplateForJackson implements SaJsonTemplate {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
System.out.println("序列化的啥:" + objectMapper.writeValueAsString(obj));
|
||||
return objectMapper.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SaJsonConvertException(e);
|
||||
@@ -121,30 +119,12 @@ public class SaJsonTemplateForJackson implements SaJsonTemplate {
|
||||
* 反序列化:json 字符串 → 对象
|
||||
*/
|
||||
@Override
|
||||
public Object jsonToObject(String jsonStr) {
|
||||
public <T> T jsonToObject(String jsonStr, Class<T> type) {
|
||||
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Object value = objectMapper.readValue(jsonStr, Object.class);
|
||||
return value;
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SaJsonConvertException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化:json 字符串 → Map
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> jsonToMap(String jsonStr) {
|
||||
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);
|
||||
return map;
|
||||
return objectMapper.readValue(jsonStr, type);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new SaJsonConvertException(e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user