diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/Fastjson2TypeHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/Fastjson2TypeHandler.java index 33831db2..0a932138 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/Fastjson2TypeHandler.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/handler/Fastjson2TypeHandler.java @@ -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 { 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 { this.propertyType = propertyType; this.genericType = genericType; this.type = TypeReference.collectionType((Class) propertyType, genericType); + this.isInterface = ((Class) ((ParameterizedType)type).getActualTypeArguments()[0]).isInterface(); } @Override protected Object parseJson(String json) { if (genericType != null && Collection.class.isAssignableFrom(propertyType)) { - return JSON.parseObject(json, type); + if(isInterface){ + return JSON.parseArray(json, Object.class,JSONReader.Feature.SupportAutoType); + }else { + return JSON.parseObject(json, type); + } + } else { - return JSON.parseObject(json, propertyType); + if(isInterface){ + return JSON.parseObject(json, Object.class,JSONReader.Feature.SupportAutoType); + }else { + return JSON.parseObject(json, propertyType); + } } } @Override protected String toJson(Object object) { - return JSON.toJSONString(object - , JSONWriter.Feature.WriteMapNullValue - , JSONWriter.Feature.WriteNullListAsEmpty - , JSONWriter.Feature.WriteNullStringAsEmpty - ); + 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 + ); + } } - } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/Fastjson2TypeHandlerTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/Fastjson2TypeHandlerTest.java new file mode 100644 index 00000000..01f684c8 --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/Fastjson2TypeHandlerTest.java @@ -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 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 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) interfaceListHandler.parseJson(interfaceListJson); + assert (ftpFileClientConfig.getDomain().equals(ftpFileClientConfigList.get(0).getDomain())); + + ftpFileClientConfigList = (List) classListHandler.parseJson(interfaceListJson); + assert (ftpFileClientConfig.getDomain().equals(ftpFileClientConfigList.get(0).getDomain())); + + } +}