Hi everybody.

I'm building an site that will load a bunch of overlays into Google Maps. The overlays are static and already in JSON format to be loaded with AJAX.
This all works fine, ajax request, json parse, adding overlay.

But the json files are quite big, the largest has almost 5mb. So I Gziped it, and the size drop to about 2mb.

The problem is loading the GZip file with AJAX. I can't make it work.

For what I readed, the browser that should decompress the gzip file, but it ain't happening.
So I tried to use a function that I found on the web, but it ain't working neither.

This is my code till now:

    function testGZip() {
        $.ajax({
            url: "Feijo.json.gz",
            success: function (result) {
                var decoded = lzw_decode(result);
                var object = JSON.parse(str);
            }
        });
    }

    // Decompress an LZW-encoded string
    // Code From http://jsolait.net/
    function lzw_decode(s) {
        var dict = {};
        var data = (s + "").split("");
        var currChar = data[0];
        var oldPhrase = currChar;
        var out = [currChar];
        var code = 256;
        var phrase;
        for (var i = 1; i < data.length; i++) {
            var currCode = data[i].charCodeAt(0);
            if (currCode < 256) {
                phrase = data[i];
            }
            else {
                phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
            }
            out.push(phrase);
            currChar = phrase.charAt(0);
            dict[code] = oldPhrase + currChar;
            code++;
            oldPhrase = phrase;
        }
        return out.join("");
    }

The Gzip file it's avaiable here if anyone wants to try it: http://186.202.61.3/ale/Feijo.json.gz

I'm using third-part libs:

  • Jquery 1.7
  • JSON 2

Obs.: The problem is not with the JSON, it's decoding the GZip.

Any help is appreciated.

Thanks.

Recommended Answers

All 6 Replies

Have you tried this code with something much simpler? This snippet is mentioned on SO, the drawbacks have to do with some unicode characters apparently.

No, I didn't. Since those are the files that I need to make it work, I gone right into it.

What did you mean by SO, sorry but I didn't undestand the abreviation.

Any way, I'll make some tests with simple text and post back the results.

Oh right, now I get it.

So, I did a couple of tests using simple text files and none worked. I try letting the broswer do the job, tried with the code posted and also with a lib called JSXCompressor.

So I'm thinking that the problem may be in the gzip file. I'm currently ziping with GZip for windows.

Maybe I should implement something in the server side to zip the files instead of using a third part software.

Any thoughts on good compression algorithms?

Thanks, I'll study this guy and try to implement it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.