DateTime serialization

3,304 views
Skip to first unread message

Francesco

unread,
Sep 27, 2011, 3:54:56 AM9/27/11
to servic...@googlegroups.com
Hi Demis,

When I try to pass a DateTime field in the JSON I see this

"\/Date(-62135596800000+0000)\/"

what kind of format is this?
Then in the HTML visualization I see

1/01/01

which is correct.

But in Linux I can't decode that DateTime stamp.

Any hint?
Is it possible to change the way DateTime is serialized?

Thanks,
Francesco

Demis Bellot

unread,
Sep 27, 2011, 9:16:18 AM9/27/11
to servic...@googlegroups.com
Hi Francesco,

The DateTime format is compatible with Microsofts JSON DateTime Wire Format see:

I'm curious with what you mean by you can't decode the DateTime stamp on Linux, what do you get instead?


Francesco

unread,
Sep 27, 2011, 9:55:26 AM9/27/11
to servic...@googlegroups.com
Nothing, my fault.

I didn't know that it is the number of milliseconds time since midnight, January 1, 1970
I had a date of 1901 and therefore there was a minus before the number of milliseconds.

Thanks,
Francesco

Francesco

unread,
Oct 4, 2011, 4:47:26 AM10/4/11
to servic...@googlegroups.com
Hi Demis,

what should I do to correctly deserialize that DateTime value on Linux?

Demis Bellot

unread,
Oct 4, 2011, 8:55:18 AM10/4/11
to servic...@googlegroups.com
I'm not following, the current behaviour should work on Linux as well?

What's the problem on Linux?



On Tue, Oct 4, 2011 at 4:47 AM, Francesco <fps...@gmail.com> wrote:
Hi Demis,

what should I do to correctly deserialize that DateTime value on Linux?



Francesco Paolo Santi

unread,
Oct 4, 2011, 10:00:02 AM10/4/11
to servic...@googlegroups.com
The problem in my case is the "/" character.
I would like to pass a UTC string or plain milliseconds instead of
having to parse the "/" character.

Is that possible or do I have to parse the DateTime to a string before
serialization?

Thanks,
Francesco

Francesco

unread,
Oct 4, 2011, 10:03:49 AM10/4/11
to servic...@googlegroups.com
Ooops I forgot to say that I am not using ASP.NET...

Demis Bellot

unread,
Oct 4, 2011, 10:45:29 AM10/4/11
to servic...@googlegroups.com
I'm still curious as the problem you're having, i.e. are you not using the JsonSerializer?
as the JsonSerialilzer should handle all that for you, i.e:

class Program
{
  public static void Main(string[] args)
  {
    var dateStr = JsonSerializer.SerializeToString(DateTime.Now);
    var now = JsonSerializer.DeserializeFromString<DateTime>(dateStr); //long form
    Console.WriteLine(now);
  }
}

I just tried this on Linux, works fine...

If for some reason you don't want to use the JsonSerializer you can use the DateTimeSerializer.cs directly.

If you're trying to parse it in JavaScript you can do:

function date(s) { 
  return new Date(parseFloat(/Date\(([^)]+)\)/.exec(s)[1])); 
}

Cheers,


On Tue, Oct 4, 2011 at 10:03 AM, Francesco <fps...@gmail.com> wrote:
Ooops I forgot to say that I am not using ASP.NET...



Francesco Paolo Santi

unread,
Oct 5, 2011, 11:38:21 AM10/5/11
to servic...@googlegroups.com
Sorry Demis,

maybe I was not clear while explaining my problem.

I have a DTO with a DateTime field that is populated in my application.
Then I call a GET method and the JSON that I receive contains the
escape "\" charachters.
When I'm parsing that JSON on linux (no asp.net) I receive that date
format with escape charachters.
How do I serialize a date field in JSON let's say in UTC format?
Do I have to change my DTO field to String and then convert DateTime
to String before serializing the JSON?

Thank you,
Francesco

Demis Bellot

unread,
Oct 5, 2011, 12:06:09 PM10/5/11
to servic...@googlegroups.com
Hi Francesco,

I'm still not sure why you cannot deserialize it using the examples above? It shows how to deserialize it in either C# or JavaScript - are you using something else?
The DateTime you get back is a UTC date, you can then use the date format functions in C# or JavaScript to format it how you like.

If it's easier you can just include the date pre-formatted as a string on the DTO. 
It doesn't need to be an additional field as calculated properties gets serialized as well, i.e:

class Dto
{
  DateTime Date { get;set;}
  string DateString { get { return Date.ToString(); }
}

Cheers,

Francesco Paolo Santi

unread,
Oct 5, 2011, 4:05:18 PM10/5/11
to servic...@googlegroups.com
Hi Demis,

the application that is consuming the ReST WS is a Java/Groovy app.
Sorry but I didn't say it before.

How can I use your C# JSON serializer/deserializer lib in my app?

Thanks a lot,
Francesco

Demis Bellot

unread,
Oct 5, 2011, 4:11:00 PM10/5/11
to servic...@googlegroups.com
You would just have to port the C# code to java:

Java's date already accepts a long in milliseconds:

Date expiry = new Date(timeInMs);

Cheers,

Francesco Paolo Santi

unread,
Oct 6, 2011, 1:45:03 AM10/6/11
to servic...@googlegroups.com
Thanks a lot Demis!!!

Unfortunately I work in a team...and I have a weird collegue who is
against every thing is MS and he doesn't want to parse anything.
Working in a team may me hard sometimes :)
So I am going to pass an UTC string...but in the end he will have to
parse it anyway!

Ciao,
Francesco

Reply all
Reply to author
Forward
0 new messages