什么是DataContractJsonSerializer和的JavaScriptSerializer之间的区别? [英] What's the difference between DataContractJsonSerializer and JavaScriptSerializer?

查看:145
本文介绍了什么是DataContractJsonSerializer和的JavaScriptSerializer之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET框架附带<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx\">System.Runtime.Serialization.Json.DataContractJsonSerializer和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx\">System.Web.Script.Serialization.JavaScriptSerializer,这两者的德/ JSON序列化。我怎么知道什么时候才能选择这些类型的比其他的呢? MSDN不说清楚它们的相对优势。

The .NET Framework ships with System.Runtime.Serialization.Json.DataContractJsonSerializer and System.Web.Script.Serialization.JavaScriptSerializer, both of which de/serialize JSON. How do I know when to choose one of these types over the other? MSDN doesn't make it clear what their relative advantages are.

我们必须消耗或发出JSON几个项目,并入选为每个类迄今一直依赖于每个项目的主要开发的意见。一些是简单的,两者具有关于从JSON生产管理的类型(类型不密切映射到流)复杂的逻辑,但没有对任何速度重视,人们需要的速度。无与WCF交互,至少现在。

We have several projects that consume or emit JSON, and the class selected for each thus far has depended on the opinion of the primary dev on each project. Some are simple, two have complex logic regarding producing managed types from JSON (the types do not map closely to the streams) but don't have any emphasis on speed, one requires speed. None interact with WCF, at least as of now.

虽然我感兴趣的备选库​​,我希望有人可能有一个回答我的问题了。

While I'm interested in alternative libraries, I am hoping that somebody might have an answer to my question too.

推荐答案

该DataContractJsonSerializer旨在与那里的序列类型通常POCO类施加给他们的DataContract属性WCF客户端应用。没有DataContract,无序列化。 WCF的映射机制,使发送和接收非常简单,但只有当你的平台是均匀的。如果你开始在不同的工具集搅拌,你的程序可能会侧身走了。

The DataContractJsonSerializer is intended for use with WCF client applications where the serialized types are typically POCO classes with the DataContract attribute applied to them. No DataContract, no serialization. The mapping mechanism of WCF makes the sending and receiving very simple, but only if your platform is homogeneous. If you start mixing in different toolsets, your program might go sideways.

的的JavaScriptSerializer可以序列的任何类型,包括匿名类型(单程),并以更符合的方式这样做。你失去了WCF的AUTOMAGIC,但你获得更多的集成选项。

The JavaScriptSerializer can serialize any type, including anonymous types (one way), and does so in a more conformant way. You lose the "automagic" of WCF, but you gain more integration options.

正如你可以在注释中看到,有很多的选择那里为AJAX序列化,并解决您的速度与可维护性的问题,它可能是值得研究他们发现,满足所有需求的解决方案队,以减少大家都做的事情自己的方式长期可维护性的问题。

As you can see by the comments, there are a lot of options out there for AJAX serialization, and to address your speed vs. maintainability questions, it might be worth investigating them to find a solution that meets the needs of all the teams, to reduce maintainability issues in the long term as everybody does things their own way.

2014年4月7日更新:
如果可以的话我建议使用JSON.NET。看到在这个问题上考虑的3库的审查 http://james.newtonking.com/json 功能比较。

2014-04-07 UPDATE: I suggest using JSON.NET if you can. See http://james.newtonking.com/json Feature Comparison for a review of the 3 libraries considered in this question.

2015年5月26日更新:
如果您的公司需要使用的商业许可的产品,或者您需要的性能每一点,你可能也想看看 https://servicestack.net/

2015-05-26 UPDATE: If your company requires the use of commercially licensable products, or you need every last bit of performance, you may also want to check out https://servicestack.net/.

这篇关于什么是DataContractJsonSerializer和的JavaScriptSerializer之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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