Reputation: 7234
I have to migrate the following code from Jackson 1.9 to 2.0
public class ObjectIdDeserializer extends JsonDeserializer<ObjectId> {
@Override
public ObjectId deserialize(JsonParser jp, DeserializationContext context) {
JsonNode oid = jp.readValueAsTree().get("$oid");
return new ObjectId(oid.getTextValue());
}
}
Because the return type of readValueAsTree()
has change from JsonNode
to TreeNode
I'm not able anymore to access to its value. I've tried hasCurrentToken()
, nextToken()
and others strange methods like those without finding a way to access gracefully to the tree nodes. The get
method that I was using rely on a map
, so it does not need any iteration.
Upvotes: 1
Views: 1002
Reputation: 11515
According to ObjectMapper
class from Jackson :
public <T extends TreeNode> T readTree(JsonParser jp) throws IOException, JsonProcessingException
{
/* ... */
JsonNode n = (JsonNode) _readValue(cfg, jp, JSON_NODE_TYPE);
if (n == null) {
n = getNodeFactory().nullNode();
}
@SuppressWarnings("unchecked")
T result = (T) n;
return result;
}
readTree
method is called by readValueAsTree
method, and as JsonNode
implements TreeNode
, just cast the TreeNode
object returned to an JsonNode
object and you'll get the result that you expect !
@Override
public ObjectId deserialize(JsonParser jp, DeserializationContext context) {
JsonNode oid = ((JsonNode)jp.readValueAsTree()).get("$oid");
return new ObjectId(oid.asText());
}
Upvotes: 3