mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add ignoreNullElement
This commit is contained in:
parent
7e09bf4c1b
commit
dfacb2c024
@ -215,7 +215,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
}
|
||||
final List<JSON> list = new ArrayList<>(c.size());
|
||||
for (final JSON json : c) {
|
||||
if (null == json && config().isIgnoreNullValue()) {
|
||||
if (null == json && config().isIgnoreNullElement()) {
|
||||
continue;
|
||||
}
|
||||
list.add(json);
|
||||
@ -245,7 +245,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
*/
|
||||
@Override
|
||||
public JSON set(final int index, final JSON element) {
|
||||
if (null == element && config().isIgnoreNullValue()) {
|
||||
if (null == element && config().isIgnoreNullElement()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -266,7 +266,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
// region ----- add
|
||||
@Override
|
||||
public boolean add(final JSON element) {
|
||||
if (null == element && config().isIgnoreNullValue()) {
|
||||
if (null == element && config().isIgnoreNullElement()) {
|
||||
return false;
|
||||
}
|
||||
return super.add(element);
|
||||
@ -274,7 +274,7 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
|
||||
@Override
|
||||
public void add(int index, final JSON element) {
|
||||
final boolean ignoreNullValue = config().isIgnoreNullValue();
|
||||
final boolean ignoreNullValue = config().isIgnoreNullElement();
|
||||
if (null == element && ignoreNullValue) {
|
||||
return;
|
||||
}
|
||||
|
@ -51,11 +51,20 @@ public class JSONConfig implements Serializable {
|
||||
* 是否忽略null值<br>
|
||||
* 此选项主要作用于两个阶段:
|
||||
* <ol>
|
||||
* <li>Java对象或JSON字符串转为JSON时</li>
|
||||
* <li>JSON写出或转为JSON字符串时</li>
|
||||
* <li>Java对象或JSON字符串转为JSONObject时</li>
|
||||
* <li>JSONObject写出或转为JSON字符串时</li>
|
||||
* </ol>
|
||||
*/
|
||||
private boolean ignoreNullValue = true;
|
||||
/**
|
||||
* 是否忽略null节点,即在JSONArray中null节点是否忽略,默认false<br>
|
||||
* 此选项主要作用于两个阶段:
|
||||
* <ol>
|
||||
* <li>Java对象或JSON字符串转为JSONArray时</li>
|
||||
* <li>JSONArray写出或转为JSON字符串时</li>
|
||||
* </ol>
|
||||
*/
|
||||
private boolean ignoreNullElement = false;
|
||||
/**
|
||||
* 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
||||
*/
|
||||
@ -214,6 +223,36 @@ public class JSONConfig implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略null节点,即在JSONArray中null节点是否忽略,默认false<br>
|
||||
* 此选项主要作用于两个阶段:
|
||||
* <ol>
|
||||
* <li>Java对象或JSON字符串转为JSONArray时</li>
|
||||
* <li>JSONArray写出或转为JSON字符串时</li>
|
||||
* </ol>
|
||||
*
|
||||
* @return 是否忽略null节点
|
||||
*/
|
||||
public boolean isIgnoreNullElement() {
|
||||
return this.ignoreNullElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否忽略null节点,即在JSONArray中null节点是否忽略,默认false<br>
|
||||
* 此选项主要作用于两个阶段:
|
||||
* <ol>
|
||||
* <li>Java对象或JSON字符串转为JSONArray时</li>
|
||||
* <li>JSONArray写出或转为JSON字符串时</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param ignoreNullElement 是否忽略null节点
|
||||
* @return this
|
||||
*/
|
||||
public JSONConfig setIgnoreNullElement(final boolean ignoreNullElement) {
|
||||
this.ignoreNullElement = ignoreNullElement;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
||||
*
|
||||
@ -309,6 +348,7 @@ public class JSONConfig implements Serializable {
|
||||
* <li>零宽连接符:{@code \u200D}</li>
|
||||
* <li>零宽无断空格:{@code \uFEFF}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return 此值为{@code false},则转义,否则去除
|
||||
*/
|
||||
public boolean isIgnoreZeroWithChar() {
|
||||
@ -323,6 +363,7 @@ public class JSONConfig implements Serializable {
|
||||
* <li>零宽连接符:{@code \u200D}</li>
|
||||
* <li>零宽无断空格:{@code \uFEFF}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param ignoreZeroWithChar 此值为{@code false},则转义,否则去除
|
||||
* @return this
|
||||
*/
|
||||
|
@ -23,10 +23,10 @@ import org.dromara.hutool.core.regex.ReUtil;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.support.InternalJSONUtil;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.support.InternalJSONUtil;
|
||||
import org.dromara.hutool.json.support.JSONFormatStyle;
|
||||
|
||||
import java.io.Closeable;
|
||||
@ -198,10 +198,6 @@ public class JSONWriter implements Appendable, Flushable, Closeable {
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public JSONWriter writeField(final MutableEntry<Object, Object> pair) {
|
||||
if (null == pair.getValue() && config.isIgnoreNullValue()) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (null != predicate) {
|
||||
if (!predicate.test(pair)) {
|
||||
// 使用修改后的键值对
|
||||
@ -210,14 +206,22 @@ public class JSONWriter implements Appendable, Flushable, Closeable {
|
||||
}
|
||||
|
||||
final Object key = pair.getKey();
|
||||
final Object value = pair.getValue();
|
||||
|
||||
if (key instanceof Integer) {
|
||||
if(null == value && config.isIgnoreNullElement()){
|
||||
return this;
|
||||
}
|
||||
// 数组模式,只写出值
|
||||
return writeValueDirect(pair.getValue(), true);
|
||||
return writeValueDirect(value, true);
|
||||
}
|
||||
|
||||
if(null == value && config.isIgnoreNullValue()){
|
||||
return this;
|
||||
}
|
||||
// 键值对模式
|
||||
writeKey(StrUtil.toString(key));
|
||||
return writeValueDirect(pair.getValue(), false);
|
||||
return writeValueDirect(value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,7 +60,7 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void addNullTest() {
|
||||
final List<String> aaa = ListUtil.view("aaa", null);
|
||||
final String jsonStr = JSONUtil.toJsonStr(JSONUtil.parse(aaa, JSONConfig.of().setIgnoreNullValue(false)));
|
||||
final String jsonStr = JSONUtil.toJsonStr(JSONUtil.parse(aaa, JSONConfig.of().setIgnoreNullElement(false)));
|
||||
assertEquals("[\"aaa\",null]", jsonStr);
|
||||
}
|
||||
|
||||
@ -186,7 +186,7 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void toListWithNullTest() {
|
||||
final String json = "[null,{'akey':'avalue','bkey':'bvalue'}]";
|
||||
final JSONArray ja = JSONUtil.parseArray(json, JSONConfig.of().setIgnoreNullValue(false));
|
||||
final JSONArray ja = JSONUtil.parseArray(json, JSONConfig.of().setIgnoreNullElement(false));
|
||||
|
||||
final List<KeyBean> list = ja.toList(KeyBean.class);
|
||||
Assertions.assertNull(list.get(0));
|
||||
@ -239,12 +239,12 @@ public class JSONArrayTest {
|
||||
|
||||
@Test
|
||||
public void putToIndexTest() {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
JSONArray jsonArray = new JSONArray(JSONConfig.of().setIgnoreNullElement(true));
|
||||
jsonArray.setValue(3, "test");
|
||||
// 默认忽略null值,因此空位无值,只有一个值
|
||||
// 忽略null值,因此空位无值,只有一个值
|
||||
assertEquals(1, jsonArray.size());
|
||||
|
||||
jsonArray = new JSONArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||
jsonArray = new JSONArray(JSONConfig.of().setIgnoreNullElement(false));
|
||||
jsonArray.setValue(2, "test");
|
||||
// 第三个位置插入值,0~2都是null
|
||||
assertEquals(3, jsonArray.size());
|
||||
@ -299,7 +299,7 @@ public class JSONArrayTest {
|
||||
|
||||
@Test
|
||||
public void putNullTest() {
|
||||
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullElement(false));
|
||||
array.addNull();
|
||||
|
||||
assertEquals("[null]", array.toString());
|
||||
|
@ -62,11 +62,11 @@ public class JSONNullTest {
|
||||
@Test
|
||||
public void setNullOfJSONArrayTest(){
|
||||
// 忽略null
|
||||
String json1 = JSONUtil.ofArray().addNull().toString();
|
||||
String json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullElement(true)).addNull().toString();
|
||||
Assertions.assertEquals("[]", json1);
|
||||
|
||||
// 不忽略null
|
||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).addNull().toString();
|
||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullElement(false)).addNull().toString();
|
||||
Assertions.assertEquals("[null]", json1);
|
||||
}
|
||||
}
|
||||
|
@ -360,7 +360,7 @@ public class JSONUtilTest {
|
||||
*/
|
||||
@Test
|
||||
public void testArrayEntity() {
|
||||
final String jsonStr = JSONUtil.toJsonStr(new ArrayEntity());
|
||||
final String jsonStr = JSONUtil.toJsonStr(new ArrayEntity(), JSONConfig.of().setIgnoreNullElement(true));
|
||||
// a为空的bytes数组,按照空的流对待
|
||||
assertEquals("{\"a\":[],\"b\":[0],\"c\":[],\"d\":[],\"e\":[]}", jsonStr);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user