Jackson、Gson和Fastjson入门代码

一、Jackson

1、先看用法,代码跑起来自然就入门了。

① pom设置


    com.fasterxml.jackson.core
    jackson-databind
    2.11.0

② 创建一个Person类如下:

Person.java

public class Person {
    private int age;
    private String name;
    private String school;
    private String major;

    public Person() {
    }

    public Person(int age, String name, String school, String major) {
        this.age = age;
        this.name = name;
        this.school = school;
        this.major = major;
    }

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

    public void setSchool(String school) {
        this.school = school;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getSchool() {
        return school;
    }

    public String getMajor() {
        return major;
    }
}

 ③ JasonDemo.java

public class JacksonDemo {
    public static void Serialize() throws IOException {
        //使用ObjectMapper来转化对象json
        ObjectMapper mapper = new ObjectMapper();
        Person person = new Person(23,"张三","天津大学","光学工程");
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        //设置序列化后的格式,INDENT_OUTPUT表示缩进输出,true表示试该配置生效
        mapper.configure(SerializationFeature.INDENT_OUTPUT,true);
        //序列化结果输出为字符串
        String str = mapper.writeValueAsString(person);
        System.out.println(str);
        //序列化结果输出为文件输出
        mapper.writeValue(new File("Jackson.json"),person);
    }
    public static void main(String[] args) throws IOException {
        Serialize();
    }
}

Jackson、Gson和Fastjson入门代码_第1张图片

Jackson、Gson和Fastjson入门代码_第2张图片

注释mapper.configure一行输出:

//mapper.configure(SerializationFeature.INDENT_OUTPUT,true);

二、Gson

① pom配置


    com.google.code.gson
    gson
    2.8.5

② 同person类

③ GsonDemo.java

这里还不会怎么直接写入json文件,直接找了字符串写入json文件的写法:

public class GsonDemo {
    public static void saveJson(String jsonStr, String fileName){
        /**
         * 将String类型写入到 *.json中
         */
        FileWriter writer;
        try{
            writer = new FileWriter(fileName);
            writer.write(jsonStr);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String readJson(String fileName){
        /**
         * @param filename 读取 *.json文件
         */
        StringBuffer content = new StringBuffer();
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new FileReader(fileName));
            String line = null;
            while ((line=reader.readLine())!=null){
                content.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if(reader!=null){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content.toString();
    }
    /**
     *   序列化
     */
    public static void Serial(){
        String fileName = "gson.json";
        Person person = new Person(23,"张三","天津大学","光学工程");
        String str = new Gson().toJson(person);
        //将对象进行实例化
        String jsonStr = new Gson().toJson(person);
        //保存json文件
        saveJson(jsonStr,fileName);
        System.out.println("序列化结果:"+jsonStr);
    }
    /**
     *    反序列化
     */
    public static void deSerial(){
        String fileName = "gson.json";
        //读取json文件
        String jsonStr = readJson(fileName);
        //将对象反序列化
        Person person1DeSerial = new Gson().fromJson(jsonStr,Person.class);
        System.out.println("反序列化结果:"+person1DeSerial);
    }

    public static void main(String[] args) {
        Serial();
        //等待一会
        try {
            Thread.sleep(1000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        deSerial();
    }
}

在deSerial()方法中直接输出反序列化后的结果,需要重写toString()方法,使用apache.commoms重写的,在pom中加入: 


    org.apache.commons
    commons-lang3
    3.1

Jackson、Gson和Fastjson入门代码_第3张图片

最后输出:

Jackson、Gson和Fastjson入门代码_第4张图片

增加Fastjson序列化反序列化情况:

//测试Fastjson
    public static void testFastson(long times){
        Num num = new Num();
        for(long i = 0;i

三、性能比较--略(可以自行百度)

1、时间性能

序列化反序列化n次对比时间(使用System.currentTimeMillis()查看代码运行时间)

*********************************
   Gson序列化10次耗时:0.003s
Jackson序列化10次耗时:0.002s
Fastson序列化10次耗时:0.001s
   Gson反序列化10次耗时:0.0s
Jackson反序列化10次耗时:0.004s
Fastson反序列化10次耗时:0.002s
*********************************
   Gson序列化100次耗时:0.01s
Jackson序列化100次耗时:0.016s
Fastson序列化100次耗时:0.003s
   Gson反序列化100次耗时:0.006s
Jackson反序列化100次耗时:0.013s
Fastson反序列化100次耗时:0.014s
*********************************
   Gson序列化1000次耗时:0.026s
Jackson序列化1000次耗时:0.029s
Fastson序列化1000次耗时:0.012s
   Gson反序列化1000次耗时:0.028s
Jackson反序列化1000次耗时:0.034s
Fastson反序列化1000次耗时:0.044s
*********************************
   Gson序列化10000次耗时:0.119s
Jackson序列化10000次耗时:0.059s
Fastson序列化10000次耗时:0.045s
   Gson反序列化10000次耗时:0.083s
Jackson反序列化10000次耗时:0.081s
Fastson反序列化10000次耗时:0.094s
*********************************
   Gson序列化100000次耗时:1.031s
Jackson序列化100000次耗时:0.243s
Fastson序列化100000次耗时:0.101s
   Gson反序列化100000次耗时:0.383s
Jackson反序列化100000次耗时:0.332s
Fastson反序列化100000次耗时:0.501s
*********************************
   Gson序列化1000000次耗时:5.932s
Jackson序列化1000000次耗时:2.182s
Fastson序列化1000000次耗时:1.007s
   Gson反序列化1000000次耗时:3.916s
Jackson反序列化1000000次耗时:3.468s
Fastson反序列化1000000次耗时:4.925s
*********************************
   Gson序列化10000000次耗时:57.236s
Jackson序列化10000000次耗时:20.925s
Fastson序列化10000000次耗时:10.006s
   Gson反序列化10000000次耗时:39.532s
Jackson反序列化10000000次耗时:34.234s
Fastson反序列化10000000次耗时:49.246s

2、内存性能

序列化反序列化n次使用Jdk自带的Jconsole.exe查看堆内存变化情况

java查看内存还可以使用Runtime.getRuntime()

public static void getMemoryGson(long times)throws JsonProcessingException{
        Runtime run = Runtime.getRuntime();
        long max = run.maxMemory();
        long total = run.totalMemory();

        for(long i = 0;i

你可能感兴趣的:(Java)