Search code examples
gwtjavabeans

GWT AutoBean - not serializing ArrayList<String>, other pojos ok?


I can't get an ArrayList to serialize using the AutoBean mechanism. I'm using GWT 2.7. Here is my setup:

public interface IUser {
    String getName();
    void setName(String name);

    ArrayList<String> getFriends();
    void setFriends(ArrayList<String> friends);
}

public class User implements IUser {
    private String name;
    private ArrayList<String> friends;

    ... getters and setters for all members attributes ...
}

then my bean factory:

public interface AutoBeanFactoryImpl extends AutoBeanFactory {
    AutoBean<IUser> user(IUser inst);
}

and finally my usage:

ArrayList<String> friends = new ArrayList<String>();
friends.add("Mary");

User user = new User();
user.setName("Fritz");
user.setFriends(friends);

AutoBeanFactoryImpl factory = GWT.create(AutoBeanFactoryImpl.class);
AutoBean<IUser> bean = factory.user(user);
String json = AutoBeanCodex.encode(bean).getPayload();

The output json does not have the friends array. It has the name ok though. Why doesn't the string array get serialized? Do I need something special for that?

Thanks


Solution

  • The problem is that you specified ArrayList instead of just plain List - in order to nicely generate code that fills in the gaps between Java and JSON, autobeans only want to work with interfaces. Aside from getters and setters, AutoBeans have special support for List, Set (which is just a List without duplicates in the JSON), and Map. If you specify a particular implementation, the AutoBean tooling can't always handle it.

    Feel free to pass in an ArrayList instance, but declare your getter and setter to be of type List.


    As an aside, those collections can be of any type that AutoBeans can otherwise handle - including numbers, booleans, strings, dates, and any other bean-like interface that also conform to what AutoBeans can deal with.