mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
修复JSONUtil.wrap忽略错误问题(issue#4210@Github)
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
### 🐣新特性
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【json 】 修复`JSONUtil.wrap`忽略错误问题(issue#4210@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.43(2026-01-04)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
45
hutool-json/src/test/java/cn/hutool/json/Issue4210Test.java
Normal file
45
hutool-json/src/test/java/cn/hutool/json/Issue4210Test.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user