Fastjson
這玩意兒不多說,Alibaba出品,出過幾次嚴重的安全漏洞,但是依然很流行。這里寫一下它怎么處理枚舉。
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
序列化為name()值
默認就是,啥也不用動
import com.alibaba.fastjson.JSON;
enum Gender {
BOY, GIRL, UNKNOW
}
class User {
private Integer id;
private Gender gender;
public User() {
}
public User(Integer id, Gender gender) {
super();
this.id = id;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}
public class MainTest {
public static void main(String[] args) throws Exception {
// 序列化為JSON輸出
User user = new User(1, Gender.BOY);
String jsonString = JSON.toJSONString(user);
System.out.println(jsonString); // {"gender":"BOY","id":1}
// 反序列化為對象
user = JSON.parseObject(jsonString, User.class);
System.out.println(user.getGender()); // BOY
}
}
序列化為ordinal()值
全局設置
代碼跟上面沒有變化,就需要在開頭添加一句代碼,任何枚舉,都會被序列化為ordinal()值
JSON.DEFAULT_GENERATE_FEATURE &= ~SerializerFeature.WriteEnumUsingName.mask;
// 全局設置,枚舉的序列化使用 ordinal()
JSON.DEFAULT_GENERATE_FEATURE &= ~SerializerFeature.WriteEnumUsingName.mask;
// 序列化為JSON輸出,枚舉值為 ordinal()
User user = new User(1, Gender.BOY);
String jsonString = JSON.toJSONString(user);
System.out.println(jsonString); // {"gender":0,"id":1}
// 反序列化為對象
user = JSON.parseObject(jsonString, User.class);
System.out.println(user.getGender()); // BOY
特殊設置
只想針對某一次序列化生效。只需要調用JSON.toJSONString的重載方法,添加配置就行。
public static String toJSONString(Object object, int defaultFeatures, SerializerFeature... features)
// 序列化為JSON輸出,本次把枚舉值序列化為 ordinal()
User user = new User(1, Gender.BOY);
String jsonString = JSON.toJSONString(user, JSON.DEFAULT_GENERATE_FEATURE & ~SerializerFeature.WriteEnumUsingName.mask);
System.out.println(jsonString); // {"gender":0,"id":1}
// 反序列化為對象
user = JSON.parseObject(jsonString, User.class);
System.out.println(user.getGender()); // BOY
序列化為自定義屬性
很多人也喜歡給枚舉定義一個私有的屬性,序列化為JSON時,希望以這個屬性值作為value,這個時候就需要自己定義JSON的序列化和反序列化實現了。Fastjson提供了2個接口。用戶控制序列化和反序列化行為,這個實在是太簡單,這里不多說。看代碼
- ObjectSerializer
- ObjectDeserializer
序列化器的定義
import java.io.IOException;
import java.lang.reflect.Type;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
public class GenderEnumSerializer implements ObjectSerializer {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
// 強制把值轉換為Gender
Gender gender = (Gender) object;
// 序列化為自定義的name屬性,輸出就行
serializer.out.writeString(gender.getName());
}
}
反序列化器的定義
import java.lang.reflect.Type;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
public class GenderEnumDeserializer implements ObjectDeserializer {
@SuppressWarnings("unchecked")
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
// 解析值為字符串
String value = parser.parseObject(String.class);
// 遍歷所有的枚舉實例
for (Gender gender : Gender.values()) {
if (gender.getName().equals(value)) {
// 成功匹配,返回實例
return (T) gender;
}
}
// 沒有匹配到,可以拋出異常或者返回null
return null;
}
@Override
public int getFastMatchToken() {
// 僅僅匹配字符串類型的值
return JSONToken.LITERAL_STRING;
}
}
對象 & 枚舉的定義
enum Gender {
BOY("男"), GIRL("女"), UNKNOW("不知道");
public final String name;
Gender(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class User {
private Integer id;
// 標識注解,指定枚舉的序列化。反序列化實現類
@JSONField(serializeUsing = GenderEnumSerializer.class, deserializeUsing = GenderEnumDeserializer.class)
private Gender gender;
public User() {
}
public User(Integer id, Gender gender) {
super();
this.id = id;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}
測試
public class MainTest {
public static void main(String[] args) throws Exception {
// 序列化為JSON輸出,枚舉值為 getName()
User user = new User(1, Gender.UNKNOW);
String jsonString = JSON.toJSONString(user);
System.out.println(jsonString); // {"gender":"不知道","id":1}
// 反序列化為對象
user = JSON.parseObject(jsonString, User.class);
System.out.println(user.getGender()); // UNKNOW
}
}
最后
很顯然,自定義 ObjectSerializer /ObjectDeserializer 的方式最為靈活,可以考慮抽象一個接口出來,讓所有的枚舉都實現接口。這樣針對接口編寫ObjectSerializer /ObjectDeserializer實現,就可以很好的復用了。
