Derek Thurn
Derek Thurn

Reputation: 15375

Building JSON server-side with Google Web Toolkit

Google Web Toolkit has a JSON library (com.google.gwt.json.client). The 'client' part of that name makes me suspect that it isn't intended for use server-side. The following code in a server-side RPC method confirms my suspicions:

System.out.println("attempting to make JSONArray");
JSONArray test = new JSONArray();
System.out.println("Made JSONArray");

By throwing a ClassNotFound(JSONArray) exception. I need to build some JSON server-side.

1) Am I correct in believing that I can't use the com.google.gwt.json.client package on the server? 2) If so, is there a good alternative with approximately the same interface that I can use to construct JSON on the server?

I'm running my app on App Engine, in case it matters.

Upvotes: 7

Views: 4544

Answers (3)

Hiram Chirino
Hiram Chirino

Reputation: 4091

One of the best options to use is to use RestyGWT on the client side and Jackson on the sever side. That way you get transparent object to JSON marshaling.

Upvotes: 1

Jason Hall
Jason Hall

Reputation: 20930

1) Am I correct in believing that I can't use the com.google.gwt.json.client package on the server?

Correct. That class contains lots of native JS methods -- important stuff like get() -- and is meant to be compiled down to JavaScript to be used in the client side.

As for 2), as you've already found, the library you found from json.org is good, and I've also heard promising things about gson.

Upvotes: 3

Derek Thurn
Derek Thurn

Reputation: 15375

I ended up using the Java library from JSON.org:

http://www.json.org/java/

It seems to compile OK for App Engine. I'm not 100% sure, but I'm fairly confident that the GWT-bundled JSON library cannot be used server side, which is quite strange.

Upvotes: 1

Related Questions