155

I want to use jackson json library for a generic method as follows:

public MyRequest<T> tester() {
    TypeReference<MyWrapper<T>> typeRef = new TypeReference<MyWrapper<T>>();  
    MyWrapper<T> requestWrapper = (MyWrapper<T>) JsonConverter.fromJson(jsonRequest, typeRef);
    return requestWrapper.getRequest();
}
public class MyWrapper<T> {

    private MyRequest<T> request;

    public MyRequest<T> getRequest() {
        return request;
    }

    public void setRequest(MyRequest<T> request) {
        this.request = request;
    }
}
public class MyRequest<T> {
     private List<T> myobjects;
        
     public void setMyObjects(List<T> ets) {
         this.myobjects = ets;
     }

     @NotNull
     @JsonIgnore
     public T getMyObject() {
         return myobjects.get(0);
     }
}

Now the problem is that when I call getMyObject() which is inside the request object Jackson returns the nested custom object as a LinkedHashMap. Is there any way in which I specify that T object needs to be returned? For example: if I sent object of type Customer then Customer should be returned from that List?

5
  • Please add the implementation of getT() Commented Jul 27, 2011 at 14:47
  • This question is similar to stackoverflow.com/questions/6062011/… but they have suggested specifying type using TypeFactory. However I do not know the type at compile time...
    – techzen
    Commented Jul 27, 2011 at 15:01
  • TypeFactory has methods that do not need static class; createCollectionType and so on.
    – StaxMan
    Commented Jul 27, 2011 at 22:48
  • Please share complete code. I am also facing same problem.
    – AZ_
    Commented Oct 9, 2013 at 3:25
  • 1
    Isn't TypeReference abstract? Commented Feb 14, 2020 at 0:41

3 Answers 3

267

This is a well-known problem with Java type erasure: T is just a type variable, and you must indicate actual class, usually as Class argument. Without such information, best that can be done is to use bounds; and plain T is roughly same as 'T extends Object'. And Jackson will then bind JSON Objects as Maps.

In this case, tester method needs to have access to Class, and you can construct

JavaType type = mapper.getTypeFactory().
  constructCollectionType(List.class, Foo.class)

and then

List<Foo> list = mapper.readValue(new File("input.json"), type);
7
  • 27
    It works : I did the following: JavaType topMost = mapper.getTypeFactory().constructParametricType(MyWrapper.class, ActualClassRuntime.class); and then did the readValue and it finally worked :)
    – techzen
    Commented Jul 28, 2011 at 3:04
  • 1
    @StaxMan would it better to use ClassMate for these kind of things from now?
    – husayt
    Commented Jan 25, 2014 at 13:50
  • 2
    @husayt yes, technically java-classmate lib is superior. But integrating it with Jackson is bit tricky just because Jackson's own type abstraction is integrated part of API. For long term it'd be great to figure out proper way to make Jackson use classmate code, either embedded or via dep.
    – StaxMan
    Commented Jan 27, 2014 at 23:06
  • 1
    @MartinAndersson why would it? JSON is just data, and has neither type (beyond simple JSON type of Lists, Maps, number, string, boolean) nor identity. Any typing there is is interpreted by Java code; and although it could use convention to encode type (and will have to, for polymorphic types), it is not needed if caller can specify type to Map data into.
    – StaxMan
    Commented Sep 30, 2014 at 16:47
  • 2
    I feel like Jackson shouldn't be having to cover off what feel like gaps in generics, but either way, it does it very well. Commented Jan 7, 2018 at 4:36
12

'JavaType' works !! I was trying to unmarshall (deserialize) a List in json String to ArrayList java Objects and was struggling to find a solution since days.
Below is the code that finally gave me solution. Code:

