users@jersey.java.net

Re: [Jersey] java to json and back

From: guilherme_maranhao <guilherme82_at_gmail.com>
Date: Tue, 24 Mar 2009 04:40:04 -0700 (PDT)

Hi,

For recommended practise, the POJOs mentioned in this post (UserTable and User) are shared from the applications that consumes and produces the services, respectively. But they aren't the real domain objects presented on them, are they?
they're just the 'contract' between the apps, aren't they?

thanks, guilherme



Hello Pedro,

On Tue, Jan 20, 2009 at 01:55:27AM -0200, Pedro Teixeira wrote:
> Hello,
>
> Can anyone point to the standard/best practice to marshal data between json
> and java?

I am afraid there is no best/standard practice how to do that.

>
> So far, I am keeping my method's arguments and return types as plain String,
> but that implies a 'boring' layer of manual serialization.
>
> I'm currently experimenting with the following APIs:
> - flexjson (best so far java to json, but could not get Deserializer to work
> yet)
> - json-lib http://json-lib.sourceforge.net/
> - http://code.google.com/p/json-simple/

I would add jackson (http://www.cowtowncoder.com/hatchery/jackson/index.html)
but there might be yet another libs.

>
> The problem is a bit harder from json to java. Is there any API to copy
> properties in an existing java instance?
>
> Is there a better way? Should I really be looking in generating extra
> temporary java structures to use with JAXB? (want to avoid and it seems that

Instead of generating extra structures, you could consider
just annotating your current structures, and you should be set.
Then you will get also possibility to provide XML besides JSON,
and you never know when you would need it ;-)

> JAXB produces strange looking json). I don't expect any magic from Jersey's

In fact, i am trying to get the simplest natural JSON produced for you
out of JAXB within Jersey. E.g. with stuff in my current workspace,
you will get:

{"columns":[{"id":"userid","label":"UserID"},{"id":"name","label":"User Name"}],"rows":[{"userid":1621,"name":"Grotefend"}]}

out of:

@XmlRootElement
public class UserTable {

    public static class JMakiTableHeader {

        public String id;
        public String label;
        }
    }
    public List<JMakiTableHeader> columns;
    public List<User> rows;
}

@XmlRootElement
public class User {
    @XmlElement(name="userid")
    public int id;
    public String name;
    @XmlTransient
    public String password;
}

out of the box, without any special configuration.
Do you consider such JSON expresion as strange?
And yes, you will be able to unmarshall easily.

You can get the same JSON now, only with a bit
of configuration with the currently available Jersey builds.
Look at [1],[2] for examples, and at [3] for the config options.


> implementation, but I just would like to check how's the community dealing
> with the engineering of (un)marshalling complex objetcs in large systems.
>
> Currently trying to decise if simple/boring code is better than spending
> time with mega-extra dynamics/reflections (that will not suit all cases).

I would recommend you give the JAXB way a try and then you will see,
if you are happy with what you get. If something does not work as you wish,
please let us know.

Thanks,

~Jakub

[1]http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/1.0.1/json-from-jaxb-1.0.1-project.zip
[2]http://download.java.net/maven/2/com/sun/jersey/samples/jmaki-backend/1.0.1/jmaki-backend-1.0.1-project.zip
[3]https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.1/api/jersey/com/sun/jersey/api/json/JSONJAXBContext.html

>
>
> regards,
> Pedro

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net




-- 
View this message in context: http://n2.nabble.com/java-to-json-and-back-tp2185708p2526044.html
Sent from the Jersey mailing list archive at Nabble.com.