Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Sample JavaScriptConverter

Vince Lee edited this page Apr 5, 2016 · 2 revisions
using Nancy.Json;

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;
  }
}
Clone this wiki locally