Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT AutoBean with POJO class instead of interface

I'm hoping someone can suggest a simple solution to my problem.

I have a POJO, say:

public class Person
{
    private String name;
    public String getName(){ return name; }
    public void setName(String name){ this.name = name; }
}

I'd like to use GWT's AutoBean functionality to serialize / deserialize this bean to JSON, but AutoBean expects an interface:

public interface Person
{
    public String getName();
    public void setName(String name);
}

I have an AutoBeanFactory setup:

public interface PersonFactory extends AutoBeanFactory
{
    AutoBean<Person> person();
}

Using the factory and the Person interface, I am able to deserialize a JSON Person:

PersonFactory personFactory = GWT.create(PersonFactory.class);
AutoBean<Person> autoBeanPerson = AutoBeanCodex.decode(personFactory, Person.class, jsonObject.toString());
Person person = autoBeanPerson.as();

However, if I replace the Person interface with the Person class, I receive an AutoBeanFactoryGenerator exception that states: "com.xxx.xxx.Person is not an interface".

How can I use AutoBean serialization / deserialization with my simple POJO?

like image 781
Naijaba Avatar asked Jun 07 '11 07:06

Naijaba


2 Answers

You simply can't. AutoBean generates lightweight, optimized implementations of the interfaces; it obviously cannot do this for classes. This is by design.

like image 170
Thomas Broyer Avatar answered Oct 10 '22 02:10

Thomas Broyer


It is possible if its a simple POJO and does not have properties of other autobean types:

1) Make PersonPojo implements Person

2) Add a wrapper method to the factory:

public interface PersonFactory extends AutoBeanFactory {
    AutoBean<Person> person( Person p ); // wrap existing
}

3) Create wrapped AutoBean and serialise with AutoBeanCodex.encode to JSON

PersonPojo myPersonPojo = new PersonPojo("joe");
AutoBean<Person> autoBeanPerson = personFactory.person( myPersonPojo );
like image 45
Andrejs Avatar answered Oct 10 '22 02:10

Andrejs