mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复ignoreNullValue在JSONArray中无效问题
This commit is contained in:
parent
735f531e3c
commit
fe4e56fed7
@ -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 {
|
||||||
|
if(false == ignoreNullValue){
|
||||||
// issue#3286, 增加安全检查,最多增加10倍
|
// issue#3286, 增加安全检查,最多增加10倍
|
||||||
Validator.checkIndexLimit(index, this.size());
|
Validator.checkIndexLimit(index, this.size());
|
||||||
while (index != this.size()) {
|
while (index != this.size()) {
|
||||||
this.add(JSONNull.NULL);
|
this.add(JSONNull.NULL);
|
||||||
}
|
}
|
||||||
this.set(element);
|
}
|
||||||
|
this.add(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user