|
| 1 | +package top.cadecode.sra.framework.util; |
| 2 | + |
| 3 | +import cn.hutool.core.util.StrUtil; |
| 4 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 5 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 6 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import org.springframework.beans.factory.InitializingBean; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.text.SimpleDateFormat; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Cade Li |
| 20 | + * @date 2022/5/23 |
| 21 | + * @description Jackson 工具类 |
| 22 | + */ |
| 23 | +@Component |
| 24 | +@RequiredArgsConstructor |
| 25 | +public class JacksonUtil implements InitializingBean { |
| 26 | + |
| 27 | + /** |
| 28 | + * 注入 Spring 管理的 ObjectMapper |
| 29 | + */ |
| 30 | + private final ObjectMapper objectMapper; |
| 31 | + |
| 32 | + private static ObjectMapper OBJECT_MAPPER; |
| 33 | + |
| 34 | + /** |
| 35 | + * 转换对象到 json 串 |
| 36 | + * |
| 37 | + * @param bean 需要转换的对象 |
| 38 | + * @return json 字符串 |
| 39 | + */ |
| 40 | + public static String toJson(Object bean) { |
| 41 | + return toJson(bean, false); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * 转换对象到 json 串 |
| 46 | + * |
| 47 | + * @param bean 需要转换的对象 |
| 48 | + * @param isPretty 是否美化输出(换行) |
| 49 | + * @return json 字符串 |
| 50 | + */ |
| 51 | + public static String toJson(Object bean, boolean isPretty) { |
| 52 | + if (bean == null) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + if (bean instanceof String) { |
| 56 | + return (String) bean; |
| 57 | + } |
| 58 | + try { |
| 59 | + if (!isPretty) { |
| 60 | + return OBJECT_MAPPER.writeValueAsString(bean); |
| 61 | + } |
| 62 | + return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(bean); |
| 63 | + } catch (JsonProcessingException e) { |
| 64 | + throw new RuntimeException("转换 Bean 到 Json 出错", e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * 转换字符串到自定义对象 |
| 70 | + * |
| 71 | + * @param json 需要转换的字符串 |
| 72 | + * @param clazz 自定义对象的 class 对象 |
| 73 | + * @return 自定义对象 |
| 74 | + */ |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + public static <T> T toBean(String json, Class<T> clazz) { |
| 77 | + if (StrUtil.isEmpty(json) || Objects.isNull(clazz)) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + if (clazz.equals(String.class)) { |
| 81 | + return (T) json; |
| 82 | + } |
| 83 | + try { |
| 84 | + return OBJECT_MAPPER.readValue(json, clazz); |
| 85 | + } catch (Exception e) { |
| 86 | + throw new RuntimeException("转换 Json 到 Bean 出错", e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * 转换字符串到集合对象,可以保留泛型 |
| 92 | + * |
| 93 | + * @param json 需要转换的字符串 |
| 94 | + * @param typeReference 自定义对象的 TypeReference 对象 |
| 95 | + * @return 自定义对象 |
| 96 | + */ |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + public static <T> T toBean(String json, TypeReference<T> typeReference) { |
| 99 | + if (StrUtil.isEmpty(json) || Objects.isNull(typeReference)) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + if (typeReference.getType().equals(String.class)) { |
| 103 | + return (T) json; |
| 104 | + } |
| 105 | + try { |
| 106 | + return OBJECT_MAPPER.readValue(json, typeReference); |
| 107 | + } catch (IOException e) { |
| 108 | + throw new RuntimeException("转换 Json 到 Bean 出错", e); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void afterPropertiesSet() { |
| 114 | + OBJECT_MAPPER = this.objectMapper; |
| 115 | + if (Objects.isNull(OBJECT_MAPPER)) { |
| 116 | + OBJECT_MAPPER = new ObjectMapper(); |
| 117 | + // 定义日期格式 |
| 118 | + String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss"; |
| 119 | + // Jackson 详细 |
| 120 | + // 列入对象的全部字段 |
| 121 | + OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS); |
| 122 | + // 取消默认转换 timestamps 形式 |
| 123 | + OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
| 124 | + // 忽略空Bean转json的错误 |
| 125 | + OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
| 126 | + // 统一所有的日期格式为以下的样式,即 yyyy-MM-dd HH:mm:ss |
| 127 | + OBJECT_MAPPER.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT)); |
| 128 | + // 忽略在 json 字符串中存在,但是在 java 对象中不存在对应属性的情况。防止错误 |
| 129 | + OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments