JsonUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.ruoyi.common.utils;
  2. import cn.hutool.core.lang.Dict;
  3. import cn.hutool.core.util.ArrayUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.fasterxml.jackson.core.JsonProcessingException;
  6. import com.fasterxml.jackson.core.type.TypeReference;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.fasterxml.jackson.databind.exc.MismatchedInputException;
  9. import com.ruoyi.common.utils.spring.SpringUtils;
  10. import lombok.AccessLevel;
  11. import lombok.NoArgsConstructor;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. /**
  16. * JSON 工具类
  17. *
  18. * @author 芋道源码
  19. */
  20. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  21. public class JsonUtils {
  22. private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class);
  23. public static ObjectMapper getObjectMapper() {
  24. return OBJECT_MAPPER;
  25. }
  26. public static String toJsonString(Object object) {
  27. if (ObjectUtil.isNull(object)) {
  28. return null;
  29. }
  30. try {
  31. return OBJECT_MAPPER.writeValueAsString(object);
  32. } catch (JsonProcessingException e) {
  33. throw new RuntimeException(e);
  34. }
  35. }
  36. public static <T> T parseObject(String text, Class<T> clazz) {
  37. if (StringUtils.isEmpty(text)) {
  38. return null;
  39. }
  40. try {
  41. return OBJECT_MAPPER.readValue(text, clazz);
  42. } catch (IOException e) {
  43. throw new RuntimeException(e);
  44. }
  45. }
  46. public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
  47. if (ArrayUtil.isEmpty(bytes)) {
  48. return null;
  49. }
  50. try {
  51. return OBJECT_MAPPER.readValue(bytes, clazz);
  52. } catch (IOException e) {
  53. throw new RuntimeException(e);
  54. }
  55. }
  56. public static <T> T parseObject(String text, TypeReference<T> typeReference) {
  57. if (StringUtils.isBlank(text)) {
  58. return null;
  59. }
  60. try {
  61. return OBJECT_MAPPER.readValue(text, typeReference);
  62. } catch (IOException e) {
  63. throw new RuntimeException(e);
  64. }
  65. }
  66. public static Dict parseMap(String text) {
  67. if (StringUtils.isBlank(text)) {
  68. return null;
  69. }
  70. try {
  71. return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class));
  72. } catch (MismatchedInputException e) {
  73. // 类型不匹配说明不是json
  74. return null;
  75. } catch (IOException e) {
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. public static List<Dict> parseArrayMap(String text) {
  80. if (StringUtils.isBlank(text)) {
  81. return null;
  82. }
  83. try {
  84. return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class));
  85. } catch (IOException e) {
  86. throw new RuntimeException(e);
  87. }
  88. }
  89. public static <T> List<T> parseArray(String text, Class<T> clazz) {
  90. if (StringUtils.isEmpty(text)) {
  91. return new ArrayList<>();
  92. }
  93. try {
  94. return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
  95. } catch (IOException e) {
  96. throw new RuntimeException(e);
  97. }
  98. }
  99. }