Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson constructParametricType is deprecated, but constructParameterizedType doesn't work the same

Tags:

jackson

Here is my code snippet, and the newer "constructParameterizedType" doesn't match my needs (unless I am missing something, which I assume I am). I have a genericized class called Result where T is any basic class that extends my "Inflatable" base class. represents the data records coming back from Salesforce REST API... so here is example of code that is working:

Class c = Class.forName("sfshare.UserRecord" );
JavaType type = mapper.getTypeFactory().constructParametricType(Result.class, c);
Result<T> res = mapper.readValue(rspData, type);

But if I use the newer (non-deprecated) "constructParameterizedType()" method, this same code will not compile because it isn't matching the parameters of constructParameterizedType. But constructParameterizedType isn't in use much yet and there are no examples to use... only the Javadoc - which doesn't make sense for my use-case.

like image 258
glfgeek Avatar asked Feb 13 '15 14:02

glfgeek


1 Answers

If you look at arguments and specifically Javadocs, you will note that there is a new type: 2nd argument is the intended 'target' for parameters. To give an example of meaning is that if you want to construct equivalent of:

ArrayList<String>

what you want to pass as arguments are:

constructParameterizedType(ArrayList.class, List.class, String.class)

or, possibly, Collection.class for second argument. Think of it as the underlying relevant type you are trying to provide parameters for.

The underlying reason for this change is somewhat complicated and has to do with handling of "add-on" interfaces like Iterable<T>: for those cases it is necessary to provide different classes.

But in most end-user use cases you will just need to pass the same class as first and second argument.

like image 94
StaxMan Avatar answered Oct 01 '22 22:10

StaxMan