Jeff
Peter
I'm somewhat surprised that Jackson has issues serializing generic
types, so if you want to keep using Key<?> you can probably get it
fixed. You're right, the Jackson devs are very responsive and they've
been fast to fix every issue I've run across.
Jeff
Now, my problem it that I use Jersey (also tried Resteasy) as a REST
layer where Jackson is used automatically. I found no way to manually
configure Jackson.
I know this is not the right mailing list, but has anyone been able to
manually configure Jackson when used with a REST layer (Jackson,
Resteasy, etc..)?
Peter
Here's the RESTEasy code I use in Mobcast to customize the
serialization of GeoPt. If you have RESTEasy's classpath scanning
turned on (the default), all you need to do is put this class in your
project. RESTEasy will use it to obtain the ObjectMapper, and you can
customize Jackson to your heart's content. It should be pretty
self-explanatory:
package us.mobcasting.server.rest.provider;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;
import com.google.appengine.api.datastore.GeoPt;
/**
* We customize the serialization of a few items using a Jackson MixIn class
* as per: http://wiki.fasterxml.com/JacksonMixInAnnotations
*
* @author Jeff Schnitzer <je...@infohazard.org>
*/
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper>
{
static abstract class GeoPtMixIn // doesn't extend GeoPt since it is final
{
@JsonProperty("t") abstract float getLatitude();
@JsonProperty("g") abstract float getLongitude();
}
public final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
OBJECT_MAPPER.getSerializationConfig().addMixInAnnotations(GeoPt.class,
GeoPtMixIn.class);
}
@Override
public ObjectMapper getContext(Class<?> type)
{
return OBJECT_MAPPER;
}
}
Resteasy handles JSON conversion automatically for you, just annotate
the service classes accordingly. There are many examples on their
site.
Peter