Jackson 工具类(对象、Map、List 解析)
另一个 Jackson ObjectMapper 封装版本,除了对象序列化/反序列化,还支持直接解析成 Map 和 List:
1public class JSON {
2 private final static ObjectMapper MAPPER = new ObjectMapper();
3
4 static {
5
6 // 美化输出
7 // mapper.enable(SerializationFeature.INDENT_OUTPUT);
8 // 允许序列化空的POJO类
9 // (否则会抛出异常)
10 MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
11 // 把java.util.Date, Calendar输出为数字(时间戳)
12 MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
13 // 在遇到未知属性的时候不抛出异常
14 MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
15 // 强制JSON 空字符串("")转换为null对象值:
16 MAPPER.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
17 // 在JSON中允许C/C++ 样式的注释(非标准,默认禁用)
18 MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
19 // 允许没有引号的字段名(非标准)
20 MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
21 // 允许单引号(非标准)
22 MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
23 // 强制转义非ASCII字符
24 MAPPER.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
25 // 所有的日期格式都统一为以下的样式,即yyyy-MM-dd HH:mm:ss
26 MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
27
28 // 将内容包裹为一个JSON属性,属性名由@JsonRootName注解指定
29 // mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
30 MAPPER.registerModule(new JavaTimeModule());
31
32 }
33
34 public static void main(String[] args) {
35
36 List<Test> tests = new ArrayList<>();
37 Test test = new Test();
38 test.setId(1);
39 test.setTest("test");
40 test.setTid(10L);
41 test.setDate(new Date());
42 test.setDateTime(LocalDateTime.now());
43 tests.add(test);
44
45 String s = toJSONString(test);
46 String s1 = toJSONString(tests);
47
48 System.out.println(s1);
49 System.out.println(s);
50
51 Test test1 = parseObject(s, Test.class);
52 System.out.println(test1);
53 System.out.println(parseMap(s));
54
55 List<Object> objects = parseArray(s1);
56 System.out.println(objects);
57 }
58
59 public static <T> T parseObject(String text, Class<T> clazz) {
60 if (!StringUtils.isEmpty(text)) {
61 try {
62 return MAPPER.readValue(text, clazz);
63 } catch (IOException e) {
64 e.printStackTrace();
65 }
66 }
67 return null;
68 }
69
70 public static Map<String, Object> parseMap(String text) {
71 if (!StringUtils.isEmpty(text)) {
72 return new JacksonJsonParser().parseMap(text);
73 }
74 return null;
75 }
76
77 public static <T> List<T> parseArray(String text, Class<T> clazz) {
78 if (!StringUtils.isEmpty(text)) {
79 try {
80 JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, clazz);
81 return MAPPER.readValue(text, javaType);
82 } catch (IOException e) {
83 e.printStackTrace();
84 }
85 }
86 return null;
87 }
88
89 public static List<Object> parseArray(String text) {
90 if (!StringUtils.isEmpty(text)) {
91 return new JacksonJsonParser().parseList(text);
92 }
93 return null;
94 }
95
96 public static String toJSONString(Object clazz) {
97 if (clazz != null) {
98 try {
99 return MAPPER.writeValueAsString(clazz);
100 } catch (JsonProcessingException e) {
101 e.printStackTrace();
102 }
103 }
104 return null;
105 }
106}
与上一篇 Jackson 序列化工具类相比,这个版本多了 parseMap / parseArray(String) 两个无类型方法,适合不方便定义 POJO、只想拿到 Map/List 结构临时取值的场景;类型明确时优先用泛型版本,避免运行期强转出错。
