修复ignoreNullValue在JSONArray中无效问题

This commit is contained in:
Looly 2024-11-05 20:04:57 +08:00
parent 735f531e3c
commit fe4e56fed7
2 changed files with 20 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package cn.hutool.json;
import cn.hutool.core.bean.BeanPath; import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Filter; import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.Validator; import cn.hutool.core.lang.Validator;
import cn.hutool.core.lang.mutable.Mutable; import cn.hutool.core.lang.mutable.Mutable;
@ -445,12 +446,17 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
if (index >= size()) { if (index >= size()) {
add(index, element); add(index, element);
return null;
} }
return this.rawList.set(index, JSONUtil.wrap(element, this.config)); return this.rawList.set(index, JSONUtil.wrap(element, this.config));
} }
@Override @Override
public void add(int index, Object element) { public void add(int index, Object element) {
final boolean ignoreNullValue = config.isIgnoreNullValue();
if (null == element && ignoreNullValue) {
return;
}
if (index < 0) { if (index < 0) {
throw new JSONException("JSONArray[{}] not found.", index); throw new JSONException("JSONArray[{}] not found.", index);
} }
@ -458,12 +464,14 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
InternalJSONUtil.testValidity(element); InternalJSONUtil.testValidity(element);
this.rawList.add(index, JSONUtil.wrap(element, this.config)); this.rawList.add(index, JSONUtil.wrap(element, this.config));
} else { } else {
// issue#3286, 增加安全检查最多增加10倍 if(false == ignoreNullValue){
Validator.checkIndexLimit(index, this.size()); // issue#3286, 增加安全检查最多增加10倍
while (index != this.size()) { Validator.checkIndexLimit(index, this.size());
this.add(JSONNull.NULL); while (index != this.size()) {
this.add(JSONNull.NULL);
}
} }
this.set(element); this.add(element);
} }
} }

View File

@ -167,7 +167,7 @@ public class JSONArrayTest {
@Test @Test
public void toListWithNullTest() { public void toListWithNullTest() {
String json = "[null,{'akey':'avalue','bkey':'bvalue'}]"; String json = "[null,{'akey':'avalue','bkey':'bvalue'}]";
JSONArray ja = JSONUtil.parseArray(json); JSONArray ja = JSONUtil.parseArray(json, JSONConfig.create().setIgnoreNullValue(false));
List<KeyBean> list = ja.toList(KeyBean.class); List<KeyBean> list = ja.toList(KeyBean.class);
assertNull(list.get(0)); assertNull(list.get(0));
@ -221,10 +221,15 @@ public class JSONArrayTest {
@Test @Test
public void putToIndexTest() { public void putToIndexTest() {
final JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray(JSONConfig.create().setIgnoreNullValue(false));
jsonArray.put(3, "test"); jsonArray.put(3, "test");
// 第三个位置插入值0~2都是null // 第三个位置插入值0~2都是null
assertEquals(4, jsonArray.size()); assertEquals(4, jsonArray.size());
jsonArray = new JSONArray(JSONConfig.create().setIgnoreNullValue(true));
jsonArray.put(3, "test");
// 第三个位置插入值忽略null则追加
assertEquals(1, jsonArray.size());
} }
// https://github.com/dromara/hutool/issues/1858 // https://github.com/dromara/hutool/issues/1858