diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java index 5cac6c295..96b19c52f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java @@ -135,7 +135,7 @@ public class ConverterRegistry implements Serializable { ServiceLoaderUtil.load(Converter.class).forEach(converter -> { try { Type type = TypeUtil.getTypeArgument(ClassUtil.getClass(converter)); - if(null != type){ + if (null != type) { putCustom(type, converter); } } catch (Exception e) { @@ -266,7 +266,6 @@ public class ConverterRegistry implements Serializable { } } - // 特殊类型转换,包括Collection、Map、强转、Array等 final T result = convertSpecial(type, rowType, value, defaultValue); if (null != result) { diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONNull.java b/hutool-json/src/main/java/cn/hutool/json/JSONNull.java index 93599a3cf..452bc36e6 100644 --- a/hutool-json/src/main/java/cn/hutool/json/JSONNull.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONNull.java @@ -7,10 +7,10 @@ import java.io.Serializable; /** * 用于定义{@code null},与Javascript中null相对应
* Java中的{@code null}值在js中表示为undefined。 - * @author Looly * + * @author Looly */ -public class JSONNull implements Serializable{ +public class JSONNull implements Serializable { private static final long serialVersionUID = 2633815155870764938L; /** @@ -35,7 +35,7 @@ public class JSONNull implements Serializable{ /** * Get the "null" string value. - *获得“null”字符串 + * 获得“null”字符串 * * @return The string "null". */ diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java b/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java index 6fb43e6db..59eb12d56 100644 --- a/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java @@ -743,7 +743,7 @@ public class JSONUtil { return jsonConfig.isIgnoreNullValue() ? null : JSONNull.NULL; } if (object instanceof JSON // - || JSONNull.NULL.equals(object) // + || ObjectUtil.isNull(object) // || object instanceof JSONString // || object instanceof CharSequence // || object instanceof Number // diff --git a/hutool-json/src/test/java/cn/hutool/json/Issue2131Test.java b/hutool-json/src/test/java/cn/hutool/json/Issue2131Test.java new file mode 100755 index 000000000..826a8274a --- /dev/null +++ b/hutool-json/src/test/java/cn/hutool/json/Issue2131Test.java @@ -0,0 +1,62 @@ +package cn.hutool.json; + +import cn.hutool.core.collection.ListUtil; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; +import org.junit.Assert; +import org.junit.Test; + +import java.beans.Transient; +import java.util.List; +import java.util.stream.Stream; + +/** + * https://github.com/dromara/hutool/issues/2131
+ * 字段定义成final,意味着setCollections无效,因此JSON转Bean的时候无法调用setCollections注入,所以是空的。 + */ +public class Issue2131Test { + + @Test + public void strToBean() { + GoodsResponse goodsResponse = new GoodsResponse(); + GoodsItem apple = new GoodsItem().setGoodsId(1L).setGoodsName("apple").setChannel("wechat"); + GoodsItem pear = new GoodsItem().setGoodsId(2L).setGoodsName("pear").setChannel("jd"); + final List collections = goodsResponse.getCollections(); + Stream.of(apple, pear).forEach(collections::add); + + String jsonStr = JSONUtil.toJsonStr(goodsResponse); + final JSONObject jsonObject = JSONUtil.parseObj(jsonStr); + + GoodsResponse result = jsonObject.toBean(GoodsResponse.class); + Assert.assertEquals(0, result.getCollections().size()); + } + + @Data + static class BaseResponse { + + @SuppressWarnings("unused") + @Transient + public final boolean successful() { + return code == 200 || code == 201; + } + + private int code = 200; + private String message; + } + + @EqualsAndHashCode(callSuper = true) + @Data + static class GoodsResponse extends BaseResponse { + // 由于定义成了final形式,setXXX无效,导致无法注入。 + private final List collections = ListUtil.list(false); + } + + @Data + @Accessors(chain = true) + static class GoodsItem{ + private long goodsId; + private String goodsName; + private String channel; + } +}