[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


http://bugzilla.novell.com/show_bug.cgi?id=586169

http://bugzilla.novell.com/show_bug.cgi?id=586169#c0


           Summary: DataContractJsonSerializer emits incorrect format for
                    DateTime
    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:

  {"StartDate":"\/Date(62135578800000)\/",...}

But instead I see MonoTouch emitting:

  {"StartDate":{"kind":1,"ticks":{"_ticks":634011156000000000}},...}

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,
DateTimeKind.Utc);
        static string GetJsonDateForWcf(DateTime utcDateTime)
        {
              return string.Format("\\/Date({0})\\/", (long)(utcDateTime -
Epoch).TotalMilliseconds);
        }

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.

See:
- 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
{
    [DataContract()]
    public class Query
    {
        [DataMember(Order=1)]
        public DateTime StartDate { get; set; }
        [DataMember(Order=2)]
        public DateTime EndDate { get; set; }
    }

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

Actual Results:  
{"StartDate":{"kind":1,"ticks":{"_ticks":634011156000000000}},"EndDate":{"kind":1,"ticks":{"_ticks":634035348000000000}}}

Expected Results:  
{"StartDate":"\/Date(1265518800000)\/","EndDate":"\/Date(1267938000000)\/"}

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