[update] jsonUtil优化

Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
This commit is contained in:
夜雪剑辰 2024-02-22 11:39:48 +00:00 committed by Gitee
parent 127c6902bf
commit 8d364b99f2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -1,5 +1,14 @@
package com.yexuejc.base.util; 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.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException; 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.TimestampDeserializer;
import com.yexuejc.base.converter.TimestampSerializer; 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 * json工具类基于jackson
* *
@ -42,7 +42,7 @@ import java.util.logging.Logger;
* @date 2018/9/3 15:28 * @date 2018/9/3 15:28
*/ */
public class JsonUtil { public class JsonUtil {
private static Logger log = Logger.getLogger(JsonUtil.class.getName()); private static final Logger log = Logger.getLogger(JsonUtil.class.getName());
private JsonUtil() { private JsonUtil() {
} }
@ -50,10 +50,9 @@ public class JsonUtil {
/** /**
* 作为单例全局使用 * 作为单例全局使用
*/ */
private static JsonMapper jsonMapper = new JsonMapper(); private static final JsonMapper jsonMapper = new JsonMapper();
static { static {
JsonUtil.setJavaTimeModule(JsonUtil.jsonMapper);
JsonUtil.initDefaultObjectMapper(JsonUtil.jsonMapper); JsonUtil.initDefaultObjectMapper(JsonUtil.jsonMapper);
} }
@ -94,14 +93,17 @@ public class JsonUtil {
* </pre> * </pre>
* *
* @param jsonMapper * @param jsonMapper
* @return
*/ */
private static void initDefaultObjectMapper(ObjectMapper jsonMapper) { private static ObjectMapper initDefaultObjectMapper(ObjectMapper jsonMapper) {
JsonUtil.setJavaTimeModule(jsonMapper);
//值为空时NON_NULL舍去字段ALWAYS:保留字段 //值为空时NON_NULL舍去字段ALWAYS:保留字段
jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
jsonMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); jsonMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
jsonMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); jsonMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return jsonMapper;
} }
/** /**
@ -121,8 +123,7 @@ public class JsonUtil {
* *
* @return * @return
*/ */
public static ObjectMapper acceptEmptyStringAsNullObject() { public static ObjectMapper acceptEmptyStringAsNullObject(JsonMapper jsonMapper) {
JsonMapper jsonMapper = new JsonMapper();
setJavaTimeModule(jsonMapper); setJavaTimeModule(jsonMapper);
//值为空时ALWAYS:保留字段默认字符串值为对象值为null //值为空时ALWAYS:保留字段默认字符串值为对象值为null
jsonMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); jsonMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);