Click or drag to resize
Json.NET

Serialize to BSON

 

This sample serializes an object to BSON.

Sample
Types
public class Event
{
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
}
Usage
Event e = new Event
{
    Name = "Movie Premiere",
    StartDate = new DateTime(2013, 1, 22, 20, 30, 0, DateTimeKind.Utc)
};

MemoryStream ms = new MemoryStream();
using (BsonWriter writer = new BsonWriter(ms))
{
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(writer, e);
}

string data = Convert.ToBase64String(ms.ToArray());

Console.WriteLine(data);
// MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAED982M8AQAAAA==