JsonUtils.java 3.1 KB

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