fastjson Deserialize not working

xpszyzbs  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(172)
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;
	}
}

相关问题