1. #1
    Sencha User
    Join Date
    Jan 2008
    Posts
    46
    Vote Rating
    0
    theyang is on a distinguished road

      0  

    Default Date fields in Json response

    Date fields in Json response


    Hello,
    I don't know if this was asked before, I did a search but cannot find a topic about this.

    I am using WCF as a Json service, and returning objects as json responses. The objects contain date fields and they appear shown below in the response:

    "PublicationStartDate":"\/Date(1207485180000+0300)\/"

    I am binding these data to a grid with jsonstore but I cannot succeed in showing the date field on the grid. The column shows empty.

    What should I do, can someone guide me please?

    Thanks.

  2. #2
    Ext JS Premium Member dj's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    573
    Vote Rating
    2
    dj has a spectacular aura about dj has a spectacular aura about dj has a spectacular aura about

      0  

    Default


    You want to have a look at http://extjs.com/deploy/dev/docs/?cl...&member=create - you need a convert function that converts your data into a valid Date object.

  3. #3
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,508
    Vote Rating
    56
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Can't you change the way the server generates the JSON to make it generate

    Code:
    PublicationStartDate: new Date(2008, 3, 7, 3, 0, 0)
    ?

  4. #4
    Ext JS Premium Member dj's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    573
    Vote Rating
    2
    dj has a spectacular aura about dj has a spectacular aura about dj has a spectacular aura about

      0  

    Default


    Quote Originally Posted by Animal View Post
    Can't you change the way the server generates the JSON to make it generate

    Code:
    PublicationStartDate: new Date(2008, 3, 7, 3, 0, 0)
    ?
    That would certainly work for JavaScript as JSON-consumer. But it is not part of the JSON-spec because it will only work in JavaScript.

    So: If this WCF thingy only serves your JavaScript frontend then changeing it on the server-side would possible be the quickest way possible.

  5. #5
    Sencha User
    Join Date
    Jan 2008
    Posts
    46
    Vote Rating
    0
    theyang is on a distinguished road

      0  

    Default


    Sure it is possible to change it on the server but I am using automatic serialization, I mean my entities are automatically serialized to json and I don't want to temper with that. I thought it would be easier to handle it on the Ext side.

  6. #6
    Sencha User
    Join Date
    Jan 2008
    Posts
    46
    Vote Rating
    0
    theyang is on a distinguished road

      0  

    Default


    Hello again,

    I need a way to intercept data loading process and parse datetime fields manually, removing the "Date" string from the response.

    Can someone please guide me?

  7. #7

  8. #8
    Ext User dotnetCarpenter's Avatar
    Join Date
    Mar 2007
    Location
    Copenhagen, Denmark
    Posts
    271
    Vote Rating
    0
    dotnetCarpenter is on a distinguished road

      0  

    Default


    You should think twice before serializing .NET DateTime. In your code you have to take timezones into account on both the client and the server. A much better way is to serialize strings. Then you have complete control of the format and Ext can convert them into dates (almost) automatically. On the server you just use Convert.ToDateTime() and you're good to go.

  9. #9
    Sencha User
    Join Date
    Jan 2008
    Posts
    46
    Vote Rating
    0
    theyang is on a distinguished road

      0  

    Default


    Serializing .Net DateTime is an option, but if I go into that I have to manually serialize all my classes and all the return values of methods that include DateTime. As I said in my second post I am using JSon serialization with DataContracts, I don't write a single line of code to serialize my objects to Json.

    I wrote a JsonStore extension that successfully receives Json responses from .Net WCF services and fills comboboxes, grids etc. Only problem seems to be with the DateTime type. Instead of sacrificing all the benefits of automatic serialization, I think extending an Ext type or smth else might be more appropriate for this and future projects.

    Record.create is worth looking at.

  10. #10
    Ext User dotnetCarpenter's Avatar
    Join Date
    Mar 2007
    Location
    Copenhagen, Denmark
    Posts
    271
    Vote Rating
    0
    dotnetCarpenter is on a distinguished road

      0  

    Default


    Its not what I meant. You can keep all the nice serialization from WCF but DateTime values is NOT a good idea to serialize. Its a much better approach to mark your public DateTime properties as string (you can still have a private backing field as DateTime, if you need that in your server-side code).

    Code:
    // C# where DateProperty is a DateTime object
    DateProperty.ToString("yyyy-MM-dd hh:mm:ss");