From f39b02a0dd5569c1fe7ea48043672cd6186aad4a Mon Sep 17 00:00:00 2001
From: yueye <768242801@qq.com>
Date: Tue, 28 Dec 2021 11:15:01 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0jackson=E9=85=8D=E7=BD=AE?=
=?UTF-8?q?=E5=8F=8A=E5=BA=8F=E5=88=97=E5=8C=96=E5=B7=A5=E5=85=B7=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ruoyi-common/ruoyi-common-core/pom.xml | 4 +
.../common/core/config/JacksonConfig.java | 51 +++++++++++
.../core/jackson/BigNumberSerializer.java | 42 +++++++++
.../ruoyi/common/core/utils/JsonUtils.java | 91 +++++++++++++++++++
.../main/resources/META-INF/spring.factories | 3 +-
5 files changed, 190 insertions(+), 1 deletion(-)
create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/JacksonConfig.java
create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/BigNumberSerializer.java
create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java
diff --git a/ruoyi-common/ruoyi-common-core/pom.xml b/ruoyi-common/ruoyi-common-core/pom.xml
index 191ca57c..3c8d3bd2 100644
--- a/ruoyi-common/ruoyi-common-core/pom.xml
+++ b/ruoyi-common/ruoyi-common-core/pom.xml
@@ -70,6 +70,10 @@
com.fasterxml.jackson.core
jackson-databind
+
+ com.fasterxml.jackson.datatype
+ jackson-datatype-jsr310
+
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/JacksonConfig.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/JacksonConfig.java
new file mode 100644
index 00000000..891e4f1e
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/JacksonConfig.java
@@ -0,0 +1,51 @@
+package com.ruoyi.common.core.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
+import com.ruoyi.common.core.jackson.BigNumberSerializer;
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
+import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.TimeZone;
+
+/**
+ * JacksonConfig
+ */
+@Configuration
+@ConditionalOnClass(ObjectMapper.class)
+@AutoConfigureBefore(JacksonAutoConfiguration.class)
+public class JacksonConfig {
+
+ @Primary
+ @Bean
+ public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder, JacksonProperties jacksonProperties) {
+ ObjectMapper objectMapper = builder.createXmlMapper(false).build();
+ // 全局配置序列化返回 JSON 处理
+ SimpleModule simpleModule = new SimpleModule();
+ simpleModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
+ simpleModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
+ simpleModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
+ simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(jacksonProperties.getDateFormat());
+ simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
+ simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
+ objectMapper.registerModule(simpleModule);
+ objectMapper.setTimeZone(TimeZone.getDefault());
+
+ return objectMapper;
+ }
+
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/BigNumberSerializer.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/BigNumberSerializer.java
new file mode 100644
index 00000000..39261a66
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/BigNumberSerializer.java
@@ -0,0 +1,42 @@
+package com.ruoyi.common.core.jackson;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
+import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
+
+import java.io.IOException;
+
+/**
+ * 超出 JS 最大最小值 处理
+ *
+ * @author Lion Li
+ */
+@JacksonStdImpl
+public class BigNumberSerializer extends NumberSerializer {
+
+ /**
+ * 根据 JS Number.MAX_SAFE_INTEGER 与 Number.MIN_SAFE_INTEGER 得来
+ */
+ private static final long MAX_SAFE_INTEGER = 9007199254740991L;
+ private static final long MIN_SAFE_INTEGER = -9007199254740991L;
+
+ /**
+ * 提供实例
+ */
+ public static final BigNumberSerializer INSTANCE = new BigNumberSerializer(Number.class);
+
+ public BigNumberSerializer(Class extends Number> rawType) {
+ super(rawType);
+ }
+
+ @Override
+ public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+ // 超出范围 序列化位字符串
+ if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) {
+ super.serialize(value, gen, provider);
+ } else {
+ gen.writeString(value.toString());
+ }
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java
new file mode 100644
index 00000000..c18e8eb3
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JsonUtils.java
@@ -0,0 +1,91 @@
+package com.ruoyi.common.core.utils;
+
+import cn.hutool.core.util.ArrayUtil;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * JSON 工具类
+ *
+ * @author 芋道源码
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class JsonUtils {
+
+ private static ObjectMapper objectMapper = SpringUtils.getBean(ObjectMapper.class);
+
+ public static String toJsonString(Object object) {
+ if (StringUtils.isNull(object)) {
+ return null;
+ }
+ try {
+ return objectMapper.writeValueAsString(object);
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static T parseObject(String text, Class clazz) {
+ if (StringUtils.isEmpty(text)) {
+ return null;
+ }
+ try {
+ return objectMapper.readValue(text, clazz);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static T parseObject(byte[] bytes, Class clazz) {
+ if (ArrayUtil.isEmpty(bytes)) {
+ return null;
+ }
+ try {
+ return objectMapper.readValue(bytes, clazz);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static T parseObject(String text, TypeReference typeReference) {
+ if (StringUtils.isBlank(text)) {
+ return null;
+ }
+ try {
+ return objectMapper.readValue(text, typeReference);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static Map parseMap(String text) {
+ if (StringUtils.isBlank(text)) {
+ return null;
+ }
+ try {
+ return objectMapper.readValue(text, new TypeReference