From 915f7c429b21703391f45d40a4b653f061f2262c Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 12 Jan 2026 09:47:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D`JSONUtil.wrap`=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98=EF=BC=88issue#421?= =?UTF-8?q?0@Github=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../main/java/cn/hutool/json/JSONUtil.java | 8 +++- .../java/cn/hutool/json/Issue4210Test.java | 45 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 hutool-json/src/test/java/cn/hutool/json/Issue4210Test.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 67654e5180..83d92d8211 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### 🐣新特性 ### 🐞Bug修复 +* 【json 】 修复`JSONUtil.wrap`忽略错误问题(issue#4210@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.43(2026-01-04) 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 0051370f85..6ec902bb17 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java @@ -739,7 +739,7 @@ public class JSONUtil { *
  • map =》 JSONObject
  • *
  • standard property (Double, String, et al) =》 原对象
  • *
  • 来自于java包 =》 字符串
  • - *
  • 其它 =》 尝试包装为JSONObject,否则返回{@code null}
  • + *
  • 其它 =》 尝试包装为JSONObject,如果{@link JSONConfig#isIgnoreError()}为true时返回{@code null}, 否则抛出异常
  • * * * @param object 被包装的对象 @@ -805,7 +805,11 @@ public class JSONUtil { // 默认按照JSONObject对待 return new JSONObject(object, jsonConfig); } catch (final Exception exception) { - return null; + // issue#4210 只有设置忽略错误时才返回null + if(jsonConfig.isIgnoreError()){ + return null; + } + throw exception; } } diff --git a/hutool-json/src/test/java/cn/hutool/json/Issue4210Test.java b/hutool-json/src/test/java/cn/hutool/json/Issue4210Test.java new file mode 100644 index 0000000000..3a0813a4cd --- /dev/null +++ b/hutool-json/src/test/java/cn/hutool/json/Issue4210Test.java @@ -0,0 +1,45 @@ +package cn.hutool.json; + +import cn.hutool.core.exceptions.InvocationTargetRuntimeException; +import cn.hutool.core.lang.Console; +import cn.hutool.core.lang.ObjectId; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.Serializable; + +public class Issue4210Test { + + @Test + void setValueTest(){ + final JSONObject jsonObject = new JSONObject(); + + final TestEntity entity = new TestEntity("张三", "社畜"); + Assertions.assertThrows(InvocationTargetRuntimeException.class, () -> jsonObject.set("entity",entity)); + } + + @Data + public static class BaseEntity implements Serializable { + + /** + * 实体唯一标识符 + */ + private String _id; + private ObjectId id; + + public String get_id() { + return _id == null ? id.toString() : _id; + } + } + + @EqualsAndHashCode(callSuper = true) + @Data + @AllArgsConstructor + public class TestEntity extends BaseEntity{ + private String name; + private String desc; + } +}