gpt4 book ai didi

java - 如何在 Jackson 中为参数化接口(interface)类型执行通用自定义反序列化器

转载 作者:行者123 更新时间:2023-12-01 12:09:22 43 4
gpt4 key购买 nike

我有一个包含许多实现类的接口(interface),我想编写一个通用反序列化器,而不是为每个实现编写一个反序列化器:

界面:

public interface IEnumerable<E extends Enum<E>> {
public String getName();
}

使用反射对 Enumerable 进行反向查找的抽象类:

public abstract class AbstractEnumerable<E extends Enum<E> & IEnumerable<E>> {
private static final XLogger log = XLoggerFactory.getXLogger(AbstractEnumerable.class.getCanonicalName());

private final TypeToken<AbstractEnumerable<E>> typeToken = new TypeToken<AbstractEnumerable<E>>(getClass()) { };

public final E getByName(String name) {
TypeToken<?> genericParam = typeToken.resolveType(AbstractEnumerable.class.getTypeParameters()[0]);
log.debug("Runtime class of generic IEnumerable parameter: {}", genericParam.getType().getTypeName());

try {
log.trace("Getting a Class object for {}", genericParam.getType().getTypeName());
Class<E> clazz = (Class<E>)Class.forName(genericParam.getType().getTypeName());

log.trace("Iterating over the enum constants in {}", genericParam.getType().getTypeName());
for(Object o : Arrays.asList(clazz.getEnumConstants())) {
E val = clazz.cast(o);
if(val.getName().equals(name) || val.name().equals(name)) {
return val;
}
}
} catch(ClassNotFoundException e) {
log.error("Unable to find the class definition for {}", genericParam.getType().getTypeName());
log.catching(e);
}

return null;
}
}

实现:

public class DnsRecordTypeEnumeration extends AbstractEnumerable<DnsRecordTypeEnumeration.DnsRecordType> {
public static enum DnsRecordType implements IEnumerable<DnsRecordType> {
DNS_TYPE_A("A"),
DNS_TYPE_AAAA("AAAA"),
DNS_TYPE_CNAME("CNAME");

private final String localizedName;

private DnsRecordType(final String localizedName) {
this.localizedName = localizedName;
}

@Override
public final String getName() {
return localizedName;
}
}
}

是否可以为所有 Enumerable 实现提供通用的自定义反序列化器?我需要访问封闭类来进行反向查找。

我尝试过这个:

public abstract class AbstractJacksonJsonDeserializer<T> extends JsonDeserializer<T> {
private static final XLogger log = XLoggerFactory.getXLogger(AbstractJacksonJsonDeserializer.class
.getCanonicalName());

private final TypeToken<AbstractJacksonJsonDeserializer<T>> typeToken =
new TypeToken<AbstractJacksonJsonDeserializer<T>>(getClass()) { };

protected Class<T> getTypeClass() {
Class<T> clazz = (Class<T>)RuntimeClassFactory.getInstance().create(typeToken, AbstractJacksonJsonDeserializer.class, 0);
log.debug("Runtime class of object to be deserialized: {}", clazz.getCanonicalName());
return (Class<T>)RuntimeClassFactory.getInstance().create(typeToken, AbstractJacksonJsonDeserializer.class, 0);
}

public class EnumerableJacksonJsonDeserializer<E extends Enum<E> & IEnumerable<E>> extends AbstractJacksonJsonDeserializer<E> {

@Override
public E deserialize(final JsonParser parser, final DeserializationContext
context) throws JsonProcessingException, IOException {
JsonNode node = parser.getCodec().readTree(parser);
String name = node.textValue();
return getEnumeration().getByName(name);
}

protected <T extends AbstractEnumerable<E>> T getEnumeration() {
Class<E> enumerableClass = getTypeClass();
Class<T> enumerationClass = (Class<T>) enumerableClass.getEnclosingClass();
return EnumerationFactory.getInstance().create(enumerationClass);
}
}

问题是我无法像这样用上面的类注释字段,因为我的自定义反序列化器采用类型参数。这会导致编译错误:

@JsonDeserialize(using = EnumerableJacksonJsonDeserializer.class)
private DnsRecordType recordType;

最佳答案

您可以编写一个模块,根据反序列化器遇到的上下文提供反序列化器:这允许您在遇到 IEnumerable 子类时获取具体类型。

我假设您引用的一些外部位确实存在,例如 EnumerationFactory。说实话,用“枚举”包装枚举的整个事情让我很困惑,但这似乎遵循您所写的内容:

@Test
public void calls_general_deserializer_for_parameterized_interface() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new SimpleModule() {
@Override
public void setupModule(SetupContext context) {
context.addDeserializers(new Deserializers.Base() {
@Override
public JsonDeserializer<?> findEnumDeserializer(Class<?> type,
DeserializationConfig config,
BeanDescription beanDesc) throws JsonMappingException {
if (IEnumerable.class.isAssignableFrom(type)) return new EnumerableDeserializer(type);
return null;
}
});
super.setupModule(context);
}
});
TestData data = mapper.readerFor(TestData.class).with(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
.with(JsonParser.Feature.ALLOW_SINGLE_QUOTES).readValue("{ recordType: 'AAAA' }");
assertThat(data.recordType, equalTo(DnsRecordType.DNS_TYPE_AAAA));
}

public static final class EnumerableDeserializer<E extends Enum<E> & IEnumerable<E>> extends
StdScalarDeserializer<E> {
private final Class<? extends AbstractEnumerable<E>> enumerationClass;
private final Class<E> enumerableClass;

public EnumerableDeserializer(Class<E> enumerableClass) {
super(enumerableClass);
this.enumerableClass = enumerableClass;
this.enumerationClass = (Class<? extends AbstractEnumerable<E>>) enumerableClass.getEnclosingClass();
}

@Override
public E deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
AbstractEnumerable<E> enumerable = getEnumeration();
return enumerable.getByName(p.getText());
}

private AbstractEnumerable<E> getEnumeration() {
return EnumerationFactory.getInstance().create(enumerationClass);
}
}

Jackson 现有的枚举反序列化似乎不支持允许每个枚举实例具有两个可能的字符串值,这似乎是您在这里需要的。说实话,您似乎可以转储封闭的枚举类,然后更简单地编写反序列化器:

public static final class DirectEnumerableDeserializer<E extends Enum<E> & IEnumerable<E>> extends
StdScalarDeserializer<E> {
private final Class<E> enumerableClass;
private final ImmutableList<E> values;
private final ImmutableMap<String, E> names;

public DirectEnumerableDeserializer(Class<E> enumerableClass) {
super(enumerableClass);
this.enumerableClass = enumerableClass;
this.values = ImmutableList.copyOf(enumerableClass.getEnumConstants());
ImmutableMap.Builder<String, E> names = ImmutableMap.builder();
for (E value : values) {
names.put(value.name(), value);
}
this.names = names.build();
}

@Override
public E deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
String key = p.getText();
E value = names.get(key);
if (value == null) {
value = findByLocalisedName(key);
if (value == null) throw ctxt.weirdStringException(key, enumerableClass, "Unrecognised name");
}
return value;
}

private E findByLocalisedName(String key) {
for (E value : values) {
if (value.getName().equals(key)) return value;
}
return null;
}
}

关于java - 如何在 Jackson 中为参数化接口(interface)类型执行通用自定义反序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348283/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com