Java Serialization vs JSON vs XML [英] Java Serialization vs JSON vs XML

查看:102
本文介绍了Java Serialization vs JSON vs XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在处理通过网络传输对象时我们应该选择什么序列化机制。有什么利弊?

I am wondering what serialized mechanism should we choose when dealing with object transferring over the network. What are the pros and cons ?

我知道大部分时间我们都使用 JSON XML for AJAX ,因为传输格式几乎是 Javascript 格式,加上 JSON 非常轻巧,占用空间小,因此 Java 序列化完全超出了表格?

I know most of the time we use JSON or XML for AJAX since the transfer format are pretty much Javascript format, and plus JSON is pretty lightweight with its small footprint, therefore is Java serialization totally out of the table ?

推荐答案

一般来说,重要的问题是哪个客户端将接收序列化对象 - 浏览器/ JavaScript引擎,如(node-js),Java客户端,未知/多个客户端。

In general the important question is which client will receive the serialized objects - browsers/JavaScript engines like (node-js), Java client, unknown/multiple clients.

JSON -
JSON语法基本上是JavaScript,因此任何带有JS引擎的组件都能很好地处理它的解析 - 即使是复杂的数据结构也是如此有效地转换为生活对象。几乎所有语言都存在JSON解析器,即使不使用JS引擎也很容易使用(例如,以Google Gson为例,它可以轻松地将JSON转换为相应的对象),这使得它成为跨语言交流的理想选择。 - 例如,在消息传递架构中。

JSON - JSON syntax is basically JavaScript and therefore any component with a JS engine will handle its parsing very well - even complicated data-structures will be converted to "living" objects efficiently. JSON parsers exist for practically any language and it is easy to use even when not using a JS engine, (Take Google Gson for example that is able to convert JSON into corresponding objects with ease) which makes is a good candidate for cross-language communication - for example in a messaging architecture.

XML -
分享JSON的许多好处 - 跨语言,轻量级等。例如,Adobe Flex处理XML非常好,甚至比JSON更好。它绝对是JSON的合适替代品。我个人更喜欢JSON的JS语法,但XML也很好。

XML - Shares many of JSON's benefits - cross-language, lightweight, etc. Adobe Flex for example handles XML very well, even better than JSON. It's definitely an appropriate substitute for JSON. I personally prefer JSON for its JS like syntax, but XML is also good.

Java序列化 -
只应考虑Java-to-Java通信。一个重要的注意事项是类定义应该在发送端和接收端,并且通常通过传递整个对象不会获得太多收益。我不排除RMI作为通信协议,它确实简化了开发。但是,生成的应用程序组件将是硬连接的,这将使其更换非常困难。

Java Serialization - Should be considered only for Java-to-Java communication. An important note is that the class definitions should be on the sending and the receiving ends and often you wouldn't gain much by passing the entire object. I wouldn't rule out RMI as a communication protocol, it does simplify development. However the resulting application components will be hard coupled which will make it very difficult to replace.

还有一个注意事项 - 序列化通常会产生开销。但是,当通过网络进行通信时,瓶颈通常是网络而不是序列化/反序列化本身。

One more notes - Serialization in general has its overhead. However when the communication is performed over a network the bottleneck is often the network rather than the serialization/deserialization itself.

这篇关于Java Serialization vs JSON vs XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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