none
DataContractJsonSerializer and DateTime objects RRS feed

  • Question

  • Hi,

    I'm trying to write a service which employs the use of an object which contains some DateTime properties, and I'm testing my service using Fiddler2 and writing the POST requests manually. However, my service always returns an internal server error. I found that my requests always work when I leave out the DateTime parameters in my Json packet.

    As a test, I wrote this quick console program to serialize and deserialize a type containing a DateTime property - and found that the DataContractSerializer can't deserialize its own serialization!

    namespace Serializer
    {
    [DataContract]
    class TestType
    {
    [DataMember]
    public DateTime ExampleDate { get; set; }
    }

    class Program
    {
    static void Main(string[] args)
    {
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TestType));
    TestType t = new TestType() { ExampleDate = DateTime.Now };
    string s = null;

    using (MemoryStream ms = new MemoryStream())
    {
    serializer.WriteObject(ms, t);
    s = ASCIIEncoding.Default.GetString(ms.GetBuffer());
    }

    TestType t2 = null;
    using (MemoryStream ms2 = new MemoryStream(ASCIIEncoding.Default.GetBytes(s)))
    {
    t2 = (TestType)serializer.ReadObject(ms2);
    }

    Console.WriteLine(s);
    Console.ReadKey();
    }
    }
    }
    When attempting to deserialize s , I get the exception "There was an error deserializing the object of type Serializer.TestType. Encountered unexpected character '\0'.". At this point I am assuming that my service is having the same difficulty. I understand that the formatting of dates and times in Json strings is non-standard, so could someone please help me (or explain to me) what is happening here and why this doesn't work?

    The web service that I am writing will likely be used by non .Net clients and there may be the possibility that I would have to construct Json packets "by hand" on the client, so really I need to know what format the deserializer is looking for where DateTime properties are concerned.

    Thanks for any help/advice you can give in advance!
    Tuesday, June 16, 2009 7:35 AM

Answers

  • Replace s = ASCIIEncoding.Default.GetString(ms.GetBuffer());

    with:

                    ms.Seek(0, SeekOrigin.Begin);
                    StreamReader sr = new StreamReader(ms);
                    s = sr.ReadToEnd();
    • Marked as answer by Steve Hobbs Thursday, June 18, 2009 8:50 AM
    Wednesday, June 17, 2009 12:27 AM

All replies

  • Replace s = ASCIIEncoding.Default.GetString(ms.GetBuffer());

    with:

                    ms.Seek(0, SeekOrigin.Begin);
                    StreamReader sr = new StreamReader(ms);
                    s = sr.ReadToEnd();
    • Marked as answer by Steve Hobbs Thursday, June 18, 2009 8:50 AM
    Wednesday, June 17, 2009 12:27 AM
  • Thanks for that; your solution did the trick.
    Thursday, June 18, 2009 8:50 AM