Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

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 ?

like image 618
peter Avatar asked Jun 19 '12 14:06

peter


People also ask

Is Java serialization faster than JSON?

The results show that Java serialization is slow for a small number of objects, but good for a large number of objects. Conversely, XML and JSON can outperform Java serialization for a small number of objects, but Java serialization is faster for a large number of objects.

When would you use JSON vs XML?

JSON is a good choice if you want to transmit data from a client to a server. JSON data is readily processed by most programming languages. This means that you will not have to do any data parsing on either the client or the server side after data has been sent. XML is good if you need to separate data from HTML.

What is advantage of JSON over XML?

JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.

Why XML is more secure than JSON?

JSON does not provide namespace support while XML provides namespaces support. JSON has no display capabilities whereas XML offers the capability to display data. JSON is less secured whereas XML is more secure compared to JSON. JSON supports only UTF-8 encoding whereas XML supports various encoding formats.


1 Answers

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 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 - 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 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.

like image 66
Ariel T Avatar answered Oct 09 '22 10:10

Ariel T