Is there a JSON library for Java that can handle something like the following scenario:
public class BaseShapeSender<T extends Shape> {
{
Collection<T> shapes;
public void send
{
// Sending the shapes collection with JSON
}
}
public class CircleSender extends BaseShapeSender<Circle>
{
}
Currently I’m using Jersey, but calling circleSender.send() with the above above scenario gives an exception
A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/json, was not found
even when using GenericEntity to send the collection (apparently Jersey can handle Collection<Circle> but the second level of generics is too much). So, I found myself duplicating the sending code in every subclass:
public class CircleSender extends BaseShapeSender<Circle>
{
Collection<Circle> circles;
public void send
{
// Sending the circles collection with JSON
}
}
I would love to avoid that if possible since the sending logic is basically the same in all subclasses.
According to this answer Jackson supports both generics as well as polymorphism. GSON (which I’ve stuck with so far) does not until issue 231 is resolved.