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

@ -17,6 +17,7 @@
* 【core 】 优化TextSimilarity.longestCommonSubstring性能issue#I42A6V@Gitee
* 【core 】 MultipartRequestInputStream改为使用long以支持大文件issue#I428AN@Gitee
* 【core 】 RobotUtl增加getDelay、getRobot方法pr#1725@Github
* 【json 】 JSON输出支持ignoreNullissue#1728@Github
### 🐞Bug修复
* 【core 】 修复RobotUtil双击右键问题pr#1721@Github

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的值根据值类型不同输出不同内容

View File

@ -0,0 +1,34 @@
package cn.hutool.json;
import org.junit.Assert;
import org.junit.Test;
public class JSONNullTest {
@Test
public void parseNullTest(){
JSONObject bodyjson = JSONUtil.parseObj("{\n" +
" \"device_model\": null,\n" +
" \"device_status_date\": null,\n" +
" \"imsi\": null,\n" +
" \"act_date\": \"2021-07-23T06:23:26.000+00:00\"}");
Assert.assertEquals(JSONNull.class, bodyjson.get("device_model").getClass());
Assert.assertEquals(JSONNull.class, bodyjson.get("device_status_date").getClass());
Assert.assertEquals(JSONNull.class, bodyjson.get("imsi").getClass());
bodyjson.getConfig().setIgnoreNullValue(true);
Assert.assertEquals("{\"act_date\":\"2021-07-23T06:23:26.000+00:00\"}", bodyjson.toString());
}
@Test
public void parseNullTest2(){
JSONObject bodyjson = JSONUtil.parseObj("{\n" +
" \"device_model\": null,\n" +
" \"device_status_date\": null,\n" +
" \"imsi\": null,\n" +
" \"act_date\": \"2021-07-23T06:23:26.000+00:00\"}", true, true);
Assert.assertFalse(bodyjson.containsKey("device_model"));
Assert.assertFalse(bodyjson.containsKey("device_status_date"));
Assert.assertFalse(bodyjson.containsKey("imsi"));
}
}