mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-06 13:54:03 +08:00
[update] jsonUtil优化
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
This commit is contained in:
parent
127c6902bf
commit
8d364b99f2
@ -1,5 +1,14 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
@ -24,15 +33,6 @@ import com.yexuejc.base.converter.LocalDateTimeSerializer;
|
||||
import com.yexuejc.base.converter.TimestampDeserializer;
|
||||
import com.yexuejc.base.converter.TimestampSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* json工具类,基于jackson
|
||||
*
|
||||
@ -42,7 +42,7 @@ import java.util.logging.Logger;
|
||||
* @date 2018/9/3 15:28
|
||||
*/
|
||||
public class JsonUtil {
|
||||
private static Logger log = Logger.getLogger(JsonUtil.class.getName());
|
||||
private static final Logger log = Logger.getLogger(JsonUtil.class.getName());
|
||||
|
||||
private JsonUtil() {
|
||||
}
|
||||
@ -50,10 +50,9 @@ public class JsonUtil {
|
||||
/**
|
||||
* 作为单例全局使用
|
||||
*/
|
||||
private static JsonMapper jsonMapper = new JsonMapper();
|
||||
private static final JsonMapper jsonMapper = new JsonMapper();
|
||||
|
||||
static {
|
||||
JsonUtil.setJavaTimeModule(JsonUtil.jsonMapper);
|
||||
JsonUtil.initDefaultObjectMapper(JsonUtil.jsonMapper);
|
||||
}
|
||||
|
||||
@ -94,14 +93,17 @@ public class JsonUtil {
|
||||
* </pre>
|
||||
*
|
||||
* @param jsonMapper
|
||||
* @return
|
||||
*/
|
||||
private static void initDefaultObjectMapper(ObjectMapper jsonMapper) {
|
||||
private static ObjectMapper initDefaultObjectMapper(ObjectMapper jsonMapper) {
|
||||
JsonUtil.setJavaTimeModule(jsonMapper);
|
||||
//值为空时,NON_NULL:舍去字段;ALWAYS:保留字段
|
||||
jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
jsonMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
||||
jsonMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||
jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
return jsonMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -121,8 +123,7 @@ public class JsonUtil {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ObjectMapper acceptEmptyStringAsNullObject() {
|
||||
JsonMapper jsonMapper = new JsonMapper();
|
||||
public static ObjectMapper acceptEmptyStringAsNullObject(JsonMapper jsonMapper) {
|
||||
setJavaTimeModule(jsonMapper);
|
||||
//值为空时,ALWAYS:保留字段,默认字符串值为“”,对象值为null
|
||||
jsonMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
|
||||
|
Loading…
x
Reference in New Issue
Block a user