纸上得来终觉浅,绝知此事要躬行。

 

JavaScriptConverter

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;
  }
}

 

posted on 2015-01-16 11:03  JRoger  阅读(474)  评论(0编辑  收藏  举报

导航