title: DynamoDB JsonMarshaller cannot Deserialize List of Object
body: <p>I have a Java class which is the data-model of a table in DynamoDB. I want to use the <code>DynamoDBMapper</code> to <code>save</code> and <code>load</code> items from Dynamo. One member of the class is a <code>List&lt;MyObject&gt;</code>. So I used the <code>JsonMarshaller&lt;List&lt;MyObject&gt;&gt;</code> to serialize and de-serialize this field. </p>
  
  <p>The list can be successfully serialized by the <code>JsonMarshaller</code>. However, when I try to retrieve the entry back and read the list, it throws an exception: <code>java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to MyObject</code>. It looks like that the <code>JsonMarshaller</code> de-serialize the data into the <code>LinkedHashMap</code> instead of <code>MyObject</code>. How can I get rid of this problem?</p>
  
  <p>The MCVE:</p>
  
  
  
  <pre><code>// Model.java
  @DynamoDBTable(tableName = "...")
  public class Model {
    private String id;
    private List&lt;MyObject&gt; objects;
  
    public Model(String id, List&lt;MyObject&gt; objects) {
      this.id = id;
      this.objects = objects;
    }
  
    @DynamoDBHashKey(attributeName = "id")
    public String getId() { return this.id; }
    public void setId(String id) { this.id = id; }
  
    @DynamoDBMarshalling(marshallerClass = ObjectListMarshaller.class)
    public List&lt;MyObject&gt; getObjects() { return this.objects; }
    public void setObjects(List&lt;MyObject&gt; objects) { this.objects = objects; }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>// MyObject.java
  public class MyObject {
    private String name;
    private String property;
  
    public MyObject() { }
    public MyObject(String name, String property) {
      this.name = name;
      this.property = property;
    }
  
    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }
  
    public String getProperty() { return this.property; }
    public void setProperty(String property) { this.property = property; }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>// ObjectListMarshaller.java
  public class ObjectListMarshaller extends JsonMarshaller&lt;List&lt;MyObject&gt;&gt; {}
  </code></pre>
  
  <hr>
  
  <pre><code>// Test.java
  public class Test {
    private static DynamoDBMapper mapper;
  
    static {
      AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider()
      mapper = new DynamoDBMapper(client);
    }
  
    public static void main(String[] args) {
      MyObject obj1 = new MyObject("name1", "property1");
      MyObject obj2 = new MyObject("name2", "property2");
      List&lt;MyObject&gt; objs = Arrays.asList(obj1, obj2);
  
      Model model = new Model("id1", objs);
      mapper.save(model); // success
  
      Model retrieved = mapper.load(Model.class, "id1");
      for (MyObject obj : retrieved.getObjects()) { // exception
      }
    }
  }
  </code></pre>