如何将 ASP.NET Web 服务配置为仅将 DateTimeOffset 对象的 LocalDateTime 属性作为 JSON 发送?
我有一堆实体类(由 Linq to SQL 生成),其中包含一些 DateTimeOffset 属性。
当我从 .asmx Web 服务以 JSON 形式发送该数据时,JSON 序列化程序会生成以下 JSON:
{"DateTime":"\/Date(1252142834307)\/",
"UtcDateTime":"\/Date(1252142834307)\/",
"LocalDateTime":"\/Date(1252142834307)\/",
"Date":"\/Date(1252101600000)\/",
"Day":5,
"DayOfWeek":6,
"DayOfYear":248,
"Hour":11,
"Millisecond":307,
"Minute":27,
"Month":9,
"Offset":{"Ticks":72000000000,
"Days":0,
"Hours":2,
"Milliseconds":0,
"Minutes":0,
"Seconds":0,
"TotalDays":0.083333333333333329,
"TotalHours":2,
"TotalMilliseconds":7200000,
"TotalMinutes":120,
"TotalSeconds":7200},
"Second":14,
"Ticks":633877468343070254,
"UtcTicks":633877396343070254,
"TimeOfDay":{"Ticks":412343070254,
"Days":0,
"Hours":11,
"Milliseconds":307,
"Minutes":27,
"Seconds":14,
"TotalDays":0.47724892390509255,
"TotalHours":11.453974173722221,
"TotalMilliseconds":41234307.025400005,
"TotalMinutes":687.23845042333335,
"TotalSeconds":41234.3070254},
"Year":2009}
}
我非常希望节省客户端的带宽和 JSON 解析时间。我还真的希望避免在将每个 DateTimeOffset 发送到 Web 服务之前手动投影它。
我意识到 JSON 序列化程序按照预期执行操作,所以我的问题是:有没有办法配置 JSON 序列化程序,以便它只发送 DateTimeOffset 对象的 LocalDateTime 属性?
谢谢,埃吉尔。
I have a bunch of entity class (generated by Linq to SQL) with a few DateTimeOffset properties in it.
When I send that over the wire as JSON from a .asmx web service, the JSON serializer generates the following JSON:
{"DateTime":"\/Date(1252142834307)\/",
"UtcDateTime":"\/Date(1252142834307)\/",
"LocalDateTime":"\/Date(1252142834307)\/",
"Date":"\/Date(1252101600000)\/",
"Day":5,
"DayOfWeek":6,
"DayOfYear":248,
"Hour":11,
"Millisecond":307,
"Minute":27,
"Month":9,
"Offset":{"Ticks":72000000000,
"Days":0,
"Hours":2,
"Milliseconds":0,
"Minutes":0,
"Seconds":0,
"TotalDays":0.083333333333333329,
"TotalHours":2,
"TotalMilliseconds":7200000,
"TotalMinutes":120,
"TotalSeconds":7200},
"Second":14,
"Ticks":633877468343070254,
"UtcTicks":633877396343070254,
"TimeOfDay":{"Ticks":412343070254,
"Days":0,
"Hours":11,
"Milliseconds":307,
"Minutes":27,
"Seconds":14,
"TotalDays":0.47724892390509255,
"TotalHours":11.453974173722221,
"TotalMilliseconds":41234307.025400005,
"TotalMinutes":687.23845042333335,
"TotalSeconds":41234.3070254},
"Year":2009}
}
I would very much like to save the bandwidth and JSON parsing time the client. I would also really like to avoid having to project every single DateTimeOffset manually before sending it off to the web service.
I realize the JSON serializer do as it is suppose to, so my question is this: Is there a way to configure the JSON serializer so that it just sends the LocalDateTime property of the DateTimeOffset object?
Thanks, Egil.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修改对象序列化输出需要您重写DateTimeOffset 对象的序列化方法。由于 DateTimeOffset 是 BCL 的一部分,因此您无法自己覆盖它。
最简单的解决方案是创建一个实现 DateTimeOffset 的自定义对象,允许您从 ISerialized 接口重写 GetObjectData。您的 Web 方法将需要返回您的自定义对象而不是 DateTimeOffset 对象,但如果此 Web 服务的使用采用 JSON 格式,则这应该不会对您的客户群产生影响。
系统生成的类有点麻烦,但您需要替换自己的类才能控制序列化输出。
Modifying the object serialization output requires you to override the serialization method for the DateTimeOffset object. Since DateTimeOffset is part of the BCL, you can't overwrite this yourself.
Easiest solution would be to create a custom object that implements DateTimeOffset, allowing you to override GetObjectData from the ISerializable interface. Your webmethod will need to return your custom object instead of the DateTimeOffset object, but if the consumption of this webservice is in JSON format, this should have no effect on your client base.
Kind of a pain with system-generated classes, but you need to substitute your own classes to get control of the serialized output.