Skip to content

Commit f55f8e8

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

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

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

+73-1
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,14 @@ public <T> T readValue(InputStream in) throws IOException
11481148
_considerFilter(_parserFactory.createParser(ctxt, in), false));
11491149
}
11501150

1151+
/**
1152+
* Overloading to alert compiler as to the valueType
1153+
*/
1154+
public <T> T readValue(InputStream src, Class<T> valueType) throws IOException
1155+
{
1156+
return (T) forType(valueType).readValue(src);
1157+
}
1158+
11511159
/**
11521160
* Method that binds content read from given input source,
11531161
* using configuration of this reader.
@@ -1163,6 +1171,14 @@ public <T> T readValue(Reader r) throws IOException
11631171
_considerFilter(_parserFactory.createParser(ctxt, r), false));
11641172
}
11651173

1174+
/**
1175+
* Overloading to alert compiler as to the valueType
1176+
*/
1177+
public <T> T readValue(Reader src, Class<T> valueType) throws IOException
1178+
{
1179+
return (T) forType(valueType).readValue(src);
1180+
}
1181+
11661182
/**
11671183
* Method that binds content read from given JSON string,
11681184
* using configuration of this reader.
@@ -1178,6 +1194,14 @@ public <T> T readValue(String content) throws IOException
11781194
_considerFilter(_parserFactory.createParser(ctxt, content), false));
11791195
}
11801196

1197+
/**
1198+
* Overloading to alert compiler as to the valueType
1199+
*/
1200+
public <T> T readValue(String src, Class<T> valueType) throws IOException
1201+
{
1202+
return (T) forType(valueType).readValue(src);
1203+
}
1204+
11811205
/**
11821206
* Method that binds content read from given byte array,
11831207
* using configuration of this reader.
@@ -1193,6 +1217,14 @@ public <T> T readValue(byte[] content) throws IOException
11931217
_considerFilter(_parserFactory.createParser(ctxt, content), false));
11941218
}
11951219

1220+
/**
1221+
* Overloading to alert compiler as to the valueType
1222+
*/
1223+
public <T> T readValue(byte[] content, Class<T> valueType) throws IOException
1224+
{
1225+
return (T) forType(valueType).readValue(content);
1226+
}
1227+
11961228
/**
11971229
* Method that binds content read from given byte array,
11981230
* using configuration of this reader.
@@ -1207,7 +1239,15 @@ public <T> T readValue(byte[] content, int offset, int length) throws IOExceptio
12071239
return (T) _bindAndClose(ctxt,
12081240
_considerFilter(_parserFactory.createParser(ctxt, content, offset, length), false));
12091241
}
1210-
1242+
1243+
/**
1244+
* Overloading to alert compiler as to the valueType
1245+
*/
1246+
public <T> T readValue(byte[] content, int offset, int length, Class<T> valueType) throws IOException
1247+
{
1248+
return (T) forType(valueType).readValue(content, offset, length);
1249+
}
1250+
12111251
@SuppressWarnings("unchecked")
12121252
public <T> T readValue(File f) throws IOException
12131253
{
@@ -1217,6 +1257,14 @@ public <T> T readValue(File f) throws IOException
12171257
_considerFilter(_parserFactory.createParser(ctxt, f), false));
12181258
}
12191259

1260+
/**
1261+
* Overloading to alert compiler as to the valueType
1262+
*/
1263+
public <T> T readValue(File src, Class<T> valueType) throws IOException
1264+
{
1265+
return (T) forType(valueType).readValue(src);
1266+
}
1267+
12201268
/**
12211269
* Method that binds content read from given input source,
12221270
* using configuration of this reader.
@@ -1239,6 +1287,14 @@ public <T> T readValue(URL url) throws IOException
12391287
_considerFilter(_parserFactory.createParser(ctxt, url), false));
12401288
}
12411289

1290+
/**
1291+
* Overloading to alert compiler as to the valueType
1292+
*/
1293+
public <T> T readValue(URL src, Class<T> valueType) throws IOException
1294+
{
1295+
return (T) forType(valueType).readValue(src);
1296+
}
1297+
12421298
/**
12431299
* Convenience method for converting results from given JSON tree into given
12441300
* value type. Basically short-cut for:
@@ -1255,6 +1311,14 @@ public <T> T readValue(JsonNode node) throws IOException
12551311
_considerFilter(treeAsTokens(node, ctxt), false));
12561312
}
12571313

1314+
/**
1315+
* Overloading to alert compiler as to the valueType
1316+
*/
1317+
public <T> T readValue(JsonNode src, Class<T> valueType) throws IOException
1318+
{
1319+
return (T) forType(valueType).readValue(src);
1320+
}
1321+
12581322
@SuppressWarnings("unchecked")
12591323
public <T> T readValue(DataInput input) throws IOException
12601324
{
@@ -1264,6 +1328,14 @@ public <T> T readValue(DataInput input) throws IOException
12641328
_considerFilter(_parserFactory.createParser(ctxt, input), false));
12651329
}
12661330

1331+
/**
1332+
* Overloading to alert compiler as to the valueType
1333+
*/
1334+
public <T> T readValue(DataInput content, Class<T> valueType) throws IOException
1335+
{
1336+
return (T) forType(valueType).readValue(content);
1337+
}
1338+
12671339
/*
12681340
/**********************************************************
12691341
/* Deserialization methods; JsonNode ("tree")

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

+18
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,24 @@ public void testPointerLoading() throws Exception {
198198
assertEquals(1234, pojo.name.get("value"));
199199
}
200200

201+
202+
public void testCanPassResultToOverloadedMethod() throws Exception {
203+
final String source = "{\"foo\":{\"bar\":{\"caller\":{\"name\":{\"value\":1234}}}}}";
204+
205+
ObjectReader reader = MAPPER.readerFor(POJO.class).at("/foo/bar/caller");
206+
207+
process(reader.readValue(source, POJO.class));
208+
}
209+
210+
void process(POJO pojo) {
211+
// do nothing - just used to show that the compiler can choose the correct method overloading to invoke
212+
}
213+
214+
void process(String pojo) {
215+
// do nothing - just used to show that the compiler can choose the correct method overloading to invoke
216+
}
217+
218+
201219
public void testPointerLoadingAsJsonNode() throws Exception {
202220
final String source = "{\"foo\":{\"bar\":{\"caller\":{\"name\":{\"value\":1234}}}}}";
203221

0 commit comments

Comments
 (0)