有没有办法重写DataContractJsonSerializer如何序列化日期? [英] Is there a way to override how DataContractJsonSerializer serializes Dates?

查看:141
本文介绍了有没有办法重写DataContractJsonSerializer如何序列化日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改DataContractJsonSerializer序列化日期的方式?

Is there a way to change how the DataContractJsonSerializer serializes dates?

当前,它将日期转换为类似的内容:

Currently, it'll convert a date to something like:

{
  "date": "/Date(1260597600000-0600)/"
}

我宁愿自utc 1970起将其序列化为毫秒.这样,其他语言也可以轻松处理json数据.

I would rather have it serialize as just the milliseconds since utc 1970. That way, other languages can easily work with the json data.

推荐答案

不,序列化程序本身没有挂钩.但是您可以使用一些序列化回调来实现相同的行为.您将创建另一个数据成员(字符串类型),并且在序列化数据之前,将调用[OnSerializing]回调以将DateTime字段的值复制到字符串中.关于序列化代理的文章中的基本体的序列化格式的细粒度控制"部分(位于

No, there's no hook in the serializer itself to do that. But you can use some of the serialization callbacks to implement this same behavior. You'd create another data member (of type string), and before the data is serialized, an [OnSerializing] callback would be invoked to copy the value of the DateTime field to the string one. The section "Fine-grained control of serialization format for primitives" in the post about serialization surrogates (at http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/06/wcf-extensibility-serialization-callbacks.aspx) shows more details of what needs to be done.

这篇关于有没有办法重写DataContractJsonSerializer如何序列化日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