I'm looking for a code correctness and best practices review on the following. I created the following in order to prevent using the same code to convert serialized JSON to something more accessible in my HttpHandler
s without incorporating this directly into my base handler. I don't always need to post over full entities when only a few values are needed.
I'm aware the empty catch
statements should be avoided -- my version has additional content.
/// <summary>
/// Returns a collection of parameters passed via JSON
/// </summary>
/// <param name="request">HttpRequest</param>
/// <returns>Dictionary<typeparamref name="string"/>,<typeparamref name="object"/> of key, value (string, object) pairs.</returns>
public static Dictionary<string, object> Json(this HttpRequest request)
{
// Per request caching.
var jsonCollection = HttpContext.Current.Items["jsonData"] as Dictionary<string, object>;
if (request.ContentType.StartsWith("application/json") || request.ContentType.StartsWith("text/json"))
{
if (jsonCollection == null)
{
string jsonData = string.Empty;
using (StreamReader reader = new StreamReader(request.InputStream))
{
if (!reader.EndOfStream)
{
try
{
jsonData = reader.ReadToEnd();
}
catch (OutOfMemoryException ex)
{
}
catch (IOException ex)
{
}
finally
{
jsonCollection = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonData);
HttpContext.Current.Items["jsonData"] = jsonCollection;
}
}
}
}
}
return jsonCollection;
}
/// <summary>
/// Get Element by index
/// </summary>
/// <param name="request"></param>
/// <param name="index"></param>
/// <returns></returns>
public static object Json(this HttpRequest request, int index)
{
object obj = null;
try
{
obj = request.Json().ElementAt(index);
}
catch (KeyNotFoundException ex)
{
obj = null;
}
return obj;
}
/// <summary>
/// Get element by key
/// </summary>
/// <param name="request"></param>
/// <param name="key"></param>
/// <returns></returns>
public static object Json(this HttpRequest request, string key)
{
object obj = null;
try
{
obj = request.Json()[key];
}
catch (KeyNotFoundException ex)
{
obj = null;
}
return obj;
}
07/22/2014 Quick (Read: Untested) Generic Typed HttpRequest.Json
public static T GetValue<T>(this IDictionary collection, string key)
{
if (collection == null)
{
return default(T);
}
var value = collection[key];
if (value == null)
{
return default(T);
}
var type = typeof(T);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
type = Nullable.GetUnderlyingType(type);
}
var converter = TypeDescriptor.GetConverter(type);
if (!converter.CanConvertTo(value.GetType()))
{
return default(T);
}
return (T)converter.ConvertTo(value, type);
}
public static T Json<T>(this HttpRequest request)
{
// Per request caching -- See GetValue<T> above.
T jsonCollection = HttpContext.Current.Items.GetValue<T>("jsonData");
if (request.ContentType.StartsWith("application/json") || request.ContentType.StartsWith("text/json"))
{
if (jsonCollection == null)
{
string jsonData = string.Empty;
using (StreamReader reader = new StreamReader(request.InputStream))
{
if (!reader.EndOfStream)
{
try
{
jsonData = reader.ReadToEnd();
}
catch (OutOfMemoryException ex)
{
}
catch (IOException ex)
{
}
finally
{
jsonCollection = JsonConvert.DeserializeObject<T>(jsonData);
HttpContext.Current.Items["jsonData"] = jsonCollection;
}
}
}
}
}
return jsonCollection;
}