Skip to content
22 changes: 22 additions & 0 deletions src/main/java/tools/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,28 @@ public <T> T readValue(JsonParser p, Class<T> valueType) throws JacksonException
return (T) _readValue(_deserializationContext(p), p, _typeFactory.constructType(valueType));
}

/**
* Factory method for constructing {@link ObjectReader} that will
* read or update instances automatically
*/
public <T> ObjectReader forAutomaticType(T... reified) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong location, should be next to other factory methods; I'll move.

Copy link
Member

@cowtowncoder cowtowncoder Sep 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need name to align with readerFor() somehow

EDIT: chose readerForDetectedType()

Type type = getClassOf(reified);
_assertNotNull("type", type);
return _newReader(deserializationConfig(), _typeFactory.constructType(type), null,
null, _injectableValues);
}

/**
* Utility method to get the class type from a varargs array.
*
* @param <T> the generic type
* @param array the varargs array
* @return the class of the array component
*/
private static <T> Class<T> getClassOf(T[] array) {
return (Class<T>) array.getClass().getComponentType();
}

/**
* Method to deserialize JSON content into a Java type, reference
* to which is passed as argument. Type is passed using so-called
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/tools/jackson/databind/ObjectReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public void testSimpleViaParser() throws Exception
assertTrue(ob instanceof List<?>);
}

@Test
public void testSimpleViaParserForAutomaticType() throws Exception
{
final String JSON = "[1]";
JsonParser p = MAPPER.createParser(JSON);
List<Integer> ob = MAPPER.forAutomaticType()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of (or in addition to), really needs to use POJO type, to ensure type capture works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... which, alas, proves failure. :-(

.readValue(p);
p.close();
assertTrue(ob != null);
assertEquals(1, ob.get(0));
}

@Test
public void testSimpleAltSources() throws Exception
{
Expand Down
Loading