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