简介
放在实体类上就会只装配列举的字段或者排除列举的成员变量
@JSONType(ignores = {
"id", "sex"}) //不序列化这两个
public class Pojo2 {
@JSONType(includes = {
"name", "sex"}) //序列化只序列化这两个属性
public class Pojo1 {
demo演示
Pojo1
package fastjsonDemo.JSONDemo.demo1.AnnotationDemo.JSONType;
import com.alibaba.fastjson.annotation.JSONType;
@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
package fastjsonDemo.JSONDemo.demo1.AnnotationDemo.JSONType;
import com.alibaba.fastjson.annotation.JSONType;
@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) {
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
package fastjsonDemo.JSONDemo.demo1.AnnotationDemo.JSONType;
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("13502141419")
.setSex("男");
pojo2 = new Pojo2();
pojo2.setId(11).setName("名字1").setPassword("123456").setPhone("13502141419")
.setSex("男");
}
/**
* 演示JSONType
*/
@Test
public void ceui1() {
String jsonString = JSON.toJSONString(pojo1);
System.out.println("jsonString = " + jsonString); //jsonString = {"name":"名字1","sex":"男"}
}
@Test
public void ceui3() {
String jsonString = JSON.toJSONString(pojo2);
System.out.println("jsonString = " + jsonString); //jsonString = {"name":"名字1","password":"123456","phone":"13502141419"}
}
}