I have a string in JSON format, and I want to convert it into a BSONDocument for insertion into a LiteDB database. How do I do the conversion? I'm using LiteDB 5.0.0-beta ( I also tested it in LiteDB v4.1.4 ). Here is the code;
MyHolder holder = new MyHolder
{
Json = "{\"title\":\"Hello World\"}"
};
BsonDocument bsonDocument = BsonMapper.Global.ToDocument(holder.Json);
// bsonDocument returns null in v5, and throws exception in v4.1.4
Another example in mongoDB, you can do this ( Convert string into MongoDB BsonDocument );
string json = "{ 'foo' : 'bar' }";
MongoDB.Bson.BsonDocument document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);
What I've also tried so far;
string json = "{ 'foo' : 'bar' }";
byte[] bytes = Encoding.UTF8.GetBytes(json);
BsonDocument bsonDocument = LiteDB.BsonSerializer.Deserialize(bytes); // throws "BSON type not supported".
And also tried;
BsonDocument bsonDocument = BsonMapper.Global.ToDocument(json); // Returns null bsonDocument.