From da8457cf435fce4f945b16cf20f45caf8fc272a4 Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Sat, 14 Aug 2021 10:46:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85JSON=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/sjhy/plugin/tool/JSON.java | 94 ++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/main/java/com/sjhy/plugin/tool/JSON.java diff --git a/src/main/java/com/sjhy/plugin/tool/JSON.java b/src/main/java/com/sjhy/plugin/tool/JSON.java new file mode 100644 index 0000000..2984b3d --- /dev/null +++ b/src/main/java/com/sjhy/plugin/tool/JSON.java @@ -0,0 +1,94 @@ +package com.sjhy.plugin.tool; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +import java.io.IOException; +import java.text.SimpleDateFormat; + +/** + * json工具 + * + * @author makejava + * @version 1.0.0 + * @date 2021/08/14 10:37 + */ +public class JSON { + + private static final ObjectMapper INSTANCE; + + private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + + static { + INSTANCE = new ObjectMapper(); + // 禁止将日期序列化成时间戳 + INSTANCE.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + // 禁止属性不存在时报错 + INSTANCE.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + // 允许字符串转换成数组 + INSTANCE.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); + // 读取到未知的枚举值转换成空 + INSTANCE.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL); + // 禁用科学计数法 + INSTANCE.enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS); + INSTANCE.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); + // 序列化忽略空值 + INSTANCE.setSerializationInclusion(JsonInclude.Include.NON_NULL); + // 配置时间格式 + INSTANCE.setDateFormat(new SimpleDateFormat(DATE_FORMAT)); + } + + public static ObjectMapper getInstance() { + return INSTANCE; + } + + /** + * 将json字符串转换成java对象 + * + * @param json json字符串 + * @param cls java对象类型 + * @param 对象类型 + * @return 对象 + */ + public static T parse(String json, Class cls) { + try { + return INSTANCE.readValue(json, cls); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + /** + * 将json字符串转换成java对象 + * + * @param json json字符串 + * @param type java对象类型 + * @param 对象类型 + * @return 对象 + */ + public static T parse(String json, TypeReference type) { + try { + return INSTANCE.readValue(json, type); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + /** + * 将json对象转换成json字符串 + * + * @param obj 对象 + * @return json字符串 + */ + public static String toJson(Object obj) { + try { + return INSTANCE.writeValueAsString(obj); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException(e); + } + } +}