Java下常用JSON的使用以及性能比较

前言

最近在研究Java下各种JSON的使用情况
待处理的实体类

package com.jsonTest;

/**
 * @author xiang.wei
 * @create 2018/5/14 13:22
 */
public class Person {

    private String code;
    private int age;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getAge() {
        return age;
    }

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

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




package com.jsonTest;


import java.util.List;

public class UserCenterSynModel implements java.io.Serializable{
    
    private static final long serialVersionUID = 5910206870550619784L;
    
    private String CUid;//主键id
    private String COldUserName;//老的用户名或者是税号
    private String CUsername;//用户名
    private String ctexnum; //税号
    private String CEmail;//邮箱--已验证的邮箱
    private String CAccountBlank;
    private String CAddress;
    private List<Person> personList; //关联用户


    public String getCUid() {
        return CUid;
    }

    public void setCUid(String CUid) {
        this.CUid = CUid;
    }

    public String getCOldUserName() {
        return COldUserName;
    }

    public void setCOldUserName(String COldUserName) {
        this.COldUserName = COldUserName;
    }

    public String getCUsername() {
        return CUsername;
    }

    public void setCUsername(String CUsername) {
        this.CUsername = CUsername;
    }

    public String getCtexnum() {
        return ctexnum;
    }

    public void setCtexnum(String ctexnum) {
        this.ctexnum = ctexnum;
    }

    public String getCEmail() {
        return CEmail;
    }

    public void setCEmail(String CEmail) {
        this.CEmail = CEmail;
    }

    public String getCAccountBlank() {
        return CAccountBlank;
    }

    public void setCAccountBlank(String CAccountBlank) {
        this.CAccountBlank = CAccountBlank;
    }

    public String getCAddress() {
        return CAddress;
    }

    public void setCAddress(String CAddress) {
        this.CAddress = CAddress;
    }

    public List<Person> getPersonList() {
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }

    @Override
    public String toString() {
        return "UserCenterSynModel{" +
                "CUid='" + CUid + '\'' +
                ", COldUserName='" + COldUserName + '\'' +
                ", CUsername='" + CUsername + '\'' +
                ", ctexnum='" + ctexnum + '\'' +
                ", CEmail='" + CEmail + '\'' +
                ", CAccountBlank='" + CAccountBlank + '\'' +
                ", CAddress='" + CAddress + '\'' +
                ", personList=" + personList +
                '}';
    }
}



fastJSON

作为阿里的一块JSON序列化和反序列化的工具,fastJSON还是很好用的
添加依赖

 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.8</version>
 </dependency>



测试代码

public class FastJSONTest {
    public static void main(String[] args) {
        UserCenterSynModel userCenterSynModel = new UserCenterSynModel();
        userCenterSynModel.setCAccountBlank("测试银行");
        userCenterSynModel.setCAddress("测试地址");
        userCenterSynModel.setCEmail("邮箱");
        userCenterSynModel.setCUid("11111");
        userCenterSynModel.setCtexnum("22222");
          List<Person> peopleList = new ArrayList<>();
        Person person1 = new Person();
        person1.setCode("测试用户1");
        person1.setAge(12);
        peopleList.add(person1);

        Person person2 = new Person();
        person2.setCode("测试用户2");
        person2.setAge(23);
        peopleList.add(person2);
        userCenterSynModel.setPersonList(peopleList);

        String json = JSON.toJSONString(userCenterSynModel);
        System.out.println("fastJSON序列化结果:"+json);
        System.out.println("fastJSON反序列化结果 :"+JSON.parseObject(json,UserCenterSynModel.class));
    }
}


测试结果:

fastJSON序列化结果:{"cAccountBlank":"测试银行","cAddress":"测试地址","cEmail":"邮箱","cUid":"11111","ctexnum":"22222","personList":[{"age":12,"code":"测试用户1"},{"age":23,"code":"测试用户2"}]}
fastJSON反序列化结果 :UserCenterSynModel{CUid='11111', COldUserName='null', CUsername='null', ctexnum='22222', CEmail='邮箱', CAccountBlank='测试银行', CAddress='测试地址', personList=[Person{code='测试用户1', age=12}, Person{code='测试用户2', age=23}]}


Gson
添加依赖

