fastjson2/docs/mixin_cn.md
2022-12-29 00:22:42 +08:00

82 lines
2.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 使用Mixin注入Annotation定制序列化和反序列化
当你需要定制化序列化一些LIB里面的类你无法修改这些类的代码你可以使用FASTJSON2的Minxin功能注入Annotation。
比如:
## 1. 要序列化的类
```java
public static class Product {
public String name;
}
```
## 2. 注入配置类
```java
public abstract class ProductMixin {
@JSONField(name = "productName")
String name;
}
```
## 3. 注入配置 & 使用
```java
// 配置注入
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);
```
## 4. 结合JSONCreator & JSONField(value=true)的例子
如下的Address类没有缺省构造函数只有一个字段我希望序列化结果为address字段的字符串。
```java
public static class Address {
private final String address;
public Address(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
```
注入使用方式如下:
```java
static class AddressMixin {
// 通过JSONCreator指定构造函数
@JSONCreator
public AddressMixin(@JSONField(value = true) String address) {
}
// 配置输出使用此字段
@JSONField(value = true)
public String getAddress() {
return null;
}
}
@Test
public void test() {
JSON.mixIn(Address.class, AddressMixin.class); // 通过mixin注入Annotation
Address address = new Address("HangZhou");
// 序列化输出结果为address字段
String str = JSON.toJSONString(address);
assertEquals("\"HangZhou\"", str);
// 通过结果可以看出使用了JSONCreator指定的构造函数
Address address1 = JSON.parseObject(str, Address.class);
assertEquals(address.getAddress(), address1.getAddress());
}
```