This project has moved and is read-only. For the latest updates, please go here.

implementing IDictionary<string,object> and IList<object>

Feb 18, 2011 at 4:23 PM

At the moment JObject implements IDictionary<string, JToken> and JArray implements IList<JToken>.

I think it would be great addition if JObject could implement IDictionary<string,object> and JArray implment IList<object>.

var result = (IDictionary<string,object>)jsonObject;
var name = (string)result["name"];
var id = (int)result["id"];
var languages = (IList<object>)result["languages"];
foreach(var lang in langauges){
   var l = (string)lang;

This would allow the users to use the core system library and not care about how to use JObject or JArray. It keeps the code transparent from Newtonsoft.Json library.

At the moment we need to manually convert JObject and JArray to IDictionary<string,object> and IList<JToken>. We do this by calling the method private static object ConvertJTokenToDictionary(JToken token)

Or might have to modify JContainer to implement IList<object> and JToken to have a new indexer public virtual object this[string key]

Apr 8, 2011 at 10:25 PM
Edited Apr 8, 2011 at 10:26 PM

+1. Why isn't Json.NET as easy to use as the JavaScriptSerializer? All I want to do is read in data using 

var serializer = new JavaScriptSerializer();
var map = serializer.Deserialize<IDictionary<string, object>>(json);

Unfortunately, the JavaScriptSerializer doesn't support nicely-formatted output (please, please correct me if I'm wrong) like Json.NET does.

Apr 21, 2011 at 7:37 PM

And, whats the problem with dynamics?

With the current build you can do:

dynamic result = jsonObject;
var name = (string);
var id = (int);
var languages = (IList<object>)result.languages;
foreach(var lang in langauges){
   var l = (string)lang;