diff --git a/CHANGELOG.md b/CHANGELOG.md index a5869a38c..37bcbb525 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * 【core 】 StrUtil增加commonPrefix和commonSuffix方法(pr#3007@Github) * 【core 】 NumberUtil增加重载parseXXX方法, 解析失败返回默认值(pr#3007@Github) * 【core 】 FileUtil增加readLines重载,支持filter(pr#3006@Github) +* 【json 】 当用户选择ignoreError时,错误对象转JSON也忽略 ### 🐞Bug修复 * 【crypto】 修复NoSuchMethodError未捕获问题(issue#2966@Github) diff --git a/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java b/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java index 9261e837b..f44332caa 100755 --- a/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java +++ b/hutool-json/src/main/java/cn/hutool/json/ObjectMapper.java @@ -113,8 +113,11 @@ public class ObjectMapper { // TODO 过滤器对Bean无效,需补充。 mapFromBean(source, jsonObject); } else { - // 不支持对象类型转换为JSONObject - throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass()); + if(false == jsonObject.getConfig().isIgnoreError()){ + // 不支持对象类型转换为JSONObject + throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass()); + } + // 如果用户选择跳过异常,则跳过此值转换 } } diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java index d6ae6adf6..0816ff98d 100644 --- a/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java +++ b/hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java @@ -45,7 +45,7 @@ public class JSONUtilTest { */ @Test public void parseNumberTest2() { - final JSONObject json = JSONUtil.parseObj(123L); + final JSONObject json = JSONUtil.parseObj(123L, JSONConfig.create().setIgnoreError(true)); Assert.assertEquals(new JSONObject(), json); }