I have been looking at a few posts on SO about how to deserialize a date in a json string.
The posts mention doing a replace on the string to reformat the Date parts.
the json string looks like this:
"/Date(1336258800000)/\"
apparently what I need to get to is this:
"new Date(1336258800000)"
The trouble is as soon as I try and to a replace, or indexOf ')/\' it doesnt find the string to replace (indexOf is -1)
can anyone see what im doing wrong?
JavaScriptSerializer jss = new JavaScriptSerializer();
//Fix an issue with Json Dates
string json = eventArgument.Replace( @"/Date(", "new Date(" ).Replace( @")/\", ")" );
int index = json.IndexOf( @")/\", 0 );
WorkPattern wp = jss.DeserializeObject( json ) as WorkPattern;
here is the full json string:
"{\"WorkPatternDays\":[{\"WorkPatternDayShifts\":[{\"WorkPatternDayShiftRates\":[{\"Duration\":8.5,\"Sequence\":0,\"WorkPatternDayShiftID\":186,\"WorkPatternDayShiftRateID\":105,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"WorkPatternDayShiftBreaks\":[{\"PaidBreak\":true,\"Duration\":1,\"EndTime\":\"/Date(1336050000000)/\",\"StartTime\":\"/Date(1336046400000)/\",\"WorkPatternDayShiftID\":186,\"WorkPatternDayShiftBreakID\":284,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"},{\"PaidBreak\":false,\"Duration\":0.25,\"EndTime\":\"/Date(1336058100000)/\",\"StartTime\":\"/Date(1336057200000)/\",\"WorkPatternDayShiftID\":186,\"WorkPatternDayShiftBreakID\":285,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"Duration\":8.5,\"EndTime\":\"/Date(1336062600000)/\",\"StartTime\":\"/Date(1336032000000)/\",\"WorkPatternDayID\":186,\"WorkPatternDayShiftID\":186,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"DayOfWeek\":1,\"DayOfWeekNumber\":1,\"WorkPatternID\":105,\"WorkPatternDayID\":186,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"},{\"WorkPatternDayShifts\":[{\"WorkPatternDayShiftRates\":[],\"WorkPatternDayShiftBreaks\":[{\"PaidBreak\":true,\"Duration\":0.5,\"EndTime\":\"/Date(1336041000000)/\",\"StartTime\":\"/Date(1336039200000)/\",\"WorkPatternDayShiftID\":187,\"WorkPatternDayShiftBreakID\":286,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"Duration\":5.5,\"EndTime\":\"/Date(1336046400000)/\",\"StartTime\":\"/Date(1336026600000)/\",\"WorkPatternDayID\":187,\"WorkPatternDayShiftID\":187,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"DayOfWeek\":3,\"DayOfWeekNumber\":3,\"WorkPatternID\":105,\"WorkPatternDayID\":187,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}],\"WorkPatternName\":\"Naths Test Work Pattern\",\"WorkPatternID\":105,\"Deleted\":false,\"UpdatedUser\":\"\",\"UpdatedDate\":\"/Date(1336258800000)/\"}"
A bit more info to see how it all fits together:
code behind:
public override void DataBind()
{
try
{
if ( this.WorkPattern != null )
{
//Create a javascript serializer
JavaScriptSerializer jss = new JavaScriptSerializer();
//Get the serialised object as a json string
string json = jss.Serialize( this.WorkPattern );
//Run the jquery code
base.RunjQueryCode(
String.Format( "loadWorkPattern({0});", json ) );
jss = null;
}
}
catch ( Exception )
{
throw;
}
}
protected override void HandlePostbacks( string eventTarget, string eventArgument )
{
try
{
switch ( eventTarget )
{
case "Save":
JavaScriptSerializer jss = new JavaScriptSerializer();
//Fix an issue with Json Dates
string json = eventArgument.Replace( @"/Date(", "new Date(" ).Replace( @")/\", ")" );
int index = json.IndexOf( @")/\\", 0 );
WorkPattern wp = jss.DeserializeObject( json ) as WorkPattern;
object o = jss.Deserialize<WorkPattern>( json );
break;
default: break;
}
base.HandlePostbacks( eventTarget, eventArgument );
}
catch ( Exception )
{
throw;
}
}
Markup / js:
function loadWorkPattern(jsonData) {
//Store the work pattern
_workPattern = jsonData;
//Loop through the work pattern days
$.each(_workPattern.WorkPatternDays, function (key, workPatternDay) {
//Loop through each shift
$.each(workPatternDay.WorkPatternDayShifts, function (key, workPatternShift) {
addShift(workPatternShift, workPatternDay.DayOfWeekNumber);
//Loop through each break
$.each(workPatternShift.WorkPatternDayShiftBreaks, function (key, workPatternDayShiftBreak) {
addBreak(workPatternDayShiftBreak);
});
});
});
}
function saveWorkPattern() {
__doPostBack('Save', JSON.stringify(_workPattern));
}
Im calling JSON.stringify to serialize the serialize the stored object before sending back to the server, is this what im doing wrong?
UPDATE
This is the working code:
string json = eventArgument.Replace( @"/Date(", "\\/Date(" ).Replace( @")/", ")\\/" );