Bladeren bron

添加jackson配置及序列化工具类

yueye 4 jaren geleden
bovenliggende
commit
f39b02a0dd

+ 4 - 0
ruoyi-common/ruoyi-common-core/pom.xml

@@ -70,6 +70,10 @@
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.datatype</groupId>
+            <artifactId>jackson-datatype-jsr310</artifactId>
+        </dependency>
 
         <!-- Alibaba Fastjson -->
         <dependency>

+ 51 - 0
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;
+    }
+
+}

+ 42 - 0
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());
+		}
+	}
+}

+ 91 - 0
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> T parseObject(String text, Class<T> clazz) {
+        if (StringUtils.isEmpty(text)) {
+            return null;
+        }
+        try {
+            return objectMapper.readValue(text, clazz);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
+        if (ArrayUtil.isEmpty(bytes)) {
+            return null;
+        }
+        try {
+            return objectMapper.readValue(bytes, clazz);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(String text, TypeReference<T> typeReference) {
+		if (StringUtils.isBlank(text)) {
+			return null;
+		}
+        try {
+            return objectMapper.readValue(text, typeReference);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+	public static <T> Map<String, T> parseMap(String text) {
+		if (StringUtils.isBlank(text)) {
+			return null;
+		}
+		try {
+			return objectMapper.readValue(text, new TypeReference<Map<String, T>>() {});
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+    public static <T> List<T> parseArray(String text, Class<T> clazz) {
+        if (StringUtils.isEmpty(text)) {
+            return new ArrayList<>();
+        }
+        try {
+            return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+}

+ 2 - 1
ruoyi-common/ruoyi-common-core/src/main/resources/META-INF/spring.factories

@@ -1,4 +1,5 @@
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-  com.ruoyi.common.core.utils.SpringUtils
+  com.ruoyi.common.core.utils.SpringUtils,\
+  com.ruoyi.common.core.config.JacksonConfig