Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • DataTable (JavaScript)
  • DataTable (Java)

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?

like image 842
lmazgon Avatar asked Nov 05 '22 05:11

lmazgon


1 Answers

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

like image 81
prototype Avatar answered Nov 07 '22 21:11

prototype