-
Notifications
You must be signed in to change notification settings - Fork 478
mixin_cn
温绍锦 edited this page May 3, 2022
·
2 revisions
当你需要定制化序列化一些LIB里面的类,你无法修改这些类的代码,你可以使用FASTJSON2的Minxin功能注入Annotation。
比如:
public static class Product {
public String name;
}
public abstract class ProductMixin {
@JSONField(name = "productName")
String name;
}
// 配置注入
JSON.mixIn(Product.class, ProductMixin.class);
// 使用
Product product = new Product();
product.name = "DataWorks";
assertEquals("{\"productName\":\"DataWorks\"}", JSON.toJSONString(product));
Product productParsed = JSON.parseObject("{\"productName\":\"DataWorks\"}", Product.class);
assertEquals("DataWorks", productParsed.name);