I'm new to marshalling and unmarshalling. I want to marshal and unmarshal a generic List using DynamoDBMashaller
I have following class with me
public class ListObjectConverter implements DynamoDBMarshaller<List<?>>
{
private static final Gson GSON = new Gson();
@Override
public String marshall(List<?> value) {
Type listOfObjectType = new TypeToken<List<?>>(){}.getType();
return GSON.toJson(value, listOfObjectType);
}
@Override
public List<?> unmarshall(Class<List<?>> clazz, String value) {
Type listOfObjectType = new TypeToken<List<?>>(){}.getType();
return GSON.fromJson(value, listOfObjectType);
}
}
Marshalling is working fine
List<Source> fsl = new ArrayList<>();
//add few element in fsl
ListObjectConverter loc = new ListObjectConverter();
String marshalled = loc.marshall(fsl);
This works perfectly.
But I'm a little confused how do I call unmarshaller in this case. Following code is not compiling. It says Found java.lang.Class<java.util.List> : required java.lang.Class<java.util.List<?>>
List<Source> fsl1 = loc.unmarshall(List.class , marshalled);
I think type eraser is coming in picture.
Is it possible to do it? If yes, how do I do that? Any alternative you could suggest?
Thanks in advance
You need cast
List<FeedSource> lst= new ArrayList<FeedSource>();
lst= loc.unmarshall((Class<List<FeedSource>>) lst.getClass(), marshalled);
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