JsonUtils工具类实现

First Post:

Last Update:

Word Count:
664

Read Time:
3 min

第一种

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
public class JsonUtils {

private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<Map<String, Object>>() {
};

private static final TypeReference<List<Object>> LIST_TYPE_REFERENCE = new TypeReference<List<Object>>() {
};

private JsonUtils() {
}

public static String serialize(Object o) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(o);
}

public static <T> T deserializeForClass(String s, Class<T> clazz) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(s, clazz);
}

public static Map<String, Object> deserializeForMap(String s) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(s, MAP_TYPE_REFERENCE);
}

public static List<Object> deserializeForList(String s) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(s, LIST_TYPE_REFERENCE);
}

/**
* 序列化为json字符串
*
* @param o
* @return
*/
public static String quietlySerialize(Object o) {
try {
return serialize(o);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}

/**
* 反序列化为指定类型的对象
*
* @param s
* @param clazz
* @param <T>
* @return
*/
public static <T> T quietlyDeserializeForClass(String s, Class<T> clazz) {
try {
return deserializeForClass(s, clazz);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}

/**
* 反序列化为 {@link Map}
*
* @param s
* @return
*/
public static Map<String, Object> quietlyDeserializeForMap(String s) {
try {
return deserializeForMap(s);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}

/**
* 反序列化为 {@link List}
*
* @param s
* @return
*/
public static List<Object> quietlyDeserializeForList(String s) {
try {
return deserializeForList(s);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}

/**
* 美化json字符串
*
* @param json
* @return
*/
public static String toPrettyString(String json) {
String prettyString = JSONUtil.toJsonPrettyStr(json);
return prettyString;
}
}

第二种

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
public class JsonUtils {

public static final ObjectMapper mapper = new ObjectMapper();

private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

/**
* 转为json
*
* @param obj
* @return
*/
public static String serialize(Object obj) {
if (obj == null) {
return null;
}
if (obj.getClass() == String.class) {
return (String) obj;
}
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
logger.error("json序列化出错:" + obj, e);
return null;
}
}

/**
* JSON转POJO
*
* @param json
* @param tClass
* @param <T>
* @return
*/
public static <T> T parse(String json, Class<T> tClass) {
try {
return mapper.readValue(json, tClass);
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}

/**
* JSON数组转List
*
* @param json
* @param eClass
* @param <E>
* @return
*/
public static <E> List<E> parseList(String json, Class<E> eClass) {
try {
return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}



/**
* Json、Json数组转Map
*
* @param json
* @param kClass
* @param vClass
* @param <K>
* @param <V>
* @return
*/
public static <K, V> Map<K, V> parseMap(String json, Class<K> kClass, Class<V> vClass) {
try {
return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}


/**
* 万能转换
* @param json
* @param type
* @param <T>
* @return
*/
public static <T> T nativeRead(String json, TypeReference<T> type) {
try {
return mapper.readValue(json, type);
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}

}