From f85f29174d41c40f847e0f6542bb0c2040344701 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 13 Jan 2022 10:57:33 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 3 ++- .../java/cn/hutool/core/io/file/FileWriter.java | 12 ++++++------ .../src/main/java/cn/hutool/json/JSONGetter.java | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1230e4e8a..1c51730b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,13 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.20 (2022-01-12) +# 5.7.20 (2022-01-13) ### 🐣新特性 * 【core 】 增加对null值友好的groupingBy操作的Collector实现,可指定map类型(pr#498@Gitee) * 【core 】 增加KetamaHash(issue#2084@Github) * 【crypto 】 增加SignUtil +* 【json 】 JSONGetter增加getBeanList方法 * ### 🐞Bug修复 * 【core 】 修复setter重载导致匹配错误(issue#2082@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/FileWriter.java b/hutool-core/src/main/java/cn/hutool/core/io/file/FileWriter.java index 2eb93cac2..7178b737d 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/FileWriter.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/FileWriter.java @@ -16,7 +16,6 @@ import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.charset.Charset; -import java.util.Collection; import java.util.Map; import java.util.Map.Entry; @@ -165,7 +164,7 @@ public class FileWriter extends FileWrapper { * @return 目标文件 * @throws IORuntimeException IO异常 */ - public File writeLines(Collection list) throws IORuntimeException { + public File writeLines(Iterable list) throws IORuntimeException { return writeLines(list, false); } @@ -177,7 +176,7 @@ public class FileWriter extends FileWrapper { * @return 目标文件 * @throws IORuntimeException IO异常 */ - public File appendLines(Collection list) throws IORuntimeException { + public File appendLines(Iterable list) throws IORuntimeException { return writeLines(list, true); } @@ -190,7 +189,7 @@ public class FileWriter extends FileWrapper { * @return 目标文件 * @throws IORuntimeException IO异常 */ - public File writeLines(Collection list, boolean isAppend) throws IORuntimeException { + public File writeLines(Iterable list, boolean isAppend) throws IORuntimeException { return writeLines(list, null, isAppend); } @@ -205,12 +204,13 @@ public class FileWriter extends FileWrapper { * @throws IORuntimeException IO异常 * @since 3.1.0 */ - public File writeLines(Collection list, LineSeparator lineSeparator, boolean isAppend) throws IORuntimeException { + public File writeLines(Iterable list, LineSeparator lineSeparator, boolean isAppend) throws IORuntimeException { try (PrintWriter writer = getPrintWriter(isAppend)) { for (T t : list) { if (null != t) { - writer.print(t); printNewLine(writer, lineSeparator); + writer.print(t); + writer.flush(); } } diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONGetter.java b/hutool-json/src/main/java/cn/hutool/json/JSONGetter.java index a7135b1dc..202406cf8 100644 --- a/hutool-json/src/main/java/cn/hutool/json/JSONGetter.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONGetter.java @@ -9,6 +9,7 @@ import cn.hutool.core.util.StrUtil; import java.time.LocalDateTime; import java.util.Date; +import java.util.List; import java.util.Optional; /** @@ -113,6 +114,21 @@ public interface JSONGetter extends OptNullBasicTypeFromObjectGetter { return (null == obj) ? null : obj.toBean(beanType); } + /** + * 从JSON中直接获取Bean的List列表
+ * 先获取JSONArray对象,然后转为Bean的List + * + * @param Bean类型 + * @param key KEY + * @param beanType Bean类型 + * @return Bean的List,如果值为null或者非JSONObject类型,返回null + * @since 5.7.20 + */ + default List getBeanList(K key, Class beanType) { + final JSONArray jsonArray = getJSONArray(key); + return (null == jsonArray) ? null : jsonArray.toList(beanType); + } + @Override default Date getDate(K key, Date defaultValue) { // 默认转换