1

I have a .NET Web Method (C#) which accepts a JSON object. JQuery code on the client side creates the JSON data and posts it to the web method using Ajax.

It works, but the data set can potentially be very large.

I'm trying to find a way of compressing the JSON string in javascript and then decompress it back to the original JSON in the web method. A reason JSON would be a good candidate for compression (as I understand it) is the constant repetition of property names throughout the data.

How can I acheive JSON compression/decompression?

Matt
  • 195
  • 1
  • 4
  • 12

1 Answers1

3

I think you should leave compression for IIS to do. In applicationhost.config you can define:

<system.webServer>
<urlCompression doDynamicCompression="true" />
<httpCompression>
  <dynamicTypes>
    <add mimeType="application/json" enabled="true" />
    <add mimeType="application/json; charset=utf-8" enabled="true" />        
  </dynamicTypes>
</httpCompression>
</system.webServer>

You can read more here: http://www.west-wind.com/weblog/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x

On the other hand, you may use more compact format - like https://code.google.com/p/protobuf-js/ or http://www.servicestack.net/mythz_blog/?p=176.

Update: As long as question author needs requests to be compressed, that is not possible to do using default browser capabilities (Why can't browser send gzip request?). Anyway, there's lz4 compression algorithm, which is very fast and has implementations in many platforms (javascript and c# included), that may be a solution, especially because someone benchmarked it and it worked faster than protobuff (I think that was end to end test, can't find details at the moment).

Community
  • 1
  • 1
Giedrius
  • 8,430
  • 6
  • 50
  • 91
  • Just to clarify - I am talking about compressing the JSON string from the browser to post to the web method, I'm not talking about compressing the response from the web method. Bearing this in mind, is your answer still relevant? – Matt Jun 06 '13 at 12:43
  • It appears indeed that your answer is incorrect. IIS will not compress _requests_ initiated by javascript. IIS compresses _responses_. Thanks anyway. – Matt Jun 07 '13 at 08:12
  • @Matt - I've updated my answer, check if it fits what you needs :) – Giedrius Jun 07 '13 at 08:24