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?)

Community
  • 1
  • 1
Karussell
  • 17,085
  • 16
  • 97
  • 197

2 Answers2

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.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
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

Karussell
  • 17,085
  • 16
  • 97
  • 197