码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScriptConverter

时间:2015-01-16 11:20:03      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

public class DatePartsConverter : JavaScriptConverter
{
  public override IEnumerable<Type> SupportedTypes
  {
    get { yield return typeof(DateTime); }
  }

  public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
  {
    if (obj is DateTime)
    {
      DateTime date = (DateTime)obj;

      var json = new Dictionary<string, object>();

      json["year"] = date.Year;
      json["month"] = date.Month;
      json["day"] = date.Day;

      return json;
    }

    return null;
  }

  public override object Deserialize(IDictionary<string, object> json, Type type, JavaScriptSerializer serializer)
  {
    if (type == typeof(DateTime))
    {
      object year, month, day;

      json.TryGetValue("year", out year);
      json.TryGetValue("month", out month);
      json.TryGetvalue("day", out day);

      if ((year is int)
       && (month is int)
       && (day is int))
        return new DateTime((int)year, (int)month, (int)day);
    }

    return null;
  }
}

 

JavaScriptConverter

标签:

原文地址:http://www.cnblogs.com/jRoger/p/4228045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!