When using the Microsoft namespaces System.Web.Helpers and System.Web.Script.Serialization I expected that the Microsoft serializer played well with Microsofts dynamic JSON object. It turns out that this was a naive assumption. The JavaScriptSerializer serializes the object to “{}”. What is the recommended way for this seemingly trivial task?
dynamic obj = new DynamicJsonObject(new Dictionary<string,object>());
obj.FirstName = "Henry";
obj.LastName = "Ford";
JavaScriptSerializer jsc = new JavaScriptSerializer();
string str = jsc.Serialize( obj );
Assert.AreNotEqual(str, "{}"); // Does not fail
Try Json.Net
It will do it successfully. You can use it with anonymous classes
and
ExpandoObjects tooDeserialization to dynamic objects is also possible