users@jersey.java.net

Re: JSON convention

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Fri, 15 Feb 2008 09:36:38 +0100

Hi Manveen,

On Thu, Feb 14, 2008 at 04:30:32PM -0500, Marc Hadley wrote:
> Jakub is working on better JSON support via JAXB, I'm sure he'll
> respond once he's back online. You can also use JSONObject to create
> whatever JSON format you desire, see this entry for an example:
>
> http://blogs.sun.com/japod/entry/json_entity_providers_in_jersey

Besides JSONObject approach, you will be able (already in json-improvements branch)
to do things like:

--cuthere--
@GET @ProduceMime("application/json")
public UserTable getUserTable() {

 List<User> users = new LinkedList<User>();
 users.add(new User("john", "John White", "passwd123"));
 users.add(new User("jim", "Jim Green", "anotherpasswd"));

 return new UserTable(users)
}
--cuthere--

Which will create following JSON data (table data model) consumable by jMaki:
--cuthere--
{"userTable":{"columns":[{"id":"userid","label":"UserID"},{"id":"name","label":"User Name"}],"rows":[{"userid":"john","name":"John White"},{"userid":"jim","name":"Jim Green"}]}}
--cuthere--

Appropriate JAXB beans please find bellow.
The plan is to provide one jMaki example in Jersey along with
jaxb beans for individual jMaki widgets.

I am finishing work on unmarshalling (JSON->Java),
after that the JSON related stuff should appear in the main trunk,
so that you could give it a try.

I will keep the list posted.

~Jakub


--cuthere--
@XmlRootElement
public class User {
    @XmlElement(name="userid")
    public String id;
    public String name;
    @XmlTransient
    public String password;
    
    public User(){}
    
    public User(String id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
}

@XmlRootElement
public class UserTable {

    public static class JMakiTableHeader {

        public String id;
        public String label;

        public JMakiTableHeader() {
        }

        public JMakiTableHeader(String id, String label) {
            this.id = id;
            this.label = label;
        }
    }
    public List<JMakiTableHeader> columns = initHeaders();
    public List<User> rows;

    static List<JMakiTableHeader> initHeaders() {
        List<JMakiTableHeader> headers = new LinkedList<JMakiTableHeader>();
        headers.add(new JMakiTableHeader("userid", "UserID"));
        headers.add(new JMakiTableHeader("name", "User Name"));
        return headers;
    }
    
    public UserTable() {}
    
    public UserTable(List<User> users) {
        this.rows = new LinkedList<User>();
        this.rows.addAll(users);
    }
}
--cuthere--


 
> Marc.
>
> On Feb 14, 2008, at 1:55 PM, Manveen Kaur wrote:
>
> >Jersey currently supports BadgerFish convention for encoding/
> >decoding JAXB beans to/from JSON. There are many clients who cannot
> >accept this format. e.g. it's not consumable directly by a jMaki
> >client.
> >
> >Are there any plans to move to another more standardized JSON
> >convention?
> >
> >thanks,
> >--Manveen
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
>
> ---
> Marc Hadley <marc.hadley at sun.com>
> CTO Office, Sun Microsystems.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>