fastjson设置字段不序列化
alibaba的fasetjson可以设置字段不序列化,使⽤@JSONField注解的serialize属性,该属性默认是可以序列化的,设置成false就表⽰不可序列化:
使⽤⽅式就是在定义字段前加上@JSONField(serialize = false)
⽐如下⾯的实体类:
import com.alibaba.fastjson.annotation.JSONField;
import java.util.Objects;
fastjson忽略属性public class Employee {
@JSONField(serialize=false)
private String name;
private int age;
private double salary;
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; Employee employee = (Employee) o;
return age == employee.age &&
Doublepare(employee.salary, salary) == 0 && Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, salary);
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
使⽤Json进⾏序列化:
import com.alibaba.fastjson.JSON;
import org.junit.Test;
public class EmployeeTest {
@Test
public void serialTest(){
Employee employee=new Employee("aa",11,2000d); System.out.JSONString(employee)); }
}
可以看到序列化之后的字符串没有name字段。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。