How to configure the Jackson ObjectMapper?

3,579 views
Skip to first unread message

Tomas Olsson

unread,
Aug 24, 2011, 12:36:01 PM8/24/11
to terrastore-discussions
Hi,
I am trying to configuring the Jackson ObjectMapper for handling
inherited abstract classes in line with what is suggested here:
http://www.cowtowncoder.com/blog/archives/2010/03/entry_372.html
However, I do not find a way of doing that with Terrastore. I have
tried the approach using a @Provider described here:
http://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html_single/index.html
but it does not seem to work.

An excerpt of the stack trace

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not
construct instance of org.codehaus.jackson.map.type.SimpleType,
problem: abstract types can only be instantiated with additional type
information
at [Source:
org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream@2f74219d;
line: 1, column: 108]
at
org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:
160)
at
org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:
214)
at
org.codehaus.jackson.map.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:
44)



My code is as follows:

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyJacksonContextResolver implements
ContextResolver<ObjectMapper> {

static {

/*
* Fixes a problem of using the wrong class in classloaders when
using Terrastore client with OSGI.
*/

ResteasyProviderFactory resteasyProviderFactory =
ResteasyProviderFactory.getInstance();

javax.ws.rs.ext.RuntimeDelegate.setInstance(resteasyProviderFactory);

/**
* Fixes a problem of ser/deser inherited classes
*/

resteasyProviderFactory.registerProvider(MyJacksonContextResolver.class);
}

@Override
public ObjectMapper getContext(Class<?> type) {
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(); // defaults for defaults (see below);
include as wrapper-array, non-concrete types
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_OBJECT); // all non-final types
System.out.println("MyJacksonContextResolver called with type
"+type.getName());

return mapper;
}

}

Sergio Bossa

unread,
Aug 25, 2011, 4:51:34 AM8/25/11
to terrastore-...@googlegroups.com
Hi Tomas,

TerrastoreClient's json mappings can only be customized by providing a
proper terrastore.client.mapping.JsonObjectDescriptor implementation,
as showed in the following test:
http://code.google.com/p/terrastore/source/browse/src/test/java/terrastore/client/test/integration/TerrastoreClientWithCustomDescriptorIntegrationTest.java?repo=javaclient

There's currently no way to set a custom ObjectMapper, but you could
open an issue about that, and also propose a patch, which would be
great too ;)

Let us know if you need any guidance about either the former or the
latter approach.

Cheers,

Sergio B.

--
Sergio Bossa
http://www.linkedin.com/in/sergiob

Tomas Olsson

unread,
Aug 25, 2011, 4:55:02 AM8/25/11
to terrastore-...@googlegroups.com
OK, I see.

Thanks
/Tomas

Tomas Olsson

unread,
Aug 25, 2011, 5:31:38 AM8/25/11
to terrastore-...@googlegroups.com
However, that means that you cannot use abstract classes with
terrastore? If I provide JsonObjectDescriptor:s for the subclasses of an
abstract class, it still fails for the same reason. If I also provide a
JsonObjectDescriptor for the abstract class I get an exception that that
it is not allowed for abstract classes. So the only fix is to remove the
abstract keywork (which I can do, but makes the code look a little bit
strange).

/Tomas

Sergio Bossa

unread,
Aug 25, 2011, 6:00:42 AM8/25/11
to terrastore-...@googlegroups.com
On Thu, Aug 25, 2011 at 11:31 AM, Tomas Olsson <t...@sics.se> wrote:

> However, that means that you cannot use abstract classes with terrastore?

I've made it working with no custom descriptors, by just applying the
org.codehaus.jackson.annotate.JsonTypeInfo annotation to the
polymorphic types: let us know if it works for you.

Tomas Olsson

unread,
Aug 25, 2011, 6:53:39 AM8/25/11
to terrastore-...@googlegroups.com
Well, it is just that I don't want to use annotations. I want the
classes to be implementation agnostic.

/Tomas

Sergio Bossa

unread,
Aug 25, 2011, 7:38:41 AM8/25/11
to terrastore-...@googlegroups.com
On Thu, Aug 25, 2011 at 12:53 PM, Tomas Olsson <t...@sics.se> wrote:

> Well, it is just that I don't want to use annotations. I want the classes to
> be implementation agnostic.

Fair enough, but saying the Terrastore Client doesn't support abstract
classes isn't that fair ;)

Tomas Olsson

unread,
Aug 25, 2011, 7:39:25 AM8/25/11
to terrastore-...@googlegroups.com
OK, that is true.
/Tomas
Reply all
Reply to author
Forward
0 new messages