简体   繁体   中英

Compress JSON message on server and decompress in client side

I send Json messages to a service which republish these messages to our users. The problem is that some of my messages are bigger than the maximum limit of the allowed message size, so I wondered if I could apply any kind of compression to my messages then decompress them in the client side.

First I tried Gzip in C#, but it seems so hard to decompress using JavaScript.

Some other voices tell to try LZMA and Zlip.

Am I on the right way, or I should think in a different way.

I found a solution, It succeed to decompress a compressed text using both 'C# and PHP'. Zlib is used for compression.

I get The solution from JSXCompressor, you can download this example : http://jsxgraph.uni-bayreuth.de/distrib/jsxcompressor.zip see testhelloworld.php

In PHP, The compression had been done using gzcompress then the compressed output had been encoded using base64_encode.

$x = 'Some text or json';
$compressed = base64_encode(gzcompress($x, 9)); // 9 could be from 1 to 9
// echo $compressed;
file_put_contents('compressed.txt', $compressed);

For decompression :

$.ajax('compressed.txt').done(function (res) {

    console.info(JXG.decompress(res));
});

On possibility would be use BSON which is a binary encoding of JSON data.

http://bsonspec.org/

On the client side there are a few libraries for encoding/decoding. One comes with Mongo DB: https://github.com/mongodb/js-bson

On the server side JSON.net supports BSON serialization/deserializtion: http://james.newtonking.com/archive/2009/12/26/json-net-3-5-release-6-binary-json-bson-support

This isn't compression exactly. Just a more compact representation of the JSON data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM