I have a string that contains a JSON. The only thing I know about this JSON is that it is valid. How to turn this string into BSON?
6 Answers
The BsonWriter
of Newtonsoft.Json
is obsolete.
You need to add a new nuget-package called 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);
}
}

- 5,549
- 7
- 49
- 94
-
I am looking at this link when BSON was first added to Newtonsoft.Json. [link](http://james.newtonking.com/archive/2009/12/26/json-net-3-5-release-6-binary-json-bson-support). _BSON stores binary data directly, avoiding the time overhead and the additional size of base64 encoded text that regular JSON has with binary data._ So what is the need for the base64 to and from conversions? I'm not disagreeing, I have seen these in other code examples as well, I am just confused. – David Wallace May 04 '18 at 13:33
-
Looking at this again, I think I can answer my own question. Precisely because BSON stores binary data directly, anything encoded in BSON format which contains binary data needs to be converted to base64 before it is safe to transmit over IP, whereas Json is completely character based and does not need this. However the claim that BSON avoids the time overhead and additional size of base64 seems a bit disingenuous, given that you will need to do this conversion anyway, further down the line. – David Wallace May 04 '18 at 16:05
-
2Surely IP copes fine with binary data, and it's only http that wants the Base64 encoding? – Chris F Carroll Aug 04 '22 at 09:21
while using json
in my project i noticed that there are simple and sweet way to convert json
into a bson
document.
string json = "{\"Name\":\"Movie Premiere\"}";
BsonDocument document = BsonDocument.Parse(json);
now you can use document
as bson anywhere.
Note- I am using this document
to insert into MongoDb
database.
Hoping this will help you.

- 5,407
- 6
- 41
- 42
-
1But I believe that approach is not using JSON.net, rather libraries from MongoDB. – shytikov Dec 18 '18 at 14:49
Behold! There is a much simpler way to do this:
BsonDocument doc = BsonDocument.Parse("{\"your\": \"json\", \"string\": \"here\"}");

- 5,366
- 30
- 38
-
If anyone figures out how to use this parse on an array that would be neat – Vincent Buscarello Oct 09 '18 at 19:04
-
2
-
That helps. Was hoping for something that could take an array OR object and dynamically know what to do with it. We've ended up spinning our own but if this is in this lib and someone knows about it would love to hear. – Vincent Buscarello Oct 09 '18 at 22:07
I think this will do the trick for you
MongoDB.Bson.BsonDocument BSONDoc
= MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);
You can also have a look on Serialize to BSON and C# - Converting JSON string to BSON document

- 13,723
- 6
- 34
- 69
https://www.nuget.org/packages/Newtonsoft.Json
PM> Install-Package Newtonsoft.Json -Version 7.0.1
using Newtonsoft.Json.Bson;
using Newtonsoft.Json;
class Program
{
public class TestClass
{
public string Name { get; set; }
}
static void Main(string[] args)
{
string jsonString = "{\"Name\":\"Movie Premiere\"}";
var jsonObj = JsonConvert.DeserializeObject(jsonString);
MemoryStream ms = new MemoryStream();
using (BsonWriter writer = new BsonWriter(ms))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, jsonObj);
}
string data = Convert.ToBase64String(ms.ToArray());
Console.WriteLine(data);
}
}

- 2,596
- 2
- 23
- 54
-
1Update for Users possibly want to use this: `BsonWriter` is obsolete, check my answer – Matthias Burger Jul 11 '17 at 08:35
Just another alternative, with the https://www.nuget.org/packages/MongoDB.Bson package, after using MongoDB.Bson;
, you can just write byte[] result = someObj.ToBson();
See Deserializing from BsonDocument to string and serializing back to BsonDocument

- 355
- 1
- 9