POJO to JSON

1,187 views
Skip to first unread message

obesga

unread,
Jun 24, 2011, 8:50:27 AM6/24/11
to Google Web Toolkit
Hello

I've a problem with an application, the application works fine but the
new 'firewall' system drops the RPC calls.
So I'm thinking about rewriting GWT-RPC calls to GWT-JSON

I've a lot of simple POJO objects that are used into the app, the
classes are written and can't change very much ( as they are used
acrros the server, including database DAO)

¿ which are your preferred / less painfull (less boilerplate
code) methods to convert from JSON to POJO in GWT ?
- Autobeans
- JSON Overlay types
- Use simple HTTP with String parse
- Other ( explain please...)

Thomas Broyer

unread,
Jun 24, 2011, 9:34:51 AM6/24/11
to google-we...@googlegroups.com


On Friday, June 24, 2011 2:50:27 PM UTC+2, obesga wrote:
Hello

I've a problem with an application, the application works fine but the
new 'firewall' system drops the RPC calls.

I'd be interesting to understand why it drops them, so that your "new" protocol won't be blacklisted either.
 
So I'm thinking about rewriting GWT-RPC calls to GWT-JSON

I've a lot of simple POJO objects that are used into the app, the
classes are written and can't change very much ( as they are used
acrros the server, including database DAO)

¿ which are   your preferred / less painfull (less boilerplate
code)    methods to convert from JSON to POJO in GWT ?
- Autobeans

You won't be able to reuse your POJOs, but if you can convert them to interfaces, then that's an option (AFAICT, you can also have POJOs implementing the interface and thus wrap a POJO instance in an AutoBean; but that'd only help with serializing, IIUC, not deserializing JSON to POJOs)

- JSON Overlay types

You won't be able to reuse your POJOs on the client side; and you'd have to serialize them to/from JSON on the server-side (fortunately, there are many tools to help you: GSON, Jackson, etc.)
Have a look at the Piriti project too as, IIRC, it could handle that quite easily (including the GWT-client part)
 
- Use simple HTTP with String parse

I don't see any advantage over JSON, and the major drawbacks of having to write a parser and serializer by hand.
 
- Other ( explain please...)

RequestFactory:
 - you'll leverage your POJOs on the server-side, and transforming GWT-RPC services to RF ones should be really easy.
 - you'll have to write the proxies, but that could be automated (that's what we do). If you domain model is stable enough, you could also simply iterate with your IDE's "extract interface" feature and a bit of tweaking by hand.

Harald Pehl

unread,
Jun 24, 2011, 10:59:42 AM6/24/11
to google-we...@googlegroups.com
Hi, 

Thomas you're right: You can use Piriti (http://code.google.com/p/piriti/) on the client side to map JSON to POJOs and serialize it back to JSON. Please note that Piriti is only meant to be used on the client side. It's not usable on the server side. But you can share your POJOs between client and server.

- Harald

Raphael André Bauer

unread,
Jun 24, 2011, 12:35:07 PM6/24/11
to google-we...@googlegroups.com

There is another great library available (guess not many people know
of it) - RestyGWT
check it out:
http://restygwt.fusesource.org/


Best,

Raphael


>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-web-toolkit/-/sqSYKUYr-9sJ.
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-web-tool...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>

--
blog: http://ars-machina.raphaelbauer.com
web: http://raphaelbauer.com

obesga

unread,
Jun 27, 2011, 5:02:16 PM6/27/11
to Google Web Toolkit
Hello, thanks for the help !

I'll try with Pitiri, I seems the most direct solution to my needs.
In server-side, I'll use GSON to convert from POJO to JSON, as I think
Pitiri is only for client-side use
(all google code, a little lock-in??)


I want to combine it with GWTQUERY and GWTQUERY-IU
http://code.google.com/p/gwtquery/
So I think the proyect would became a strange GWT App....

Thank you !!



On Jun 24, 6:35 pm, Raphael André Bauer
<raphael.andre.ba...@gmail.com> wrote:

Elhanan Maayan

unread,
Jun 28, 2011, 1:29:30 AM6/28/11
to google-we...@googlegroups.com
how can you automate writing proxies?
in regards to deserliziing why not use the solution i wrote before, get a string, turn it to autobean and then use the visitor pattern with ReflectionHelper to copy data autobean to the bean?


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

Elhanan Maayan

