Turn ExpandoObject into static type

As many others have shown, the new ExpandoObject introduced in C# 4 can be pretty useful in some scenarios.

However, on the odd occasion when you want to convert an ExpandoObject to a static type you have defined you can be forgiven for feeling a little lost as there are no well documented ways to do that. At least that’s how I felt, until I found out about the ConvertToType methods on the JavaScriptSerializer!

JavaScriptSerializer

The JavaScriptSerializer, like the DataContractJsonSerializer class, lets you serialize and deserialize objects to and from JSON string. But unlike the DataContractJsonSerializer class, you are not confined to working with types that are marked with the DataContract attribute.

One of the deserialization methods on the JavaScriptSerializer class – DeserializeObject – lets you a JSON string to an object graph, i.e. a Dictionary<string, object> object where the key is the name of the property and the value is the property value. And you can use one of the overloaded ConvertToType methods to convert this dictionary to a static type of your choice, provided that the type has a parameterless constructor (which unfortunately, means you won’t be able to convert an ExpandoObject to an anonymous type).

ExpandoObject as IDictionary<string, object>

Why is this important? It’s because the ExpandoObject implements the IDictionary<string, object> interface and therefore can be used with the ConvertToType methods!

Here’s what you do:

   1: // first you need to instantiate the serializer

   2: var jsSerializer = new JavaScriptSerializer();

   3:

   4: // get an expando object and convert it to an instance of MyClass

   5: var expando = GetExpandoObject();

   6: var obj = jsSerializer.ConvertToType<MyClass>(expando);

   7:

   8: Console.WriteLine(obj.Id);      // 0db9d9a6-8c7e-4bea-82a8-4d5641c7c0de

   9: Console.WriteLine(obj.Name);    // Yan

  10: Console.WriteLine(obj.Age);     // 29

  11:

  12: ...

  13:

  14: public ExpandoObject GetExpandoObject()

  15: {

  16:     dynamic expando = new ExpandoObject();

  17:     expando.Id = Guid.NewGuid();

  18:     expando.Name = "Yan";

  19:     expando.Age = 29;

  20:

  21:     return expando;

  22: }

  23:

  24: public class MyClass

  25: {

  26:     public Guid Id { get; set; }

  27:

  28:     public string Name { get; set; }

  29:

  30:     public int Age { get; set; }

  31: }

Pretty cool, eh? ;-)

But what if the shape of the expando object doesn’t match your type? E.g. there are properties defined on MyClass but not on the expando object, or vice versa, or both? Well, it’s smart enough to work that out itself and only set the properties which it is able to set:

   1: // same as before

   2:

   3: Console.WriteLine(obj.Id);          // 0db9d9a6-8c7e-4bea-82a8-4d5641c7c0de

   4: Console.WriteLine(obj.Name);        // null

   5: Console.WriteLine(obj.Age);         // 29

   6: Console.WriteLine(obj.NickName);    // null

   7:

   8: ...

   9:

  10: public ExpandoObject GetExpandoObject()

  11: {

  12:     dynamic expando = new ExpandoObject();

  13:     expando.Id = Guid.NewGuid();

  14:     expando.Name = "Yan";

  15:     expando.Age = 29;

  16:     expando.Wife = "Yinan";    // not defined on MyClass

  17:

  18:     return expando;

  19: }

  20:

  21: public class MyClass

  22: {

  23:     public Guid Id { get; set; }

  24:

  25:     public string Name { get; private set; } // non-public setter

  26:

  27:     public int Age { get; set; }

  28:

  29:     public string NickName { get; set; } // not defined on ExpandoObject

  30: }

  • ciantic

    You are using “JavaScriptSerializer.ConvertToType” wonder if there is a better way? Asp.Net uses Newtonsoft Json, but I can’t find anything equivalent from it.

    Also one would think ExpandoObject could do it itself without serializations, but apparently not.

  • Yan Cui

    Hey,

    The `JavaScriptSerializer.ConvertToType` was the one I found at the time, and it is convenient but I doubt it performs very well (JavaScriptSerializer performs very poorly in general in all my benchmark tests).

    I’d have thought Json.net (i.e. Newtonsoft.json) has something similar, though I’ve never really looked for it, maybe drop a question on StackOverflow and see if someone more familiar with Json.Net can give you the answer.

    Alternatively, if you don’t mind getting your hands dirty, you can also just DIY it, and it would be pretty easy too, Jon Harrop published an article a few years back on his F# Journal describing how to do structural typing with F#’s dynamic operator (?) : http://fsharpnews.blogspot.co.uk/2012/09/structural-typing.html
    I find it a very good read, and there were a few follow-up articles on using IL emit in general which you might find interesting too, and hopefully help justify the cost of getting access to the articles. His solution is more general purpose but won’t work on ExpandoObject purely because reflecting on ExpandoObject won’t give you the dynamically added properties, nonetheless it’d give you a nice framework to think about a solution that will work though.

    Ultimately, the steps to a DIY solution (that works on an ExpandoObject) is pretty simple:
    1. reflect on the target type to identify properties
    2. cast the ExpandoObject to IDictionary
    3. instantiate an instance of the target type
    4. for each property, find the key in the dictionary, and use the matched value to set the property with
    There are other edge cases to consider, e.g. get-only properties, private setters, abstract target type, etc. but as an easy solution the above should work. To cover the edge cases you might be better off creating a dynamically generated proxy type that inherits from the target, and set the properties on the proxy type instead.

    Now that I’ve typed all that, maybe it’d be easier if I just do an updated post to show how you might do that.. If you’re still interested, I can put something together this weekend perhaps.

  • Harry Knight

    Thank you so much for this concise and understandable methodology. In my particular case, the performance cost of JavaScriptSerializer is well worth the flexibility. Kudos on a great article!