1
zj
2025-08-18 8b5ed9e9ff4400ad5638bab870e52af4f90d101a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.nq.utils.redis;
 
 
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.text.SimpleDateFormat;
 
 
public class JsonUtil {
 
    private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
 
 
    private static ObjectMapper objectMapper = new ObjectMapper();
 
 
    static {
 
        objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS);
 
 
        objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
 
 
        objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
 
 
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
 
 
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
    }
 
 
    public static <T> String obj2String(T obj) {
 
        if (obj == null) {
 
            return null;
 
        }
 
        try {
 
            return (obj instanceof String) ? (String) obj : objectMapper.writeValueAsString(obj);
 
        } catch (Exception e) {
 
            log.warn("XC JsonUtil Parse obj to string error ", e);
 
            return null;
 
        }
 
    }
 
 
    public static <T> String obj2StringPretty(T obj) {
 
        if (obj == null) {
 
            return null;
 
        }
 
        try {
 
            return (obj instanceof String) ? (String) obj : objectMapper
 
                    .writerWithDefaultPrettyPrinter().writeValueAsString(obj);
 
        } catch (Exception e) {
 
            log.warn("XC JsonUtil Parse obj to string error ", e);
 
            return null;
 
        }
 
    }
 
 
    public static <T> T string2Obj(String str, Class<T> clazz) {
 
        if (StringUtils.isEmpty(str) || clazz == null) {
 
            return null;
 
        }
 
        try {
 
            return (T) (clazz.equals(String.class) ? str : objectMapper.readValue(str, clazz));
 
        } catch (Exception e) {
 
            log.warn("XC 1 JsonUtil Parse string to obj error", e);
 
            return null;
 
        }
 
    }
 
 
    public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
 
        if (StringUtils.isEmpty(str) || typeReference == null) {
 
            return null;
 
        }
 
        try {
 
            return (T) (typeReference.getType().equals(String.class) ? str : objectMapper
 
                    .readValue(str, typeReference));
 
        } catch (Exception e) {
 
            log.warn("XC 2 JsonUtil Parse string to obj error", e);
 
            return null;
 
        }
 
    }
 
 
    public static <T> T string2Obj(String str, Class<?> collectionClass, Class... elementClasses) {
 
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
 
 
        try {
 
            return (T) objectMapper.readValue(str, javaType);
 
        } catch (Exception e) {
 
            log.warn("XC 3 JsonUtil Parse string to obj error", e);
 
            return null;
 
        }
 
    }
 
 
    public static void main(String[] args) {
    }
 
}