4

I'm using Jackson 1.9.2 with the XML dataformat module. I need to tweak the way that Jackson serializes arrays, lists, collections.

By default, with an int array property called myProperty containing a couple numbers, Jackson / XML is producing the following:

<myProperty> <myProperty>1</myProperty> <myProperty>2</myProperty> </myProperty> 

What I need to produce is:

<myProperty> <item>1</item> <item>2</item> </myProperty> 

I can do this on a per-POJO basis using a combination of JacksonXmlElementWrapper and JacksonXmlProperty like so:

@JacksonXmlElementWrapper(localname='myProperty') @JacksonXmlProperty(localname='item') public int[] myProperty; 

This solution, however, would require that I manually apply these annotations to every array, list, collection in my POJOs. A much better solution would allow me to apply a solution once, globally, for all array, list, collection types. Any ideas on how to implement such a solution?

Thanks!

2 Answers 2

1

You could sub-class AnnotationIntrospector that XML module provides, and override method that inspects @JacksonXmlElementWrapper, and make it return whatever wrapper name you want.

Sign up to request clarification or add additional context in comments.

5 Comments

I like this approach. Any clues how I might do the same for @JacksonXmlProperty(localName='item')?
Yes, that annotation is also handled via AnnotationIntrospector so you can rewrite it.
Sorry to be dense - I see that it can be done with findSerializablePropertyName, but I can't simply return "item" from that method: I need to selectively return "item" when the context is within a non-map container type (list, array or collection). Conditioning findSerializablePropertyName in this way is what I don't see how to do.
Object you get passed can be cast to actual AnnotatedField or AnnotatedMethod, which gives you bit more access to see if it might be a match. But it is possible that there isn't quite enough information, depending on your needs.
Unfortunately I haven't found an easy way to get the information I need to make this workable - thanks again for the suggestion, though.
0

I ended up creating my own JsonGenerator to generate XML for the legacy format my group needs to support.

I would have liked to use the Jackson XML dataformat module, which provides more safety, is more tested, and may be faster. Unforunately, I was not able to find a solution using StaxMan's suggestion.

3 Comments

That is unfortunate, but understandable. One more suggestion: if you are by-passing databinding, you must as well use raw Stax (XML) api (see javax.xml.stream.XMLStreamWriter). It's relatively simple to use for content generation, efficient (just remember to reuse XMLOutputFactory if you need multiple writers).
Thanks for the suggestion. Why is it necessary to use? In my JsonGeneratorBase subclass I am simply writing Strings to a stream.
I would strongly discourage anyone from ever writing XML using String writing -- this will not handle escaping of "funny characters". XMLStreamWriter is typically faster than String concatenation as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.