我试图为我的价值类建立一个自定义反序列化器类。但是ObjectMapper .readValue被多次调用,直到引发StackOverflowException。以下仅是示例:
@JsonDeserialize(using = UserDeserializer.class)
@Getter
public class User {
private String name;
}
public class UserDeserializer extends StdDeserializer<User> {
public UserDeserializer() {
super(User.class);
}
@Override
public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return ctxt.readValue(p, User.class);
}
}
public class UserDeserializerTest {
@Test
public void whenUserIsDeserializedFromJsonThenNameShouldEqualBob() {
String = "{ \"name\" : \"bob\" }";
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
assertThat(user.getName).isEqualTo("bob");
}
}
Maven依赖项:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
引发以下StackOverflowException:
Exception in thread "main" java.lang.StackOverflowError
...
// The following lines are repeated.
at com.fasterxml.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:754)
at com.fasterxml.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:746)
at example.UserDeserializer.deserialize(UserDeserializer.java:25)
at example.UserDeserializer.deserialize(UserDeserializer.java:15)
Process finished with exit code 1
我尝试通过模块注册反序列化器并删除@JsonAnnotation,但仍然抛出相同的异常:
SimpleModule module = new SimpleModule("User Simple Module");
module.addDeserializer(User.class, new UserDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.addModule(module);
mapper.readValue(json, User.class);
然后,我尝试在UserDeserializer中创建一个新的ObjectMapper,并从User类和测试通过中删除@JsonDeserialize。
@Override
public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return new ObjectMapper().readValue(p, User.class);
}
在执行上述工作的同时,我想使用@JsonDeserialize注释在每个类的基础上注册反序列化器,而不是注册全局自定义对象映射器。我有这样的理由是有原因的。
我尝试了其他代码组合,例如:
// Add the 'as' keyword to the annotation
@JsonDeserialise(using = UserDeserialize.class, as = User.class)
// Locally assigning a mapper from the JsonParser inside the UserDeserializer class
ObjectMapper mapper = (ObjectMapper) p.getCodec();
mapper.readValue(p, User.class);
// Constructing a new JSON Parser inside the deserializer
ObjectMapper mapper = (ObjectMapper) p.getCodec();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(p.getText());
return (User) parser.readValueAs(handledType());
// Reading the Json into a Tree
ObjectMapper mapper = (ObjectMapper) p.getCodec();
ObjectNode root = (ObjectNode) p.readValueAsTree();
return mapper.readValue(root.traverse(), User.class);
但是将JSON读取到Tree中会抛出:
Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: No ObjectCodec defined for parser, needed for deserialization
at example.User.main(User.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.IllegalStateException: No ObjectCodec defined for parser, needed for deserialization
at com.fasterxml.jackson.core.JsonParser._codec(JsonParser.java:1565)
at com.fasterxml.jackson.core.JsonParser.readValueAsTree(JsonParser.java:1559)
at example.UserDeserializer.deserialize(UserDeserializer.java:37)
at example.UserDeserializer.deserialize(UserDeserializer.java:17)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3674)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1996)
at example.UserDeserializer.deserialize(UserDeserializer.java:38)
at example.UserDeserializer.deserialize(UserDeserializer.java:17)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714)
at example.User.main(User.java:36)
... 5 more
我还尝试在UserDeserializer中实现ContextualDeserializer,但仍然获得与以前相同的StackOverflowException:
private UserDeserializer(Class<?> vc) {
super(vc);
}
public JsonDeserializer<User> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
return new UserDeserializer(ctxt.getContextualType().getRawClass());
}
@Override
public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return (User) new ObjectMapper().readValue(p, handledType());
}
我希望这只是PEBKAC错误的坏情况,并且可以在您的帮助/指导下找到解决方案。
亲切的问候
最佳答案
我同意KDM,请尝试以下解串器:
public class UserDeserializer extends JsonDeserializer<User> {
@Override
public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
User user = new User();
user.setName(node.get("name").asText());
return user;
}
}
关于java - 多次调用ObjectMapper readValue,直到引发StackOverflowException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32372752/