com.jn.easyjson.fastjson.codec.EnumCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easyjson-fastjson Show documentation
Show all versions of easyjson-fastjson Show documentation
Adapter easyjson to Alibaba fastjson
package com.jn.easyjson.fastjson.codec;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.jn.langx.util.Strings;
import com.jn.langx.util.enums.Enums;
import com.jn.langx.util.function.Supplier0;
import com.jn.langx.util.reflect.Reflects;
import com.jn.langx.util.reflect.type.Primitives;
import com.jn.langx.util.reflect.type.Types;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class EnumCodec implements ObjectSerializer, ObjectDeserializer, Typed {
private boolean serialUseIndex;
private boolean serialUseToString;
private String serialUseField;
public boolean isSerialUseIndex() {
return serialUseIndex;
}
public void setSerialUseIndex(boolean serialUseIndex) {
this.serialUseIndex = serialUseIndex;
}
public boolean isSerialUseToString() {
return serialUseToString;
}
public void setSerialUseToString(boolean serialUseToString) {
this.serialUseToString = serialUseToString;
}
public String getSerialUseField() {
return serialUseField;
}
public void setSerialUseField(String serialUseField) {
this.serialUseField = serialUseField;
}
@Override
public T deserialze(DefaultJSONParser parser, final Type type, Object fieldName) {
final JSONLexer lexer = parser.lexer;
final int token = lexer.token();
if (token == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
if ((serialUseToString) && token == JSONToken.LITERAL_STRING) {
String stringValue = lexer.stringVal();
return (T) Enums.ofToString((Class extends Enum>) type, stringValue);
} else if (serialUseIndex && token == JSONToken.LITERAL_INT) {
int intValue = lexer.intValue();
return (T) Enums.ofValue(intValue, (Class extends Enum>) type);
} else if (Strings.isNotEmpty(serialUseField)) {
return (T) Enums.ofField((Class extends Enum>) type, serialUseField, new Supplier0