I had seen something like this done before where a StringBuilder was used to construct a large JSON string which was then pushed out in the response with the rest of the page markup. This approach seemed messy and difficult to maintain. I did a bit of research on some approaches to constructing a potentially complex and changing JSON object using C# and came across the ExpandoObject class. MSDN has a nice succinct explanation of ExpandoObject:
"Represents an object whose members can be dynamically added and removed at run time."You can find this class in .NET 4 in the System.Dynamic namespace. ExpandoObject lets you effectively "model" an object on the fly with properties that don't really exist (just like using the ViewBag in ASP.NET MVC). In my case, it saved me from writing a number of classes or complex strings just to hold data that was to be output in page responses. Instead, I could use ExpandoObject and then use a JSON serializer to stringify the object.
The example below shows how easy and flexible it is to use ExpandoObject with the Newtonsoft .NET JSON library (available as a NuGet package):
dynamic orderInfo = new ExpandoObject();
orderInfo.OrderDate = DateTime.Today;
orderInfo.ProductTypesAdded = new List<dynamic>();
orderInfo.ProductTypesAdded.Add("Radio");
orderInfo.ProductTypesAdded.Add("Television");
orderInfo.ProductTypesAdded.Add("Recorder");
orderInfo.ReachedCheckout = true;
orderInfo.PaymentMethod = "Card";
orderInfo.DeliveryInfo = new ExpandoObject();
orderInfo.DeliveryInfo.Method = "First";
orderInfo.DeliveryInfo.Country = "UK";
var json = JsonConvert.SerializeObject(orderInfo);
The resulting JSON is:
orderInfo.OrderDate = DateTime.Today;
orderInfo.ProductTypesAdded = new List<dynamic>();
orderInfo.ProductTypesAdded.Add("Radio");
orderInfo.ProductTypesAdded.Add("Television");
orderInfo.ProductTypesAdded.Add("Recorder");
orderInfo.ReachedCheckout = true;
orderInfo.PaymentMethod = "Card";
orderInfo.DeliveryInfo = new ExpandoObject();
orderInfo.DeliveryInfo.Method = "First";
orderInfo.DeliveryInfo.Country = "UK";
var json = JsonConvert.SerializeObject(orderInfo);
{
"OrderDate":"2013-10-11T00:00:00+01:00",
"ProductTypesAdded":[
"Radio",
"Television",
"Recorder"
],
"ReachedCheckout":true,
"PaymentMethod":"Card",
"DeliveryInfo":{
"Method":"First",
"Country":"UK"
}
}
"OrderDate":"2013-10-11T00:00:00+01:00",
"ProductTypesAdded":[
"Radio",
"Television",
"Recorder"
],
"ReachedCheckout":true,
"PaymentMethod":"Card",
"DeliveryInfo":{
"Method":"First",
"Country":"UK"
}
}
No comments:
Post a Comment