Commit fd4adf2cea7448f17326fd38380916e765742e08
1 parent
0bac2838
使用 lombok 优化工具类
1。添加 @UtilityClass, 作用私有空参构造函数 2。使用 @SneakyThrows 替代手动转换为 RuntimeException
Showing
1 changed file
with
22 additions
and
40 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java
| ... | ... | @@ -7,6 +7,8 @@ import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | 7 | import com.fasterxml.jackson.databind.JsonNode; |
| 8 | 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | 9 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 10 | +import lombok.SneakyThrows; | |
| 11 | +import lombok.experimental.UtilityClass; | |
| 10 | 12 | |
| 11 | 13 | import java.io.IOException; |
| 12 | 14 | import java.util.ArrayList; |
| ... | ... | @@ -17,6 +19,7 @@ import java.util.List; |
| 17 | 19 | * |
| 18 | 20 | * @author 芋道源码 |
| 19 | 21 | */ |
| 22 | +@UtilityClass | |
| 20 | 23 | public class JsonUtils { |
| 21 | 24 | |
| 22 | 25 | private static ObjectMapper objectMapper = new ObjectMapper(); |
| ... | ... | @@ -36,78 +39,57 @@ public class JsonUtils { |
| 36 | 39 | JsonUtils.objectMapper = objectMapper; |
| 37 | 40 | } |
| 38 | 41 | |
| 42 | + @SneakyThrows | |
| 39 | 43 | public static String toJsonString(Object object) { |
| 40 | - try { | |
| 41 | - return objectMapper.writeValueAsString(object); | |
| 42 | - } catch (JsonProcessingException e) { | |
| 43 | - throw new RuntimeException(e); | |
| 44 | - } | |
| 44 | + return objectMapper.writeValueAsString(object); | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | + @SneakyThrows | |
| 47 | 48 | public static byte[] toJsonByte(Object object) { |
| 48 | - try { | |
| 49 | - return objectMapper.writeValueAsBytes(object); | |
| 50 | - } catch (JsonProcessingException e) { | |
| 51 | - throw new RuntimeException(e); | |
| 52 | - } | |
| 49 | + return objectMapper.writeValueAsBytes(object); | |
| 53 | 50 | } |
| 54 | 51 | |
| 52 | + @SneakyThrows | |
| 55 | 53 | public static <T> T parseObject(String text, Class<T> clazz) { |
| 56 | 54 | if (StrUtil.isEmpty(text)) { |
| 57 | 55 | return null; |
| 58 | 56 | } |
| 59 | - try { | |
| 60 | - return objectMapper.readValue(text, clazz); | |
| 61 | - } catch (IOException e) { | |
| 62 | - throw new RuntimeException(e); | |
| 63 | - } | |
| 57 | + | |
| 58 | + return objectMapper.readValue(text, clazz); | |
| 64 | 59 | } |
| 65 | 60 | |
| 61 | + @SneakyThrows | |
| 66 | 62 | public static <T> T parseObject(byte[] bytes, Class<T> clazz) { |
| 67 | 63 | if (ArrayUtil.isEmpty(bytes)) { |
| 68 | 64 | return null; |
| 69 | 65 | } |
| 70 | - try { | |
| 71 | - return objectMapper.readValue(bytes, clazz); | |
| 72 | - } catch (IOException e) { | |
| 73 | - throw new RuntimeException(e); | |
| 74 | - } | |
| 66 | + | |
| 67 | + return objectMapper.readValue(bytes, clazz); | |
| 75 | 68 | } |
| 76 | 69 | |
| 70 | + @SneakyThrows | |
| 77 | 71 | public static <T> T parseObject(String text, TypeReference<T> typeReference) { |
| 78 | - try { | |
| 79 | - return objectMapper.readValue(text, typeReference); | |
| 80 | - } catch (IOException e) { | |
| 81 | - throw new RuntimeException(e); | |
| 82 | - } | |
| 72 | + return objectMapper.readValue(text, typeReference); | |
| 83 | 73 | } |
| 84 | 74 | |
| 75 | + @SneakyThrows | |
| 85 | 76 | public static <T> List<T> parseArray(String text, Class<T> clazz) { |
| 86 | 77 | if (StrUtil.isEmpty(text)) { |
| 87 | 78 | return new ArrayList<>(); |
| 88 | 79 | } |
| 89 | - try { | |
| 90 | - return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); | |
| 91 | - } catch (IOException e) { | |
| 92 | - throw new RuntimeException(e); | |
| 93 | - } | |
| 80 | + | |
| 81 | + return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); | |
| 94 | 82 | } |
| 95 | 83 | |
| 96 | 84 | // TODO @Li:和上面的风格保持一致哈。parseTree |
| 85 | + @SneakyThrows | |
| 97 | 86 | public static JsonNode readTree(String text) { |
| 98 | - try { | |
| 99 | - return objectMapper.readTree(text); | |
| 100 | - } catch (IOException e) { | |
| 101 | - throw new RuntimeException(e); | |
| 102 | - } | |
| 87 | + return objectMapper.readTree(text); | |
| 103 | 88 | } |
| 104 | 89 | |
| 90 | + @SneakyThrows | |
| 105 | 91 | public static JsonNode readTree(byte[] text) { |
| 106 | - try { | |
| 107 | - return objectMapper.readTree(text); | |
| 108 | - } catch (IOException e) { | |
| 109 | - throw new RuntimeException(e); | |
| 110 | - } | |
| 92 | + return objectMapper.readTree(text); | |
| 111 | 93 | } |
| 112 | 94 | |
| 113 | 95 | } | ... | ... |