Skip to content

Commit a512148

Browse files
robinrooscowtowncoder
authored andcommitted
Issue #2636 - provide overloadings of ObjectReader.readValue() taking Class valueType
1 parent 4c248dd commit a512148

File tree

3 files changed

+213
-35
lines changed

3 files changed

+213
-35
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectReader.java

+168-12
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,8 @@ public void writeTree(JsonGenerator g, TreeNode rootNode) {
14191419
* using configuration of this reader.
14201420
* Value return is either newly constructed, or root value that
14211421
* was specified with {@link #withValueToUpdate(Object)}.
1422+
*
1423+
* @param src Source to read content from
14221424
*/
14231425
@SuppressWarnings("unchecked")
14241426
public <T> T readValue(InputStream src) throws IOException
@@ -1429,11 +1431,28 @@ public <T> T readValue(InputStream src) throws IOException
14291431
return (T) _bindAndClose(_considerFilter(createParser(src), false));
14301432
}
14311433

1434+
/**
1435+
* Same as {@link #readValue(InputStream)} except that target value type
1436+
* overridden as {@code valueType}
1437+
*
1438+
* @param src Source to read content from
1439+
* @param valueType Target type to bind content to
1440+
*
1441+
* @since 2.11
1442+
*/
1443+
@SuppressWarnings("unchecked")
1444+
public <T> T readValue(InputStream src, Class<T> valueType) throws IOException
1445+
{
1446+
return (T) forType(valueType).readValue(src);
1447+
}
1448+
14321449
/**
14331450
* Method that binds content read from given input source,
14341451
* using configuration of this reader.
14351452
* Value return is either newly constructed, or root value that
14361453
* was specified with {@link #withValueToUpdate(Object)}.
1454+
*
1455+
* @param src Source to read content from
14371456
*/
14381457
@SuppressWarnings("unchecked")
14391458
public <T> T readValue(Reader src) throws IOException
@@ -1444,11 +1463,28 @@ public <T> T readValue(Reader src) throws IOException
14441463
return (T) _bindAndClose(_considerFilter(createParser(src), false));
14451464
}
14461465

1466+
/**
1467+
* Same as {@link #readValue(Reader)} except that target value type
1468+
* overridden as {@code valueType}
1469+
*
1470+
* @param src Source to read content from
1471+
* @param valueType Target type to bind content to
1472+
*
1473+
* @since 2.11
1474+
*/
1475+
@SuppressWarnings("unchecked")
1476+
public <T> T readValue(Reader src, Class<T> valueType) throws IOException
1477+
{
1478+
return (T) forType(valueType).readValue(src);
1479+
}
1480+
14471481
/**
14481482
* Method that binds content read from given JSON string,
14491483
* using configuration of this reader.
14501484
* Value return is either newly constructed, or root value that
14511485
* was specified with {@link #withValueToUpdate(Object)}.
1486+
*
1487+
* @param src String that contains content to read
14521488
*/
14531489
@SuppressWarnings("unchecked")
14541490
public <T> T readValue(String src) throws JsonProcessingException, JsonMappingException
@@ -1465,37 +1501,98 @@ public <T> T readValue(String src) throws JsonProcessingException, JsonMappingEx
14651501
}
14661502
}
14671503

