【发布时间】:2014-11-12 04:28:20
【问题描述】:
我有一个现有的类层次结构,如下所示:
public interface Service {
String getId();
String getName();
}
public class FooTask extends AbstractTask {
private final static ObjectMapper JSON_MAPPER = new ObjectMapper();
static {
JSON_MAPPER.registerModule(new SimpleModule().addDeserializer(Result.class, new ResultDeserializer());
}
public FooTask(Service service) {
super(service);
}
@Override public Result call() throws Exception {
InputStream json = <... execute some code to retrieve JSON ...>
Result result = JSON_MAPPER.readValue(json, Result.class);
}
private static class ResultDeserializer {
@Override public Result deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
//
// Need to access service#getId() down here... but we're in a static nested class
// and I don't know how to access it. Is there some way to pass that info via the DeserializationContext?
//
<... Deserialization logic ...>
}
}
}
我需要在反序列化时将一些信息传递给反序列化器,但我找不到在反序列化时将一些上下文信息传递给反序列化器的方法。这可能吗?如果是这样,怎么做?我宁愿不必在每次实例化 FooTask 或调用 #call() 方法时都分配一个新的 ObjectMapper。
【问题讨论】: