users@jersey.java.net

Problem with JSON serialization of wrapped lists

From: Frank Martínez <mnesarco_at_gmail.com>
Date: Fri, 4 Apr 2008 10:50:58 -0500

Hi Guys,

Does anybody know why a list with an unique element is serialized as a
JSonObject instead of a JSonArray with one element?

i.e.

POJO to be serialized:

@XmlRootElement
public class BuildingList {

    @XmlElement
    private List<Building> buildings;

    public BuildingList(List<Building> buildings) {
        this.buildings = buildings;
    }

    public BuildingList() {
    }

}

Resource method:

    @GET
    @ProduceMime("application/json")
    public BuildingList getJson() {

        BuildingDAO dao = ConfigServlet.getSession().getDAO(BuildingDAO.class);
        List<Building> list = dao.findAll();
        return new BuildingList(list);

    }


Result:

If there are more than one element:

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

But if there are just one element:

  {"buildings": { ... } }

The problem is that the Javascript client is awaiting for an array.

Any ideas?

Thanks,
Frank.


-- 
Frank D. Martínez M.
Asimov Technologies Ltda.
Blog: http://www.ibstaff.net/fmartinez/