Typefactory class at jackson has many deprecated methods inside it. I am using it like that:
public List<T> getX(Class<T> clz) {
ObjectMapper mapper = new ObjectMapper();
try {
String jsonData = mapper.writeValueAsString(data);
a = mapper.readValue(jsonData, TypeFactory.collectionType(List.class, clz));
} catch (Exception e) {
System.out.println(e.getMessage());
}
return a;// a is a global variable
}
and it warns me that collectionType
is deprecated. What to use instead of it?
ObjectMapper. enable is deprecated since 2.7.
Class used for creating concrete JavaType instances, given various inputs. Instances of this class are accessible using ObjectMapper as well as many objects it constructs (like DeserializationConfig and SerializationConfig )), but usually those objects also expose convenience methods ( constructType ).
TypeFactory itself is NOT deprecated, just static methods; instance methods like "constructType" are available. So question is rather where do you get a TypeFactory instance to use.
Most often you get an instance from ObjectMapper
using its getTypeFactory
method; or from SerializationConfig
/ DeserializationConfig
. They carry an instance around.
If none of these works, there is TypeFactory.instance
as well that you can use.
The reason for deprecation is that some extension modules will want to replace the standard factory: specifically, Scala module needs to extend type handling with Scala Collection (etc) types -- these will not work by default, since Scala does not extend java.util versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With