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

Feb 18, 2011 at 3: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>. http://facebooksdk.codeplex.com/SourceControl/changeset/view/9c6a1cbc9914#Source%2fFacebook%2fJsonSerializer.cs 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 9:25 PM
Edited Apr 8, 2011 at 9: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 6:37 PM

And, whats the problem with dynamics?

With the current build you can do:

dynamic result = 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;
}