I recently ported some code from JsonFx to Newtonsoft's Json.Net to improve deserialization performance dramatically. JsonFx was deserializing to dynamic objects quite well, and when I tried to use Json.Net, I kept running into various problems. The main difficulty was that it deserialized to JValue objects by default and before passing these into methods they needed to be cast or the data referenced through the .Value properties. I wanted to just access the data directly as before without having to jump through extra hoops. While I got close a few times, there was always some problem that kept confounding me. I finally figured out the right incantation to deserialize to ExpandoObjects and I thought I'd share it here since I could not find this when I was searching.
JsonConvert.DeserializeObject(data, typeof(ExpandoObject), new ExpandoObjectConverter());
The following code which looks similar didn't work for me because the data didn't always represent an object. It was sometimes a list or a primitive.
JsonConvert.DeserializeObject<ExpandoObject>(data, new ExpandoObjectConverter());
No comments:
New comments are not allowed.