views:

48

answers:

1

Hello,
I'm trying to force brackets on lists that contain only one element.

I want something like this:
{"id":"0","industries":[{"id":"0","name":"Technologies"}],"name":"Google Inc."}

But I get:
{"id":"0","industries":{"id":"0","name":"Technologies"},"name":"Google Inc."}

Here is my Entity:

@Entity
@XmlRootElement
public class Company {
 private int id;

 private String name;
 private String description;

 @XMLElement(name="industries")
 private List<Industry> industryList;

    [...]

And finally, my JAXB Context Resolver:

public JAXBContextResolver() throws Exception {

MappedBuilder builder = JSONConfiguration.mapped(); builder.arrays("industries"); builder.rootUnwrapping(true);

this.context = new JSONJAXBContext(builder.build(), Company.class); }

A: 

I'm not too sure about this, but try removing the @XMLElement annotation for industryList

I have done stuff the other way around: using jaxb to generate java classes from xsd schema files. I've looked at the generated classes with collection fields, and they don't have any specific annotations on them.

Also you may wanna try JSON Lib: http://json-lib.sourceforge.net/

you could do things like:

jsonString = JSONObject.fromObject(pojoObject)

which will generate json string that will incorporate e.g. collections of complex types.

You could then send the jsonString using e.g. HttpServletResponse.

I would recommend serializing DTO objects rather than serialize your entity objects.

Dzhu