gpt4 book ai didi

java - ObjectMapper readValue 多次调用,直到抛出 StackOverflowException

转载 作者:行者123 更新时间:2023-12-01 11:11:01 25 4
gpt4 key购买 nike

我正在尝试为我的值类构建自定义反序列化器类。但是 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 读入树中会抛出:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com