Click or drag to resize
Json.NETSerialize to BSON
 

This sample serializes an object to BSON.

Sample
Types
1public class Event
2{
3    public string Name { get; set; }
4    public DateTime StartDate { get; set; }
5}
Usage
 1Event e = new Event
 2{
 3    Name = "Movie Premiere",
 4    StartDate = new DateTime(2013, 1, 22, 20, 30, 0, DateTimeKind.Utc)
 5};
 6
 7MemoryStream ms = new MemoryStream();
 8using (BsonWriter writer = new BsonWriter(ms))
 9{
10    JsonSerializer serializer = new JsonSerializer();
11    serializer.Serialize(writer, e);
12}
13
14string data = Convert.ToBase64String(ms.ToArray());
15
16Console.WriteLine(data);
17// MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAED982M8AQAAAA==