常用json的框架

1常用的框架 阿里的fastjson,谷歌的gson

javabean序列化json,性能Jackson>fastJson>Gson>Json

测试方法:循环序一百万次,看谁先完成

jackson  fastjson   gson类各有优缺点,但是优化无非是空间换时间,运行速度很快,但是cpu温度较高,负载较大;或者空间换时间,运行效率很低,但是性能稳定

2jackson的处理和关闭

指定字段不返回:@JsonIgore

指定日期样式 @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")

空字段不返回 @JsonInclude(Include.NON_NULL)

指定别名:@JsonProperty

上例子:

2.1创建一个高管类Executives:

package com.example.demo.bean;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Executives {
/**
* 姓名
*/
private String name;
/**
* 性别
* @JsonIgnore不返回字段
*/
@JsonIgnore
private String sex;
/**
* 工作
*/

private String work;
/**
* 住址
*/
private String address;
/**
* 存款
* @JsonProperty("account") 不返回真实的字段给别人,返回别名给别人
*/
@JsonProperty("account")
private int deposit;
/**
* 创建时间
* @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8") 转换时间格式
*/
@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
private Date createTime;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getDeposit() {
return deposit;
}
public void setDeposit(int deposit) {
this.deposit = deposit;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Executives(String name, String sex, String work, String address, int deposit, Date createTime) {
super();
this.name = name;
this.sex = sex;
this.work = work;
this.address = address;
this.deposit = deposit;
this.createTime = createTime;
}


}

2.2创建一个TestJsonController,用来测试返回的json数据

package com.example.demo.controller;

import java.util.Date;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.Executives;

@RestController
public class TestJsonController {



@GetMapping("/testJson")
public Object testJson() {
return new Executives("李明", "男", "程序员", "湖北省武汉市青山区", 9000, new Date());

}


}

2.3然后run application后,通过postman去请求接口

 @JsonIgore 忽略了性别   

@JsonProperty(“account”)对字段存款deposit引用了别名account 

  @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")  引用了正常的时间样式

原文地址:https://www.cnblogs.com/zhushilai/p/13497085.html