我在Jackson的自定义解串器中遇到问题。我想访问默认的序列化器以填充要反序列化的对象。填充之后,我将执行一些自定义操作,但首先,我想使用默认的Jackson行为反序列化对象。
这是我目前拥有的代码。
public class UserEventDeserializer extends StdDeserializer<User> { private static final long serialVersionUID = 7923585097068641765L; public UserEventDeserializer() { super(User.class); } @Override @Transactional public User deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec oc = jp.getCodec(); JsonNode node = oc.readTree(jp); User deserializedUser = null; deserializedUser = super.deserialize(jp, ctxt, new User()); // The previous line generates an exception java.lang.UnsupportedOperationException // Because there is no implementation of the deserializer. // I want a way to access the default spring deserializer for my User class. // How can I do that? //Special logic return deserializedUser; } }
我需要的是一种初始化默认反序列化器的方法,以便在启动特殊逻辑之前可以预先填充POJO。
从自定义反序列化器中调用反序列化时,无论我如何构造序列化器类,似乎都是从当前上下文中调用该方法。由于我的POJO中有注释。由于明显的原因,这会导致堆栈溢出异常。
我曾尝试初始化a,BeanDeserializer但是过程极其复杂,而且我还没有找到正确的方法来进行初始化。我也尝试过重载AnnotationIntrospector,但毫无用处,以为这可能会帮助我忽略中的注释DeserializerContext。最终,它表明我可能已经取得了一些成功,JsonDeserializerBuilders尽管这需要我做一些神奇的事情来掌握Spring的应用程序上下文。我将不胜感激任何可以引导我使用更简洁解决方案的方法,例如,如何在不阅读JsonDeserializer注释的情况下构造反序列化上下文。
BeanDeserializer
AnnotationIntrospector
DeserializerContext
JsonDeserializerBuilders
JsonDeserializer
正如StaxMan已经建议的那样,您可以通过编写a BeanDeserializerModifier并通过进行注册来实现SimpleModule。下面的示例应该起作用:
BeanDeserializerModifier
SimpleModule
public class UserEventDeserializer extends StdDeserializer<User> implements ResolvableDeserializer { private static final long serialVersionUID = 7923585097068641765L; private final JsonDeserializer<?> defaultDeserializer; public UserEventDeserializer(JsonDeserializer<?> defaultDeserializer) { super(User.class); this.defaultDeserializer = defaultDeserializer; } @Override public User deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { User deserializedUser = (User) defaultDeserializer.deserialize(jp, ctxt); // Special logic return deserializedUser; } // for some reason you have to implement ResolvableDeserializer when modifying BeanDeserializer // otherwise deserializing throws JsonMappingException?? @Override public void resolve(DeserializationContext ctxt) throws JsonMappingException { ((ResolvableDeserializer) defaultDeserializer).resolve(ctxt); } public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (beanDesc.getBeanClass() == User.class) return new UserEventDeserializer(deserializer); return deserializer; } }); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(module); User user = mapper.readValue(new File("test.json"), User.class); } }