修复JSONBeanParser在遇到List时没有被正确递归问题

This commit is contained in:
Looly 2023-07-20 23:39:24 +08:00
parent b74f3f1e3e
commit 4624b16751
4 changed files with 63 additions and 73 deletions

View File

@ -29,6 +29,7 @@
* 【jwt 】 修复JWTSignerUtil中ES256签名不符合规范问题issue#3205@Github
* 【core 】 修复UserInfo获取country问题issue#I7MCKW@Gitee
* 【extra 】 修复MVEL加载错误问题issue#3214@Github
* 【json 】 修复JSONBeanParser在遇到List时没有被正确递归问题issue#I7M2GZ@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.20(2023-06-16)

View File

@ -6,6 +6,7 @@ import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.bean.copier.ValueProvider;
import cn.hutool.core.convert.AbstractConverter;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapProxy;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
@ -67,6 +68,16 @@ public class BeanConverter<T> extends AbstractConverter<T> {
@Override
protected T convertInternal(Object value) {
final Class<?>[] interfaces = this.beanClass.getInterfaces();
for (Class<?> anInterface : interfaces) {
if("cn.hutool.json.JSONBeanParser".equals(anInterface.getName())){
// issue#I7M2GZ
final T obj = ReflectUtil.newInstanceIfPossible(this.beanClass);
ReflectUtil.invoke(obj, "parse", value);
return obj;
}
}
if(value instanceof Map ||
value instanceof ValueProvider ||
BeanUtil.isBean(value.getClass())) {

View File

@ -0,0 +1,51 @@
package cn.hutool.json;
import cn.hutool.core.lang.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* https://gitee.com/dromara/hutool/issues/I7M2GZ
*/
public class IssueI7M2GZTest {
@Data
@AllArgsConstructor
public static class JSONBeanParserImpl implements JSONBeanParser<Object> {
private String name;
private Integer parsed;
@Override
public void parse(final Object object) {
setName("new Object");
setParsed(12);
}
}
@Data
public static class MyEntity<T> {
private List<T> list;
}
@Test
public void toListTest() {
final List<JSONBeanParserImpl> list = new ArrayList<>();
list.add(new JSONBeanParserImpl("Object1", 1));
final MyEntity<JSONBeanParserImpl> entity = new MyEntity<>();
entity.setList(list);
final String json = JSONUtil.toJsonStr(entity);
//Console.log(json);
final MyEntity<JSONBeanParserImpl> result = JSONUtil.toBean(json, new TypeReference<MyEntity<JSONBeanParserImpl>>() {
}, false);
Assert.assertEquals("new Object", result.getList().get(0).getName());
Assert.assertNotNull(result.getList().get(0).getParsed());
Assert.assertEquals(Integer.valueOf(12), result.getList().get(0).getParsed());
}
}

View File

@ -1,73 +0,0 @@
package cn.hutool.json;
import cn.hutool.core.lang.TypeReference;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* https://gitee.com/dromara/hutool/issues/I7M2GZ
*/
public class issueI7M2GZTest {
public static class JSONBeanParserImpl implements JSONBeanParser {
private String name;
private Boolean isParsed;
public Boolean getParsed() {
return isParsed;
}
public void setParsed(Boolean parsed) {
isParsed = parsed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public JSONBeanParserImpl() {
}
public JSONBeanParserImpl(String name) {
this.name = name;
}
@Override
public void parse(Object object) {
setParsed(true);
}
}
public static class MyEntity<T> {
private List<T> list;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
@Test
public void toListTest() {
List<JSONBeanParserImpl> list = new ArrayList<>();
list.add(new JSONBeanParserImpl("Object1"));
MyEntity<JSONBeanParserImpl> entity = new MyEntity<>();
entity.setList(list);
String json = JSONUtil.toJsonStr(entity);
MyEntity<JSONBeanParserImpl> result = JSONUtil.toBean(json, new TypeReference<MyEntity<JSONBeanParserImpl>>() {
}, false);
Assert.assertEquals("Object1", result.getList().get(0).getName());
Assert.assertNotNull(result.getList().get(0).getParsed());
}
}