Good point!
Thanks,
Gili
On 03/04/2011 6:01 AM, Tauren Mills wrote:
> I'm not sure how often getContext() is called, but I implemented mine
> to not create a new ObjectMapper each time:
>
> @Provider
> public class ObjectMapperProvider implements ContextResolver
> {
> ObjectMapper mapper;
>
> public ObjectMapperProvider(){
> mapper = new ObjectMapper();
> // ...
> }
>
> @Override
> public ObjectMapper getContext(Class<?> type) {
> return mapper;
> }
> }
>
>
> On Sat, Apr 2, 2011 at 9:33 PM, Gili <cowwoc_at_bbs.darktech.org
> <mailto:cowwoc_at_bbs.darktech.org>> wrote:
>
>
> Gili wrote:
> >
> > Hi,
> >
> > I'd like to register a Jackson deserializer for a 3rd-party
> class I cannot
> > annotate. I believe the only way to do this is by customizing the
> > ObjectMapper that Jackson uses. I tried injecting my own
> ObjectMapper
> > instance using Guice but it looks like
> > com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy
> instantiates
> > ObjectMapper directly.
> >
> > How am I supposed to register custom (de)serializers for 3rd-party
> > classes?
> >
> > Thank you,
> > Gili
> >
>
> Answering my own question. On the server side, all you need to do is
> register a ContextResolve as a JAX-RS provider and
> JacksonProviderProxy will
> use it:
>
> @Provider
> public class ObjectMapperProvider implements ContextResolver
> {
> @Override
> public ObjectMapper getContext(Class<?> type)
> {
> final ObjectMapper result = new ObjectMapper();
> SimpleModule module = new
> SimpleModule(getClass().getName(), new
> Version(1, 0, 0, null)).
> addDeserializer(PhoneNumber.class, new
> PhoneNumberDeserializer()).addSerializer(
> PhoneNumber.class, new
> PhoneNumberSerializer());
> result.registerModule(module);
> return result;
> }
> }
>
> On the client side you need to register the provider as follows:
>
> ClientConfig.getClasses().add(ObjectMapperProvider.class);
>
> Gili
>
> --
> View this message in context:
> http://jersey.576304.n2.nabble.com/Customizing-ObjectMapper-tp6234597p6234646.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>