I created an extension method that uses the built-in ASP.NET serializer to serialize my objects into JSON to send back to my server via AJAX like so:
namespace ExtensionMethods.Json
{
public static class JsonHelper
{
public static string ToJson(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public static string ToJson(this object obj, int recursionDepth)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
return serializer.Serialize(obj);
}
}
}
//usage
String json = myObject.ToJson();
This works fine, except for dates, as it sends back dates in this format:
/Date(1291276800000)/
Is there a way to fix this serverside so that the date comes into something more manageable, or will have to do some stupid character parsing on the client side (ie, scrape the digits out of the parens and try to set a date using that number as milliseconds)? Or is there a better way I'm simply overlooking? I've tried Date.parse([the date])
but it errors out with "Invalid date format".