Potty Little Details

Just another WordPress.com weblog

LosFormatter: The Missing Serializer

leave a comment »

The limited object serialization (LOS) formatter is designed for highly compact ASCII format serialization. This class supports serializing any object graph, but is optimized for those containing strings, arrays, and hashtables. It offers second order optimization for many of the .NET primitive types.This is a private format, and needs to remain consistent only for the lifetime of a Web request. You are not allowed to persist objects serialized with this formatter for any significant length of time.The LosFormatter fills in a gap between the verbose XmlSerializer and the terse BinaryFormatter. You can think of the LosFormatter as a BinaryFormatter “light” that is optimized for objects containing very simple types. It’s also a nice convenience that LosFormatter creates an ASCII string representation of your object graph.
Serializing an object with the LosFormatter
string LosSerializeObject(object obj)
System.Web.UI.LosFormatter los = new System.Web.UI.LosFormatter();
StringWriter writer = new StringWriter();
los.Serialize(writer, obj);
return writer.ToString();
The LosFormatter creates an interestingly formatted string just before Base64 encodes it. For example, the code


looks like this when decoded:


This indicates an integer with the value 5. It’s not XML even though it uses angle brackets—it’s just an encoding schema. You may think it is odd to use more bytes after Base64 encoding, but remember that the “==” at the end of a Base64-encoded string is a standard suffix. Additionally, the overhead would be less if the value to be encoded were longer. Usually you won’t need to use the LosFormatter, but it’s good to know it’s available in your Toolbox.

Put an integer value in ViewState programmatically as in the following code. You’ll be storing this integer within ViewState as the page loads. The ASP.NET subsystem will serialize everything within ViewState as the page renders. That means that the number 5 in this example will be serialized into the _VIEWSTATE hidden form field. We’ll then use an inspection utility to examine the contents of the hidden form field and see what it holds.

private void Page_Load(object sender, System.EventArgs e)
ViewState[“example”] = 5;

object RetrieveObjectFromViewState( string serializedObject)
System.Web.UI.LosFormatter los = new System.Web.UI.LosFormatter();
return los.Deserialize(serializedObject);


Written by oneil

September 9, 2008 at 4:25 pm

Posted in ASP DOT NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: