users@jersey.java.net

Natural JSON Config Not Giving Me Single Element Arrays

From: Paul Sanders <tendancer_at_gmail.com>
Date: Tue, 17 Nov 2009 16:10:30 -0800

I've followed the instructions I've found to define my own
JSONJAXBContext with natural build specified but when I output the
JSON representation for an object that has only single elements in
their children List:

public class Permission implements Comparable
{
    private Integer id;
    private String key;
    private String description;
    private Integer featureId;
    private List<Permission> children; // optional, holds the
children permission of this one
    private String label;
}

I get the following (my formatting):

[
   {"children": [
         {"children": {
               "description":"Ability to view a tool permission",
                "featureId":1,
                "id":7,
                "key":"admin.perm.edit",
                "label":"edit"},
          "description":"Ability to view a tool permission",
          "featureId":1,
          "id":7,
          "key":"admin.perm.edit",
          "label":"perm"}
        ],
     "description":"Ability to view a tool permission",
     "featureId":1,
     "id":7,
     "key":"admin.perm.edit",
     "label":"admin"}
]

The second "children" value is a singleton array but appears without
the "[" that I was expecting. If I change my context to be mapped but
specify that "children" is an an array I get this:

{
   "permission": {
         "children": [
             {"children": [
                {"description":"Ability to view a tool permission",
                 "featureId":"1",
                 "id":"7",
                 "key":"admin.perm.edit",
                 "label":"edit"
             }],
          "description":"Ability to view a tool permission",
          "featureId":"1",
          "id":"7",
          "key":"admin.perm.edit",
          "label":"perm"
        }],
    "description":"Ability to view a tool permission",
    "featureId":"1",
    "id":"7",
    "key":"admin.perm.edit",
    "label":"admin"}
}

which is what I would expect - both children arrays are single valued
but enclosed in "[". I'd really like to use the natural configuration,
can anyone suggest what I am doing wrong? I'm using 1.1.4 of the
jersey framework and 2.1.10 of jaxb-impl.

Here are the two lines I used for the configuration....

                //this.context = new
JSONJAXBContext(JSONConfiguration.mapped().arrays("children").build(),
classes.toArray(new Class[classes.size()]));
                //this.context = new
JSONJAXBContext(JSONConfiguration.natural().build(),
classes.toArray(new Class[classes.size()]));

Thanks for any help you can give

Paul.