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