Quantcast

Set custom objectMapper to Jackson Extension

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

Set custom objectMapper to Jackson Extension

Esteban F.
Hello

How can I set a Jackson ObjectMapper to the extension of Jackson Restlet to support polymorphism:

objectMapper.enableDefaultTyping (); / /

I can not use annotations in the beans, because these should be used in GWT too.

Best regards

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

Re: Set custom objectMapper to Jackson Extension

Tim Peierls
Search for recent messages from me in this forum. I've answered this in detail in several threads.

On Tue, Apr 19, 2011 at 12:06 PM, Esteban F. <[hidden email]> wrote:
Hello

How can I set a Jackson ObjectMapper to the extension of Jackson Restlet to
support polymorphism:

objectMapper.enableDefaultTyping (); / /

I can not use annotations in the beans, because these should be used in GWT
too.

Best regards

Esteban

--
View this message in context: http://restlet-discuss.1400322.n2.nabble.com/Set-custom-objectMapper-to-Jackson-Extension-tp6287812p6287812.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2721440

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

Re: Set custom objectMapper to Jackson Extension

Esteban F.
Thanks Tim!!
I found very useful the code in this post: http://restlet-discuss.1400322.n2.nabble.com/Jackson-Mix-in-Annotations-td6211060.html#a6231831

I implemented my JacksonCustomConverter, that override the two methods create and set the mapper with mapper.enableDefaultTyping().
I post the code:

public class JacksonCustomConverter extends JacksonConverter {
    @Override
    protected <T> JacksonRepresentation<T> create(MediaType mediaType, T source) {
        ObjectMapper mapper = createMapper();
        JacksonRepresentation jr = new JacksonRepresentation<T>(mediaType, source);
        jr.setObjectMapper(mapper);
        return jr;
    }

    @Override
    protected <T> JacksonRepresentation<T> create(Representation source, Class<T> objectClass) {
        ObjectMapper mapper = createMapper();
        JacksonRepresentation jr = new JacksonRepresentation<T>(source, objectClass);
        jr.setObjectMapper(mapper);
        return jr;
    }


    private ObjectMapper createMapper() {
        JsonFactory jsonFactory = new JsonFactory();
        jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
        ObjectMapper mapper = new ObjectMapper(jsonFactory);
        mapper.enableDefaultTyping();
        return mapper;
    }

}

Regards
Loading...