unread,
Jun 28, 2011, 1:31:41 AM6/28/11
to google-we...@googlegroups.com
in regards to to re-using pojo's that may sound like a good idea in the begining (hell, i like it myself) however bare in mind that your'e restricting yourself to the jre emulation limitations on the client side, meaning you won't be able to use any cross-cutting domain services (like persistence,aspects, di via spring) becouse won't exists on the client side.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

Thomas Broyer

unread,
Jun 28, 2011, 4:36:23 AM6/28/11
to google-we...@googlegroups.com


On Tuesday, June 28, 2011 7:29:30 AM UTC+2, Elhanan wrote:
how can you automate writing proxies?

We wrote a code generator (in the form of a maven plugin) that scans our domain classes and generates proxies for all of them (this is made easy because we have 3 base classes that all domain classes extend).

Elhanan Maayan

unread,
Jun 28, 2011, 4:49:33 AM6/28/11
to google-we...@googlegroups.com
i wonder if there are open source projects for that. actually it would a good tooling addition.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

Thomas Broyer

unread,
Jun 28, 2011, 5:02:15 AM6/28/11
to google-we...@googlegroups.com

Jeff Larsen

unread,
Jun 28, 2011, 9:56:47 AM6/28/11
to google-we...@googlegroups.com
I was only able to get it to work with Appengine projects and not any of our legacy projects. I don't know the requirements to get the plugin to generate proxies, and whatever they are, they aren't big enough because we have legacy JDBC code and a lot of entities are built that way instead of JPA etc. 

dd

unread,
Jun 30, 2011, 6:55:59 PM6/30/11
to google-we...@googlegroups.com
### AutoBeans can be used on client *and* server side:

  static interface MyAutoBeanFactory extends AutoBeanFactory {

    AutoBean<MyPojo> login();

  }


  // In non-GWT code, use AutoBeanFactoryMagic.create(MyFactory.class);

  static final MyAutoBeanFactory FACTORY = GWT.create(MyAutoBeanFactory.class);



  public MyPojo getMyPojo(String json) {

    AutoBean<MyPojo> bean = AutoBeanCodex.decode(FACTORY, MyPojo.class, json);

    return bean.as();

  }


  public String getJson(MyPojo myPojo) {

    AutoBean<MyPojo> bean = AutoBeanUtils.getAutoBean(myPojo);

    return AutoBeanCodex.encode(bean).getPayload();

  }



### JSON Overlay types work on client side only (look at the native javascript code /*-{ }-*/):


  public final native T getPojo(String json) /*-{

    return eval(json); // eval('(' + json + ')');

  }-*/;


  public String getJson(T pojo) {

    return new JSONObject(pojo).toString();

  }



### RequestFactory is great above persistence layers like JPA (and uses AutoBeans under the hood!) but has a lot of boilerplate code.

### Btw: I generate the RequestFactory + JPA stuff with Xtext + Xpand (using a domain specific language (dsl) for my domain model).



- Daniel

dd

unread,
Jun 30, 2011, 7:03:42 PM6/30/11
to google-we...@googlegroups.com
sorry, i've been fuzzy: In JSON Overlay example type T has to satisfy <T extends JavaScriptObject>

Elhanan Maayan

unread,
Jul 1, 2011, 6:02:47 AM7/1/11
to google-we...@googlegroups.com
our CTO wants to generate jpa stuff, can you bring some articles on how to do stuff with xtext?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

dd

unread,
Jul 2, 2011, 9:09:22 AM7/2/11
to google-we...@googlegroups.com
Xtext is a great tool. A good starting point for reading is
 
The Xtext plugin for eclipse ships with the so called 'domain model' example.
It is a DSL (Domain Secific Language) for describing your entities and their relationships.
Next step is to write a generator based on Xpand - a transformation language.
 
There is an analogy to the world of xml:
- with the help Xtext you are writing a grammar like XSD
- your model is syntactically correct if it is conform to your grammar. A model is like XML.
  (your model is semantically correct, if it correctly decribes your business within your functional domain)
- Xpand is like XSLT. It is a language to transform your models to whatever you want. in your case JPA.
 
Your challange is to write a generator which transforms your model (of entities)
to JPA (or GWT or both or whatever) code.
 
I wrote a full blown extended version of the original domain model example with a code generator
for jpa (+ gwt request factory).
If your company needs professional help in the context of generative development with jpa / gwt please contact me directly:
 
Cheers
 
Daniel Dietrich
 
Reply all
Reply to author
Forward
0 new messages