当用户选择ignoreError时,错误对象转JSON也忽略

This commit is contained in:
Looly
2023-03-26 06:02:22 +08:00
parent 1ac0a904c0
commit 317d8ee527
3 changed files with 7 additions and 3 deletions

View File

@@ -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());
}
// 如果用户选择跳过异常,则跳过此值转换
}
}

View File

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