mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
!348 Fastjson2TypeHandler 支持接口
Merge pull request !348 from tangxin/main
This commit is contained in:
commit
f0e7b13d76
@ -16,9 +16,11 @@
|
||||
package com.mybatisflex.core.handler;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONReader;
|
||||
import com.alibaba.fastjson2.JSONWriter;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
|
||||
@ -31,8 +33,11 @@ public class Fastjson2TypeHandler extends BaseJsonTypeHandler<Object> {
|
||||
private Class<?> genericType;
|
||||
private Type type;
|
||||
|
||||
private boolean isInterface = false;
|
||||
|
||||
public Fastjson2TypeHandler(Class<?> propertyType) {
|
||||
this.propertyType = propertyType;
|
||||
this.isInterface = propertyType.isInterface();
|
||||
}
|
||||
|
||||
|
||||
@ -40,24 +45,41 @@ public class Fastjson2TypeHandler extends BaseJsonTypeHandler<Object> {
|
||||
this.propertyType = propertyType;
|
||||
this.genericType = genericType;
|
||||
this.type = TypeReference.collectionType((Class<? extends Collection>) propertyType, genericType);
|
||||
this.isInterface = ((Class<?>) ((ParameterizedType)type).getActualTypeArguments()[0]).isInterface();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object parseJson(String json) {
|
||||
if (genericType != null && Collection.class.isAssignableFrom(propertyType)) {
|
||||
if(isInterface){
|
||||
return JSON.parseArray(json, Object.class,JSONReader.Feature.SupportAutoType);
|
||||
}else {
|
||||
return JSON.parseObject(json, type);
|
||||
}
|
||||
|
||||
} else {
|
||||
if(isInterface){
|
||||
return JSON.parseObject(json, Object.class,JSONReader.Feature.SupportAutoType);
|
||||
}else {
|
||||
return JSON.parseObject(json, propertyType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String toJson(Object object) {
|
||||
if(isInterface){
|
||||
return JSON.toJSONString(object
|
||||
, JSONWriter.Feature.WriteMapNullValue
|
||||
, JSONWriter.Feature.WriteNullListAsEmpty
|
||||
, JSONWriter.Feature.WriteNullStringAsEmpty, JSONWriter.Feature.WriteClassName
|
||||
);
|
||||
}else {
|
||||
return JSON.toJSONString(object
|
||||
, JSONWriter.Feature.WriteMapNullValue
|
||||
, JSONWriter.Feature.WriteNullListAsEmpty
|
||||
, JSONWriter.Feature.WriteNullStringAsEmpty
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,102 @@
|
||||
package com.mybatisflex.coretest;
|
||||
|
||||
|
||||
|
||||
import com.mybatisflex.core.handler.Fastjson2TypeHandler;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tangxin
|
||||
* @since 2023-09-24
|
||||
*/
|
||||
public class Fastjson2TypeHandlerTest {
|
||||
public interface ConfigData {
|
||||
|
||||
}
|
||||
|
||||
public static class FtpFileClientConfig implements ConfigData {
|
||||
|
||||
|
||||
private String basePath;
|
||||
|
||||
|
||||
private String domain;
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
|
||||
public void setBasePath(String basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
public String getDomain() {
|
||||
return domain;
|
||||
}
|
||||
|
||||
public void setDomain(String domain) {
|
||||
this.domain = domain;
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyFastjson2TypeHandler extends Fastjson2TypeHandler {
|
||||
|
||||
|
||||
public MyFastjson2TypeHandler(Class<?> propertyType) {
|
||||
super(propertyType);
|
||||
}
|
||||
|
||||
|
||||
public MyFastjson2TypeHandler(Class<?> propertyType, Class<?> genericType) {
|
||||
super(propertyType, genericType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parseJson(String json) {
|
||||
return super.parseJson(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toJson(Object object) {
|
||||
return super.toJson(object);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Test() {
|
||||
List<ConfigData> configDataList = new ArrayList<>(0);
|
||||
MyFastjson2TypeHandler interfaceHandler = new MyFastjson2TypeHandler(ConfigData.class);
|
||||
MyFastjson2TypeHandler classHandler = new MyFastjson2TypeHandler(FtpFileClientConfig.class);
|
||||
MyFastjson2TypeHandler interfaceListHandler = new MyFastjson2TypeHandler(configDataList.getClass(), ConfigData.class);
|
||||
MyFastjson2TypeHandler classListHandler = new MyFastjson2TypeHandler(configDataList.getClass(), FtpFileClientConfig.class);
|
||||
FtpFileClientConfig ftpFileClientConfig = new FtpFileClientConfig();
|
||||
ftpFileClientConfig.setDomain("http://test.com");
|
||||
ftpFileClientConfig.setBasePath("/var/upload");
|
||||
List<FtpFileClientConfig> ftpFileClientConfigList = new ArrayList<>(1);
|
||||
ftpFileClientConfigList.add(ftpFileClientConfig);
|
||||
String interfaceJson = interfaceHandler.toJson(ftpFileClientConfig);
|
||||
System.out.println("interface :" + interfaceJson);
|
||||
String classJson = classHandler.toJson(ftpFileClientConfig);
|
||||
System.out.println("class :" + classJson);
|
||||
String interfaceListJson = interfaceHandler.toJson(ftpFileClientConfigList);
|
||||
System.out.println("interfaceList :" + interfaceListJson);
|
||||
String classListJson = classHandler.toJson(ftpFileClientConfigList);
|
||||
System.out.println("classList :" + classListJson);
|
||||
|
||||
FtpFileClientConfig ftpFileClientConfig1 = (FtpFileClientConfig) interfaceHandler.parseJson(interfaceJson);
|
||||
assert (ftpFileClientConfig.getDomain().equals(ftpFileClientConfig1.getDomain()));
|
||||
|
||||
FtpFileClientConfig ftpFileClientConfig2 = (FtpFileClientConfig) classHandler.parseJson(interfaceJson);
|
||||
assert (ftpFileClientConfig.getDomain().equals(ftpFileClientConfig2.getDomain()));
|
||||
|
||||
ftpFileClientConfigList = (List<FtpFileClientConfig>) interfaceListHandler.parseJson(interfaceListJson);
|
||||
assert (ftpFileClientConfig.getDomain().equals(ftpFileClientConfigList.get(0).getDomain()));
|
||||
|
||||
ftpFileClientConfigList = (List<FtpFileClientConfig>) classListHandler.parseJson(interfaceListJson);
|
||||
assert (ftpFileClientConfig.getDomain().equals(ftpFileClientConfigList.get(0).getDomain()));
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user