Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • JavaScript • Angular
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

LosFormatter for easy Serialization

On this page:

Ah, the ASP.NET LosFormatter – it’s a wonderful thing. I’m working on this resource provider and one of the things it has to do is deal with resources other than strings. There are a examples in several places that do a really half assed job of dealing with object resources that either don’t work or are horrendously complex.


But using the LosFormatter is a great way to do this easily with just a few lines of code. The really nice thing about the LosFormatter is that it encodes the type information right into the formatted output. So you don’t need to deal with figuring out exactly what type the object is and instantiating it. The LosFormatter takes care of this…


So to serialize a value like a Bitmap code like this can be used:


LosFormatter output = new LosFormatter();

StringWriter writer = new StringWriter();

output.Serialize(writer, Value);

Value = writer.ToString();


The LosFormatter formats the output into a Hex string, which is perfect for storage in the database as well even if it’s a bit more verbose than the equivalent binary code that the plain old Binary Serializer would produce.


To get the value back out is even easier:


string StringValue = reader["Value"] as string;


LosFormatter Formatter = new LosFormatter();

object Result = Formatter.Deserialize( StringValue );

return Result;


One of the reasons I like this approach over the binary serialization is that all code can now run pretty much a single datapath to get the data out of the database rather than having to check whether it’s dealing with strings or ‘other types’. This greatly reduces the complexity of the code for the various read and write operations.


There’s a caveat with this though just as there is with binary serialization: It requires full trust or that the assembly is in the GAC. I’ve taken to running my Web apps in dev mode in Medium trust to see where things go wrong and this was one of them.


Incidentally Serialization is one of the big problems for medium trust applications – I have lots of places where I use Serialization for persistence in various places and it all falls in Medium Trust. Not just in Web apps – Serialization is also severely restricted in low rights desktop apps. Bummer…

Posted in ASP.NET  

The Voices of Reason


# DotNetSlackers: LosFormatter for easy Serialization

Andreas Kraus
October 14, 2006

# re: LosFormatter for easy Serialization

Thanks for the tip!

Rick Strahl
October 15, 2006

# re: LosFormatter for easy Serialization

Just as a side note: If you don't need string serialization then using the binary formatter may be faster. The LosFormatter can optimize certain types by using Type Converters for storage vs. serializing which is generally more expensive. However, if you're dealing with complex types and custom types of your own chances are that the LosFormatter will use serialization anyway.

Anyway you can also do binary serialization pretty easily with the following code:

public static byte[] Serialize(object value)
    if (value == null)
        return null;

    byte[] inMemoryBytes;
    using (MemoryStream inMemoryData = new MemoryStream())
        new BinaryFormatter().Serialize(inMemoryData, value);
        inMemoryBytes = inMemoryData.ToArray();

    return inMemoryBytes;
public static object Deserialize(byte[] serializedObject)
    if (serializedObject == null)
        return null;

    using (MemoryStream dataInMemory = new MemoryStream(serializedObject))
        return new BinaryFormatter().Deserialize(dataInMemory);

# A Continuous Learner's Weblog: Links (10/15/2006)

September 03, 2007

# re: LosFormatter for easy Serialization

Duh, why haven't I realized this before?

Thanks for the tip, this was just what I was looking for!

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2022