0

I have a strange issue marshalling an empty object collection to json using jersey with the jaxb based json support. My object looks like

...
@XmlWrapper(name = "stuff") @XmlElement(name = "s")
private List<Foo> foos;
...

Marshaling this to json produces the expected results

... stuff: [{ "s": ... }, { "s": ... }] ...

except when the list is empty. I would expect to see

... stuff: [] ...

but I see

... stuff: [null] ...

instead. Any idea what's wrong? The problem seems to be related to the @XmlElementWrapper annotation, removing it I don't get the the stuff property in the output at all.

agnul
  • 12,608
  • 14
  • 63
  • 85

3 Answers3

0

I managed to solve JSON array and primitive field "bug" in Jersey json library. Secret ingredient is JSONConfiguration and ContextResolver magic. See my following post it has a full code example, customized ContextResolver and rest Application class might be somewhat fuzzy logic in first look.

How to serialize Java primitives using Jersey REST

  • json array for zero or single-element Java lists
  • primitive integer or boolean fields without quotation chars
Community
  • 1
  • 1
Whome
  • 10,181
  • 6
  • 53
  • 65
0

Are you serializing an empty list, or are you serializing an un-instantiated null object?

ie. I would expect:

private List<Foo> foos; - would serialize to 'stuff: [null]'

and I would also expect:

private List<Foo> foos = new ArrayList<Foo>(); - we serialize to 'stuff: []'

If that isn't the case, you can always direct Jackson (which is the default JSON serializer bundled with Jersey) to omit the writing of bean properties as null value..

Derek Troy-West
  • 2,469
  • 1
  • 24
  • 27
  • I'm serializing an empty list (new ArrayList()), but if I do a simple foos.add(null) I have the same result. – agnul Nov 21 '11 at 10:58
  • How strange, did you try using @JsonWriteNullProperties(value = false)? It's been a while since I used Jersey but I don't recall having any problems serializing empty collections.. – Derek Troy-West Nov 21 '11 at 11:16
  • The thing is I'm not using jackson (and thereforse can't use the annotation above) but the JAXB based solution outlined in section 5.2 in the jersey user guide. The problem seems the @XmlElementWrapper annotation, removing it I don't get the "stuff" property at all if the list is empty. – agnul Nov 21 '11 at 11:38
  • Jackson allows use of mix-ins (http://wiki.fasterxml.com/JacksonMixInAnnotations) which do NOT require modifying 3rd party types. Further, you can configure default handling for ObjectMapper; see ""how to prevent writing of null properties" on [http://wiki.fasterxml.com/JacksonFAQ] – StaxMan Nov 25 '11 at 17:54
0

I would suggest using POJO mapping based on Jackson. I am not sure why you want that intermediate "s" in there, but POJO would produce (and consume) simpler structure:

"stuff" : [ { ... }, { ... } ]

For that you need no annotations with POJO mapping; JAXB annotations are only needed for XML processing, since XML has no natural mechanism to distinguish arrays from objects (unlike JSON).

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Thing is being sent to a third party, if I could convince them to drop the intermediate "s" I might as well try to make them read the "[null]". ;-) Also jackson is not an option, I'm trying to reuse the same code/objects that I use for XML output. – agnul Nov 24 '11 at 09:14
  • Jackson works perfectly fine XML (wrt JAXB) and as Jersey handler for JSON. So that should not be problematic in itself. But yeah, if 3rd party insists on adding extra structure that is problematic -- not sure why (that does not help with JSON at all, JSON != XML), but perhaps they produce this using Jettison. – StaxMan Nov 25 '11 at 17:56