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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014534808/article/details/80305316

前言

最近在研究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

展开阅读全文

Java 高级篇:JVM 垃圾回收机制

03-11

JVM 的内存模型和 JVM 的垃圾回收机制一直是 Java 业内从业者绕不开的话题(实际调优、面试),Jack 和大家再来一起学习一下 JVM 的垃圾回收机制。作为 Java 从业者如果不掌握这些知识点可能很难跳过高级走向架构师阶段。 本场 Chat,作者将会围绕 JVM 的内存模型,JVM 的垃圾回收,JVM 的参数设置和如何监控这四个话题展开。通过本场 Chat 相信大家会学会如下内容: 1. JVM 内存模型是什么样的,都存什么? 2. JVM 的垃圾回收机制是什么?都有哪些算法? 3. JVM 和垃圾回收对应的参数有哪些?如何去设置? 4. JVM 生产监控的指标有哪些? 5. 轻松应对 JVM 的面试和实际工作。 通过本场 Chat 希望能和大家一起提升和学习。 实录内容提要: 1. 递归和 for 循环分别影响的是哪块内存? 1. 垃圾回收的触发时机是什么? 1. 如何判断一个对象该不该回收? 1. 对象四种引用方式是什么,在什么场景用到? 1. -D -XX -X 有什么区别? 1. 问题默认开启的是哪个 GC ? 1. 我们一般需要关注的参数有哪些? 1. 实际生产环境你们设置了哪些参数? 1. 监控的工具有哪些? 1. Java 8 引入了元空间,元空间和永久代有什么不一样?元空间和永久代可以理解为方法区的不同实现吗? 1. 国外有哪些一手JVM资料的获取途径?尤其是在 JVM 规范、 HotSpot 实现方面? 1. 既然实际中很少去配 JVM 参数,为什么面试的时候还总问 JVM 优化的问题?如果缺乏 1. JVM 调优经验,面试中该如何应对? 1. 能否把对 JVM 的监控集成到系统里面? *当前内容版权归码字科技所有并授权显示,盗版必究。[阅读原文](http://gitbook.cn/gitchat/activity/5aa53c73fa9584265fa3db68)*

没有更多推荐了,返回首页