2

I would like to write data in Java and read that back via Javascript. Currently I'm using json but due to one huge array (of doubles) this approach is slow (parsing and network).

What is a good alternative? I found massage pack but this seems to be a bit overkill for what I'm trying to do. I would rather use a simple solution like base64 but couldn't make it working on the javascript reading site. Or should I use the "charset=x-user-defined method"? (is it more efficient?)

6
  • 2
    What type of data do you want to send? JSON is usually a good chose, combined with HTTP compression.
    – Bergi
    Nov 4, 2012 at 10:53
  • as stated: a simple json with a very big array of floats/doubles
    – Karussell
    Nov 4, 2012 at 10:55
  • @Bergi what would I have to enable on the Javascript side to read http compressed json?
    – Karussell
    Nov 4, 2012 at 10:57
  • 1
    The browser automatically decompresses.. all you need to do server-side is to compress the response using gzip for instance and then send the header Content-Encoding: gzip to make it work
    – Esailija
    Nov 4, 2012 at 11:35
  • @Esailija thanks. But I would rather not depend on client support. Or at least I would support gzip AND binary data.
    – Karussell
    Nov 4, 2012 at 18:15

2 Answers 2

0

You could try protobufs. There isn't an official implementation for Javascript, but there are (at least) a couple of 3rd-party implementations: Google Protocol Buffers - JavaScript

Protobufs is reputed to be both fast and to have a more compact (non-textual) representation than JSON and XML.

1
  • 1
    thanks. Was also on the radar but is I would prefer an easier or at least more official version.
    – Karussell
    Nov 4, 2012 at 18:19
0

Well, I wasn't satisfied with the existing solutions (either to much overhead or too slow when using simple json). So here, for the records, an html5 solutions via 'arraybuffer'

    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function(e) {
        if (this.status == 200) {
            var dv = new DataView(this.response);
            var json = {
                "info" : {
                    "took" : 0
                },
                "route": {
                    "time": 0, 
                    "distance": 0, 
                    "data" : {}
                }
            };

            var i = 0;
            json.info.took = dv.getFloat32(i);                
            i += 4;
            json.route.distance = dv.getFloat32(i);
            i += 4;
            json.route.time = dv.getInt32(i);
            i += 4;
            var locations = dv.getInt32(i);
            var tmpArray = [];
            json.route.data = {
                "type" : "LineString",
                "coordinates": tmpArray
            };
            for(var index = 0; index < locations; index ++) {
                i += 4;
                var lat = dv.getFloat32(i);
                i += 4;
                var lng = dv.getFloat32(i);
                tmpArray.push([lng, lat]);
            }            
            callback(json);
        } else
            errCallback(e);
    };
    xhr.send();

To make this working with the cross origin policy you need to enable it on the server (response.setHeader("Access-Control-Allow-Origin", "*")) and on the client (jQuery.support.cors = true). Here the full working example with a simple Java servlet using DataOutputStream. As alternative there is the json fallback.

An alternative approach would be to encode all the numbers into a string and put this into a json (via base64).

Another interesting project I stumpled upon was http://binaryjs.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.