diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index 4610a58e51..260fe1d72f 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -3485,6 +3485,20 @@ public T treeToValue(TreeNode n, JavaType valueType) } } + /** + * Same as {@link #treeToValue(TreeNode, JavaType)} but target type specified + * using fully resolved {@link TypeReference}. + * + * @since 2.16 + */ + public T treeToValue(TreeNode n, TypeReference toValueTypeRef) + throws IllegalArgumentException, + JsonProcessingException + { + JavaType valueType = constructType(toValueTypeRef); + return treeToValue(n, valueType); + } + /** * Method that is reverse of {@link #treeToValue}: it * will convert given Java value (usually bean) into its diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestConversions.java b/src/test/java/com/fasterxml/jackson/databind/node/TestConversions.java index 2f4b4a524f..18b7bb9230 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/TestConversions.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/TestConversions.java @@ -7,6 +7,7 @@ import static org.junit.Assert.*; +import com.fasterxml.jackson.core.type.TypeReference; import org.junit.Assert; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -170,6 +171,17 @@ public void testTreeToValue() throws Exception // ... also JavaType r1 = mapper.treeToValue(root, mapper.constructType(Root.class)); assertEquals(13, r1.leaf.value); + + // ... also TypeReference + r1 = mapper.treeToValue(root, new TypeReference() {}); + assertEquals(13, r1.leaf.value); + + JSON = "[{\"leaf\":{\"value\":13}}, {\"leaf\":{\"value\":12}}]"; + root = mapper.readTree(JSON); + List array = mapper.treeToValue(root, new TypeReference>() {}); + assertEquals(2, array.size()); + assertEquals(13, array.get(0).leaf.value); + assertEquals(12, array.get(1).leaf.value); } // [databind#1208]: should coerce POJOs at least at root level