2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-04 e55c8b0a92eb9715edd90c31dfd4de51a47b588b
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
package com.sandu.ximon.admin.utils;
 
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import jdk.nashorn.internal.ir.ObjectNode;
 
import java.io.IOException;
 
public class JsonUtil {
    private static ObjectMapper objectMapper;
 
    static {
        objectMapper = new ObjectMapper();
 
        // 璁剧疆FAIL_ON_EMPTY_BEANS灞炴�э紝褰撳簭鍒楀寲绌哄璞′笉瑕佹姏寮傚父
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
 
        // 璁剧疆FAIL_ON_UNKNOWN_PROPERTIES灞炴�э紝褰揓SON瀛楃涓蹭腑瀛樺湪Java瀵硅薄娌℃湁鐨勫睘鎬э紝蹇界暐
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
 
    /**
     * Convert Object to JsonString
     *
     * @param jsonObj
     * @return
     */
    public static String jsonObj2Sting(Object jsonObj) {
        String jsonString = null;
 
        try {
            jsonString = objectMapper.writeValueAsString(jsonObj);
        } catch (IOException e) {
            System.out.printf("pasre json Object[{}] to string failed.",jsonString);
        }
 
        return jsonString;
    }
 
    /**
     * Convert JsonString to Simple Object
     *
     * @param jsonString
     * @param cls
     * @return
     */
    public static <T> T jsonString2SimpleObj(String jsonString, Class<T> cls) {
        T jsonObj = null;
 
        try {
            jsonObj = objectMapper.readValue(jsonString, cls);
        } catch (IOException e) {
            System.out.printf("pasre json Object[{}] to string failed.",jsonString);
        }
 
        return jsonObj;
    }
 
    /**
     * Method that will convert object to the ObjectNode.
     *
     * @param object
     *            the source data; if null, will return null.
     * @return the ObjectNode data after converted.
     * @throws Exception
     */
    public static <T> ObjectNode convertObject2ObjectNode(T object)
            throws Exception {
        if (null == object) {
            return null;
        }
 
        ObjectNode objectNode = null;
 
        if (object instanceof String) {
            objectNode = convertJsonStringToObject((String) object,
                    ObjectNode.class);
        } else {
            objectNode = convertValue(object, ObjectNode.class);
        }
 
        return objectNode;
    }
 
    /**
     * Method that will convert the json string to destination by the type(cls).
     *
     * @param jsonString
     *            the source json string; if null, will return null.
     * @param cls
     *            the destination data type.
     * @return
     * @throws Exception
     */
    public static <T> T convertJsonStringToObject(String jsonString,
                                                  Class<T> cls) throws Exception {
        if (StringUtil.strIsNullOrEmpty(jsonString)) {
            return null;
        }
 
        try {
            T object = objectMapper.readValue(jsonString, cls);
            return object;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
 
    /**
     * Method that will convert from given value into instance of given value
     * type.
     *
     * @param fromValue
     * @param toValueType
     * @return
     * @throws Exception
     */
    private static <T> T convertValue(Object fromValue, Class<T> toValueType)
            throws Exception {
        try {
            return objectMapper.convertValue(fromValue, toValueType);
        } catch (IllegalArgumentException e) {
            throw new Exception(e);
        }
    }
 
 
}