lmazgon
lmazgon

Reputation: 1254

Passing a DataTable object from JavaScript to Java

I am using the Google Visualization API on the client side and I create a DataTable object. Then I want to pass it to my server and upload it via the Spreadsheet API to a spreadsheet. Probably the best way is to use JSON, so I converted it with the method toJSON() and sent it over POST to my server. I tried to use these 2 classes:

Now I noticed, that these 2 classes aren't compatible, at least not over JSON. The JavaScript class converts for example to this:

{"cols":[
         {"id":"Col1","label":"","type":"string"}
         {"id":"Col2","label":"","type":"date"}
        ],
 "rows":[
         {"c":[{"v":"a"},{"v":"Date(2010,10,6)"}]},
         {"c":[{"v":"b"},{"v":"Date(2010,10,7)"}]}
        ]
}

But the Java side DataTable has different names for the parameters, and I am using Gson which has different type values:

cols -> columns
c -> cells
v -> value

type:"string" -> type:"TEXT"
type:"number" -> type:"NUMBER"

And I am afraid that there are even more incompatibilities.

So.. how can I convert the JavaScript DataTable to the Java object DataTable?

Upvotes: 5

Views: 3164

Answers (2)

prototype
prototype

Reputation: 7970

I ran into the same problem in reverse. It appears that the DataTable object in the Java Datasource Library is not parallel to the Javascript DataTable object in the Google Visualization API.

Returning a Java Datasource Library DataTable object requires using the JsonRenderer, rather than the default serialization. And it appears only to work passing from the server to the client. Am not sure if it can be done the other direction.

@WebService
@Path("/tables")
public class DataManager extends GenericManager<db, Long> {

    @Path("/hello/")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public DataTable getDataTable() {
        DataTable data = new DataTable();
        ... populate object ...
        return data;
    } 

However, the Java DataTable object returned by default serialization is not the same thing as the Google Visualization API javascript DataTable. You can't pass it to a GVis chart.

Instead, from Java, you use the JsonRenderer class (see this Google Groups email) to convert it to a Json-like string that's missing quotes around attributes for modest compression.

    @Path("/hello/")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getDataTable() {
        DataTable data = new DataTable();
        CharSequence charSequence = JsonRenderer.renderDataTable(dataTable, true, true);
        return charSequence.toString();
    } 

That string can be parsed in Javascript by surrounding with parentheses, not shown in the object literal notation in the examples (see this Google Group forum):

jQuery.ajax({
    context: this,
    type: 'Get',
    url: url,
    success: function(data) {
         var args = eval('('+data+')');  // add parens around the returned string                          
         var dataTable = new google.visualization.DataTable(args);  
         ...
 });

I don't see a method for going the reverse way to the Java Datasource Library DataTable object. So not quite an answer but you're not alone

Upvotes: 2

&#220;mit
&#220;mit

Reputation: 17489

Well I am using python on the backend and GWT on the frontend and passing a DataTable from the backend to the frontend works without any problems. I am using the google-visualization-python api on the backend to create the DataTable.
Parsing is done with following code:

DataTable dataTable = DataTable.create(JSONParser.parseLenient(data).isObject().getJavaScriptObject());

I also convert the parsed DataTable back to JSON to store the json string in localStorage and parsing the stored json string also works fine.

The GWT DataTable is just a simple wrapper which ultimately just calls the function of the underlying Javascript DataTable via JSNI.So I don't see any reason why they should be incompatible.

Make sure you use the latest gwt-visualization API (1.1.2) ?

Upvotes: 0

Related Questions