Aside from some basic usage differences, it appears that ExpandoObject
and DynamicJsonObject
behave similarly and could probably be used more-or-less interchangeably, accounting for said differences. For instance, I found this Gist that starts with an ExpandoObject, then converts it to a DynamicJsonObject in order to facilitate converting it to a string:
dynamic expando = new ExpandoObject();
expando.Value = 10;
expando.Product = "Apples";
var dictionaryResult = System.Web.Helpers.Json.Encode(new DynamicJsonObject(expando));
However, it seems I'm able to accomplish the same result by starting with a DynamicJsonObject
:
dynamic jsonobj = new DynamicJsonObject(new Dictionary<string, object>());
jsonObj.Value = 10;
jsonObj.Product = "Oranges";
var System.Web.Helpers.Json.Encode(jsonObj);
In both cases, I end up with a string containing the desired value:
{"Value": "10", "Product": "Oranges"}
Of course I realize that the similarities in this scenario merely demonstrate overlapping of the capabilities of two different tools, either one being more useful than the other in certain cases.
So here's my scenario -- I'm using this as a convenient & flexible way to build an otherwise complex and somewhat arbitrary JSON string prior to delivery over a REST API. The actual properties assigned to the JSON object must vary depending on certain conditions, so using a dynamic object is quite convenient.
(Given this use case, the DynamicJsonObject would seem to be the "appropriate" choice, even if only because of its name!)
Can someone help clarify the real, fundamental difference between these two and why I might use one over the other?