Encoding data as BSON using Json.NET

BSON encoding is a reasonable alternative to JSON if you have an implementation of an encoder and decoder on each side of the connection. Unfortunately, there's no good encoder and decoder available yet for JavaScript, but there are implementations for a number of other platforms, including .NET and C++. Let's look at how to encode a class using BSON with Json.NET in C#.

Getting ready

First, you'll need to have the Json.NET assembly available to your application. As you saw in the last chapter, in the recipe How to deserialize an object using Json.NET, the easiest way to do this is with NuGet. If you haven't already, add the Json.NET assembly to your solution using the steps in that recipe.

How to do it…

Using ...

Get JavaScript JSON Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.