zoukankan      html  css  js  c++  java
  • 自定义json日期序列化

    C#的datetime 序列化为日期特别恶心,因为JavaScriptSerializer方法序列化出来是\/Date(****)\/以前都是在JS上再用正则处理,所以总会在用JSON的时候特别不顺手。所以今天折腾了一下序列化方法。

    public class DateTimeConverter : JavaScriptConverter
        {
            public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
            {
                if (string.IsNullOrEmpty(dictionary["Value"].ToString()) || type != typeof(DateTime?))
                    return null;
                if (string.IsNullOrEmpty(dictionary["Value"].ToString()) || type != typeof(DateTime))
                    return DateTime.MinValue;
                string dateString = dictionary["Value"].ToString().Replace("new Date(", "").Replace(")", "");
                string[] spli = dateString.Split(',');
                return DateTime.Parse(string.Format("{0}-{1}-{2} {3}:{4}:{5}", spli[0], spli[1], spli[2], spli[3], spli[4], spli[5]));
            }
    
            public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
            {
                IDictionary<string, object> result = new Dictionary<string, object>();
                if (obj == null)
                    result["Value"] = string.Empty;
                else
                {
                    DateTime jsdate = (DateTime)obj;
                    result["Value"] = string.Format("new Date({0},{1},{2},{3},{4},{5})", jsdate.Year, jsdate.Month-1, jsdate.Day, jsdate.Hour, jsdate.Minute, jsdate.Second);
                }
                return result;
            }
    
            public override IEnumerable<Type> SupportedTypes
            {
                get
                {
                    IList<Type> typelist = new List<Type>();
                    typelist.Add(typeof(DateTime));
                    typelist.Add(typeof(DateTime?));
                    return typelist;
                }
            }

    以上代码序列化已经测试,反序列化很少用,所以没有测试。

    在JS上new Date()有以下几个构造函数

     1  new Date("month dd,yyyy hh:mm:ss");
     2  new  Date("month dd,yyyy");
     3  new  Date(yyyy,mth,dd,hh,mm,ss);
     4  new Date(yyyy,mth,dd);
     5  new Date(ms);

    第一个,第二个方式月份是英文,直接使用不方便。

    第四个忽略了时间,第五个更蛋痛,居然是时间和GMT时间1970年1月1日之间相差的毫秒数

    所以能简单实用的只有第四个了。

    这里之所以月份要减一,因为JS的上Mth是从0开始,而C#的datetim.Month是从1开始。

    现在我们将序列化规则弄出来了,下一步就是实现序列化

     var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                jsonSerializer.RegisterConverters(new System.Web.Script.Serialization.JavaScriptConverter[] { new DateTimeConverter() });
    jsonSerializer.Serialize(DateTime.Now);

    这样将得到序列化后的Json格式字符串了。

  • 相关阅读:
    DGA域名可以是色情网站域名
    使用cloudflare加速你的网站隐藏你的网站IP
    167. Two Sum II
    leetcode 563. Binary Tree Tilt
    python 多线程
    leetcode 404. Sum of Left Leaves
    leetcode 100. Same Tree
    leetcode 383. Ransom Note
    leetcode 122. Best Time to Buy and Sell Stock II
    天津Uber优步司机奖励政策(12月28日到12月29日)
  • 原文地址:https://www.cnblogs.com/maydear/p/2825580.html
Copyright © 2011-2022 走看看