Datetime parse

Mar 23, 2009 at 3:47 PM
Hi,

When i use the Serialize method on my object datetime the result StringBuilder is this:

{"Data":new Date(1237819082406)} but should be {"Data":"new Date(1237819082406)"}

Json parse return an error.

This is my code:

Public Class myClass
    <JsonProperty("Data")> Public Data As DateTime 
End Class

Public Shared Function ObjectToJson(ByVal myObject As Object) As StringBuilder
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim writer As JsonWriter = New JsonTextWriter(sw)
        Dim serializer As JsonSerializer = New JsonSerializer()
        serializer.Converters.Add(New Converters.JavaScriptDateTimeConverter)
        serializer.Serialize(writer, myClass) 
        ObjectToJson = sb
 End Function

thanks for your help.
Alessio.


Coordinator
Mar 23, 2009 at 10:46 PM
No the first is correct. JavaScriptDateTimeConverter formats the date as a JS Date constructor, not a string.
Mar 24, 2009 at 12:20 PM
Thanks for your response!

But if pass this string to the js function... 

function CallbackComplete(s, e) {            
var jsonObj = JSON.parse(e.result); 
var mydate = eval("(" + jsonObj.Data + ")"
}

... JSON.parse(e.result) raises an error!

I can resolve this behavior?

Thank!

Alessio.

Coordinator
Mar 25, 2009 at 7:16 AM
The answer depends upon how JSON.parse works. You need to find a date format it likes.
Mar 25, 2009 at 9:43 AM
My problem is this!

I use the json2.js library... you know there is one that behaves correctly for my scenario?

Sorry for my questions ... but I am not very experienced!

Thanks for your help!

Alessio.
Coordinator
Mar 26, 2009 at 9:08 AM
Edited Mar 26, 2009 at 10:04 AM
I don't know, I have never used json2.js. Perhaps you could do some investigation and then post the answer for everyone else.
Coordinator
Apr 12, 2009 at 11:42 PM
http://james.newtonking.com/archive/2009/04/12/native-json-in-ie8-firefox-3-5-plus-json-net.aspx