Archive for the ‘JSON’ Category

BISON Critique

February 14, 2007

Via Ajaxian I got to BISON by Kai J├Ąger. I’ll ignore the fact that it’s actually quite useless, and also contradicts with an existing GNU utility, and refer to some of the implementation details.

First of all, the writer complains that he was not able to send data with null characters, and also that the data was always converted to UTF-8, which is problematic for binary data. To solve the problem of the null character he uses yEnc, and as for the UTF-8 problem, he simply says it will probably make the BISON data longer (sometimes even longer then the JSON equivalent), which renders the entire effort only useful as a JavaScript exercise.

However, AJAX is certainly capable of sending binary data to the server. For example, Gmail does it when you add an attachment. The attachment is a binary data that can contain null characters. Of course, Gmail might also be using some encoding, but on the other hand, Gmail, being a web app, have no access to the file data until it is passed to the server. I guess Gmail (and everyone else that wants to upload binary data using AJAX) is simply setting the correct header:

xmlHttp.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);

This should solve both problem, as the send data will be encoded in the correct encoding.

BTW, during the encoding discussion he mentions that he chose yEnc since base64 encoding “can sometimes be twice the size of the original unencoded message”. Anyone who knows how base64 encoding works knows that this is almost never true, and as Wikipedia says, “the actual length of MIME-compliant base64-encoded binary data is usually about 137% of the original data length, though for very short messages the overhead can be a lot higher.

I had a look in the JavaScript code itself, and the first thing that came to me was his extensions to the String.prototype object which are done in quite a clumsy way, and can be done a lot nicer (and I think more efficiently) using array trickery.

For example:

String.prototype.repeat = function(times) {
    var repeatedStr = “”
    for (var i = 0; i < times; i++) {
        repeatedStr += this;
    }
    return repeatedStr;
}

can be written as

String.prototype.repeat = function(times) {
    return new Array(times+1).join(this);
}

and this

String.prototype.reverse = function() {
    var reversedString = “”
    for (var i = this.length – 1; i >= 0; i–) {
        reversedString += this.charAt(i);
    }
    return reversedString;
}

can be written as

String.prototype.reverse = function() {
    return this.split(“”).reverse().join(“”)
}

 

That’s all for now. I still need to look at the rest of the code to see what it does. I also want to have a look at yEnc – it looks interesting.

Advertisements