Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (68 sloc) 3.88 KB
{"exec-mode":"default","platform":"MacNet45","uti":"com.xamarin.workbook","packages":[{"id":"Newtonsoft.Json","version":"8.0.3"},{"id":"Xamarin.Forms","version":"2.2.0.31"}]}

Add a Nuget to a Workbook

This workbook shows how to include and use a Nuget package from a workbook. The demo uses Json.NET to serialize and de-serialize a C# class.

  • Add the Json.NET Nuget

  • Create the C# object

  • Serialize & Deserialize using Json.NET

...borrowing some JSON from James Montemagno’s sample.

Add Json.NET

  1. Start by adding a Nugets — choose File > Add Package... to choose Nuget packages to reference.

⚠️ No need to follow these instructions - the package is already referenced by this workbook

and then

For this workbook, Json.NET was chosen, which results in the #r clause being added automatically:

#r "Newtonsoft.Json"

Check the workbook’s source to see how this is persisted (if you’re curious):

{"platform":"iOS","uti":"com.xamarin.workbook","packages":[{"id":"Newtonsoft.Json","version":"8.0.3"}]}

  1. To write some code using the Nuget package, first add a using statement:
using Newtonsoft.Json;

Create C# object

  1. Now setup a C# class and some test data...
public class Monkey
{
	public string Name { get; set; }
	public string Location { get; set; }
	public string Details { get; set; }
	public string Image { get; set; }
	public int Population { get; set; }
}
var monkey1 = new Monkey{Name="Blue Monkey", Location="Central and East Africa"};
var monkey2 = new Monkey{Name="Golden Lion Tamarin", Location="Brazil"};
var monkeyList = new List<Monkey> {monkey1, monkey2};

Serialize to JSON

  1. Using the JsonConvert class from the Nuget package, the C# data can be converted to JSON with a single line of code!
var toJson = JsonConvert.SerializeObject(monkeyList);

Deserialize from JSON

  1. To test deserialization, here is a more complete JSON string (thanks to James Montemagno’s sample here):
var fromJson = @"[{""Name"":""Baboon"",""Location"":""Africa & Asia"",""Details"":""Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae."",""Image"":""http://upload.wikimedia.org/wikipedia/commons/thumb/9/96/Portrait_Of_A_Baboon.jpg/314px-Portrait_Of_A_Baboon.jpg"",""Population"":10000},
{""Name"":""Capuchin Monkey"",""Location"":""Central & South America"",""Details"":""The capuchin monkeys are New World monkeys of the subfamily Cebinae. Prior to 2011, the subfamily contained only a single genus, Cebus."",""Image"":""http://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Capuchin_Costa_Rica.jpg/200px-Capuchin_Costa_Rica.jpg"",""Population"":23000}]";
  1. Converting a JSON string back to C# objects is also a one-liner - and results in a C# collection that can be utilized in app code:
var monkeys = JsonConvert.DeserializeObject<List<Monkey>>(fromJson);

The example above uses a JSON string, but the data could also be loaded from a local file or an internet download (either a file reference or REST web service endpoint).

One More Thing...

To load the JSON string from a remote website, just add System.Net to the workbook

using System.Net;

and then use WebClient to download it before serialization

WebClient client = new WebClient();
var response = client.DownloadData ("https://raw.githubusercontent.com/jamesmontemagno/MonkeysApp-AppIndexing/master/MonkeysApp/monkeydata.json"); // GET
var json = System.Text.Encoding.UTF8.GetString(response);
var monkeys = JsonConvert.DeserializeObject<List<Monkey>>(json);