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
 38        Test test = new Test();
 39
 40        test.setId(1);
 41        test.setTest("test");
 42        test.setTid(10L);
 43
 44        test.setDate(new Date());
 45        test.setDateTime(LocalDateTime.now());
 46        tests.add(test);
 47        String s = toJSONString(test);
 48
 49        System.out.println(s);
 50        Test list = parseObject(s, Test.class);
 51
 52        System.out.println(list.toString());
 53
 54
 55    }
 56
 57
 58    public static <T> T parseObject(String text, Class<T> clazz) {
 59        if (text != null) {
 60            try {
 61                return mapper.readValue(text, clazz);
 62            } catch (IOException e) {
 63                e.printStackTrace();
 64            }
 65        }
 66        return null;
 67    }
 68
 69    public static <T> T parseObject(String text, TypeReference<T> typeReference) {
 70        if (text != null) {
 71            try {
 72                return mapper.readValue(text, typeReference);
 73            } catch (IOException e) {
 74                e.printStackTrace();
 75            }
 76        }
 77        return null;
 78    }
 79
 80    public static <T> List<T> parseArray(String text, Class<T> clazz) {
 81        if (text != null) {
 82            try {
 83                JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, clazz);
 84                return mapper.readValue(text, javaType);
 85            } catch (IOException e) {
 86                e.printStackTrace();
 87            }
 88        }
 89        return null;
 90    }
 91
 92
 93    public static String toJSONString(Object clazz) {
 94        if (clazz != null) {
 95            try {
 96                return mapper.writeValueAsString(clazz);
 97            } catch (JsonProcessingException e) {
 98                e.printStackTrace();
 99            }
100        }
101        return "";
102    }
103
104}
105
106