using Newtonsoft.Json.Bson; using System.IO; // create a BSON reader from a byte array byte[] bsonData = GetBsonData(); BsonReader reader = new BsonReader(new MemoryStream(bsonData)); // read and parse the BSON data while (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName && reader.Value.ToString() == "name") { reader.Read(); string name = reader.Value.ToString(); Console.WriteLine($"Name: {name}"); } }
using Newtonsoft.Json.Bson; using System.IO; // create a BSON reader from a stream Stream stream = GetBsonStream(); BsonReader reader = new BsonReader(stream); // read and parse the BSON data while (reader.Read()) { if (reader.TokenType == JsonToken.StartObject) { // deserialize the current object into a Person instance Person person = JsonSerializer.DeserializeIn this example, we create a BsonReader from a stream containing BSON data. We then read the data and deserialize each object into a Person instance using the built-in JsonSerializer class. We print out the name and age of each person object.(reader); Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); } }