This is a migrated thread and some comments may be shown as answers.

[Solved] JSON Date Serialization

7 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shawn
Top achievements
Rank 1
Shawn asked on 22 Apr 2010, 02:23 PM
I am having a problem with the JSON that is created via the GridModel.

Dates are not being serialized properly, so they cannot be deserialized back into a DateTime parameter.

They are currently being serialized as:

{"Date", "2009-10-24T04:59:59.680Z"}

It should be returned as:

{"Date", "/\Date(1024093200000-0600)/\"}

(the dates i showed arent the same)

I assume this is due to a different serializer being used for the grid model?


7 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 22 Apr 2010, 02:52 PM
Hello Shawn,

Date deserialization seems to work properly in our ajax editing example. What is not working in your case?

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Shawn
Top achievements
Rank 1
answered on 22 Apr 2010, 03:08 PM
Ah, upon further inspection, i see that it is coming down properly, but some where along the line, either im doing something wrong, or somehow the value of the date field is being formatted.

In the OnRowDataBound event im wiring to a textbox onchange event.  When the data object gets to the SaveGame Function, the data property value has been changed (or im doing something wrong).  The first json is what i am getting from the server, the second is what is being posted by the the SaveGame function

function OnRowDataBound(e) { 
             
            $("input", e.row).change(function () { SaveGame(e.dataItem) }); 
 
        } 
 
function SaveGame(data) { 
             
            PostAjax("/siteadmin/models/TeamSports.svc/SaveGame", data); 
        } 
 
 
{"__type":"Game","GameID":6454,"HTeamID":229,"VTeamID":232,"HTeamScore":51,"VTeamScore":14,"GDate":"\/Date(1256360400000-0500)\/","SportID":3,"ConfGame":0,"HTeamName":"Team1","HConfID":216,"VTeamName":"Team2","VConfID":260,"HRegionID":1,"VRegionID":1,"HDivID":7,"VDivID":7,"HStateID":14,"VStateID":14,"GameTypeID":0,"TieGame":0,"MustSee":0,"ModifiedColumns":[],"ExtraColumns":[{"Key":"HTeamName","Value":"Team1"},{"Key":"VTeamName","Value":"Team2"}]} 
 
 
{"json": {"__type":"Game","GameID":6454,"HTeamID":229,"VTeamID":232,"HTeamScore":51,"VTeamScore":14,"GDate":"2009-10-24T04:59:59.680Z","SportID":3,"ConfGame":0,"HTeamName":"Team1","HConfID":216,"VTeamName":"Team2","VConfID":260,"HRegionID":1,"VRegionID":1,"HDivID":7,"VDivID":7,"HStateID":14,"VStateID":14,"GameTypeID":0,"TieGame":0,"MustSee":0,"ModifiedColumns":[],"ExtraColumns":[{"Key":"HTeamName","Value":"Team1"},{"Key":"VTeamName","Value":"Team2"}]}} 
 

0
Atanas Korchev
Telerik team
answered on 22 Apr 2010, 03:18 PM
Hello Shawn,

Which version are you using? There was a known issue where DateTime objects were not converted to javascript dates during the onrowdatabound event. They remained as string encoded in the form /Date()/.
You can get the latest version from this forum thread.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Shawn
Top achievements
Rank 1
answered on 22 Apr 2010, 03:21 PM
thats my issue exactly.  The datetimes are being formatted to a javascript datetime, but i need them to be in the "/\date.." format so that i when i send them back to the server, WCF can deserialize them.
0
Atanas Korchev
Telerik team
answered on 22 Apr 2010, 03:37 PM
Hi Shawn,

Perhaps I need to see the source of your PostAjax function as our WCF editing demo is working as expected - serializing dates to /Date()/. I think your code has to format the date as you want it to provided you don't seem to be using the built-in inline editing.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Shawn
Top achievements
Rank 1
answered on 22 Apr 2010, 03:57 PM
here is my PostAjax code.  Perhaps you can suggest another function besides JSON.Stringify?  Or another way to send the json object?

function PostAjax(url, data) { 
 
    //data.replace("_", ""); 
    $.ajax({ 
        url: url, 
        contentType: 'application/json; charset=utf-8', 
        type: 'POST', 
        dataType: 'json', 
        error: function (xhr, status) { 
            alert(status); 
        }, 
        data: "{\"json\": " + JSON.stringify(data) + "}", 
        success: function (result) { 
            alert("woot!"); 
        } 
    }); 


0
Atanas Korchev
Telerik team
answered on 22 Apr 2010, 04:08 PM
Hello Shawn,

JSON.stringify is not aware of the /Date()/ format as far as I know that's why it serializes the date as a string. We are manually serializing the date in the required format.

In your case I suggest you convert the date to string before passing it to JSON.stringify:

data.GDate = '\\/Date(' + data.GDate.getTime() + ')\\/';

Also make sure you are using the build I referred to in one of my previous replies. That build would make sure you are always given a valid JavaScript date object and not the serialized string.

Greetings,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Shawn
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Shawn
Top achievements
Rank 1
Share this question
or