JavaScriptSerializer not deserializing DateTime/TimeSpan Properly

前端 未结 2 1567
抹茶落季
抹茶落季 2020-12-11 11:46

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 empt

相关标签:
2条回答
  • 2020-12-11 12:22

    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;
        }
    }
    
    0 讨论(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.

    0 讨论(0)
提交回复
热议问题