From fe4e56fed7197a5de89d18bb25fae6cb849f8c78 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 5 Nov 2024 20:04:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DignoreNullValue=E5=9C=A8JSONA?= =?UTF-8?q?rray=E4=B8=AD=E6=97=A0=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/json/JSONArray.java | 18 +++++++++++++----- .../java/cn/hutool/json/JSONArrayTest.java | 9 +++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONArray.java b/hutool-json/src/main/java/cn/hutool/json/JSONArray.java index 1ee8183ee..8e0a709bb 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONArray.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONArray.java @@ -2,6 +2,7 @@ package cn.hutool.json; import cn.hutool.core.bean.BeanPath; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Filter; import cn.hutool.core.lang.Validator; import cn.hutool.core.lang.mutable.Mutable; @@ -445,12 +446,17 @@ public class JSONArray implements JSON, JSONGetter, List, Rando if (index >= size()) { add(index, element); + return null; } return this.rawList.set(index, JSONUtil.wrap(element, this.config)); } @Override public void add(int index, Object element) { + final boolean ignoreNullValue = config.isIgnoreNullValue(); + if (null == element && ignoreNullValue) { + return; + } if (index < 0) { throw new JSONException("JSONArray[{}] not found.", index); } @@ -458,12 +464,14 @@ public class JSONArray implements JSON, JSONGetter, List, Rando InternalJSONUtil.testValidity(element); this.rawList.add(index, JSONUtil.wrap(element, this.config)); } else { - // issue#3286, 增加安全检查,最多增加10倍 - Validator.checkIndexLimit(index, this.size()); - while (index != this.size()) { - this.add(JSONNull.NULL); + if(false == ignoreNullValue){ + // issue#3286, 增加安全检查,最多增加10倍 + Validator.checkIndexLimit(index, this.size()); + while (index != this.size()) { + this.add(JSONNull.NULL); + } } - this.set(element); + this.add(element); } } diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java index aa3fa67ac..05b8f7825 100755 --- a/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java +++ b/hutool-json/src/test/java/cn/hutool/json/JSONArrayTest.java @@ -167,7 +167,7 @@ public class JSONArrayTest { @Test public void toListWithNullTest() { String json = "[null,{'akey':'avalue','bkey':'bvalue'}]"; - JSONArray ja = JSONUtil.parseArray(json); + JSONArray ja = JSONUtil.parseArray(json, JSONConfig.create().setIgnoreNullValue(false)); List list = ja.toList(KeyBean.class); assertNull(list.get(0)); @@ -221,10 +221,15 @@ public class JSONArrayTest { @Test public void putToIndexTest() { - final JSONArray jsonArray = new JSONArray(); + JSONArray jsonArray = new JSONArray(JSONConfig.create().setIgnoreNullValue(false)); jsonArray.put(3, "test"); // 第三个位置插入值,0~2都是null 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