/// <summary> /// Main method - serialize document. Uses ByteWriter /// </summary> public byte[] Serialize(BsonDocument doc) { var count = doc.GetBytesCount(true); var writer = new ByteWriter(count); this.WriteDocument(writer, doc); return(writer.Buffer); }
/// <summary> /// Main method - serialize document. Uses ByteWriter /// </summary> public byte[] Serialize(BsonDocument doc) { var count = doc.GetBytesCount(true); var writer = new ByteWriter(count); this.WriteDocument(writer, doc); return writer.Buffer; }
/// <summary> /// Write a bson document /// </summary> public void WriteDocument(ByteWriter writer, BsonDocument doc) { writer.Write(doc.GetBytesCount(false)); foreach (var key in doc.Keys) { this.WriteElement(writer, key, doc[key] ?? BsonValue.Null); } writer.Write((byte)0x00); }
/// <summary> /// Write a bson document /// </summary> public void WriteDocument(ByteWriter writer, BsonDocument doc) { writer.Write(doc.GetBytesCount(false)); foreach (var key in doc.Keys) { this.WriteElement(writer, key, doc[key] ?? BsonValue.Null); } writer.Write((byte)0x00); }
/// <summary> /// Serialize BsonDocument into a binary array /// </summary> public static byte[] Serialize(BsonDocument doc) { if (doc == null) { throw new ArgumentNullException(nameof(doc)); } var buffer = new byte[doc.GetBytesCount(true)]; using (var writer = new BufferWriter(buffer)) { writer.WriteDocument(doc, false); } return(buffer); }