zhanglin
2023-07-20 cde69bd6e9ce00b22df3690d85ea295459e3b168
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
package com.ruoyi.order.tools.utils;
 
 
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
 
/**
 * json工具类简单封装
 *
 * @author GaBin  on  15/8/29
 */
public class JsonUtils {
 
    /**
     * <pre>
     *     将对象转换成json字符串
     * </pre>
     *
     * @param object 实例对象
     * @return String
     */
    public static String bean2Json(Object object) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        try {
            return objectMapper.writeValueAsString(object);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * <pre>
     *     将json字符串转化成对象
     * </pre>
     *
     * @param clazz 转化成的对象类型
     * @param json  需要转化的json字符串
     * @param <T>   泛型
     * @return T
     */
    public static <T> T json2Bean(Class<T> clazz, String json) {
        if (json == null) {
            return null;
        }
        if (json.contains("<html>")) {
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        try {
            return objectMapper.readValue(json, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * <pre>
     *     将json字符串转化成List对象
     * </pre>
     *
     * @param clazz Class类型
     * @param json  json字符串
     * @param <T>   泛型
     * @return List
     */
    public static <T> List<T> json2List(Class<T> clazz, String json) {
        if (json == null || json.contains("<html>")) {
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JavaType javaType = getCollectionType(objectMapper, ArrayList.class, clazz);
            return objectMapper.readValue(json, javaType);
        } catch (Exception ignored) {
        }
        return null;
    }
 
    /**
     * @param clazz Class类型
     * @param json  json字符串
     * @param <T>   泛型
     * @return T
     */
    public static <T> Map<String, T> json2Map(Class<T> clazz, String json) {
        if (json == null || json.contains("<html>")) {
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JavaType javaType = getCollectionType(objectMapper, clazz);
            return objectMapper.readValue(json, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static JavaType getCollectionType(ObjectMapper objectMapper, Class<?> collectionClass, Class<?>... elementClasses) {
        TypeFactory typeFactory = objectMapper.getTypeFactory();
        return typeFactory.constructParametricType(collectionClass, elementClasses);
    }
 
    public static JavaType getCollectionType(ObjectMapper objectMapper, Class<?> elementClasse) {
        TypeFactory typeFactory = objectMapper.getTypeFactory();
        return typeFactory.constructMapType(TreeMap.class, String.class, elementClasse);
    }
 
}