Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 Json.NET to encode BSON is fairly simple, once you have a class you want to encode:

public class Record {
  public string Callsign { get; set; }
  public double Lat { get; set; }
  public double Lng { get; set; }
} 
…
var r = new Record {
  Callsign...