如何使用 Jackson 的 ContextualDeserializer 作为根值?
我正在尝试为从某个抽象类扩展或实现某个接口的所有类实现一个泛型反序列化程序。在这个例子中,我使用的是 接口 。我需要确定具体类型,以便我可以创建一个实例。StringConvertible
程序员 Bruce 的一篇旧论坛帖子引导我使用 ContextDeserializer,当 StringConvertible 是另一个类中的属性时,它就可以工作了。
但是当我想直接反序列化StringConvertible时,我找不到获取具体类型的方法,因为参数是。显然,根据Jackson JSON用户组的这个问题/答案,这是意料之中的:beanProperty
null
The only case where property should be null is when serializing a "root value", meaning the object instance passed directly to ObjectMapper's (or ObjectWriter's) writeValue() method -- in this case there simply isn't a referring property. But otherwise it should always be passed.
有关这两种情况的示例,请参阅下面的 main 方法:
@JsonDeserialize(using = StringConvertibleDeserializer.class)
public final class SomeStringConvertible implements StringConvertible {
private final String value;
public SomeStringConvertible(final String value) {
this.value = value;
}
@Override
@JsonValue
public String stringValue() {
return value;
}
}
public final class SomeWrapper {
public SomeStringConvertible stringConvertible;
public SomeWrapper() {
}
}
public class StringConvertibleDeserializer extends StdDeserializer<StringConvertible> implements ContextualDeserializer {
private final Class<? extends StringConvertible> targetClass;
StringConvertibleDeserializer() {
super(StringConvertible.class);
this.targetClass = null;
}
StringConvertibleDeserializer(final Class<? extends StringConvertible> targetClass) {
super(StringConvertible.class);
this.targetClass = targetClass;
}
@Override
public JsonDeserializer<?> createContextual(final DeserializationContext deserializationContext, @Nullable final BeanProperty beanProperty)
throws JsonMappingException {
final StringConvertibleDeserializer contextualDeserializer;
// ==== Determine target type =====
final Class<? extends StringConvertible> targetClass;
JavaType type = beanProperty.getType(); // -> beanProperty is null when the StringConvertible type is a root value
targetClass = (Class<? extends StringConvertible>) type.getRawClass();
// ==== Create contextual deserializer =====
contextualDeserializer = new StringConvertibleDeserializer(targetClass);
// ==== Return =====
return contextualDeserializer;
}
@Override
public StringConvertible deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
final StringConvertible value;
// ==== Create instance using the target type =====
if (targetClass.equals(SomeStringConvertible.class))
value = new SomeStringConvertible(jsonParser.getText());
else {
throw new RuntimeException();
}
// ==== Return =====
return value;
}
}
public final class JacksonModule extends SimpleModule {
public JacksonModule() {
super();
addDeserializer(StringConvertible.class, new StringConvertibleDeserializer());
}
}
public final class Main {
public static void main(String[] args) {
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JacksonModule());
final String wrappedValueJSON = "{\"stringConvertible\":\"hello world\"}";
final String rootValueJSON = "\"hello world\"";
try {
mapper.readValue(wrappedValueJSON, SomeWrapper.class); // This works fine
mapper.readValue(rootValueJSON, SomeStringConvertible.class); // This causes a NPE in createContextual(...) because beanProperty is null
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
问题:在存在根值的情况下,如何获取类型具体类型?或者,如果有比这更好的解决方案,你会有什么建议呢?