1504+
/**
1505+
* Same as {@link #readValue(String)} except that target value type
1506+
* overridden as {@code valueType}
1507+
*
1508+
* @param src String that contains content to read
1509+
* @param valueType Target type to bind content to
1510+
*
1511+
* @since 2.11
1512+
*/
1513+
@SuppressWarnings("unchecked")
1514+
public <T> T readValue(String src, Class<T> valueType) throws IOException
1515+
{
1516+
return (T) forType(valueType).readValue(src);
1517+
}
1518+
14681519
/**
14691520
* Method that binds content read from given byte array,
14701521
* using configuration of this reader.
14711522
* Value return is either newly constructed, or root value that
14721523
* was specified with {@link #withValueToUpdate(Object)}.
1524+
*
1525+
* @param content Byte array that contains encoded content to read
14731526
*/
14741527
@SuppressWarnings("unchecked")
1475-
public <T> T readValue(byte[] src) throws IOException
1528+
public <T> T readValue(byte[] content) throws IOException
14761529
{
14771530
if (_dataFormatReaders != null) {
1478-
return (T) _detectBindAndClose(src, 0, src.length);
1531+
return (T) _detectBindAndClose(content, 0, content.length);
14791532
}
1480-
return (T) _bindAndClose(_considerFilter(createParser(src), false));
1533+
return (T) _bindAndClose(_considerFilter(createParser(content), false));
1534+
}
1535+
1536+
/**
1537+
* Same as {@link #readValue(byte[])} except that target value type
1538+
* overridden as {@code valueType}
1539+
*
1540+
* @param content Byte array that contains encoded content to read
1541+
* @param valueType Target type to bind content to
1542+
*
1543+
* @since 2.11
1544+
*/
1545+
@SuppressWarnings("unchecked")
1546+
public <T> T readValue(byte[] content, Class<T> valueType) throws IOException
1547+
{
1548+
return (T) forType(valueType).readValue(content);
14811549
}
14821550

