Commit 55d89771083db2a1aae8110eb590c02eeb5598fa
Merge branch 'master' of https://github.com/YunaiV/ruoyi-vue-pro
Showing
1 changed file
with
19 additions
and
11 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java
| @@ -2,11 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json; | @@ -2,11 +2,13 @@ package cn.iocoder.yudao.framework.common.util.json; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.ArrayUtil; | 3 | import cn.hutool.core.util.ArrayUtil; |
| 4 | import cn.hutool.core.util.StrUtil; | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | -import com.fasterxml.jackson.core.JsonProcessingException; | ||
| 6 | import com.fasterxml.jackson.core.type.TypeReference; | 5 | import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | import com.fasterxml.jackson.databind.JsonNode; | 6 | import com.fasterxml.jackson.databind.JsonNode; |
| 8 | import com.fasterxml.jackson.databind.ObjectMapper; | 7 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | import com.fasterxml.jackson.databind.SerializationFeature; | 8 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 9 | +import lombok.SneakyThrows; | ||
| 10 | +import lombok.experimental.UtilityClass; | ||
| 11 | +import lombok.extern.slf4j.Slf4j; | ||
| 10 | 12 | ||
| 11 | import java.io.IOException; | 13 | import java.io.IOException; |
| 12 | import java.util.ArrayList; | 14 | import java.util.ArrayList; |
| @@ -17,6 +19,8 @@ import java.util.List; | @@ -17,6 +19,8 @@ import java.util.List; | ||
| 17 | * | 19 | * |
| 18 | * @author 芋道源码 | 20 | * @author 芋道源码 |
| 19 | */ | 21 | */ |
| 22 | +@UtilityClass | ||
| 23 | +@Slf4j | ||
| 20 | public class JsonUtils { | 24 | public class JsonUtils { |
| 21 | 25 | ||
| 22 | private static ObjectMapper objectMapper = new ObjectMapper(); | 26 | private static ObjectMapper objectMapper = new ObjectMapper(); |
| @@ -36,29 +40,26 @@ public class JsonUtils { | @@ -36,29 +40,26 @@ public class JsonUtils { | ||
| 36 | JsonUtils.objectMapper = objectMapper; | 40 | JsonUtils.objectMapper = objectMapper; |
| 37 | } | 41 | } |
| 38 | 42 | ||
| 43 | + @SneakyThrows | ||
| 39 | public static String toJsonString(Object object) { | 44 | public static String toJsonString(Object object) { |
| 40 | - try { | ||
| 41 | - return objectMapper.writeValueAsString(object); | ||
| 42 | - } catch (JsonProcessingException e) { | ||
| 43 | - throw new RuntimeException(e); | ||
| 44 | - } | 45 | + return objectMapper.writeValueAsString(object); |
| 45 | } | 46 | } |
| 46 | 47 | ||
| 48 | + @SneakyThrows | ||
| 47 | public static byte[] toJsonByte(Object object) { | 49 | public static byte[] toJsonByte(Object object) { |
| 48 | - try { | ||
| 49 | - return objectMapper.writeValueAsBytes(object); | ||
| 50 | - } catch (JsonProcessingException e) { | ||
| 51 | - throw new RuntimeException(e); | ||
| 52 | - } | 50 | + return objectMapper.writeValueAsBytes(object); |
| 53 | } | 51 | } |
| 54 | 52 | ||
| 53 | + | ||
| 55 | public static <T> T parseObject(String text, Class<T> clazz) { | 54 | public static <T> T parseObject(String text, Class<T> clazz) { |
| 56 | if (StrUtil.isEmpty(text)) { | 55 | if (StrUtil.isEmpty(text)) { |
| 57 | return null; | 56 | return null; |
| 58 | } | 57 | } |
| 58 | + | ||
| 59 | try { | 59 | try { |
| 60 | return objectMapper.readValue(text, clazz); | 60 | return objectMapper.readValue(text, clazz); |
| 61 | } catch (IOException e) { | 61 | } catch (IOException e) { |
| 62 | + log.error("json parse err,json:{}", text, e); | ||
| 62 | throw new RuntimeException(e); | 63 | throw new RuntimeException(e); |
| 63 | } | 64 | } |
| 64 | } | 65 | } |
| @@ -67,9 +68,11 @@ public class JsonUtils { | @@ -67,9 +68,11 @@ public class JsonUtils { | ||
| 67 | if (ArrayUtil.isEmpty(bytes)) { | 68 | if (ArrayUtil.isEmpty(bytes)) { |
| 68 | return null; | 69 | return null; |
| 69 | } | 70 | } |
| 71 | + | ||
| 70 | try { | 72 | try { |
| 71 | return objectMapper.readValue(bytes, clazz); | 73 | return objectMapper.readValue(bytes, clazz); |
| 72 | } catch (IOException e) { | 74 | } catch (IOException e) { |
| 75 | + log.error("json parse err,json:{}", bytes, e); | ||
| 73 | throw new RuntimeException(e); | 76 | throw new RuntimeException(e); |
| 74 | } | 77 | } |
| 75 | } | 78 | } |
| @@ -78,6 +81,7 @@ public class JsonUtils { | @@ -78,6 +81,7 @@ public class JsonUtils { | ||
| 78 | try { | 81 | try { |
| 79 | return objectMapper.readValue(text, typeReference); | 82 | return objectMapper.readValue(text, typeReference); |
| 80 | } catch (IOException e) { | 83 | } catch (IOException e) { |
| 84 | + log.error("json parse err,json:{}", text, e); | ||
| 81 | throw new RuntimeException(e); | 85 | throw new RuntimeException(e); |
| 82 | } | 86 | } |
| 83 | } | 87 | } |
| @@ -86,9 +90,11 @@ public class JsonUtils { | @@ -86,9 +90,11 @@ public class JsonUtils { | ||
| 86 | if (StrUtil.isEmpty(text)) { | 90 | if (StrUtil.isEmpty(text)) { |
| 87 | return new ArrayList<>(); | 91 | return new ArrayList<>(); |
| 88 | } | 92 | } |
| 93 | + | ||
| 89 | try { | 94 | try { |
| 90 | return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); | 95 | return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); |
| 91 | } catch (IOException e) { | 96 | } catch (IOException e) { |
| 97 | + log.error("json parse err,json:{}", text, e); | ||
| 92 | throw new RuntimeException(e); | 98 | throw new RuntimeException(e); |
| 93 | } | 99 | } |
| 94 | } | 100 | } |
| @@ -98,6 +104,7 @@ public class JsonUtils { | @@ -98,6 +104,7 @@ public class JsonUtils { | ||
| 98 | try { | 104 | try { |
| 99 | return objectMapper.readTree(text); | 105 | return objectMapper.readTree(text); |
| 100 | } catch (IOException e) { | 106 | } catch (IOException e) { |
| 107 | + log.error("json parse err,json:{}", text, e); | ||
| 101 | throw new RuntimeException(e); | 108 | throw new RuntimeException(e); |
| 102 | } | 109 | } |
| 103 | } | 110 | } |
| @@ -106,6 +113,7 @@ public class JsonUtils { | @@ -106,6 +113,7 @@ public class JsonUtils { | ||
| 106 | try { | 113 | try { |
| 107 | return objectMapper.readTree(text); | 114 | return objectMapper.readTree(text); |
| 108 | } catch (IOException e) { | 115 | } catch (IOException e) { |
| 116 | + log.error("json parse err,json:{}", text, e); | ||
| 109 | throw new RuntimeException(e); | 117 | throw new RuntimeException(e); |
| 110 | } | 118 | } |
| 111 | } | 119 | } |