DataContractJsonSerializer 和 JavaScriptSerializer 有什么区别? [英] What's the difference between DataContractJsonSerializer and JavaScriptSerializer?

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

问题描述

.NET Framework 随 系统一起提供.Runtime.Serialization.Json.DataContractJsonSerializerSystem.Web.Script.Serialization.JavaScriptSerializer,两者都对 JSON 进行反序列化.我怎么知道何时选择这些类型中的一种而不是另一种?MSDN 没有说明它们的相对优势是什么.

我们有几个使用或发出 JSON 的项目,到目前为止为每个项目选择的类取决于主要开发人员对每个项目的意见.有些很简单,两种在从 JSON 生成托管类型方面具有复杂的逻辑(这些类型不会紧密映射到流),但不强调速度,一种需要速度.没有人与 WCF 交互,至少到目前为止是这样.

虽然我对替代图书馆感兴趣,但我希望有人也能回答我的问题.

解决方案

DataContractJsonSerializer 旨在用于 WCF 客户端应用程序,其中序列化类型通常是应用了 DataContract 属性的 POCO 类.没有 DataContract,没有序列化.WCF 的映射机制使发送和接收非常简单,但前提是您的平台是同类的.如果您开始混合使用不同的工具集,您的程序可能会横向运行.

JavaScriptSerializer 可以序列化任何类型,包括匿名类型(一种方式),并且以一种更一致的方式进行.您失去了 WCF 的自动魔力",但您获得了更多集成选项.

正如您从评论中看到的,AJAX 序列化有很多选项,为了解决您的速度与可维护性问题,可能值得研究它们以找到满足所有需求的解决方案团队,从长远来看减少可维护性问题,因为每个人都按自己的方式做事.

2014-04-07 更新:如果可以,我建议使用 JSON.NET.请参阅 http://james.newtonking.com/json 功能比较以查看此问题中考虑的 3 个库.>

2015-05-26 更新:如果您的公司需要使用商业许可产品,或者您需要每一点性能,您可能还需要查看 https://servicestack.net/.

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.

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.

解决方案

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.

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.

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-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-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天全站免登陆