修复JSONUtil.wrap忽略错误问题(issue#4210@Github)

This commit is contained in:
Looly
2026-01-12 09:47:23 +08:00
parent 6c6d6f6ed2
commit 915f7c429b
3 changed files with 52 additions and 2 deletions

View File

@@ -5,6 +5,7 @@
### 🐣新特性
### 🐞Bug修复
* 【json 】 修复`JSONUtil.wrap`忽略错误问题issue#4210@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.43(2026-01-04)

View File

@@ -739,7 +739,7 @@ public class JSONUtil {
* <li>map =》 JSONObject</li>
* <li>standard property (Double, String, et al) =》 原对象</li>
* <li>来自于java包 =》 字符串</li>
* <li>其它 =》 尝试包装为JSONObject否则返回{@code null}</li>
* <li>其它 =》 尝试包装为JSONObject如果{@link JSONConfig#isIgnoreError()}为true时返回{@code null}, 否则抛出异常</li>
* </ul>
*
* @param object 被包装的对象
@@ -805,8 +805,12 @@ public class JSONUtil {
// 默认按照JSONObject对待
return new JSONObject(object, jsonConfig);
} catch (final Exception exception) {
// issue#4210 只有设置忽略错误时才返回null
if(jsonConfig.isIgnoreError()){
return null;
}
throw exception;
}
}
/**

View File

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