mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Converter转换规则变更,空对象、空值转为Bean时,创建默认对象,而非nul
This commit is contained in:
@@ -16,6 +16,7 @@ import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.bean.RecordUtil;
|
||||
import org.dromara.hutool.core.convert.impl.*;
|
||||
import org.dromara.hutool.core.lang.Opt;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
import org.dromara.hutool.core.reflect.kotlin.KClassUtil;
|
||||
@@ -258,6 +259,12 @@ public class CompositeConverter extends RegisterConverter {
|
||||
return (T) ClassConverter.INSTANCE.convert(type, value);
|
||||
}
|
||||
|
||||
// 空值转空Bean
|
||||
if(ObjUtil.isEmpty(value)){
|
||||
// issue#3649 空值转空对象,则直接实例化
|
||||
return ConstructorUtil.newInstanceIfPossible(rowType);
|
||||
}
|
||||
|
||||
// 表示非需要特殊转换的对象
|
||||
return null;
|
||||
}
|
||||
|
@@ -12,40 +12,52 @@
|
||||
|
||||
package org.dromara.hutool.core.convert;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import lombok.Data;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* ConverterRegistry 单元测试
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class CompositeConverterTest {
|
||||
@Test
|
||||
void convertEmptyTest() {
|
||||
final Object convert = CompositeConverter.getInstance().convert(EmptyBean.class, "");
|
||||
assertNotNull(convert);
|
||||
assertEquals(new EmptyBean(), convert);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class EmptyBean {}
|
||||
|
||||
@Test
|
||||
public void getConverterTest() {
|
||||
final Converter converter = CompositeConverter.getInstance().getConverter(CharSequence.class, false);
|
||||
Assertions.assertNotNull(converter);
|
||||
assertNotNull(converter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTest(){
|
||||
public void customTest() {
|
||||
final int a = 454553;
|
||||
final CompositeConverter compositeConverter = CompositeConverter.getInstance();
|
||||
|
||||
CharSequence result = (CharSequence) compositeConverter.convert(CharSequence.class, a);
|
||||
Assertions.assertEquals("454553", result);
|
||||
assertEquals("454553", result);
|
||||
|
||||
//此处做为示例自定义CharSequence转换,因为Hutool中已经提供CharSequence转换,请尽量不要替换
|
||||
//替换可能引发关联转换异常(例如覆盖CharSequence转换会影响全局)
|
||||
compositeConverter.putCustom(CharSequence.class, new CustomConverter());
|
||||
result = (CharSequence) compositeConverter.convert(CharSequence.class, a);
|
||||
Assertions.assertEquals("Custom: 454553", result);
|
||||
assertEquals("Custom: 454553", result);
|
||||
}
|
||||
|
||||
public static class CustomConverter implements Converter{
|
||||
public static class CustomConverter implements Converter {
|
||||
@Override
|
||||
public Object convert(final Type targetType, final Object value) throws ConvertException {
|
||||
return "Custom: " + value.toString();
|
||||
|
@@ -34,7 +34,7 @@ public class Issue3136Test {
|
||||
final SmsRes smsRes = XmlUtil.xmlToBean(XmlUtil.parseXml(xmlStr).getDocumentElement(), SmsRes.class);
|
||||
|
||||
Assertions.assertEquals("02", smsRes.getCode());
|
||||
Assertions.assertNull(smsRes.getMessage());
|
||||
Assertions.assertEquals(new Message(), smsRes.getMessage());
|
||||
}
|
||||
|
||||
@Data
|
||||
|
@@ -293,6 +293,12 @@ public class JSONConverter implements Converter, Serializable {
|
||||
return (T) RecordConverter.INSTANCE.convert(type, value);
|
||||
}
|
||||
|
||||
// 空值转空Bean
|
||||
if(ObjUtil.isEmpty(value)){
|
||||
// issue#3649 空值转空对象,则直接实例化
|
||||
return ConstructorUtil.newInstanceIfPossible(rowType);
|
||||
}
|
||||
|
||||
// 表示非需要特殊转换的对象
|
||||
return null;
|
||||
}
|
||||
|
@@ -12,8 +12,7 @@
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.Data;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -23,14 +22,19 @@ public class Issue2564Test {
|
||||
* 实力类 没有get set方法,不能被认为是一个bean
|
||||
*/
|
||||
@Test()
|
||||
public void emptyToBeanTest(){
|
||||
final String x = "{}";
|
||||
final A a = JSONUtil.toBean(x, JSONConfig.of().setIgnoreError(true), A.class);
|
||||
public void emptyToBeanTest() {
|
||||
// 空对象转为一个空Bean,返回默认空实例
|
||||
String x = "{}" ;
|
||||
A a = JSONUtil.toBean(x, JSONConfig.of().setIgnoreError(true), A.class);
|
||||
Assertions.assertEquals(new A(), a);
|
||||
|
||||
// 非空对象转为一个空bean,转换失败
|
||||
x = "{\"a\": 1}" ;
|
||||
a = JSONUtil.toBean(x, JSONConfig.of().setIgnoreError(true), A.class);
|
||||
Assertions.assertNull(a);
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class A{
|
||||
@Data
|
||||
public static class A {
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import lombok.Data;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class Issue3649Test {
|
||||
@Test
|
||||
void toEmptyBeanTest() {
|
||||
final Object bean = JSONUtil.toBean("{}", JSONConfig.of().setIgnoreError(false), EmptyBean.class);
|
||||
Assertions.assertEquals(new EmptyBean(), bean);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class EmptyBean {}
|
||||
}
|
Reference in New Issue
Block a user