JavaScriptSerializer not deserializing DateTime/TimeSpan Properly
Asked Answered
A

2

1

Having a problem where DateTime/TimeSpan doesn't seem to deserialize properly with JavaScriptSerializer. When I get the Object back after deserializing the TimeSpan is empty and if I use DateTime then the times are all out of whack. Did find this article but it didn't really help me too much. http://www.west-wind.com/weblog/ShowPost.aspx?id=471402

Anyone have any ideas? Should I maybe try the json.net library?

public class JsonFilter : ActionFilterAttribute
{
    public string Param { get; set; }
    public Type JsonDataType { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
        {
            string inputContent;
            using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
            {
                inputContent = sr.ReadToEnd();
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var result = serializer.Deserialize(inputContent, JsonDataType);
            filterContext.ActionParameters[Param] = result;
        }
    }
}

public class RosterItem
{
    public RosterItem()
    {
        comments = new List<Form.Comment>();
    }
    public Boolean dirty { get; set; }
    public int id { get; set; }
    public int staffId { get; set; }
    public String description { get; set; }
    public int activityId { get; set; }
    public DateTime date { get; set; }
    public TimeSpan startTime { get; set; }
    public TimeSpan endTime { get; set; }
    public List<Form.Comment> comments { get; set; }
}

    [JsonFilter(Param = "rosterItem", JsonDataType = typeof(RosterItem))]
    public int SaveRosterEntry(RosterItem rosterItem)
    {
        RosterEntry rosterEntry = rosterEntryRepository.GetRosterEntry(rosterItem.id);
        if (rosterEntry == null)
        {
            rosterEntry = new RosterEntry();
            rosterEntryRepository.Add(rosterEntry);
        }
        rosterEntry.ActivityID = rosterItem.activityId;
        rosterEntry.StartTime = rosterItem.startTime;
        rosterEntry.EndTime = rosterItem.endTime;
        rosterEntry.RosterDate = rosterItem.date;
        rosterEntry.RosterEmployeeID = rosterItem.staffId;            
        rosterEntryRepository.Save();
        return rosterEntry.RosterEntryID;
    }
Avocado answered 8/12, 2010 at 1:37 Comment(0)
A
0

This will fix your issue if you ever have the same problem.

http://blog.devarchive.net/2008/02/serializing-datetime-values-using.html

All DateTime objects need to be specified explicitly as UTC.

Avocado answered 15/12, 2010 at 5:31 Comment(0)
R
3

I found the answer in the following post on GitHub:

https://github.com/NancyFx/Nancy/issues/336

Basically the answer was to create a new TimeSpanJsonConverter that inherits from JavaScriptConverter and then pass that to an instance of your serializer class:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer()
serializer.RegisterConverters(new[] { new TimeSpanJsonConverter() });

The full class for reference is (written by GrumpyDev):

public class TimeSpanJsonConverter : JavaScriptConverter
{
    public override IEnumerable<Type> SupportedTypes
    {
        get
        {
            return new[] { typeof(TimeSpan) };
        }
    }

    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        return new TimeSpan(
            this.GetValue(dictionary, "days"),
            this.GetValue(dictionary, "hours"),
            this.GetValue(dictionary, "minutes"),
            this.GetValue(dictionary, "seconds"),
            this.GetValue(dictionary, "milliseconds"));
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        var timeSpan = (TimeSpan)obj;

        var result = new Dictionary<string, object>
            {
                { "days", timeSpan.Days },
                { "hours", timeSpan.Hours },
                { "minutes", timeSpan.Minutes },
                { "seconds", timeSpan.Seconds },
                { "milliseconds", timeSpan.Milliseconds }
            };

        return result;
    }

    private int GetValue(IDictionary<string, object> dictionary, string key)
    {
        const int DefaultValue = 0;

        object value;
        if (!dictionary.TryGetValue(key, out value))
        {
            return DefaultValue;
        }

        if (value is int)
        {
            return (int)value;
        }

        var valueString = value as string;
        if (valueString == null)
        {
            return DefaultValue;
        }

        int returnValue;
        return !int.TryParse(valueString, out returnValue) ? DefaultValue : returnValue;
    }
}
Roundshouldered answered 4/9, 2012 at 14:4 Comment(1)
The solution is great for solving TimeSpan serialization, but I think this issue is related to DateTime.Nich
A
0

This will fix your issue if you ever have the same problem.

http://blog.devarchive.net/2008/02/serializing-datetime-values-using.html

All DateTime objects need to be specified explicitly as UTC.

Avocado answered 15/12, 2010 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.