1

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?

Brian Lacy
  • 18,785
  • 10
  • 55
  • 73
  • Actually the answer is here.. http://stackoverflow.com/questions/3565481/differences-between-expandoobject-dynamicobject-and-dynamic – hackp0int Nov 18 '13 at 19:32
  • Actually, it isn't. That question does compare `ExpandoObject` with `DynamicObject`, from which `DynamicJsonObject` is derived, but (for starters) the descriptions of `ExpandoObject` in the answers offered there seem to apply equally well to `DynamicJsonObject`. – Brian Lacy Nov 18 '13 at 20:07
  • I think I could be mistaken I will recheck my self. – hackp0int Nov 18 '13 at 22:03

0 Answers0