JsonMarshallerUnmarshaller<T> {
    T targetClass;

    public ArrayList<T> unmarshal(String jsonString) {
        ObjectMapper mapper = new ObjectMapper();

        AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
        mapper.getDeserializationConfig()
            .withAnnotationIntrospector(introspector);

        mapper.getSerializationConfig()
            .withAnnotationIntrospector(introspector);
        JavaType type = mapper.getTypeFactory().
            constructCollectionType(
                ArrayList.class, 
                targetclass.getClass());

        try {
            Class c1 = this.targetclass.getClass();
            Class c2 = this.targetclass1.getClass();
            ArrayList<T> temp = (ArrayList<T>) 
                mapper.readValue(jsonString,  type);
            return temp ;
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null ;
    }  
}
6
  • 2
    How to initialize TargetClass ?
    – AZ_
    Commented Oct 9, 2013 at 3:23
  • Please show me a small example. I am passing Class<?> target and then getting target.getClassName().
    – AZ_
    Commented Oct 10, 2013 at 8:08
  • 1
    Add a constructor as follows : JsonMarshallerUnmarshaller<T>{ private Class<T> targetClass ; JsonMarshallerUnmarshaller(Class<T> c){ targetClass = c ; } } Make appropriate changes now to the 'unmarshal' function to use this class instead of doing getClass everywhere. Commented Oct 11, 2013 at 19:02
  • Couple of notes: code can be simplified a lot by noting that all exceptions are subtypes of IOException (need just one catch), and that the default annotation introspector is already JacksonAnnotationIntrospector -- so no need to do anything to ObjectMapper, just construct it and it works.
    – StaxMan
    Commented Nov 4, 2015 at 17:34
  • 1
    So this code I don't even get to compile. Got any live example to paste in instead?
    – Wrench
    Commented Nov 22, 2016 at 23:43
0

I modified rushidesai1's answer to include a working example.

JsonMarshaller.java

import java.io.*;
import java.util.*;

public class JsonMarshaller<T> {
    private static ClassLoader loader = JsonMarshaller.class.getClassLoader();

    public static void main(String[] args) {
        try {
            JsonMarshallerUnmarshaller<Station> marshaller = new JsonMarshallerUnmarshaller<>(Station.class);
            String jsonString = read(loader.getResourceAsStream("data.json"));
            List<Station> stations = marshaller.unmarshal(jsonString);
            stations.forEach(System.out::println);
            System.out.println(marshaller.marshal(stations));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("resource")
    public static String read(InputStream ios) {
        return new Scanner(ios).useDelimiter("\\A").next(); // Read the entire file
    }
}

Output

Station [id=123, title=my title, name=my name]
Station [id=456, title=my title 2, name=my name 2]
[{"id":123,"title":"my title","name":"my name"},{"id":456,"title":"my title 2","name":"my name 2"}]

JsonMarshallerUnmarshaller.java

import java.io.*;
import java.util.List;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;

public class JsonMarshallerUnmarshaller<T> {
    private ObjectMapper mapper;
    private Class<T> targetClass;

    public JsonMarshallerUnmarshaller(Class<T> targetClass) {
        AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();

        mapper = new ObjectMapper();
        mapper.getDeserializationConfig().with(introspector);
        mapper.getSerializationConfig().with(introspector);

        this.targetClass = targetClass;
    }

    public List<T> unmarshal(String jsonString) throws JsonParseException, JsonMappingException, IOException {
        return parseList(jsonString, mapper, targetClass);
    }

    public String marshal(List<T> list) throws JsonProcessingException {
        return mapper.writeValueAsString(list);
    }

    public static <E> List<E> parseList(String str, ObjectMapper mapper, Class<E> clazz)
            throws JsonParseException, JsonMappingException, IOException {
        return mapper.readValue(str, listType(mapper, clazz));
    }

    public static <E> List<E> parseList(InputStream is, ObjectMapper mapper, Class<E> clazz)
            throws JsonParseException, JsonMappingException, IOException {
        return mapper.readValue(is, listType(mapper, clazz));
    }

    public static <E> JavaType listType(ObjectMapper mapper, Class<E> clazz) {
        return mapper.getTypeFactory().constructCollectionType(List.class, clazz);
    }
}

Station.java

public class Station {
    private long id;
    private String title;
    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return String.format("Station [id=%s, title=%s, name=%s]", id, title, name);
    }
}

data.json

[{
  "id": 123,
  "title": "my title",
  "name": "my name"
}, {
  "id": 456,
  "title": "my title 2",
  "name": "my name 2"
}]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.