Advanced Cookie Compression With .NET & Ajax.NET
So I have been working on a project where we want to store more information in the cookie. I was trying to figure out the best way to store an object to the cookie and get it back out so it was easy to use. I personally hate the flat nature of a cookie anyway. Cookie dictionaries don’t really provide the level of sofistication some sites need for their cookies. Serialization is great, but it is also very heavy in for the result in XML. I ended up making a very small object, very large. Since I have been working extensively with the new MS Ajax framework, I started looking into JSON and the benefits it could provide for me.
Let’s start with a simple object:
using System; using System.Collections.Generic; using System.Text; namespace JSONCookie.Customer { [Serializable] public class Address { private Guid _id; private string _address1; private string _address2; private string _city; private string _state; private int _postalCode; private DateTime _lastUpdated; public Guid ID { get { return _id; } set { _id = value; } } public string Address1 { get { return _address1; } set { _address1 = value; } } public string Address2 { get { return _address2; } set { _address2 = value; } } public string City { get { return _city; } set { _city = value; } } public string State { get { return _state; } set { _state = value; } } public int PostalCode { get { return _postalCode; } set { _postalCode = value; } } public DateTime LastUpdated { get { return _lastUpdated; } set { _lastUpdated = value; } } } }
As you can see the above is a very simple object with strings, int, Guid, and DateTime fields just to show how the data looks when converted. Now, say you want to store this object into a cookie*. How would we get it to the text level we need for cookie use? There are many articles out there on how to compress a cookie using .NET’s MemoryStram and DeflateStream compression, but all have been showing how to do this with the XML provided back from serialization of an object.
Here is are the two methods I will be comparing:
/// <summary> /// Here we are just doing plain 'ol serialization /// </summary> /// <param name="obj">Object to serialize</param> /// <returns>Serialized object as string</returns> public static string SerializeIt(object obj) { XmlSerializer ObjSerializer = new XmlSerializer(typeof(Customer.Address)); TextWriter XMLObject = new StringWriter(); ObjSerializer.Serialize(XMLObject, obj); return XMLObject.ToString(); } /// <summary> /// Here we take an object and Serialize it into JSON /// </summary> /// <param name="obj">Object to be serialized</param> /// <returns>JSON string</returns> public static string JSONit(object obj) { StringBuilder JSONObject = new StringBuilder(); JavaScriptSerializer JSON = new JavaScriptSerializer(); JSON.Serialize(obj, JSONObject); return JSONObject.ToString(); }
Now, let’s see what the results are:
Serialized
<?xml version="1.0" encoding="utf-16"?> <ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Customer> <ID>83a8e9c8-dc2c-4ba7-a17d-86a7f0d1820b</ID> <Name>Carina</Name> <Blah>Alberto</Blah> <Addresses> <Address> <ID>26c71b32-6e91-4663-b740-81893c28939d</ID> <Address1>4957 Isabela</Address1> <Address2>Apt: 200</Address2> <City>Jaylyn</City> <State>Camden</State> <PostalCode>95638</PostalCode> <Updated>2007-09-28T16:37:26.9334017-07:00</Updated> </Address> <Address> <ID>156e4962-f810-4b7c-be39-6bb07f70989b</ID> <Address1>55114 Josephine</Address1> <Address2>Apt: 625</Address2> <City>Jermaine</City> <State>Konner</State> <PostalCode>31317</PostalCode> <Updated>2007-09-28T16:37:26.9334017-07:00</Updated> </Address> <Address> <ID>5cce9640-95d0-4579-b248-abce62c2225f</ID> <Address1>2940 Lillie</Address1> <Address2>Apt: 208</Address2> <City>Jonas</City> <State>Cael</State> <PostalCode>31946</PostalCode> <Updated>2007-09-28T16:37:26.9334017-07:00</Updated> </Address> <Address> <ID>85655341-4a98-41d8-8cae-155bf902240b</ID> <Address1>80959 Giovani</Address1> <Address2>Apt: 872</Address2> <City>Jade</City> <State>Keshawn</State> <PostalCode>98534</PostalCode> <Updated>2007-09-28T16:37:26.9334017-07:00</Updated> </Address> <Address> <ID>7718ea1c-3282-47b1-9f1f-b546ca905a61</ID> <Address1>76381 Maia</Address1> <Address2>Apt: 367</Address2> <City>Graham</City> <State>Natalie</State> <PostalCode>79905</PostalCode> <Updated>2007-09-28T16:37:26.9334017-07:00</Updated> </Address> </Addresses> </Customer> </ArrayOfCustomer>
JSON’ized
[{"ID":"83a8e9c8-dc2c-4ba7-a17d-86a7f0d1820b","Name":"Carina","Blah":"Alberto","Addresses":[{"ID":"26c71b32-6e91-4663-b740-81893c28939d","Address1":"4957 Isabela","Address2":"Apt: 200","City":"Jaylyn","State":"Camden","PostalCode":95638,"Updated":"\/Date(1191022646933)\/"},{"ID":"156e4962-f810-4b7c-be39-6bb07f70989b","Address1":"55114 Josephine","Address2":"Apt: 625","City":"Jermaine","State":"Konner","PostalCode":31317,"Updated":"\/Date(1191022646933)\/"},{"ID":"5cce9640-95d0-4579-b248-abce62c2225f","Address1":"2940 Lillie","Address2":"Apt: 208","City":"Jonas","State":"Cael","PostalCode":31946,"Updated":"\/Date(1191022646933)\/"},{"ID":"85655341-4a98-41d8-8cae-155bf902240b","Address1":"80959 Giovani","Address2":"Apt: 872","City":"Jade","State":"Keshawn","PostalCode":98534,"Updated":"\/Date(1191022646933)\/"},{"ID":"7718ea1c-3282-47b1-9f1f-b546ca905a61","Address1":"76381 Maia","Address2":"Apt: 367","City":"Graham","State":"Natalie","PostalCode":79905,"Updated":"\/Date(1191022646933)\/"}]}]
The JSON version is a little more difficult to read, but honestly, who cares… that isn’t your job, you use .NET for that. Now this is great, but not really all that helpful. We currently have one customer with 5 addresses and a lot of data… we could just jamb this into a cookie, but that would actually present some more format issues and to fix them we would nee to take up more space. So… let’s compress. I have attached the project for this so you can try it out and see how the compression works. From here… just put it into your cookie. Here are some interesting stats.
| Serialization | Size in KB |
| JSON | 0.98 KB |
| JSON Compressed | 0.84 KB |
| Serialized | 1.96 KB |
| Serialized Compressed | 0.1.08 KB |
* This data is just an easy example of what you can do to cram into a cookie. Customer data is a very bad example as you wouldn’t want to make this information public even if just session based. There are many other applications for this to utilize the power of the client machine vs. relying on IIS to maintain state on your entire application. This is actually a mini db at the client level. Just make sure you realize how mini… 4kb total… or so they say. Still haven’t broken this with slightly larger cookies in most mainstream browsers.
- JSON Cookie Example (.NET 2.0)

on July 21, 2010 at 12:38 am
Permalink
Buy:Tramadol.Viagra Super Active+.Cialis Soft Tabs.Soma.Maxaman.Viagra Super Force.Cialis Super Active+.Super Active ED Pack.Zithromax.Viagra.VPXL.Cialis.Viagra Soft Tabs.Cialis Professional.Propecia.Viagra Professional.Levitra….
on July 21, 2010 at 6:48 am
Permalink
Buy:Prevacid.100% Pure Okinawan Coral Calcium.Retin-A.Human Growth Hormone.Zovirax.Actos.Synthroid.Arimidex.Accutane.Petcam (Metacam) Oral Suspension.Nexium.Mega Hoodia.Zyban.Valtrex.Prednisolone.Lumigan….