0

I'm trying to create a custom deserializer for generic lists. Lets say I get a json representation of class B:

public class B{
    List<A> listObject;
}

where A is some other class which I see only at runtime. I'd like to create a deserializer that will be able to infer the type of listObject as list with inner type A and deserialize it as such instead of using the default hashmap deserializer.

I tried using contextual deserializer, similar to what was suggested here and then adding it as a custom deserializer for List

addDeserializer(List.class, new CustomListDeserializer())

But I'm not sure how am I supposed to read the json and create the list in deserialize function (in the Wrapper example above it's pretty simple, you read the value and set it as a value field, but if my 'wrapper' is List, how do I read the values and add them?)

I tried using readValue with CollectionType constructed with constructCollectionType(List.class, valueType) but then I go into an infinite loop, since readValue uses the deserializer from which it was called.

Any ideas?

mich8bsp
  • 325
  • 1
  • 3
  • 9

1 Answers1

0

Thanks for the suggestion. I solved it by parsing the json as an array of inner generic type and then converting to list, as follows:

Class<?> classOfArray = Array.newInstance(valueType.getRawClass(), 0).getClass(); Object[] parsedArray = (Object[]) parser.getCodec().readValue(parser, classOfArray); return Arrays.asList(parsedArray);

mich8bsp
  • 325
  • 1
  • 3
  • 9