我想用杰克逊实现某种鸭子打字。我已经在这里看到了示例 6 http://programmerbruce.blogspot.de/2011/05/deserialize-json-with-jackson-into.html。但是这个不起作用,如果基类本身不是抽象的(导致无限循环)。所以我的问题是:有没有办法实现某种回调,我可以在其中进行鸭子输入(检查 JSON 字符串中的相关属性),然后返回杰克逊应该用来像往常一样进行反序列化的类型?
这是一些示例代码。它适用于子类ExtendedOptions但不适用于基类Options ...
public class Options {
private int size;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Override
public String toString() {
return "Options";
}
}
public class ExtendedOptions extends Options {
private String feature;
public String getFeature() {
return feature;
}
public void setFeature(String feature) {
this.feature = feature;
}
@Override
public String toString() {
return "ExtendedOptions";
}
}
public class OptionsDeserializer extends
StdDeserializer<Options> {
OptionsDeserializer() {
super(Options.class);
}
@Override
public Options deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException,
JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode root = (ObjectNode) mapper.readTree(jp);
Iterator<Entry<String, JsonNode>> elementsIterator = root.fields();
while (elementsIterator.hasNext()) {
Entry<String, JsonNode> element = elementsIterator.next();
String name = element.getKey();
// has "feature"? => It's an ExtendedOptions object
if ("feature".equals(name)) {
return mapper.treeToValue(root, ExtendedOptions.class);
}
}
// otherwise it's just an Options object
return mapper.treeToValue(root, Options.class);
}
}
public class JacksonTest {
public static void main(String[] args) throws JsonParseException,
JsonMappingException, Exception {
String optionsString = "{ \"size\": 5 }";
String extendedOptionsString = "{ \"size\": 5, \"feature\" : \"theFeature\" }";
OptionsDeserializer deserializer = new OptionsDeserializer();
@SuppressWarnings("deprecation")
SimpleModule module = new SimpleModule(
"PolymorphicDeserializerModule", new Version(1, 0, 0,
null));
module.addDeserializer(Options.class, deserializer);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
// works
ExtendedOptions extendedOptions = (ExtendedOptions) mapper.readValue(extendedOptionsString, Options.class);
System.out.println(extendedOptions);
// results in infinite loop!!!
Options options = mapper.readValue(optionsString, Options.class);
System.out.println(options);
}
}
编辑:
我已经尝试根据 StaxMan 的提示实现 BeanDeserializerModifier。这是 modifyDeserializer 方法:
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
List<BeanPropertyDefinition> properties = beanDesc.findProperties();
for(BeanPropertyDefinition property: properties) {
String name = property.getName();
// has "feature"? => It's an ExtendedOptions object
System.out.println(name);
if("feature".equals(name)) {
System.out.println("It's an extended object!");
// should return special deserializer here...
return deserializer;
}
}
System.out.println("It's not an extended object!");
return deserializer;
}
那不起作用,因为beanDesc 仅包含有关Option类的信息。这意味着您无法获得有关当前 json 流的任何信息,因此您无法决定必须返回哪个 Deserializer。但是我找到了一个可行的解决方案,但它不是 100% 完美的:
@Override
public Options deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException,
JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode root = (ObjectNode) mapper.readTree(jp);
Iterator<Entry<String, JsonNode>> elementsIterator = root.fields();
while (elementsIterator.hasNext()) {
Entry<String, JsonNode> element = elementsIterator.next();
String name = element.getKey();
// has "feature"? => It's an ExtendedOptions object
if ("feature".equals(name)) {
return mapper.treeToValue(root, ExtendedOptions.class);
}
}
// otherwise it's just an Options object
ObjectMapper origMapper = new ObjectMapper();
return origMapper.treeToValue(root, Options.class);
}
在这里,我只是创建了一个 ObjectMapper 的新实例来反序列化基本类型Options。正如我所说,它有效。但:
1)创建一个新的 ObjectMapper 实例可能很昂贵(使用静态属性?)。
2) 此解决方案不适用于嵌套的多态对象。例如,如果 Options 将包含一个本身是多态的属性。
所以这里有另一个问题:有没有办法取消注册反序列化器?如果是这样,我可以用以下内容替换最后两行:
mapper.unregister(this);
Options result = mapper.treeToValue(root, Options.class);
mapper.register(this);
return result;
编辑 2
好吧,你是对的。注销不是一个好的解决方案。我没有想到多线程 ;-) 无论如何,我尝试了这个:
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
return new OptionsDeserializer();
}
但这导致我进入同样的无限循环。