Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson TypeFactory Static Methods Deprecated What to Use?

Tags:

java

jackson

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?

like image 898
kamaci Avatar asked Nov 15 '11 08:11

kamaci


People also ask

Is ObjectMapper deprecated?

ObjectMapper. enable is deprecated since 2.7.

What is Typefactory in Java?

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 ).


1 Answers

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.

like image 169
StaxMan Avatar answered Sep 30 '22 14:09

StaxMan