mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -77,9 +77,7 @@ public class JSONConverter implements Converter<JSON> {
|
||||
}
|
||||
}
|
||||
|
||||
final T targetValue = ignoreError ?
|
||||
Convert.convertQuietly(targetType, value):
|
||||
Convert.convert(targetType, value);
|
||||
final T targetValue = Convert.convertWithCheck(targetType, value, null, ignoreError);
|
||||
|
||||
if (null == targetValue && false == ignoreError) {
|
||||
if (StrUtil.isBlankIfStr(value)) {
|
||||
|
27
hutool-json/src/test/java/cn/hutool/json/Issue1200Test.java
Normal file
27
hutool-json/src/test/java/cn/hutool/json/Issue1200Test.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.json.test.bean.ResultBean;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 测试在bean转换时使用BeanConverter,默认忽略转换失败的字段。
|
||||
* 现阶段Converter的问题在于,无法更细粒度的控制转换失败的范围,例如Bean的一个字段为List,
|
||||
* list任意一个item转换失败都会导致这个list为null。
|
||||
*
|
||||
* TODO 需要在Converter中添加ConvertOption,用于更细粒度的控制转换规则
|
||||
*/
|
||||
public class Issue1200Test {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void toBeanTest(){
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(ResourceUtil.readUtf8Str("issue1200.json"));
|
||||
Console.log(jsonObject);
|
||||
|
||||
final ResultBean resultBean = jsonObject.toBean(ResultBean.class);
|
||||
Console.log(resultBean);
|
||||
}
|
||||
}
|
@@ -1,9 +1,11 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.json.test.bean.Exam;
|
||||
import cn.hutool.json.test.bean.JsonNode;
|
||||
@@ -157,6 +159,14 @@ public class JSONArrayTest {
|
||||
Assert.assertEquals("bvalue", list.get(1).getBkey());
|
||||
}
|
||||
|
||||
@Test(expected = ConvertException.class)
|
||||
public void toListWithErrorTest(){
|
||||
String json = "[['aaa',{'akey':'avalue','bkey':'bvalue'}]]";
|
||||
JSONArray ja = JSONUtil.parseArray(json);
|
||||
|
||||
List<List<KeyBean>> list = ja.toBean(new TypeReference<List<List<KeyBean>>>() {});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBeanListTest() {
|
||||
List<Map<String, String>> mapList = new ArrayList<>();
|
||||
|
@@ -0,0 +1,20 @@
|
||||
package cn.hutool.json.test.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ResultBean {
|
||||
public List<List<List<ItemsBean>>> items;
|
||||
|
||||
@Data
|
||||
public static class ItemsBean {
|
||||
public DetailBean detail;
|
||||
|
||||
@Data
|
||||
public static class DetailBean {
|
||||
public String visitorStatus;
|
||||
}
|
||||
}
|
||||
}
|
15
hutool-json/src/test/resources/issue1200.json
Normal file
15
hutool-json/src/test/resources/issue1200.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"items": [
|
||||
[
|
||||
[
|
||||
{
|
||||
"fromAccount": "test禾信-cd09dss",
|
||||
"url": "https://m.baidu.com/s?wd=aaa",
|
||||
"fromType": "搜索推广"
|
||||
},
|
||||
"去杭州旅游旅游攻略",
|
||||
"杭州旅游攻略"
|
||||
]
|
||||
]
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user