mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add ignoreNullElement
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user