BsonWriter of Newtonsoft.Json deprecated.
You need to add a new nuget package named Json.NET BSON (just search for newtonsoft.json.bson ) and work with BsonDataWriter and BsonDataReader instead of BsonWriter and BsonReader :
public static string ToBson<T>(T value) { using (MemoryStream ms = new MemoryStream()) using (BsonDataWriter datawriter = new BsonDataWriter(ms)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(datawriter, value); return Convert.ToBase64String(ms.ToArray()); } } public static T FromBson<T>(string base64data) { byte[] data = Convert.FromBase64String(base64data); using (MemoryStream ms = new MemoryStream(data)) using (BsonDataReader reader = new BsonDataReader(ms)) { JsonSerializer serializer = new JsonSerializer(); return serializer.Deserialize<T>(reader); } }
source share