Version Tolerant Binary Serialization With Microsoft Bond In C#

You’ve been there too. A month ago you wrapped up your first release. You were happy and went for a beer with your workmates. The next month things didn’t go so well. As you start integration testing again, you realize something has gone wrong. You dig down, and discover your app had serialized some useful data to disk not so long ago. Yet, as the new version does its thing, it crashes with a vengeance, when it tries to get that data back from the same place. As you sigh, and realize there won’t be any beer that night, you notice that those serialized bits of data have changed schema between deployments. You think – I do want the app to re-use that data without breaking… So how on Earth do I get around this?

The answer to this is remarkably simple – if your serialization code needs to tolerate versions… Then make it version tolerant!

homer-simpson-woohoo

Now there are many ways of making serialization version tolerant, and they all depend on how exactly you are serializing your data.

A direct approach can be to just JSON.NET-the-lot and go with that. And that’s fine, it works and pays the bills. Happy camping, Homer.

But what if you care about the milliseconds? What if you do want the brute speed and small size of binary serialization… and the benefits of text based version tolerance?

Well that’s a what version tolerant binary serializer is there for. (Again, the answer is so simple, it should be a criminal offense).

Below I’ll show you how to setup and use the Microsoft Bond serializer, so you get the best of both worlds.

JamesBondMartini

My name is Bond… Microsoft Bond. Binary Martini… Shaken, not stirred.

Bond is Microsoft’s equivalent of Google’s Protocol Buffers and they both share the same traits:

  • Some annoying platform independent script compilation (though Visual Studio makes it a lot better) – this is in exchange for…
  • Fast binary (de)serialization by default. Bond uses a layered approach so you can mix and match features. The more you use, the more you pay, but it goes the other way too.
  • Compact binary format.
  • Platform and language agnostic – well kinda.

You can find the full sales pitch here.

Anyway, onwards to the how-to:

Install Bond.CSharp

The first step is to go grab the Bond.CSharp NuGet package.

Screen.1

This package will bring the Bond dependency lot with it and make life easier for you. If for some reason you need to micromanage your dependencies, you can also go with the several other Bond.* packages.

Restart Visual Studio

The Bond Package will activate a new Custom Build option in Visual Studio.
You need to restart the IDE for this to take effect.

Create a Bond File

Now create a dot bond file to hold your serializable types. And don’t shake your head now, stick with me here.

Screen.2

Bond shares the same necessary evil of Protocol Buffers. As it is a platform-independant serializer, you need to specify its types in a platform-independant way. Bond needs to know what types actually are, regardless of the platform in use. One platform may use Big Endian encoding while another uses Little Endian. One platform may encode strings with a length prefix while another platform uses an end marker. Who knows? Yet you still want both platform to speak to each other, even in high speed binary. Both Bond and Protobuf’s answer to this is to use their own uniform types and translate as needed for each different platform.

Set the file’s Build Action to BondCodegen

Screen.3

This will let Bond do some magic in the background and generate C# classes for you that mirror the types you wrote. You can also compile these bond files with the bond compiler, but I won’t dive into that here.

Write Some Code To Save

This will serialize an instance of that SomeBondClass to disk – but you can also send the bytes via the pipe, of course.

Screen.4

Note the lack of the using clauses. That surprised me at first, streams being all IDisposable and all. Not so much with Bond, it cleans after itself. If only my dog did that as well.

Check That File Up

Screen.6

This binary file stands at a measly 15 bytes. Serialize this tiny same class as JSON and you end up with 50 bytes. You are using only 30% of bytes as you would with JSON. Won’t even bother with XML at this point.

Write Some Code To Load

Screen.7

This will read the serialized data back into a class for you. Here’s the result:

Screen.8

As expected.

Mess It Up

Okay, this is all nice but we haven’t made of use of any version tolerance whatsoever. So let’s make it so and break things up.

Introduce a change into your Bond class:

Screen.9

I just added a new property here for show, but do whatever you want.

Read It Back Again

This:

Screen.10

Outputs this:

Screen.11

Which is the behavior you’d get with say, JSON.NET – though for a much smaller file size, which is ideal for piping over or persisting to disk.

That’s It

If you want to dig in further, go check out the project at GibHub or the full manual at GitHub. Because GitHub.

I’m growing fond of Bond over time as another useful tool for implementing real-time systems. Anything that helps grab that extra millisecond helps. I may even start drinking Martini again.

Till next time.

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 )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.