This commit is contained in:
Looly 2020-11-05 01:42:35 +08:00
parent 394d05f9d2
commit 637e1942f9
7 changed files with 107 additions and 3 deletions

View File

@ -486,6 +486,7 @@ public class Assert {
* </pre>
*
* @param <T> 数组元素类型
* @param <X> 异常类型
* @param array 被检查的数组
* @param errorSupplier 错误抛出异常附带的消息生产接口
* @return 被检查的数组

View File

@ -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)) {

View 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);
}
}

View File

@ -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<>();

View File

@ -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;
}
}
}

View File

@ -0,0 +1,15 @@
{
"items": [
[
[
{
"fromAccount": "test禾信-cd09dss",
"url": "https://m.baidu.com/s?wd=aaa",
"fromType": "搜索推广"
},
"去杭州旅游旅游攻略",
"杭州旅游攻略"
]
]
]
}

View File

@ -6,13 +6,19 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.StyleSet;
import cn.hutool.poi.excel.editors.TrimEditor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.ss.util.RegionUtil;
@ -379,6 +385,33 @@ public class CellUtil {
SheetUtil.getCell(sheet, y, x));
}
/**
* 为特定单元格添加批注
*
* @param cell 单元格
* @param commentText 批注内容
* @param commentAuthor 作者
* @param anchor 批注的位置大小等信息null表示使用默认
* @since 5.4.8
*/
public static void setComment(Cell cell, String commentText, String commentAuthor, ClientAnchor anchor) {
final Sheet sheet = cell.getSheet();
final Workbook wb = sheet.getWorkbook();
final Drawing<?> drawing = sheet.createDrawingPatriarch();
final CreationHelper factory = wb.getCreationHelper();
if (anchor == null) {
anchor = factory.createClientAnchor();
anchor.setCol1(cell.getColumnIndex() + 1);
anchor.setCol2(cell.getColumnIndex() + 3);
anchor.setRow1(cell.getRowIndex());
anchor.setRow2(cell.getRowIndex() + 2);
}
Comment comment = drawing.createCellComment(anchor);
comment.setString(factory.createRichTextString(commentText));
comment.setAuthor(StrUtil.nullToEmpty(commentText));
cell.setCellComment(comment);
}
// -------------------------------------------------------------------------------------------------------------- Private method start
/**