import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter.Feature;
import org.junit.jupiter.api.Test;
@Test
public void test() {
Bean bean = new Bean();
bean.id = 100;
assertEquals("{\"id\":\"100\"}", JSON.toJSONString(bean));
public static class Bean {
@JSONField(serializeFeatures = Feature.WriteNonStringValueAsString)
public int id;
1.6 通过JSONField(value = true)配置JavaBean序列化字段和反序列化构造方式
将@JSONField(value = true)配置在其中一个字段上,序列化时,会将对象序列化时按照该字段的值输出。将@JSONField(value = true)配置在构造函数参数上,而且该构造函数只有1个参数,就会按照这个参数来构造对象
public static class Bean2 {
private final int code;
public Bean2(@JSONField(value = true) int code) {
this.code = code;
@JSONField (value = true)
public int getCode() {
return code;
@Test
public void test2() {
Bean2 bean = new Bean2(101);
String str = JSON.toJSONString(bean);
assertEquals("101", str);
Bean2 bean1 = JSON.parseObject(str, Bean2.class);
assertEquals(bean.code, bean1.code);
1.6 通过JSONField(value = true)配置Enum基于其中一个字段序列化和反序列化
将@JSONField(value = true)配置在enum字段上,序列化和序列化都会基于这个字段来进行,比如:
public enum Type {
X(101, "Big"),
M(102, "Medium"),
S(103, "Small");
private final int code;
private final String name;
Type(int code, String name) {
this.code = code;
this.name = name;
@JSONField(value = true)
public int getCode() {
return code;
public String getName() {
return name;
public class Bean1 {
public Type type;
@Test
public void test1() {
Bean1 bean = new Bean1();
bean.type = Type.M;
String str = JSON.toJSONString(bean);
assertEquals("{\"type\":102}", str);
Bean1 bean1 = JSON.parseObject(str, Bean1.class);
assertEquals(bean.type, bean1.type);
2. @JSONType
JSONType是配置在类/接口上的注解,可以配置改类的所有字段的NamingStrategy、序列化和反序列化忽略的字段、JSONReader/JSONWriter的Features等。
@JSONType(ignores = {"id2", "id3"})
public static class Bean {
public int getId() {
return 101;
public int getId2() {
return 102;
public int getId3() {
return 103;
2.2 配置序列化时保持原生类字段顺序
从2.0.13版本开始,您可以通过@JSONType(alphabetic = false)
配置序列化时保持原生类字段顺序。
@Slf4j
public class JSONTypeAlphabetic {
@JSONType(alphabetic = false)
public static class Bean {
public int f3;
public int f1;
public int f2;
public int f0;
@Test
public void test() {
Bean bean = new Bean();
bean.f0 = 101;
bean.f1 = 102;
bean.f2 = 103;
bean.f3 = 104;
log.info(JSON.toJSONString(bean));
//{"f3":104,"f1":102,"f2":103,"f0":101}
2.3 配置序列化时的JSONReader
/JSONWriter
的Features
您可以通过@JSONType(serializeFeatures= ...)
或@JSONType(deserializeFeatures = ...)
注解配置序列化和反序列时JSONWriter
/JSONReader
的Features
。
更多Features
配置请参考 features_cn.md 。
@Slf4j
public class JSONTypeFeatures {
// 反序列化时对字符串进行trim
// 序列化时输出为null的字段
@JSONType(deserializeFeatures = JSONReader.Feature.TrimString, serializeFeatures = JSONWriter.Feature.WriteNulls)
public static class OrdersBean {
public String filed1;
public String filed2;
@Test
public void test() {
OrdersBean bean = new OrdersBean();
bean.filed1 = "fastjson2";
log.info(JSON.toJSONString(bean));
//{"filed1":"fastjson2","filed2":null}
String json="{\"filed1\":\" fastjson2 \",\"filed2\":\"2\"}";
OrdersBean bean2 = JSON.parseObject(json, OrdersBean.class);
log.info(bean2.filed1);
//fastjson2
2.4 配置序列化时字段顺序
您可以通过@JSONType(orders = {"filed1", "filed2"})
注解指定序列化时的字段顺序。
@Slf4j
public class JSONTypeOrders {
@JSONType(orders = {"filed4", "filed3", "filed2", "filed1"})
public static class OrdersBean {
public String filed1;
public String filed2;
public String filed3;
public String filed4;
@Test
public void test() {
OrdersBean bean = new OrdersBean();
bean.filed1 = "1";
bean.filed2 = "2";
bean.filed3 = "3";
bean.filed4 = "4";
log.info(JSON.toJSONString(bean));
//{"filed4":"4","filed3":"3","filed2":"2","filed1":"1"}
3. 基于mixin机制注入Annotation
当你需要定制化序列化一些LIB里面的类,你无法修改这些类的代码,你可以使用FASTJSON2的Minxin功能注入Annotation。
具体使用介绍参考这里: https://alibaba.github.io/fastjson2/mixin_cn