13693261870
2024-12-30 edc9a6674eb9b40e33a74c5f022d279712ed3b7c
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
package com.se.system.utils;
 
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.Map;
 
@SuppressWarnings("ALL")
public class JsonUtils {
    public static final ObjectMapper OM = new ObjectMapper();
 
    static {
        OM.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OM.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        OM.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        OM.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        OM.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    }
 
    public JsonUtils() {
    }
 
    public static String objectToJson(Object data) {
        try {
            return OM.writeValueAsString(data);
        } catch (JsonProcessingException var2) {
            var2.printStackTrace();
            return null;
        }
    }
 
    public static String objectToJsonWithType(Object data, TypeReference typeReference) {
        try {
            return OM.writerFor(typeReference).writeValueAsString(data);
        } catch (JsonProcessingException var3) {
            var3.printStackTrace();
            return null;
        }
    }
 
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            return OM.readValue(jsonData, beanType);
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }
 
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = OM.getTypeFactory().constructParametricType(List.class, new Class[]{beanType});
 
        try {
            return (List) OM.readValue(jsonData, javaType);
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }
 
    public static Map<String, Object> parseMap(String jsonStr) throws IOException {
        return (Map) OM.readValue(jsonStr, Map.class);
    }
 
    public static List<String> parseList(String jsonStr) throws IOException {
        return (List) OM.readValue(jsonStr, new TypeReference<List<String>>() {
        });
    }
}