Quantcast

Customizing ObjectMapper

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Customizing ObjectMapper

Gili
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Customizing ObjectMapper

Pavel Bucek-2
Hello Gili,

see
http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/ext/MessageBodyReader.html
and
http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/ext/MessageBodyWriter.html.

sample:
http://java.net/projects/jersey/sources/svn/content/trunk/jersey/experimental/view-client/jersey-view-client-samples/atompub-simple-view-client-sample/src/main/java/com/sun/jersey/samples/viewclient/atompubsimple/provider/AtomEntryProvider.java?rev=4816

Pavel

On 4/3/11 4:34 AM, 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
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Customizing-ObjectMapper-tp6234597p6234597.html
> Sent from the Jersey mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Customizing ObjectMapper

Gili
In reply to this post by Gili
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<ObjectMapper> as a JAX-RS provider and JacksonProviderProxy will use it:

@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper>
{
        @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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Customizing ObjectMapper

Tauren Mills
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 <[hidden email]> 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.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Customizing ObjectMapper

Gili

    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 <[hidden email]> 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.


Loading...