mirror of
				https://gitee.com/dromara/hutool.git
				synced 2025-10-31 16:36:56 +08:00 
			
		
		
		
	修复ignoreNullValue在JSONArray中无效问题
This commit is contained in:
		| @@ -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<Integer>, List<Object>, 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<Integer>, List<Object>, 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); | ||||
| 		} | ||||
|  | ||||
| 	} | ||||
|   | ||||
| @@ -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<KeyBean> 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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Looly
					Looly