Where did that JSON field go? Serializing IHtmlString to JSON.

TL;DR If your brain consumes Stack Overflow questions better than blog posts, go see “How do I serialize IHtmlString to JSON with Json.NET?” over there. IHtmlString doesn’t play nicely with JSON serialization If you have an IHtmlString in your JSON (regardless of arguments against putting raw HTML in JSON), you will probably need to customize the serialization to get the HTML out of that variable. In fact, the serialization will probably be invalid compared to what you expect; it does make sense if you think about how the serialization process works. Fortunately, putting together a quick Json.NET JsonConverter will make the issue go away. What you might expect from some object containing an IHtmlString variable named x: { x = “some <span>text</span>” } What default .NET JavaScript serialization and default Json.NET serialization will give you for said object: { x = { } } How to fix it with Json.NET: public class IHtmlStringConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(Type objectType) { return typeof(IHtmlString).IsAssignableFrom(objectType); } … public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { IHtmlString source = value as IHtmlString; if (source == null) { return; } writer.WriteValue(source.ToString()); } } Background While working on some random API, we… Continue reading

Initial setup for a new ASP.NET MVC site in IIS7

Background Over the years, I have spent far too many hours running the same set of commands against ASP.NET and ASP.NET MVC sites to bring them up to what I consider a starting point. Most of the time, I have to refresh myself about how to do at least one of them. This is a list of those commands in a central location for myself and anyone else to use. As with any good instructions, there is no guarantee you won’t completely destroy your server, site, or soul using these commands. I can only say they worked for me once. Remove unnecessary HTTP headers Server X-Powered-By X-AspNet-Version X-AspNetMvc-Version Adding dynamic content compression (e.g., GZIP) GZIP for JSON and JSONP content types (e.g., “application/json”) Complete appcmd command-line list Remove unnecessary HTTP headers By default, IIS and ASP.NET give you a few headers in each and every HTTP response sent out. Server: Microsoft-IIS/7.5 (or 7.0, or whatever IIS version you have) X-Powered-By: ASP.NET X-AspNet-Version: 4.0.303319 (or 2.0.50727, or 1.1.4322, or whatever version you have) X-AspNetMvc-Version: 3.0 (or 2.0, or 1.0, or whatever version you have) People will rant and rave about removing these headers for security and whether that helps. I am… Continue reading

Getting dynamic ExpandoObject to serialize to JSON as expected

Serializing ExpandoObjects I am currently creating a JSON API for a handful of upcoming Sierra Trading Post projects. When I found myself generating JSON for a stripped-down representation of a number of domain classes, all wrapped with some metadata, I turned to dynamic and things have been going quite well. Unfortunately, there was a hurdle to getting the JSON to look the way I wanted. If you start playing around with serializing ExpandoObject to JSON, you will probably start finding a number of options. The easiest solution to find is the one that comes with .NET, JavaScriptSerializer under System.Web.Script.Serialization. It will happily serialize an ExpandoObject for you, but it probably won’t look the way you expect. Your searches will probably vary, but I found Newtonsoft’s JSON.NET, which handled ExpandoObject right out of the NuGet box. Then I stumbled on ServiceStack.Text (also “NuGettable”). While it does even weirder things than the .NET serializer with ExpandoObjects, it supposedly does them very fast. Test code dynamic test = new ExpandoObject(); test.x = “xvalue”; test.y = DateTime.Now; BCL JavaScriptSerializer (using System.Web.Script.Serialization;) JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); string jsonOfTest = javaScriptSerializer.Serialize(test); // [{“Key”:”x”,”Value”:”xvalue”},{“Key”:”y”,”Value”:”\/Date(1314108923000)\/”}] Not quite what I was looking for but it makes sense if… Continue reading