This project has moved. For the latest updates, please go here.


Serialize Guid as binary field with subtype UUID (0x04) when using BsonWriter


In version 4.5, using BsonWriter results in having a Guid written as a string.This is wrong, as per the specification: type should be binary (\0x05) with a subtype of (\0x04) which is for a UUID, which by definition ( is:A UUID is a 16-octet (128-bit) number...The same as a Guid in .NET).See the following example:[TestMethod]public void TestMethod1(){ var source = new { myUuid = Guid.NewGuid(), }; using (var buffer = new MemoryStream()) using (var writer = new BsonWriter(buffer)) { var serializer = new JsonSerializer(); serializer.Serialize(writer, source); byte[] myBson = buffer.ToArray(); }}Note myUuid is written as a string at index 12 on.This was discovered on Stack Overflow: part that needs to be changed on BsonWriter (and the BsonReader will possibly have to be updated as well) is: public override void WriteValue(Guid value) { base.WriteValue(value); this.AddToken((BsonToken) new BsonString((object) value.ToString(), true)); }
Closed Apr 7, 2013 at 3:14 AM by JamesNK