tags:

views:

86

answers:

0

I have the following class:

public static class TestSomething {

    Integer test;

    public TestSomething(Integer test) {
        this.test = test;
    }

    // getter and setter for test
}

Ok, now create a collection of this class and serialize it with Gson:

Collection<TestSomething> tests = Arrays.asList(
    new TestSomething(1), 
    new TestSomething(2), 
    new TestSomething(3)
);
String json = new Gson().toJson(tests, new TypeToken<Collection<TestSomething>>() {}.getType());

After this, the String json is set to

[{"test":1},{"test":2},{"test":3}]

Which is great.

But now, all of my model classes inherit from a generic type Identifiable<T> which provides just two methods T getId() and void setId(T). So I change the TestSomething-class from above to

public static class TestSomething extends Identifiable<Long> {
    // same as above
}

When I try to put this through Gson.toJson(), Gson ends up with the following Exception:

java.lang.UnsupportedOperationException: Expecting parameterized type, got class path.to.TestSomething.
 Are you missing the use of TypeToken idiom?
 See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener
        at com.google.gson.TypeInfoFactory.getActualType(TypeInfoFactory.java:97)
        ...

So, what do I have to do to get this work?