What I am trying to achieve is convert a JsonNode
to a POJO (i.e. deserialize it) inside a custom deserializer.
Most other answers, like this one here, suggest using ObjectMapper
, but the deserialize
method specifically does not have the object mapper, so the solutions do not work. This question is therefore not a duplicate.
Here is my custom deserializer:
class AccountDeserializer extends StdDeserializer<Input> {
public AccountDeserializer() {
this(null);
}
public AccountDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Account deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode root = jp.getCodec().readTree(jp);
User user = root.get("user").????;
// other statements
Account acc = new Account(user);
return acc;
}
}
(User
is a simple class)