You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID" <dk...@att.com> on 2011/09/15 23:09:01 UTC

Optimizing lists output with Jettison

I had been using Jackson for an earlier service using CXF.  I'm now trying to use the default of Jettison.  One thing I'm noticing is that the default output isn't very efficient wrt output of lists.  In the older service, if I had a list attribute, I would get something like this:

    "listprop":[{...firstentry...},{...secondentry...},...]

Now, with Jettison out of the box, I get something like this:

    "listprop":{"listtype":[{...firstentry...},{...secondentry...},...]}

You see the addition of the thing that looks like a "listtype".  This adds additional verbosity, which isn't ideal.  Is there a straightforward way to optimize this out?

Re: Optimizing lists output with Jettison

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On 15/09/11 22:09, KARR, DAVID wrote:
> I had been using Jackson for an earlier service using CXF.  I'm now trying to use the default of Jettison.  One thing I'm noticing is that the default output isn't very efficient wrt output of lists.  In the older service, if I had a list attribute, I would get something like this:
>
>      "listprop":[{...firstentry...},{...secondentry...},...]
>
> Now, with Jettison out of the box, I get something like this:
>
>      "listprop":{"listtype":[{...firstentry...},{...secondentry...},...]}
>
> You see the addition of the thing that looks like a "listtype".  This adds additional verbosity, which isn't ideal.  Is there a straightforward way to optimize this out?

Jettison may need to be fixed at the code level. I was able to produce 
the optimized output after applying a transform feature to JSONPrivider 
(indirectly), it may not qualify as a straightforward option :-), but 
show the feature can do some fairly 'crazy' transforms :-). So, here is 
the original test code:

JSONProvider p = new JSONProvider();
p.setSerializeAsArray(true);
p.setArrayKeys(Arrays.asList(new String[]{"list"}));
// Tags contains a List of Tag objects
Tags tags = new Tags();
tags.addTag(createTag("a", "b"));
tags.addTag(createTag("c", "f"));
// write and capture the output and print which produces:

{"Tags":{"list":[{"group":"b","name":"a"},{"group":"f","name":"c"}]}}

So we have a sub-optimal output with a 'list' wrapper.

The updated test code:

JSONProvider p = new JSONProvider();
// drop top-level Tags
p.setOutDropElements(Arrays.asList(new String[]{"Tags"}));
// rename list to Tags
p.setOutTransformElements(Collections.singletonMap("list", "Tags"));
// Tags contains a List of Tag objects
Tags tags = new Tags();
tags.addTag(createTag("a", "b"));
tags.addTag(createTag("c", "f"));
// write and capture the output and print which produces:

{"Tags":[{"group":"b","name":"a"},{"group":"f","name":"c"}]}

:-)

Cheers, Sergey