Json工具类--使用2.x版本的Jackson实现了Json的序列化和反序列化

简介:使用2.x版本的Jackson(com.fasterxml.jackson包)实现了Json的序列化和反序列化!

1.工具类代码:


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by admin on 2017/9/30.
 * 使用2.x版本的Jackson(com.fasterxml.jackson包)实现了Json的序列化和反序列化
 */
public class TestJackson2 {
    public static final ObjectMapper OM = new ObjectMapper();
    static{
        /**
         * 设置将时间转换成指定的格式
         */
        OM.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    public static void main(String[] args) {
        Person person = new Person();
        person.setBirthday(new Date());
        String s1 = toJson(person,new SimpleDateFormat("yyyy年MM月dd日"));
        System.out.println(s1);

        Person person1 = fromJson(s1, Person.class,new SimpleDateFormat("yyyy年MM月dd日"));
        System.out.println(person1);
    }

    /**
     * 将Json串反序列化为ArrayList集合
     * @param json
     * @param object
     * @param 
     * @return
     */
    public static  ArrayList readValuesAsArrayList(String json, Class object) {
        try {
            return OM.readValue(json, OM.getTypeFactory().constructParametricType(ArrayList.class, object));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将Json串反序列化为ArrayList集合
     * @param json
     * @param t
     * @param 
     * @return
     */
    public static  ArrayList readValuesAsArrayList(String json, TypeReference> t) {
        try {
            return OM.readValue(json, t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将对象序列化为Json串
     * @param obj
     * @return
     */
    public static String toJson(Object obj){
        try {
            return OM.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将对象序列化为Json串,可以指定日期格式
     * @param obj
     * @param sdf
     * @return
     */
    public static String toJson(Object obj,SimpleDateFormat sdf){
        try {
            OM.setDateFormat(sdf);
            String result = OM.writeValueAsString(obj);
            OM.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将Json串反序列化成对象
     * @param json
     * @param clazz
     * @param 
     * @return
     */
    public static  T fromJson(String json, Class clazz){
        try {
            return OM.readValue(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将Json串反序列化成对象,可以指定日期格式
     * @param json
     * @param clazz
     * @param sdf
     * @param 
     * @return
     */
    public static  T fromJson(String json, Class clazz,SimpleDateFormat sdf){
        try {
            OM.setDateFormat(sdf);
            T t = OM.readValue(json, clazz);
            OM.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 测试对象Person
     */
    public static class Person implements Serializable {
        private String name;
        private Integer age;
        //@JsonFormat(pattern = "yyyy-MM-dd")
        private Date birthday;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", birthday=" + birthday +
                    '}';
        }
    }

}

你可能感兴趣的:(工具类,my_java_tools)