Created
January 25, 2021 07:59
-
-
Save pangda0xff/2996224cdbff126b4aa69f6194173e10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.keruyun.kic.commons.enums; | |
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.databind.*; | |
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; | |
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; | |
import java.io.IOException; | |
public class CodeTextEnumDeserializer | |
extends StdScalarDeserializer<CodeTextEnum<?, ?>> implements ContextualDeserializer { | |
public CodeTextEnumDeserializer() { | |
super(CodeTextEnum.class); | |
} | |
public CodeTextEnumDeserializer(JavaType javaType) { | |
super(javaType.getRawClass()); | |
} | |
@Override | |
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) | |
throws JsonMappingException { | |
JavaType type = ctxt.getContextualType() != null ? ctxt.getContextualType() : property.getMember().getType(); | |
return new CodeTextEnumDeserializer(type); | |
} | |
@Override | |
public CodeTextEnum<?, ?> deserialize(JsonParser parser, DeserializationContext ctxt) | |
throws IOException { | |
return fromCode(handledType(), parser.getNumberValue()); | |
} | |
@SuppressWarnings("unchecked") | |
private <E extends Enum<E> & CodeTextEnum<Number, E>> CodeTextEnum<?, ?> fromCode(Class<?> clz, Number value) { | |
return CodeTextEnum.fromCode((Class<E>) clz, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment