[Mono-bugs] [Bug 586169] New: DataContractJsonSerializer emits incorrect format for DateTime

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sun Mar 7 21:01:18 EST 2010



           Summary: DataContractJsonSerializer emits incorrect format for
    Classification: Mono
           Product: Mono: Class Libraries
           Version: 2.6.x
          Platform: x86-64
        OS/Version: Mac OS X 10.6
            Status: NEW
          Severity: Major
          Priority: P5 - None
         Component: WCF
        AssignedTo: atsushi at ximian.com
        ReportedBy: rswood at elyceum.net
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---

User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us)
AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10

The DataContractJsonSerializer emits dates in an unexpected way. Using WCF,
Microsoft's admittedly odd format is a string with a special escape sequence
intended to be detected at parse time as a date. It should be:


But instead I see MonoTouch emitting:


First of all, the MS format is milliseconds from the epochal date of 1/1/1970,
not ticks; but this annotated type format is even more unexpected. In
Silverlight, Dates are automatically emitted using the expected format. The am
trying to take code that worked in Silverlight (3.0) and run it in MonoTouch.

I've looked over the code in SVN and do not see any sign that writers or
readers support this format as they should.

A hacky way of outputing this for UTC would be:

    static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0,
        static string GetJsonDateForWcf(DateTime utcDateTime)
              return string.Format("\\/Date({0})\\/", (long)(utcDateTime -

In actual implementation, the time zone offset would have to be accounted for
(see MSDN docs).

The readers should either detect the special escape sequence or use a regex on
strings after the fact to convert to a DateTime.

- http://msdn.microsoft.com/en-us/library/bb412170.aspx
- http://msdn.microsoft.com/en-us/library/dd948679.aspx
- http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx

Reproducible: Always

Steps to Reproduce:
1. Create a new Console Project
2. Add references to System.Runtime.Serialization and System.ServiceModel.Web
3. Modify Main.cs to look like:

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace JsonSerialization
    public class Query
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

    class MainClass
        public static void Main (string[] args)
            DataContractJsonSerializer serializer = new
            Query query = new Query()
                StartDate = DateTime.Today.ToUniversalTime().AddMonths(-1), 
                EndDate = DateTime.Today.ToUniversalTime() 

Actual Results:  

Expected Results:  

Note: if the datetime is not UTC, the timezone offset would follow the
milliseconds in the format "-0500" for instance for EST.

Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list