Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an ExpandoObject be a list?

I am using Json.Net to deserialize json results into ExpandoObjects using code like the following:

    var converter = new ExpandoObjectConverter();

    dynamic d = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);

This works great for everything except responses that have only a list at the root of the json schema such as this:

string json = @"[{""title"": ""the title""}]";

In the bowels of JsonConvert is this method:

public static T DeserializeObject<T>(string value, params JsonConverter[] converters)
{
    return (T)DeserializeObject(value, typeof(T), converters);
}

The return value DeserializeObject is a properly constructed List<object> with on ExpandoObject in it but the code fails trying to cast that list to an ExpandoObject itself (the value of T).

I've got a failing unit test and could fix this but am not sure how to stuff a List directly into an ExpandoObject.

Is this possible? DynamicObject has TryGetIndex so I assume an ExpandoObject can have an indexer. Would it be necessary to carefully construct a list-like instance of expando by dynamically supplying the implementation of an indexing method or otherwise create a non-expando DynamicObject to hold the list and invoke the correctly methods from dynamic method calls?

like image 899
dkackman Avatar asked Nov 01 '22 22:11

dkackman


1 Answers

DynamicObject has TryGetIndex so I assume an ExpandoObject can have an indexer

No, it can't. Just try it, it returns a RuntimeBinderException:

Cannot apply indexing with [] to an expression of type 'System.Dynamic.ExpandoObject'

An ExpandoObject is not a list; it's more like a dictionary.

Would it be necessary to carefully construct a list-like instance of expando by dynamically supplying the implementation of an indexing method or otherwise create a non-expando DynamicObject to hold the list and invoke the correctly methods from dynamic method calls?

Well, you could create a custom DynamicObject that behaves like a list, but I don't see any benefit in doing that: you might as well use a normal list.

like image 114
Thomas Levesque Avatar answered Nov 13 '22 00:11

Thomas Levesque