Comparison of serialization in .Net

A brief comparison of available serialization techniques

Experience and data from practical use in enterprise applications

Here we’ll see a comparative study of file size and processing time to serialize some typical data to different serialization formats, and to compress (zip) the same.

Serializations techniques that we’ll see are:

  1. XML
  2. JSON
  3. Binary Formatter
  4. Protocol Buffers

Please note this is the default compression ratio used in .Net SharpZipLib. The compression factor can be changed and the size/performance will based vary on that. Here, I have not included the real data, but for understanding - it was a big fat C# object with thousands of properties/collections with more nested data in multiple levels.

  Size of data(KB) Serialization time(ms) Zipped file size(KB) Compression time(ms)
JSON 12460 1100 205 1200
XML 19755 600 259 2000
BinFor 8209 770 1064 3800
ProtBuf 5347 155 205 620

Now, before making any judgement, here are few important points to consider:

NOTE #1: Not all serializations are equally code friendly. What I mean here, some of them does not need any changes in the actual class for them to work, while some of them needs lot of work.

  • JSON: No customization needed as such
  • XML: Based on the nature of data and desired format of output XML, it may need some XML specific annotations. Also, there are constraints like, Dictionary<TKey, TData> doesn’t work out of the box.
  • Protocol Buffer: It’s mandatory to decorate the class and each property with annotation and ProtoMember unique integer.
  • Binary Formatter: All the classes involved, needs a Serializable attribute.

NOTE #2: Consider how platform agnostic they are how easily usable across all major technology stack.

  • XML is pretty much platform independent.
  • JSON is also similarly platform independent. Also, JSON being JavaScript native, supported in all browsers and web.
  • ProtoBuf also has pretty good platform support, though I didn’t test it cross-platform myself.
  • Binary Formatting works only with .Net. Though now .Net is making its way through all major platforms (Windows, Linux, MacOS) with .NetCore, I’m not sure how much Binary Formatter is supported.

NOTE #3: Readability
JSON > XML > Binary Format, Protobuf (not human readable)

Conclusion (IMHO)

So, we can see ProtoBuf wins easily on performance. But a big overhead is attribute annotation needed for each property of each class that needs to be serialized.

On the other hand JSON, though not the most performant, wins on ease of use, readability & wide platform acceptance.

I’ll be posting sample code that I used for all the serialization and file compression. Comeback for the code snippets soon.

comments powered by Disqus