I've exposed an ASP.NET ScriptService that returns the results of a user defined query. In this case, I'm returning the resulting dataset as a JSON serialized IEnumerable<IDictionary>. Each IDictionary represents a dataset row and contains an arbitrary number of key/value pairs. Although most returned values are primitive types, occasionally the dataset may contain domain objects (i.e. complex types).
[ScriptMethod]
[WebMethod(true)]
public static IEnumerable<IDictionary> FetchGridContents(int queryId, string columnKey, bool sortAsc)
{
return GetSortedGridData(listID, columnKey, sortAsc);
}
Everything gets serialized into JSON without incident. The issue I'm contending with at the moment is how to HTMLEncode the returned dataset. I need to encode both "simple" String values as well as any String property of complex types (at any level of nesting).
The only way I see to extend/override the behavior of the JavaScriptSerializer class is to create a custom converter as described here. Creating a specific converter for each type that may be returned in the dataset seems like a maintenance nightmare. I'm hoping there is a cleaner solution. One idea I was bouncing around was using a combination of Linq and reflection to generically and recursively apply HttpUtility.HTMLEncode() to IEnumerable but the performance/complexity of a such a solution worries me.
Surely someone has run into such a requirement before?