enhance jsonwriter support ignoreNull

This commit is contained in:
Looly
2021-07-28 01:06:05 +08:00
parent 085453e7d8
commit 95d35527c2
4 changed files with 76 additions and 12 deletions

View File

@@ -556,7 +556,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
final JSONWriter jsonWriter = JSONWriter.of(writer, indentFactor, indent, config)
.beginObj();
this.forEach((key, value)-> jsonWriter.writeKey(key).writeValue(value));
this.forEach(jsonWriter::writeField);
jsonWriter.end();
return writer;

View File

@@ -142,23 +142,33 @@ public class JSONWriter extends Writer {
}
/**
* 写出值,自动处理分隔符和缩进,自动判断类型,并根据不同类型写出特定格式的值
* 写出值,自动处理分隔符和缩进,自动判断类型,并根据不同类型写出特定格式的值<br>
* 如果写出的值为{@code null}或者{@link JSONNull}且配置忽略null则跳过。
*
* @param value 值
* @return this
*/
public JSONWriter writeValue(Object value) {
if (arrayMode) {
if (needSeparator) {
writeRaw(CharUtil.COMMA);
}
// 换行缩进
writeLF().writeSpace(indentFactor + indent);
} else {
writeRaw(CharUtil.COLON).writeSpace(1);
if(JSONUtil.isNull(value) && config.isIgnoreNullValue()){
return this;
}
needSeparator = true;
return writeObjValue(value);
return writeValueDirect(value);
}
/**
* 写出字段名及字段值,如果字段值是{@code null}且忽略null值则不写出任何内容
*
* @param key 字段名
* @param value 字段值
* @return this
* @since 5.7.6
*/
public JSONWriter writeField(String key, Object value){
if(JSONUtil.isNull(value) && config.isIgnoreNullValue()){
return this;
}
return writeKey(key).writeValueDirect(value);
}
@Override
@@ -181,6 +191,25 @@ public class JSONWriter extends Writer {
}
// ------------------------------------------------------------------------------ Private methods
/**
* 写出值,自动处理分隔符和缩进,自动判断类型,并根据不同类型写出特定格式的值
*
* @param value 值
* @return this
*/
private JSONWriter writeValueDirect(Object value) {
if (arrayMode) {
if (needSeparator) {
writeRaw(CharUtil.COMMA);
}
// 换行缩进
writeLF().writeSpace(indentFactor + indent);
} else {
writeRaw(CharUtil.COLON).writeSpace(1);
}
needSeparator = true;
return writeObjValue(value);
}
/**
* 写出JSON的值根据值类型不同输出不同内容