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

This commit is contained in:
Looly 2023-03-26 09:01:50 +08:00
parent b884cdaedd
commit 6394b41cb1
2 changed files with 16 additions and 2 deletions

View File

@ -161,7 +161,7 @@ public class ObjectMapper {
} else if (source instanceof JSONTokener) { } else if (source instanceof JSONTokener) {
mapFromTokener((JSONTokener) source, jsonArray, filter); mapFromTokener((JSONTokener) source, jsonArray, filter);
} else { } else {
Iterator<?> iter; final Iterator<?> iter;
if (ArrayUtil.isArray(source)) {// 数组 if (ArrayUtil.isArray(source)) {// 数组
iter = new ArrayIter<>(source); iter = new ArrayIter<>(source);
} else if (source instanceof Iterator<?>) {// Iterator } else if (source instanceof Iterator<?>) {// Iterator
@ -169,7 +169,11 @@ public class ObjectMapper {
} else if (source instanceof Iterable<?>) {// Iterable } else if (source instanceof Iterable<?>) {// Iterable
iter = ((Iterable<?>) source).iterator(); iter = ((Iterable<?>) source).iterator();
} else { } else {
throw new JSONException("JSONArray initial value should be a string or collection or array."); if(false == jsonArray.getConfig().isIgnoreError()){
throw new JSONException("JSONArray initial value should be a string or collection or array.");
}
// 如果用户选择跳过异常则跳过此值转换
return;
} }
final JSONConfig config = jsonArray.getConfig(); final JSONConfig config = jsonArray.getConfig();

View File

@ -31,6 +31,16 @@ public class JSONUtilTest {
Console.log(jsonArray); Console.log(jsonArray);
} }
/**
* 数字解析为JSONArray报错
*/
@Test
public void parseNumberToJSONArrayTest2() {
final JSONArray json = JSONUtil.parseArray(123L,
JSONConfig.create().setIgnoreError(true));
Assert.assertNotNull(json);
}
/** /**
* 数字解析为JSONArray报错 * 数字解析为JSONArray报错
*/ */