fastjson2 全局枚举序列化 反序列化
时间: 2023-07-18 17:36:41 浏览: 172
在 fastjson2 中,可以通过实现 SerializerFeature 和 Feature 接口来实现全局枚举的序列化和反序列化。
1. 全局枚举的序列化
可以通过实现 SerializerFeature 接口来实现全局枚举的序列化,例如:
```java
public enum MyEnum {
A, B, C;
}
public class MyEnumSerializer implements ContextValueSerializer, SerializerFeature {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
if (object == null) {
serializer.getWriter().writeNull();
return;
}
Enum<?> e = (Enum<?>) object;
serializer.getWriter().writeValue(e.ordinal());
}
@Override
public boolean writeAsArray() {
return false;
}
@Override
public boolean writeNull() {
return true;
}
@Override
public boolean writeEnumUsingName() {
return false;
}
@Override
public boolean writeEnumUsingToString() {
return false;
}
@Override
public boolean writeEnumUsingOrdinal() {
return true;
}
@Override
public boolean writeSlashAsSpecial() {
return false;
}
@Override
public boolean writeTabAsSpecial() {
return false;
}
@Override
public boolean writeQuoteCharAsSpecial() {
return false;
}
@Override
public boolean writeSingleQuoteAsChar() {
return false;
}
@Override
public boolean writeClassName() {
return false;
}
@Override
public boolean notWriteDefaultValue() {
return false;
}
@Override
public boolean browserSecure() {
return false;
}
@Override
public boolean writeDateUseDateFormat() {
return false;
}
@Override
public boolean sortField() {
return false;
}
@Override
public boolean writeTab() {
return false;
}
@Override
public boolean prettyFormat() {
return false;
}
@Override
public boolean writeContentLength() {
return false;
}
@Override
public boolean quoteFieldNames() {
return false;
}
@Override
public boolean useSingleQuotes() {
return false;
}
@Override
public boolean writeNonStringValueAsString() {
return false;
}
@Override
public boolean notWriteRootClassName() {
return false;
}
@Override
public boolean beanToArray() {
return false;
}
@Override
public boolean writeBigDecimalAsPlain() {
return false;
}
@Override
public boolean ignoreNonFieldGetter() {
return false;
}
@Override
public boolean writeEnumUsingToStringUsingGlobalSetting() {
return false;
}
@Override
public boolean writeEnumUsingNameUsingGlobalSetting() {
return false;
}
@Override
public boolean writeDirect() {
return false;
}
@Override
public boolean browserCompatible() {
return false;
}
}
```
然后,在初始化 JSON 序列化器时,可以将该序列化器添加到全局的 SerializerFeature 中,例如:
```java
SerializeConfig config = new SerializeConfig();
config.put(MyEnum.class, MyEnumSerializer.INSTANCE);
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, MyEnum.class);
```
这样,无论什么时候序列化 MyEnum 类型的枚举值,都会使用 MyEnumSerializer 中定义的方式进行序列化。
2. 全局枚举的反序列化
可以通过实现 Feature 接口来实现全局枚举的反序列化,例如:
```java
public enum MyEnum {
A, B, C;
}
public class MyEnumFeature implements Feature {
@Override
public int getMask() {
return 0;
}
@Override
public boolean isEnabled(int features) {
return true;
}
@Override
public boolean isEnabled(int features, int feature) {
return true;
}
@Override
public int config(int features, int feature, boolean state) {
if (feature == MyEnumFeature.WRITE_ENUM_USING_ORDINAL.mask) {
return state ? features | MyEnumFeature.WRITE_ENUM_USING_ORDINAL.mask
: features & ~MyEnumFeature.WRITE_ENUM_USING_ORDINAL.mask;
}
return features;
}
public static final MyEnumFeature WRITE_ENUM_USING_ORDINAL = new MyEnumFeature(1);
public static final MyEnumFeature WRITE_ENUM_USING_NAME = new MyEnumFeature(2);
private final int mask;
private MyEnumFeature(int mask) {
this.mask = mask;
}
}
```
然后,在初始化 JSON 解析器时,可以将该 Feature 添加到全局的 Feature 中,例如:
```java
ParserConfig config = new ParserConfig();
config.addCompatibleFeature(MyEnumFeature.WRITE_ENUM_USING_ORDINAL);
JSON.DEFAULT_PARSER_FEATURE |= MyEnumFeature.WRITE_ENUM_USING_ORDINAL.mask;
```
这样,无论什么时候反序列化 MyEnum 类型的枚举值,都会使用 MyEnumFeature 中定义的方式进行反序列化。
阅读全文