如何从Jackson自定义解串器或转换器中访问@JacksonInject
值?请提供完整的源代码:
我看到了DeserializationContext.findInjectableValue()
,但我不确定如何构建相应的BeanProperty
。
我还看了@JsonDeserialize(converter = ...)
,但没有看到任何方法将注入的值传递给转换器。这支持了吗?
答案 0 :(得分:1)
这是我的(未经测试的)答案:
public class JacksonDeserializer extends StdDeserializer<MyPojo>
implements ContextualDeserializer
{
private static final long serialVersionUID = 1L;
/**
* The JSON property being deserialized (null if root object).
*/
private final BeanProperty property;
JacksonDeserializer()
{
super(MyPojo.class);
this.property = null;
}
/**
* @param property the JSON property being deserialized (null if root object)
*/
JacksonDeserializer(BeanProperty property)
{
super(MyPojo.class);
this.property = property;
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException
{
return new JacksonDeserializer(property);
}
@Override
public MyPojo deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException
{
MyInjectable injectable = (MyInjectable) ctxt.findInjectableValue("myInjectable", property, null);
assert (scope != null);
// The rest of the deserializer goes here
}
}
我希望这会有所帮助。