为什么 JavaScriptSerializer 忽略我的转换器?

发布于 2024-09-06 10:54:46 字数 1264 浏览 12 评论 0原文

我正在尝试读取一个 JSON 对象,其中包含日期/时间,其格式无法由 .NET 的 DateTime 结构直接解析。为了避免在日期/时间结构中出现“int”字段,我编写了一个自定义 DateTimeConverter:

public class DateTimeConverter : JavaScriptConverter {
  public override IEnumerable<Type> SupportedTypes {
    get { return new Type[] { typeof(DateTime), typeof(DateTime?) }; }
  }

  public override IDictionary<string, object> Serialize(
    object obj, JavaScriptSerializer serializer
  ) { throw new NotImplementedException(); }

  public override object Deserialize(
    IDictionary<string, object> dictionary, Type type,
    JavaScriptSerializer serializer
  ) {
    return DateTime.Now;
  }
}

但是当我使用 JavaScriptSerializer 读取 JSON 字符串时,它不使用我的自定义转换器:

public struct TextAndDate {
  public string Text;
  public DateTime Date;
}

static void Main() {
  string json =
    "{" +
    "  \"text\": \"hello\", " +
    "  \"date\": \"1276692024\"" +
    "}";

  var serializer = new JavaScriptSerializer();
  serializer.RegisterConverters(new [] { new DateTimeConverter() });
  var test = serializer.Deserialize<TextAndDate>(json);
}

转换器 当我直接反序列化 DateTime 值时使用,而不是当我反序列化包含 DateTime 值的类型时使用。

为什么? 有什么方法可以解决这个问题而不编写自定义 DateTime 类型或使用 int 吗?

I'm trying to read a JSON object which contains the date/time in a format that cannot be directly parsed by .NET's DateTime structure. In order to avoid having an 'int' field in my structure for the date/time, I wrote a custom DateTimeConverter:

public class DateTimeConverter : JavaScriptConverter {
  public override IEnumerable<Type> SupportedTypes {
    get { return new Type[] { typeof(DateTime), typeof(DateTime?) }; }
  }

  public override IDictionary<string, object> Serialize(
    object obj, JavaScriptSerializer serializer
  ) { throw new NotImplementedException(); }

  public override object Deserialize(
    IDictionary<string, object> dictionary, Type type,
    JavaScriptSerializer serializer
  ) {
    return DateTime.Now;
  }
}

But when I read a JSON string with the JavaScriptSerializer, it does not use my custom converter:

public struct TextAndDate {
  public string Text;
  public DateTime Date;
}

static void Main() {
  string json =
    "{" +
    "  \"text\": \"hello\", " +
    "  \"date\": \"1276692024\"" +
    "}";

  var serializer = new JavaScriptSerializer();
  serializer.RegisterConverters(new [] { new DateTimeConverter() });
  var test = serializer.Deserialize<TextAndDate>(json);
}

The converter is used when I directly deserialize a DateTime value, just not when I deserialize a type containing a DateTime value.

Why?
Any way around this without writing a custom DateTime type or using int?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的心爱 2024-09-13 10:54:46

您应该在您的 DateTimeConverter 类中进行一些小的更改:

public class DateTimeConverter : JavaScriptConverter {
    public override IEnumerable<Type> SupportedTypes {
        get { return new Type[] { typeof (TextAndDate) }; }
    }

    public override IDictionary<string, object> Serialize (
        object obj, JavaScriptSerializer serializer
    ) { throw new NotImplementedException (); }

    public override object Deserialize (
        IDictionary<string, object> dictionary, Type type,
        JavaScriptSerializer serializer
    ) {
        if (type == typeof (TextAndDate)) {
            TextAndDate td = new TextAndDate ();
            if (dictionary.ContainsKey ("text"))
                td.Text = serializer.ConvertToType<string> (
                                            dictionary["text"]);
            //if (dictionary.ContainsKey ("date"))
                td.Date = DateTime.Now;
            return td;
        }
        else
            return null;
    }
}

更新 基于评论:在我看来,您应该使用消息检查器技术(请参阅http://msdn.microsoft.com/en-us/library/aa717047.aspx)。查看 如何在 .NET WCF 中忽略 DateTime 的时区客户端? 为例。

You should make small changes in your DateTimeConverter class:

public class DateTimeConverter : JavaScriptConverter {
    public override IEnumerable<Type> SupportedTypes {
        get { return new Type[] { typeof (TextAndDate) }; }
    }

    public override IDictionary<string, object> Serialize (
        object obj, JavaScriptSerializer serializer
    ) { throw new NotImplementedException (); }

    public override object Deserialize (
        IDictionary<string, object> dictionary, Type type,
        JavaScriptSerializer serializer
    ) {
        if (type == typeof (TextAndDate)) {
            TextAndDate td = new TextAndDate ();
            if (dictionary.ContainsKey ("text"))
                td.Text = serializer.ConvertToType<string> (
                                            dictionary["text"]);
            //if (dictionary.ContainsKey ("date"))
                td.Date = DateTime.Now;
            return td;
        }
        else
            return null;
    }
}

UPDATED based on comment: It seems to me that you should use Message Inspectors technique (see http://msdn.microsoft.com/en-us/library/aa717047.aspx). Look at How to ignore timezone of DateTime in .NET WCF client? for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文