views:

675

answers:

3

Suppose I have a pojo:

public class MyPojo {
    int id;
    public int getId()
    { return this.id }

    public setId(int id)
    { this.id = id }

    public static void main(String[] args){
    MyPojo mp = new MyPojo();
    mp.setId(4);
    ObjectMapper mapper = new ObjectMapper();
    mapper.getSerializationConfig().set(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
    LOG.debug(mapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.WRAP_ROOT_VALUE));
    LOG.debug(mapper.writeValueAsString(mp));
    }
}

When I serialize using the Jackson ObjectMapper, I just get

true
{"id":4}

but I want

true
{"MyPojo":{"id":4}}

I've searched all over, Jacksons documentation is really unorganized and mostly out of date.

+1  A: 

I'm not using jackson, but searching I found this configuration that seems to be what you want: WRAP_ROOT_VALUE

Feature that can be enabled to make root value (usually JSON Object but can be any type) wrapped within a single property JSON object, where key as the "root name", as determined by annotation introspector (esp. for JAXB that uses @XmlRootElement.name) or fallback (non-qualified class name). Feature is mostly intended for JAXB compatibility.

Default setting is false, meaning root value is not wrapped.

So that you can configure mapper:

objectMapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);

I hope it helps you...

Aito
welcome to the wacky world of Jacksons documentation of features that don't actually work. Thanks for the help though.
DanInDC
FWIW, this would be the expected implementation (what was descibed as intended). Inclusion of feature enumeration is unfortunate, since all other features are implemented (AFAIK), and it certainly makes no sense to add them before implementation.If you want it implemented, you can (besides contributing implementation) vote for it in Jira and/or lobby for that on mailing lists.
StaxMan
A: 

How about simplest possible solution; just use a wrapper class like:

class Wrapper { public MyPojo MyPojo; }

and wrapping/unwrapping in your code?

Beyond this, it would help to know WHY you would like additional json object entry like this? I know this is done by libs that emulate json via xml api (because of impedance between xml and json, due to conversion from xml to json), but for pure json solutions it is usually not needed.

Is it to allow you do figure out what actual type is? If so, perhaps you could consider enabled polymorphic type information, to let Jackson handle it automatically? (see 1.5 release notes, entry for PTH, for details).

StaxMan
Why: Because I am taking the serialized JSON, turning it into a JSONObject and then appending that JSONObject to a root JSONObject. My root object is a transport object holding a few different kinds of objects. I need to know the type on the other end when i deserialize them. I've got a working system that I'm not that annoyed by. Thanks for the reply.
DanInDC
Ah ok. Like I said, Jackson can figure out type on its own without wrapping, but maybe that's not the case with other libs.Glad to know you got things working either way.
StaxMan
A: 

I would be interested in hearing the OP's solution for this. I'm having similar issues where my RESTful web service is serializing objects as either XML or JSON for clients. The Javascript clients need to know the wrapping type so that can parse it. Coupling the type to a URI pattern is not an option.

Thanks.

Edit: I noticed that Spring MappingJacksonJsonMarshaller adds the wrapping class when marshalling, so I stepped through the code in debug and noticed that Spring passes in a HashMap with a single key-value pair such that the key is the wrapping name and the value is the object. So, I extended JacksonJaxbJsonProvider, override the writeTo() method and added the following:

HashMap<String, Object> map = new HashMap<String, Object>();
map.put(value.getClass().getSimpleName(), value);
super.writeTo(map, type, genericType, annotations, mediaType, httpHeaders,entityStream);

It's a bit of a hack, but it works nicely.

Tony
Could you elaborate more on your edit? I don't think MappingJacksonJsonMarshaller exists but this seems like a solution to a problem I'm having... or at least one worth trying.
AHungerArtist