Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Serialization/ Deserialization to/from XML instead of binary

I have a complex set of data models that currently implement java.io.Serializable, and I have successfully serialized and deserialized them with ObjectOutputStream and ObjectInputStream.

However, the result are binary files (as expected), and I was wondering if Java supports serialization and deserialization in the same manner to a non-binary format, such as XML.


I see that C# has this feature: XML vs Binary performance for Serialization/Deserialization.

Performance speed/ efficiency is not a consideration in this case.

like image 465
bguiz Avatar asked Feb 08 '10 01:02

bguiz


People also ask

Which annotation is needed for serialization and deserialization of XML format in the model classes?

Jackson annotations are useful in defining and controlling the process of serialization and deserialization across various formats such as XML, JSON, and YAML.

What is serialization alternative in Java?

Google protocol buffers, popularly known as protobuf is an alternate and faster way to serialize Java objects. It's probably the best alternative to Java serialization and useful for both data storage and data transfer over the network.

Is there any other method to avoid serialization?

To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.

Which methods are used during serialization and deserialization process in Java?

The ObjectOutputStream class contains writeObject() method for serializing an Object. The ObjectInputStream class contains readObject() method for deserializing an object.


2 Answers

Further, I would suggest you to look at Simple and XStream frameworks. I found both good. You can choose to go with either one, or may be XmlEncoder as suggested by Jack.

like image 151
Adeel Ansari Avatar answered Oct 03 '22 06:10

Adeel Ansari


Yes, Java has it and it's called XML Encoding. Check it out here!

The approach is quite similar to the normal serialization..

like image 44
Jack Avatar answered Oct 03 '22 06:10

Jack