import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import java.lang.reflect.Type;
public class LicenseKeyTypesDeserializer implements ObjectDeserializer
{
@Override
public LicenseKeyTypes deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
Integer intValue = parser.parseObject(int.class);
if(LicenseKeyTypes.get(intValue) != null)
return LicenseKeyTypes.get(intValue);
throw new IllegalStateException();
}
@Override
public int getFastMatchToken() {
return JSONToken.LITERAL_INT;
}
}
import com.incoresoft.licenseserver.models.Interfaces.IEnum;
import lombok.Getter;
@Getter
public enum LicenseKeyTypes implements IEnum<Integer>
{
Text(0),
Floating(1),
BasicKey(2),
CryptoKey(3),
Online(4);
private Integer id;
private LicenseKeyTypes(int id)
{
this.id = id;
}
public static LicenseKeyTypes get(final Integer id)
{
switch (id)
{
case 0:
return Text;
case 1:
return Floating;
case 2:
return BasicKey;
case 3:
return CryptoKey;
case 4:
return Online;
}
return BasicKey;
}
}
public class LicenseTemplate
{
@JSONField(deserializeUsing = LicenseKeyTypesDeserializer.class)
private LicenseKeyTypes selectedLicenseType;
public static LicenseTemplate fromJSON(JSONObject json)
{
LicenseTemplate template = template = JSON.toJavaObject(json, LicenseTemplate.class);
return template;
}
}
1条答案
按热度按时间2izufjch1#
不使用lombok试试看?