zoukankan      html  css  js  c++  java
  • JavaScriptSerializer之DateTime序列化

    ​ ECMAScript 第五版(15.9.5.4)指导toJSON方法的实现应该把一个Date对象处理成UTC时间的ISO 8601格式字符串,例如:"2011-12-28T08:20:31.917Z",这是一个比较通用的一个格式,同时当需要转回Date对象时可以直接将该字串传给Date构造器,例如:new Date("2011-12-28T08:20:31.917Z")

    ​ .NET的JavaScriptSerialzer转化出来的字串类似"/Date(1600237540407)/",这种形式降低了可操作性性,虽然提供了扩展点,但只允许你从一个对象转换成为另外一个对象,而不能转换为自定义的字符串.借助继承Uri实现 IDictionary<string, object>来进行Hack.

    Hack Code

    public class DateTimeJavaScriptConverter : JavaScriptConverter
    {
    	public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    	{
    		return new JavaScriptSerializer().ConvertToType(dictionary, type);
    	}
    	
    	public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    	{
    		if (!(obj is DateTime)) return null;
    		return new CustomString(((DateTime)obj).ToUniversalTime().ToString("O"));
    	}
    	
    	public override IEnumerable<Type> SupportedTypes
    	{
    	  get { return new[] { typeof(DateTime) }; }
    	}
    	
    	private class CustomString : Uri, IDictionary<string, object>
    	{
    		public CustomString(string str)
    		  : base(str, UriKind.Relative)
    		{
    		}
    		
    		void IDictionary<string, object>.Add(string key, object value)
    		{
    			throw new NotImplementedException();
    		}
    		
    		bool IDictionary<string, object>.ContainsKey(string key)
    		{
    			throw new NotImplementedException();
    		}
    		
    		ICollection<string> IDictionary<string, object>.Keys
    		{
    			get { throw new NotImplementedException(); }
    		}
    		
    		bool IDictionary<string, object>.Remove(string key)
    		{
    			throw new NotImplementedException();
    		}
    		
    		bool IDictionary<string, object>.TryGetValue(string key, out object value)
    		{
    			throw new NotImplementedException();
    		}
    		
    		ICollection<object> IDictionary<string, object>.Values
    		{
    			get { throw new NotImplementedException(); }
    		}
    		
    		object IDictionary<string, object>.this[string key]
    		{
    			get
    			{
    			  throw new NotImplementedException();
    			}
    			set
    			{
    				throw new NotImplementedException();
    			}
    		}
    		
    		void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
    		{
    		  throw new NotImplementedException();
    		}
    		
    		void ICollection<KeyValuePair<string, object>>.Clear()
    		{
    		  throw new NotImplementedException();
    		}
    		
    		bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
    		{
    		  throw new NotImplementedException();
    		}
    		
    		void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
    		{
    		  throw new NotImplementedException();
    		}
    		
    		int ICollection<KeyValuePair<string, object>>.Count
    		{
    		  get { throw new NotImplementedException(); }
    		}
    		
    		bool ICollection<KeyValuePair<string, object>>.IsReadOnly
    		{
    		  get { throw new NotImplementedException(); }
    		}
    		
    		bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
    		{
    		  throw new NotImplementedException();
    		}
    		
    		IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
    		{
    		  throw new NotImplementedException();
    		}
    		
    		IEnumerator IEnumerable.GetEnumerator()
    		{
    		  throw new NotImplementedException();
    		}
    	}
    }
    
  • 相关阅读:
    Struts2 HelloWorld_1
    Java Web JavaMail 邮件发送
    Struts2 ActionWildcard(通配符配置)约定优于配置
    Struts2 Action
    Struts2 struts2简介
    Struts2 ActionMethod DMI(动态方法调用)
    Java Web Servlet过滤器
    com学习笔记(2)基本的com接口QueryInterface的实现
    com学习笔记(4)动态链接
    silverlight Visifire图表转图片偷天换日的做法
  • 原文地址:https://www.cnblogs.com/oxf5deb3/p/13679027.html
Copyright © 2011-2022 走看看