开发者

Consuming a JSON service with Groovy to return List<Object>

Forgive what is a totally noob question - I was evaluating a problem today, and thought, "this is probably the kind of thing that Groovy is really really good at".

I am trying to build a quick POC that demonstrates the benefits of serializing an object over AMF as opposed to JSON. (I'm aware of the benchmark studies that are available, but my client wants more proof).

There is an existing JSON service, which ideally I'd like to call from within a Java service layer, consume the JSON objects, and return them as a List<Object> back to a flex client, using BlazeDS.

I'm not sure how well BlazeDS would play with exposing a groovy object as a messaging endpoint, so I figure I might need to wrap my service in a Java wrapper, to keep things simple.

Ie., I'm thinking it would look something along the lines of...

public class JSONService {
    // Probably something Groovy 
    IJSONDelegate jsonDelegate;

开发者_如何学C    public JSONService(IJSONDelegate jsonDelegate)
    {
         this.jsonDelegate = jsonDelegate
    }

    public Object loadJSON(String url)
    {
        return jsonDelegate.loadJSON(url);
    }
}

Given that Groovy is a dynamic language, is this a suitable approach?

Ie., can Groovy deserialize some JSON text into a Groovy Object, which can then be passed around through Java classes?

Are there likely to be any issues in serialization of this class?


You would need a library called json-lib, theres a GroovyJsonBuilder and parses JSON to a POJO. For the dynamic groovy objects you would need to use the Expando class.

http://json-lib.sourceforge.net/


I'm not sure how to do this with Groovy, but you could just reuse my CensusServiceServlet. It simply exposes a dataset through various serialization options (XML, JSON, and AMF3).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