14831551
/**
14841552
* Method that binds content read from given byte array,
14851553
* using configuration of this reader.
14861554
* Value return is either newly constructed, or root value that
14871555
* was specified with {@link #withValueToUpdate(Object)}.
1556+
*
1557+
* @param buffer Byte array that contains encoded content to read
1558+
* @param offset Offset of the first content byte in {@code buffer}
1559+
* @param length Length of content in {@code buffer}, in bytes
14881560
*/
14891561
@SuppressWarnings("unchecked")
1490-
public <T> T readValue(byte[] src, int offset, int length) throws IOException
1562+
public <T> T readValue(byte[] buffer, int offset, int length) throws IOException
14911563
{
14921564
if (_dataFormatReaders != null) {
1493-
return (T) _detectBindAndClose(src, offset, length);
1565+
return (T) _detectBindAndClose(buffer, offset, length);
14941566
}
1495-
return (T) _bindAndClose(_considerFilter(createParser(src, offset, length),
1567+
return (T) _bindAndClose(_considerFilter(createParser(buffer, offset, length),
14961568
false));
14971569
}
1498-
1570+
1571+
/**
1572+
* Same as {@link #readValue(byte[],int,int)} except that target value type
1573+
* overridden as {@code valueType}
1574+
*
1575+
* @param buffer Byte array that contains encoded content to read
1576+
* @param offset Offset of the first content byte in {@code buffer}
1577+
* @param length Length of content in {@code buffer}, in bytes
1578+
* @param valueType Target type to bind content to
1579+
*
1580+
* @since 2.11
1581+
*/
1582+
@SuppressWarnings("unchecked")
1583+
public <T> T readValue(byte[] buffer, int offset, int length, Class<T> valueType) throws IOException
1584+
{
1585+
return (T) forType(valueType).readValue(buffer, offset, length);
1586+
}
1587+
1588+
/**
1589+
* Method that binds content read from given {@link File}
1590+
* using configuration of this reader.
1591+
* Value return is either newly constructed, or root value that
1592+
* was specified with {@link #withValueToUpdate(Object)}.
1593+
*
1594+
* @param src File that contains content to read
1595+
*/
14991596
@SuppressWarnings("unchecked")
15001597
public <T> T readValue(File src) throws IOException
15011598
{
@@ -1506,6 +1603,21 @@ public <T> T readValue(File src) throws IOException
15061603
return (T) _bindAndClose(_considerFilter(createParser(src), false));
15071604
}
15081605

1606+
/**
1607+
* Same as {@link #readValue(File)} except that target value type
1608+
* overridden as {@code valueType}
1609+
*
1610+
* @param src File that contains content to read
1611+
* @param valueType Target type to bind content to
1612+
*
1613+
* @since 2.11
1614+
*/
1615+
@SuppressWarnings("unchecked")
1616+
public <T> T readValue(File src, Class<T> valueType) throws IOException
1617+
{
1618+
return (T) forType(valueType).readValue(src);
1619+
}
1620+
15091621
/**
15101622
* Method that binds content read from given input source,
15111623
* using configuration of this reader.
@@ -1528,26 +1640,55 @@ public <T> T readValue(URL src) throws IOException
15281640
return (T) _bindAndClose(_considerFilter(createParser(src), false));
15291641
}
15301642

1643+
/**
1644+
* Same as {@link #readValue(URL)} except that target value type
1645+
* overridden as {@code valueType}
1646+
*
1647+
* @param src URL pointing to resource that contains content to read
1648+
* @param valueType Target type to bind content to
1649+
*
1650+
* @since 2.11
1651+
*/
1652+
@SuppressWarnings("unchecked")
1653+
public <T> T readValue(URL src, Class<T> valueType) throws IOException
1654+
{
1655+
return (T) forType(valueType).readValue(src);
1656+
}
1657+
15311658
/**
15321659
* Convenience method for converting results from given JSON tree into given
15331660
* value type. Basically short-cut for:
15341661
*<pre>
15351662
* objectReader.readValue(src.traverse())
15361663
*</pre>
1664+
*
1665+
* @param content Tree that contains content to convert
15371666
*/
15381667
@SuppressWarnings({ "unchecked" })
1539-
public <T> T readValue(JsonNode src) throws IOException
1668+
public <T> T readValue(JsonNode content) throws IOException
15401669
{
1541-
_assertNotNull("src", src);
1670+
_assertNotNull("content", content);
15421671
if (_dataFormatReaders != null) {
1543-
_reportUndetectableSource(src);
1672+
_reportUndetectableSource(content);
15441673
}
1545-
return (T) _bindAndClose(_considerFilter(treeAsTokens(src), false));
1674+
return (T) _bindAndClose(_considerFilter(treeAsTokens(content), false));
15461675
}
15471676

15481677
/**
1549-
* @since 2.8
1678+
* Same as {@link #readValue(JsonNode)} except that target value type
1679+
* overridden as {@code valueType}
1680+
*
1681+
* @param content Tree that contains content to convert
1682+
* @param valueType Target type to convert content to
1683+
*
1684+
* @since 2.11
15501685
*/
1686+
@SuppressWarnings({ "unchecked" })
1687+
public <T> T readValue(JsonNode content, Class<T> valueType) throws IOException
1688+
{
1689+
return (T) forType(valueType).readValue(content);
1690+
}
1691+
15511692
@SuppressWarnings("unchecked")
15521693
public <T> T readValue(DataInput src) throws IOException
15531694
{
@@ -1557,6 +1698,21 @@ public <T> T readValue(DataInput src) throws IOException
15571698
return (T) _bindAndClose(_considerFilter(createParser(src), false));
15581699
}
15591700

1701+
/**
1702+
* Same as {@link #readValue(DataInput)} except that target value type
1703+
* overridden as {@code valueType}
1704+
*
1705+
* @param content DataInput that contains content to read
1706+
* @param valueType Target type to bind content to
1707+
*
1708+
* @since 2.11
1709+
*/
1710+
@SuppressWarnings("unchecked")
1711+
public <T> T readValue(DataInput content, Class<T> valueType) throws IOException
1712+
{
1713+
return (T) forType(valueType).readValue(content);
1714+
}
1715+
15601716
/*
15611717
/**********************************************************
15621718
/* Deserialization methods; JsonNode ("tree")

src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,24 @@ public static class Pojo1637 {
311311
public Set<String> set2;
312312
}
313313

314+
// [databind#2636]
315+
public void testCanPassResultToOverloadedMethod() throws Exception {
316+
final String source = "{\"foo\":{\"bar\":{\"caller\":{\"name\":{\"value\":1234}}}}}";
317+
318+
ObjectReader reader = MAPPER.readerFor(POJO.class).at("/foo/bar/caller");
319+
320+
process(reader.readValue(source, POJO.class));
321+
}
322+
323+
void process(POJO pojo) {
324+
// do nothing - just used to show that the compiler can choose the correct method overloading to invoke
325+
}
326+
327+
void process(String pojo) {
328+
// do nothing - just used to show that the compiler can choose the correct method overloading to invoke
329+
throw new Error();
330+
}
331+
314332
/*
315333
/**********************************************************
316334
/* Test methods, ObjectCodec

0 commit comments

Comments
 (0)