FastJson之@JSONType注解简介
放在实体类上就会只装配列举的字段或者排除列举的成员变量
@JSONType(ignores ={"id","sex"})//不序列化这两个
public class Pojo2 {
@JSONType(includes ={"name","sex"})//序列化只序列化这两个属性
public class Pojo1 {
demo演⽰
Pojo1
@JSONType(includes ={"name","sex"})//序列化只序列化这两个属性成员变量public class Pojo1 {
private int id;
private String name;
private String sex;
private String password;
private String phone;
//*******************
public int getId(){
return id;
}
public Pojo1 setId(int id){
this.id = id;
return this;
}
public String getName(){
return name;
}
public Pojo1 setName(String name){
this.name = name;
return this;
}
public String getSex(){
return sex;
}
public Pojo1 setSex(String sex){
this.sex = sex;
return this;
}
public String getPassword(){
return password;
}
public Pojo1 setPassword(String password){
this.password = password;
return this;
}
public String getPhone(){
return phone;
}
public Pojo1 setPhone(String phone){
this.phone = phone;
return this;
}
}
Pojo2
@JSONType(ignores ={"id","sex"})//不序列化这两个成员变量public class Pojo2 {
private int id;
private String name;
private String sex;
private String password;
private String phone;
//************
public int getId(){
return id;
}
public Pojo2 setId(int id){
fastjson忽略属性this.id = id;
return this;
}
public String getName(){
return name;
}
public Pojo2 setName(String name){
this.name = name;
return this;
}
public String getSex(){
return sex;
}
public Pojo2 setSex(String sex){
this.sex = sex;
return this;
}
public String getPassword(){
return password;
}
public Pojo2 setPassword(String password){
this.password = password;
return this;
}
public String getPhone(){
return phone;
}
public Pojo2 setPhone(String phone){
this.phone = phone;
return this;
}
}
JSONTypeDemo
import com.alibaba.fastjson.JSON;
import org.junit.Test;
public class JSONTypeDemo {
static Pojo1 pojo1;
static Pojo2 pojo2;
static{
pojo1 =new Pojo1();
pojo1.setId(11).setName("名字1").setPassword("123456").setPhone("135********")
.setSex("男");
pojo2 =new Pojo2();
pojo2.setId(11).setName("名字1").setPassword("123456").setPhone("135********")
.setSex("男");
}
/**
* 演⽰JSONType
*/
@Test
public void ceui1(){
String jsonString = JSONString(pojo1);
System.out.println("jsonString = "+ jsonString);//jsonString = {"name":"名字1","sex":"男"}
}
@Test
public void ceui3(){
String jsonString = JSONString(pojo2);
System.out.println("jsonString = "+ jsonString);//jsonString = {"name":"名字1","password":"123456","phone":"135********"} }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论