Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Metsys.Bson – the BSON Library

Earlier this month I detailed the implementation of the bson serialization we used in Norm – the C# MongoDB driver. I’ve since extracted the serialization/deserialization code and created a standalone project for it – in the hopes that it might prove helpful to someone. If you need an efficient binary protocol to transfer data, look no further.

There are two methods you need to be aware of: Serializer.Serialize and Deserializer.Deserialize.

User u1 = new User{...};
byte[] bytes = Serializer.Serialize(u1);
User u2 = Deserializer.Deserialize<User>(bytes);

You can also configure some behavior:

BsonConfiguration.ForType<User>(t => t.UseAlias(u => u.Id, "_id").Ignore(u => u.FullName).IgnoreIfNull(u => u.Status));

IgnoreIfNull exists because there can be a different meaning to a value assigned to null and no value (something we run into when implementing the Map Reduce functionality of Norm). The other two are hopefully more obvious.

Json.NET also provides a BSON implementation. While it may have more feature (I’m going to assume), a simple test showed Metsys.Bson running about 4 times faster.

You can grab it from: http://github.com/karlseguin/Metsys.Bson

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

2 Responses to Metsys.Bson – the BSON Library

  1. karl says:

    Nope, pretty much same speed as the stuff in NoRM…Protobuf is going to be hard to beat :)

  2. Is this further optimized than the current one in NoRM? I need a fast serializer and while BSON was more flexible, I ended up using protobuf, since it was another 4x faster than NoRM’s BSON serializer for my scenario