如何在Jackson中为参数化接口类型执行常规自定义反序列化器

时间:2014-12-07 21:52:33

标签: java json generics jackson json-deserialization

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

界面:

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;

1 个答案:

答案 0 :(得分:1)

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

我假设您提到的某些外部位确实存在,例如EnumerationFactory。用&#34;枚举&#34;整个事情。包裹枚举使我困惑,但这似乎跟随你放下的东西:

@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);
    }
}

杰克逊现有的Enum反序列化似乎并不支持允许每个枚举实例拥有两个可能的字符串值,这似乎就是你需要的。 TBH似乎你可以转储封闭的枚举类,只需更简单地编写反序列化器:

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;
    }
}