-
Notifications
You must be signed in to change notification settings - Fork 512
fastjson2_annotations
温绍锦 edited this page Apr 23, 2022
·
9 revisions
JSONField是作用在Field、Method、Parameter上的Annotation,可以用来指定序列化字段的顺序、名字、格式、是否忽略、配置JSONReader/JSONWriter的Features等。
可以通过JSONField.name来配置序列化输出的字段名和反序列化是映射的字段名
- 配置在public field上
public class A {
@JSONField(name = "ID")
public int id;
}
- 配置在public getter/setter method上
public class A {
private int id;
@JSONField(name = "ID")
public int getId() {return id;}
@JSONField(name = "ID")
public void setId(int value) {this.id = id;}
}
在Date类型的字段,经常需要用定制的格式做序列化和反序列化,可以通过JSONField.format来做配置。
public class VO {
// 配置date序列化和反序列使用yyyyMMdd日期格式
@JSONField(format = "yyyyMMdd")
public Date date;
}
可以通过JSONField.serialize配置该字段是否要序列化,通过JSONField.deserialize配置该字段是否需要反序列化。
- 配置序列化反序列化忽略特定字段
public class VO {
@JSONField(serialize = false)
public Date date;
}
- 配置反序列化忽略特定字段
public class VO {
@JSONField(deserialize = false)
public Date date;
}
可以通过JSONField.ordinal来配置序列化输出的顺序
public static class FloorV2 implements Area {
@JSONField(ordinal = 1)
public String type;
@JSONField(ordinal = 2)
public String templateId;
}