O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Decoding data from BSON using Json.NET

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.

Getting ready

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.

How to do it…

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required