mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
add config
This commit is contained in:
parent
979bbe2905
commit
c9cac93337
@ -92,21 +92,26 @@ public class FastJSON2Engine extends AbstractJSONEngine {
|
||||
|
||||
@Override
|
||||
protected void initEngine() {
|
||||
JSONEngineConfig config;
|
||||
if(null == this.readerContext){
|
||||
this.readerContext = JSONFactory.createReadContext();
|
||||
final String dateFormat = ObjUtil.defaultIfNull(this.config, JSONEngineConfig::getDateFormat, "millis");
|
||||
this.readerContext.setDateFormat(ObjUtil.defaultIfNull(dateFormat, "millis"));
|
||||
|
||||
config = ObjUtil.defaultIfNull(this.config, JSONEngineConfig::of);
|
||||
this.readerContext.setDateFormat(ObjUtil.defaultIfNull(config.getDateFormat(), "millis"));
|
||||
}
|
||||
if(null == this.writerContext){
|
||||
final List<JSONWriter.Feature> features = ListUtil.of();
|
||||
if(ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)){
|
||||
config = ObjUtil.defaultIfNull(this.config, JSONEngineConfig::of);
|
||||
if(config.isPrettyPrint()){
|
||||
features.add(JSONWriter.Feature.PrettyFormat);
|
||||
}
|
||||
if(!config.isIgnoreNullValue()){
|
||||
features.add(JSONWriter.Feature.WriteMapNullValue);
|
||||
}
|
||||
this.writerContext = JSONFactory.createWriteContext(features.toArray(new JSONWriter.Feature[0]));
|
||||
|
||||
// 自定义配置
|
||||
final String dateFormat = ObjUtil.defaultIfNull(this.config, JSONEngineConfig::getDateFormat, "millis");
|
||||
this.writerContext.setDateFormat(ObjUtil.defaultIfNull(dateFormat, "millis"));
|
||||
// 自定义其它配置
|
||||
this.writerContext.setDateFormat(ObjUtil.defaultIfNull(config.getDateFormat(), "millis"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,11 +78,13 @@ public class GsonEngine extends AbstractJSONEngine {
|
||||
return;
|
||||
}
|
||||
|
||||
// 自定义配置
|
||||
final JSONEngineConfig config = ObjUtil.defaultIfNull(this.config, JSONEngineConfig::of);
|
||||
final GsonBuilder builder = new GsonBuilder();
|
||||
if (ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)) {
|
||||
if (config.isPrettyPrint()) {
|
||||
builder.setPrettyPrinting();
|
||||
}
|
||||
final String dateFormat = ObjUtil.apply(this.config, JSONEngineConfig::getDateFormat);
|
||||
final String dateFormat = config.getDateFormat();
|
||||
if (StrUtil.isNotEmpty(dateFormat)) {
|
||||
builder.setDateFormat(dateFormat);
|
||||
builder.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, typeOfT, context) -> TimeUtil.parse(json.getAsString(), dateFormat));
|
||||
@ -95,6 +97,9 @@ public class GsonEngine extends AbstractJSONEngine {
|
||||
builder.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, typeOfT, context) -> TimeUtil.of(json.getAsJsonPrimitive().getAsLong()));
|
||||
builder.registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (date, type, jsonSerializationContext) -> new JsonPrimitive(TimeUtil.toEpochMilli(date)));
|
||||
}
|
||||
if(!config.isIgnoreNullValue()){
|
||||
builder.serializeNulls();
|
||||
}
|
||||
|
||||
this.gson = builder.create();
|
||||
}
|
||||
|
@ -64,6 +64,7 @@ public class HutoolJSONEngine extends AbstractJSONEngine {
|
||||
final JSONConfig hutoolSJONConfig = JSONConfig.of();
|
||||
if(null != this.config){
|
||||
hutoolSJONConfig.setDateFormat(this.config.getDateFormat());
|
||||
hutoolSJONConfig.setIgnoreNullValue(this.config.isIgnoreNullValue());
|
||||
}
|
||||
|
||||
this.hutoolSJONConfig = hutoolSJONConfig;
|
||||
|
@ -44,6 +44,10 @@ public class JSONEngineConfig implements Serializable {
|
||||
* 日期格式,null表示默认的时间戳
|
||||
*/
|
||||
private String dateFormat;
|
||||
/**
|
||||
* 是否忽略null值
|
||||
*/
|
||||
private boolean ignoreNullValue = true;
|
||||
|
||||
/**
|
||||
* 获取是否启用格式化输出
|
||||
@ -85,4 +89,24 @@ public class JSONEngineConfig implements Serializable {
|
||||
this.dateFormat = dateFormat;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略null值
|
||||
*
|
||||
* @return 是否忽略null值
|
||||
*/
|
||||
public boolean isIgnoreNullValue() {
|
||||
return this.ignoreNullValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否忽略null值
|
||||
*
|
||||
* @param ignoreNullValue 是否忽略null值
|
||||
* @return this
|
||||
*/
|
||||
public JSONEngineConfig setIgnoreNullValue(final boolean ignoreNullValue) {
|
||||
this.ignoreNullValue = ignoreNullValue;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
@ -112,18 +113,22 @@ public class JacksonEngine extends AbstractJSONEngine {
|
||||
JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES
|
||||
);
|
||||
|
||||
// 支持Java8+日期格式
|
||||
registerModule(mapper, "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule");
|
||||
|
||||
// 自定义配置
|
||||
if (ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)) {
|
||||
final JSONEngineConfig config = ObjUtil.defaultIfNull(this.config, JSONEngineConfig::of);
|
||||
if(config.isPrettyPrint()){
|
||||
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
}
|
||||
final String dateFormat = ObjUtil.apply(this.config, JSONEngineConfig::getDateFormat);
|
||||
final String dateFormat = config.getDateFormat();
|
||||
if(StrUtil.isNotEmpty(dateFormat)){
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
mapper.setDateFormat(DateUtil.newSimpleFormat(dateFormat));
|
||||
}
|
||||
|
||||
// 支持Java8+日期格式
|
||||
registerModule(mapper, "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule");
|
||||
if(config.isIgnoreNullValue()){
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
}
|
||||
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
@ -36,4 +36,17 @@ public class FastJSONTest {
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("fastjson");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
@ -37,4 +37,17 @@ public class GsonTest {
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("gson");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
@ -33,4 +33,17 @@ public class HutoolJSONTest {
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("hutool");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
@ -40,4 +40,17 @@ public class JacksonTest {
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01T01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("jackson");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user