7

Using Jersey I'm defining a service like:

@Path("/studentIds")
public void writeList(JsonArray<Long> studentIds){
 //iterate over studentIds and save them
}

Where JsonArray is:

public class JsonArray<T> extends ArrayList<T> {  
    public JsonArray(String v) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
        TypeReference<ArrayList<T>> typeRef = new TypeReference<ArrayList<T>>() {};
        ArrayList<T> list = objectMapper.readValue(v, typeRef);
        for (T x : list) {
            this.add((T) x);
        }
    }
}

This works just fine, but when I do something more complicated:

@Path("/studentIds")
public void writeList(JsonArray<TypeIdentifier> studentIds){
 //iterate over studentIds and save them by type
}

Where the Bean is a simple POJO such as

public class TypeIdentifier {
    private String type;
    private Long id;
//getters/setters 
}

The whole thing breaks horribly. It converts everything to LinkedHashMap instead of the actual object. I can get it to work if I manually create a class like:

public class JsonArrayTypeIdentifier extends ArrayList<TypeIdentifier> { 
    public JsonArrayTypeIdentifier(String v) throws IOException  {
        ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
        TypeReference<ArrayList<TypeIdentifier>> typeRef = new TypeReference<ArrayList<TypeIdentifier>>(){};
        ArrayList<TypeIdentifier> list = objectMapper.readValue(v, typeRef);
        for(TypeIdentifier x : list){
            this.add((TypeIdentifier) x);
        }
    }
 }

But I'm trying to keep this nice and generic without adding extra classes all over. Any leads on why this is happening with the generic version only?

Will Shaver
  • 12,471
  • 5
  • 49
  • 64
  • See possible duplicate with an accepted answer: http://stackoverflow.com/questions/6062011/jackson-is-not-deserialising-a-generic-list-that-it-has-serialised – AlikElzin-kilaka Apr 14 '13 at 15:23

1 Answers1

1

First of all, it works with Longs because that is sort of native type, and as such default binding for JSON integral numbers.

But as to why generic type information is not properly passed: this is most likely due to problems with the way JAX-RS API passes type to MessageBodyReaders and MessageBodyWriters -- passing java.lang.reflect.Type is not (unfortunately!) enough to pass actual generic declarations (for more info on this, read this blog entry).

One easy work-around is to create helper types like:

class MyTypeIdentifierArray extends JsonArray<TypeIdentifier> { }

and use that type -- things will "just work", since super-type generic information is always retained.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Your work around doesn't work. :( Still gives me LinkedHashMap as the parse results. Back to my long workaround. Sigh. – Will Shaver Jan 18 '12 at 17:38
  • It definitely should work -- can you show me exactly how are you trying to parse them? LinkedHashMap is only returned when type information is missing (or if `Object.class` is defined as type). Which Jackson version is this with? – StaxMan Jan 18 '12 at 19:50
  • The JsonArray class *is* exactly how I'm trying to parse them. What is returned from this class is a list of LinkedHashMap. If I use the JsonArrayTypeIdentifier then it returns a list of TypeIdentifier. – Will Shaver Jan 19 '12 at 21:16
  • Did you read my reply completely? I suggested you should use sub-class thereof (`MyTypeIdentifierArray`) and NOT `JsonArray` directly -- problem may come from the fact that Jersey does not pass all necessary type information. You can verify this by using ObjectMapper first directly; this should indeed work. – StaxMan Jan 19 '12 at 21:54
  • Yeah. Nothing Jackson can do: JAX-RS spec has a flaw in generic type passing. – StaxMan Feb 05 '14 at 19:35