Click or drag to resize
Json.NETJson.NET vs Windows.Data.Json
 

Windows 8 introduced a new way to work with JSON via the Windows.Data.Json namespace. Similar to LINQ to JSON in Json.NET, it defines classes that can be used to parse values, strings, objects, and arrays from JSON text or serialize value types into JSON text.

Below is a comparison of Json.NET's LINQ to JSON to Window 8's Windows.Data.Json.

Exclusive Json.NET Features
  • Runs on .NET 2, .NET 3, .NET 4, Silverlight, and Windows Phone 7

  • Dynamic programming

  • Write indented JSON

  • Customize reading and writing JSON with JsonConverters

  • Read and write ISO8601 dates

  • Better LINQ support

Creating JSON

The big difference between the two libraries when creating JSON is Windows.Data.Json requires string/integer/double values to be explicitly converted to JsonValue objects.

Note that there is a weird limitation to creating JSON with Windows.Data.Json: it doesn't allow you to set properties to null or put null values in an array.

Creating JSON with Json.NET and Windows.Data.Json
 1// Windows.Data.Json
 2// -----------------
 3JsonObject jsonObject = new JsonObject
 4  {
 5    {"CPU", JsonValue.CreateStringValue("Intel")},
 6    {
 7      "Drives", new JsonArray
 8                  {
 9                    JsonValue.CreateStringValue("DVD read/writer"),
10                    JsonValue.CreateStringValue("500 gigabyte hard drive")
11                  }
12    }
13  };
14string json1 = jsonObject.Stringify();
15
16// LINQ to JSON
17// ------------
18JObject jObject = new JObject
19  {
20    {"CPU", "Intel"},
21    {
22      "Drives", new JArray
23                  {
24                    "DVD read/writer",
25                    "500 gigabyte hard drive"
26                  }
27    }
28  };
29string json2 = jObject.ToString();
Querying JSON

Windows.Data.Json requires a value to be cast to its exact type with the GetObject/GetArray methods before it can be used, making Windows.Data.Json's code verbose compared to LINQ to JSON.

Querying JSON with Json.NET and Windows.Data.Json
 1string json = @"{
 2  'channel': {
 3    'title': 'James Newton-King',
 4    'link': 'http://james.newtonking.com',
 5    'description': 'James Newton-King's blog.',
 6    'item': [
 7      {
 8        'title': 'Json.NET 1.3 + New license + Now on CodePlex',
 9        'description': 'Annoucing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
10        'link': 'http://james.newtonking.com/projects/json-net.aspx',
11        'category': [
12          'Json.NET',
13          'CodePlex'
14        ]
15      }
16    ]
17  }
18}";
19
20// Windows.Data.Json
21// -----------------
22JsonObject jsonObject = JsonObject.Parse(json);
23string itemTitle1 = jsonObject["channel"].GetObject()["item"].GetArray()[0].GetObject()["title"].GetString();
24
25// LINQ to JSON
26// ------------
27JObject jObject = JObject.Parse(json);
28string itemTitle2 = (string)jObject["channel"]["item"][0]["title"];
Benchmarks

Json.NET is slightly slower at writing JSON than Windows.Data.Json but considerably faster at parsing JSON.

jsonnetwindowsdatajson
Converting Between LINQ to JSON and Windows.Data.Json

Json.NET supports converting between the types of the two libraries. This feature is useful if you are working with another API that uses Windows.Data.Json types but you want to use Json.NET inside your own code.

Use FromObject to convert a Windows.Data.Json value to LINQ to JSON and use ToObject to convert LINQ to JSON to Windows.Data.Json.

Converting between Json.NET and Windows.Data.Json
 1JsonObject jsonObject = new JsonObject
 2  {
 3    {"CPU", JsonValue.CreateStringValue("Intel")},
 4    {"Drives", new JsonArray {
 5        JsonValue.CreateStringValue("DVD read/writer"),
 6        JsonValue.CreateStringValue("500 gigabyte hard drive")
 7      }
 8    }
 9  };
10
11// convert Windows.Data.Json to LINQ to JSON
12JObject o = JObject.FromObject(jsonObject);
13
14// convert LINQ to JSON to Windows.Data.Json
15JArray a = (JArray)o["Drives"];
16JsonArray jsonArray = a.ToObject<JsonArray>();
See Also