 <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>

测试代码

public class GsonTest {
    public static void main(String[] args) {
        UserCenterSynModel userCenterSynModel = new UserCenterSynModel();
        userCenterSynModel.setCAccountBlank("测试银行");
        userCenterSynModel.setCAddress("测试地址");
        userCenterSynModel.setCEmail("邮箱");
        userCenterSynModel.setCUid("11111");
        userCenterSynModel.setCtexnum("22222");
                List<Person> peopleList = new ArrayList<>();
        Person person1 = new Person();
        person1.setCode("测试用户1");
        person1.setAge(12);
        peopleList.add(person1);

        Person person2 = new Person();
        person2.setCode("测试用户2");
        person2.setAge(23);
        peopleList.add(person2);
        userCenterSynModel.setPersonList(peopleList);
       
        //序列化
        Gson gson = new Gson();
        String jsonStr=gson.toJson(userCenterSynModel);
        System.out.println(jsonStr);
        //反序列化
        UserCenterSynModel userCenterSynModel1 = gson.fromJson(jsonStr, UserCenterSynModel.class);
        System.out.println(userCenterSynModel1);

    }
}



测试结果

Gson序列化结果:{"CUid":"11111","ctexnum":"22222","CEmail":"邮箱","CAccountBlank":"测试银行","CAddress":"测试地址","personList":[{"code":"测试用户1","age":12},{"code":"测试用户2","age":23}]}
Gson反序列化结果:UserCenterSynModel{CUid='11111', COldUserName='null', CUsername='null', ctexnum='22222', CEmail='邮箱', CAccountBlank='测试银行', CAddress='测试地址', personList=[Person{code='测试用户1', age=12}, Person{code='测试用户2', age=23}]}


Jackson
添加依赖

 <!--jackson的使用-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.0</version>
        </dependency>
        

 

测试代码

public class JacksonTest {
    public static void main(String[] args) throws IOException {
        UserCenterSynModel userCenterSynModel = new UserCenterSynModel();
        userCenterSynModel.setCAccountBlank("测试银行");
        userCenterSynModel.setCAddress("测试地址");
        userCenterSynModel.setCEmail("邮箱");
        userCenterSynModel.setCUid("11111");
        userCenterSynModel.setCtexnum("22222");
       
              List<Person> peopleList = new ArrayList<>();
        Person person1 = new Person();
        person1.setCode("测试用户1");
        person1.setAge(12);
        peopleList.add(person1);

        Person person2 = new Person();
        person2.setCode("测试用户2");
        person2.setAge(23);
        peopleList.add(person2);
        userCenterSynModel.setPersonList(peopleList);
        //序列化
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(userCenterSynModel);
        System.out.println(json);
        //反序列化
        ObjectMapper mapper = new ObjectMapper();
        UserCenterSynModel user = mapper.readValue(json, UserCenterSynModel.class);
        System.out.println(user);
    }
}

测试结果

Jackson 序列化结果:{"ctexnum":"22222","personList":[{"code":"测试用户1","age":12},{"code":"测试用户2","age":23}],"caddress":"测试地址","cemail":"邮箱","coldUserName":null,"cusername":null,"cuid":"11111","caccountBlank":"测试银行"}
Jackson反序列化结果:UserCenterSynModel{CUid='11111', COldUserName='null', CUsername='null', ctexnum='22222', CEmail='邮箱', CAccountBlank='测试银行', CAddress='测试地址', personList=[Person{code='测试用户1', age=12}, Person{code='测试用户2', age=23}]}


json-lib
添加依赖

        <!--json-lib的使用-->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>


测试代码

public class NetJSONTest {
    public static void main(String[] args) {
        UserCenterSynModel userCenterSynModel = new UserCenterSynModel();
        userCenterSynModel.setCAccountBlank("测试银行");
        userCenterSynModel.setCAddress("测试地址");
        userCenterSynModel.setCEmail("邮箱");
        userCenterSynModel.setCUid("11111");
        userCenterSynModel.setCtexnum("22222");
           List<Person> peopleList = new ArrayList<>();
        Person person1 = new Person();
        person1.setCode("测试用户1");
        person1.setAge(12);
        peopleList.add(person1);

        Person person2 = new Person();
        person2.setCode("测试用户2");
        person2.setAge(23);
        peopleList.add(person2);
        userCenterSynModel.setPersonList(peopleList);
       
        JSONObject jsonObject = JSONObject.fromObject(userCenterSynModel);
        System.out.println("json-lib序列化结果:" + jsonObject.toString());
        UserCenterSynModel userCenterSynModel1 = (UserCenterSynModel) JSONObject.toBean(jsonObject, UserCenterSynModel.class);
        System.out.println("json-lib反序列化结果:"+userCenterSynModel1);
    }
}


测试结果

json-lib序列化结果:{"CAccountBlank":"测试银行","CAddress":"测试地址","CEmail":"邮箱","COldUserName":"","CUid":"11111","CUsername":"","ctexnum":"22222","personList":[{"age":12,"code":"测试用户1"},{"age":23,"code":"测试用户2"}]}
json-lib反序列化结果:UserCenterSynModel{CUid='11111', COldUserName='', CUsername='', ctexnum='22222', CEmail='邮箱', CAccountBlank='测试银行', CAddress='测试地址', personList=[net.sf.ezmorph.bean.MorphDynaBean@490d6c15[
  {code=测试用户1, age=12}
], net.sf.ezmorph.bean.MorphDynaBean@7d4793a8[
  {code=测试用户2, age=23}
]]}



性能比较

请参考:https://blog.csdn.net/vickyway/article/details/48157819




作者:码农飞哥
微信公众号:码农飞哥