JsonConvert.SerializeObject与JsonSerializer.Serialize [英] JsonConvert.SerializeObject vs JsonSerializer.Serialize

查看:428
本文介绍了JsonConvert.SerializeObject与JsonSerializer.Serialize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我无法弄清楚为什么JsonConvert.SerializeObject序列化DateTime对象的方式与JsonSerializer.Serialize不同.

Alright I can't figure out why JsonConvert.SerializeObject serializes DateTime objects differently than JsonSerializer.Serialize.

给班上

public class Test
{
     [JsonConverter(typeof(JavaScriptDateTimeConverter))]
     public DateTime DeliveryDate { get { return DateTime.Now; } }
}

@Html.Raw(JsonConvert.SerializeObject(new Test()))

输出:

"DeliveryDate": "2013-03-01T07:00:00.000Z"

但是当我像在JsonNetResult中那样使用JsonSerializer.Serialize时: http://james.newtonking .com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx

but when I use JsonSerializer.Serialize like in JsonNetResult: http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx

我得到以下输出:

"DeliveryDate": new Date(1362520794703)

我不知道为什么会有这种不一致.我本以为JsonConvert.SerializeObject将在内部使用JsonSerializer.

I can't figure out why there is this inconsistency. I would had thought JsonConvert.SerializeObject would use JsonSerializer internally.

推荐答案

好的,我已经弄清楚了,我想分享一下,以防有人遇到这种情况.

Alright I've figured it out and I want to share in case anyone ever comes across this scenario.

所以很久以前,我在MVC4中序列化DateTime对象JsonResult时遇到麻烦.基本上我的DateTime对象已被序列化为"\/Date(1239018869048)\/",我想我从JSON.NET上的作者那里得到了一个答案,建议将[JsonConverter(typeof(JavaScriptDateTimeConverter))]添加到模型类的DateTime属性中,并在其中使用@Html.Raw(JsonConvert.SerializeObject(Model)风景.如此确定,我确实做到了,并解决了我当时的短期问题

So a long time ago I was having trouble serializing DateTime objects the JsonResult in MVC4. Basically my DateTime objects were being serialized to "\/Date(1239018869048)\/" I think I read an answer from the author for JSON.NET on SO recommending to add [JsonConverter(typeof(JavaScriptDateTimeConverter))] to the DateTime properties of the model class and using @Html.Raw(JsonConvert.SerializeObject(Model) in the View. So sure enough I did that and that fixed my short term problem at the time

时间流逝,今天,在用户向服务器发布内容后,我需要支持即时更新javascript viewModel.今天提出了我的错误.事实证明,我的所有DateTime属性都装饰有该属性,当我尝试将其序列化回客户端时,序列化程序的行为与预期的一样.这使我相信JsonConvert.SerializeObject实际上尊重属性.

Time goes by and today I need to support updating the javascript viewModel on the fly after the user posts some stuff to the server. Which brings up my error today. Well it turns out that ALL my DateTime properties were decorated with the attribute and when I tried to serialize them back down to the client the serializer WAS behaving like as expected. Which lead me believing the JsonConvert.SerializeObject was in fact NOT respecting the attributes.

我删除了令人反感的属性后,一切开始运转良好.调整周围的东西后,我发现我只能使用默认的DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind,而我忘记了日期时间字符串中的Z.

After I removed the offending attributes everything started to work fantastic. Tweaking things around I found I can just use default DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind and I can forget about the Z in my date time strings.

这篇关于JsonConvert.SerializeObject与JsonSerializer.Serialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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