Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDBMarshaller for generic list

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

like image 913
Aniket Divekar Avatar asked Mar 19 '26 12:03

Aniket Divekar


1 Answers

You need cast

 List<FeedSource> lst= new ArrayList<FeedSource>();

 lst= loc.unmarshall((Class<List<FeedSource>>) lst.getClass(), marshalled);
like image 171
Dhananjay Avatar answered Mar 22 '26 00:03

Dhananjay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!