users@jersey.java.net

Re: [Jersey] Serializing empty arrays as a result

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Wed, 11 Nov 2009 10:20:07 +0100

On Tue, Nov 10, 2009 at 05:13:18PM -0800, Tatu Saloranta wrote:
> On Tue, Nov 10, 2009 at 11:11 AM, Tom Schindl
> <tom.schindl_at_bestsolution.at> wrote:
> > Hi,
> >
> > I'm running into a problem with the Jersey-JSON serialization because I
> > need to satisfy an API which require me to serialize empty Collections.
> >
> > The external party requires me to send JSON in the following format:
> >
> > --------8<--------
> > {
> >  a : "Blabla",
> >  l : []
> > }
> > --------8<--------
> >
> > but it looks like this is not possible to achieve with jersey because if
> > the l-attribute holds an empty list or null it is not part of the
> > serialized response:
>
> I let others explain if and how this is possible with JAXB and bundled
> json handlers, but I will add the usual comment that

Unless you wanted to use the low-level JSON approach and JSONArray/JSONObject
classes, you would indeed need to leverage the JacksonJsonProvider as suggested by Tatu.

~Jakub


> JacksonJsonProvider does include empty lists and nulls by default (can
> be changed so that nulls are excluded).
> JacksonFAQ [http://wiki.fasterxml.com/JacksonFAQ] (6.2.1) outlines how
> this provider can be used -- it can use JAXB annotations, as well as
> Jackson's own, but most of the time basic property auto-detection
> works fine.
>
> -+ Tatu +-
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
Jakub Podlešák
Software Engineer at SUN Microsystems And CZJUG Co-Leader
http://blogs.sun.com/japod