June 2015
Intermediate to advanced
192 pages
4h 8m
English
Using Json.NET, decoding BSON is the opposite of encoding; given a class that describes the data to decode and a blob of binary data, invoke a reader to read the data.
Of course, you need a reference to the Json.NET assembly in your project in order to do this. See recipe How to Deserialize an object using Json.NET in Chapter 7, Using JSON in a Type-safe Manner, to learn how to add a reference to Json.NET in your application using NuGet.
Starting with a stream, you'll use a BsonReader with a JsonSerializer to deserialize the BSON. Assuming data is byte[] of BSON data:
MemoryStream ms = new MemoryStream(data); using (var reader = new Newtonsoft.Json.Bson.BsonReader(ms)) { var serializer ...Read now
Unlock full access