Contents
工具类
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
46
47 String s = toJSONString(test);
48 String s1 = toJSONString(tests);
49
50 System.out.println(s1);
51 System.out.println(s);
52
53 Test test1 = parseObject(s, Test.class);
54 System.out.println(test1);
55 System.out.println(parseMap(s));
56
57 List<Object> objects = parseArray(s1);
58 System.out.println(objects);
59
60 System.out.println(Ball.ball(1));
61
62
63 }
64
65
66 public static <T> T parseObject(String text, Class<T> clazz) {
67 if (!StringUtils.isEmpty(clazz)) {
68 try {
69 return MAPPER.readValue(text, clazz);
70 } catch (IOException e) {
71 e.printStackTrace();
72 }
73 }
74 return null;
75 }
76
77 public static Map<String, Object> parseMap(String text) {
78 if (!StringUtils.isEmpty(text)) {
79 return new JacksonJsonParser().parseMap(text);
80 }
81 return null;
82 }
83
84 public static <T> List<T> parseArray(String text, Class<T> clazz) {
85 if (!StringUtils.isEmpty(text)) {
86 try {
87 JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, clazz);
88 return MAPPER.readValue(text, javaType);
89 } catch (IOException e) {
90 e.printStackTrace();
91 }
92 }
93 return null;
94 }
95
96 public static List<Object> parseArray(String text) {
97 if (!StringUtils.isEmpty(text)) {
98 return new JacksonJsonParser().parseList(text);
99 }
100 return null;
101 }
102
103
104 public static String toJSONString(Object clazz) {
105 if (!StringUtils.isEmpty(clazz)) {
106 try {
107 return MAPPER.writeValueAsString(clazz);
108 } catch (JsonProcessingException e) {
109 e.printStackTrace();
110 }
111 }
112 return null;
113 }
114
115}
116