Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse json with gwt 2.0

Tags:

json

gwt

i'm trying to parse JSON coming from a flow in my gwt 2.0 application.

What is the best way ? Should I use javascriptobject ? JSonParser ? I'm lost with what I'm founding on the web because there's never the gwt version.

String text = "{\"item\":[{\"Id\":\"1\",\"Name\":\"Bob\"},{\"Id\":\"2\",\"Name\":\"John\"},{\"Id\":\"3\",\"Name\":\"Bill\"}]}";

How can I play with my list of items ?

Thanks in advance for any help

like image 686
Tim Avatar asked Aug 10 '10 12:08

Tim


1 Answers

The answer depends on how much you trust that JSON :) Sure, it might be coming from your application, but if insert some untrusted user input, you are facing a possible security hole.

So:

  • for JSONs from trusted sources, I use JavaScript Overlay Types. They make integrating JSON with GWT seamless and I'd definitely recommend this approach. However, internally, this calls the eval() function which means (at least) two things: JSON parsing will be extremely fast (it uses browsers native code for that) and will be possibly insecure. Google for more info about JSON related security issues. JSONParser can also parse JSON via eval(), when you invoke its parseLenient(String jsonString) method, but it's definitely less attractive than JSO.
  • for untrusted sources/input, you should use JSONParser via JSONParser.parseStrict(String jsonString) (available in GWT >=2.1) - you'll have to write more code that way, but you can be sure that the input is properly handled. You might also look into integrating the "official" JSON parser from json.org with JSO - write a JSNI function that returns the parsed object and cast it to your JSO - in theory it should work ;) (that's what GWT does internally with JSOs, at least from what I understood)

As for accessing lists in JSON, there are appropriate classes for that: JsArray (generic, for lists of other JSOs), JsArrayString, etc. If you look at their implementation, they are just JSNI wrappers around the native JS arrays, so they are very fast (but limited, for some reason).


Edit in response to Tim's comment:

I wrote a simple abstract class that helps to minimize the boilerplate code, when dealing with JSOs and JSON:

import com.google.gwt.core.client.JavaScriptObject;

public abstract class BaseResponse extends JavaScriptObject {
    // You can add some static fields here, like status codes, etc.

    /**
     * Required by {@link JavaScriptObject}
     */
    protected BaseResponse() { }

    /**
     * Uses <code>eval</code> to parse a JSON response from the server
     * 
     * @param responseString the raw string containing the JSON repsonse
     * @return an JavaScriptObject, already cast to an appropriate type
     */
    public static final native <T extends BaseResponse> T getResponse(String responseString) /*-{
        // You should be able to use a safe parser here
        // (like the one from json.org)
        return eval('(' + responseString + ')');
    }-*/;
}

Then you write your actual JSO as such:

import com.example.client.model.User;

public class LoginResponse extends BaseResponse {

    protected LoginResponse() { }

    public final native String getToken() /*-{
        return this.t;
    }-*/;

    public final native int getId() /*-{
        return parseInt(this.u[0]);
    }-*/;

    // ...

    // Helper method for converting this JSO to a POJO
    public final User getUser() {
        return new User(getLogin(), getName(), getLastName());
    }
}

And finally in your code:

// response.getText() contains the JSON string
LoginResponse loginResponse = LoginResponse.getResponse(response.getText());
// ^ no need for a cast \o/

Your JSON looks like this (courtesy of JSONLint, a great JSON validator):

{
    "item": [
        {
            "Id": "1",
            "Name": "Bob"
        },
        {
            "Id": "2",
            "Name": "John"
        },
        {
            "Id": "3",
            "Name": "Bill"
        }
    ]
}

So, I'd write a JSO that describes the items of that list:

public class TestResponse extends BaseResponse {

    protected TestResponse() { }

    public final native String getId() /*-{
        return this.Id;
    }-*/;

    public final native String getName() /*-{
        return this.Name;
    }-*/;

    // Static helper for returning just the list
    // Code untested but you should get the idea ;)
    public static final native JsArray<TestResponse> getTestList(String json) /*-{
        var stuff = eval('(' + json + ')');
            return stuff.item;
    }-*/;
}

Then, in your code you call TestResponse.getTestList(someJsonString) and play around with the JsArray you get (the TestResponses it contains are created automagically). Cool, eh? ;) It might be a bit confusing at first, but believe me, it will make sense once you start using it and it's a lot easier than parsing via JSONParser >_>

like image 113
Igor Klimer Avatar answered Nov 15 '22 18:11

Igor Klimer